| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* bq2415x charger driver |
| 4 |
* |
| 5 |
* Copyright (C) 2011-2013 Pali Rohár <[email protected]> |
| 6 |
* |
| 7 |
* Datasheets: |
| 8 |
* https://www.ti.com/product/bq24150 |
| 9 |
* https://www.ti.com/product/bq24150a |
| 10 |
* https://www.ti.com/product/bq24152 |
| 11 |
* https://www.ti.com/product/bq24153 |
| 12 |
* https://www.ti.com/product/bq24153a |
| 13 |
* https://www.ti.com/product/bq24155 |
| 14 |
* https://www.ti.com/product/bq24157s |
| 15 |
* https://www.ti.com/product/bq24158 |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <linux/kernel.h> |
| 19 |
#include <linux/module.h> |
| 20 |
#include <linux/param.h> |
| 21 |
#include <linux/err.h> |
| 22 |
#include <linux/workqueue.h> |
| 23 |
#include <linux/sysfs.h> |
| 24 |
#include <linux/platform_device.h> |
| 25 |
#include <linux/power_supply.h> |
| 26 |
#include <linux/idr.h> |
| 27 |
#include <linux/i2c.h> |
| 28 |
#include <linux/slab.h> |
| 29 |
#include <linux/acpi.h> |
| 30 |
|
| 31 |
#include <linux/power/bq2415x_charger.h> |
| 32 |
|
| 33 |
/* timeout for resetting chip timer */ |
| 34 |
#define BQ2415X_TIMER_TIMEOUT 10 |
| 35 |
|
| 36 |
#define BQ2415X_REG_STATUS 0x00 |
| 37 |
#define BQ2415X_REG_CONTROL 0x01 |
| 38 |
#define BQ2415X_REG_VOLTAGE 0x02 |
| 39 |
#define BQ2415X_REG_VENDER 0x03 |
| 40 |
#define BQ2415X_REG_CURRENT 0x04 |
| 41 |
|
| 42 |
/* reset state for all registers */ |
| 43 |
#define BQ2415X_RESET_STATUS BIT(6) |
| 44 |
#define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5)) |
| 45 |
#define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3)) |
| 46 |
#define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7)) |
| 47 |
|
| 48 |
/* status register */ |
| 49 |
#define BQ2415X_BIT_TMR_RST 7 |
| 50 |
#define BQ2415X_BIT_OTG 7 |
| 51 |
#define BQ2415X_BIT_EN_STAT 6 |
| 52 |
#define BQ2415X_MASK_STAT (BIT(4)|BIT(5)) |
| 53 |
#define BQ2415X_SHIFT_STAT 4 |
| 54 |
#define BQ2415X_BIT_BOOST 3 |
| 55 |
#define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2)) |
| 56 |
#define BQ2415X_SHIFT_FAULT 0 |
| 57 |
|
| 58 |
/* control register */ |
| 59 |
#define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7)) |
| 60 |
#define BQ2415X_SHIFT_LIMIT 6 |
| 61 |
#define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5)) |
| 62 |
#define BQ2415X_SHIFT_VLOWV 4 |
| 63 |
#define BQ2415X_BIT_TE 3 |
| 64 |
#define BQ2415X_BIT_CE 2 |
| 65 |
#define BQ2415X_BIT_HZ_MODE 1 |
| 66 |
#define BQ2415X_BIT_OPA_MODE 0 |
| 67 |
|
| 68 |
/* voltage register */ |
| 69 |
#define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7)) |
| 70 |
#define BQ2415X_SHIFT_VO 2 |
| 71 |
#define BQ2415X_BIT_OTG_PL 1 |
| 72 |
#define BQ2415X_BIT_OTG_EN 0 |
| 73 |
|
| 74 |
/* vender register */ |
| 75 |
#define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7)) |
| 76 |
#define BQ2415X_SHIFT_VENDER 5 |
| 77 |
#define BQ2415X_MASK_PN (BIT(3)|BIT(4)) |
| 78 |
#define BQ2415X_SHIFT_PN 3 |
| 79 |
#define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2)) |
| 80 |
#define BQ2415X_SHIFT_REVISION 0 |
| 81 |
|
| 82 |
/* current register */ |
| 83 |
#define BQ2415X_MASK_RESET BIT(7) |
| 84 |
#define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6)) |
| 85 |
#define BQ2415X_SHIFT_VI_CHRG 4 |
| 86 |
/* N/A BIT(3) */ |
| 87 |
#define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2)) |
| 88 |
#define BQ2415X_SHIFT_VI_TERM 0 |
| 89 |
|
| 90 |
|
| 91 |
enum bq2415x_command { |
| 92 |
BQ2415X_TIMER_RESET, |
| 93 |
BQ2415X_OTG_STATUS, |
| 94 |
BQ2415X_STAT_PIN_STATUS, |
| 95 |
BQ2415X_STAT_PIN_ENABLE, |
| 96 |
BQ2415X_STAT_PIN_DISABLE, |
| 97 |
BQ2415X_CHARGE_STATUS, |
| 98 |
BQ2415X_BOOST_STATUS, |
| 99 |
BQ2415X_FAULT_STATUS, |
| 100 |
|
| 101 |
BQ2415X_CHARGE_TERMINATION_STATUS, |
| 102 |
BQ2415X_CHARGE_TERMINATION_ENABLE, |
| 103 |
BQ2415X_CHARGE_TERMINATION_DISABLE, |
| 104 |
BQ2415X_CHARGER_STATUS, |
| 105 |
BQ2415X_CHARGER_ENABLE, |
| 106 |
BQ2415X_CHARGER_DISABLE, |
| 107 |
BQ2415X_HIGH_IMPEDANCE_STATUS, |
| 108 |
BQ2415X_HIGH_IMPEDANCE_ENABLE, |
| 109 |
BQ2415X_HIGH_IMPEDANCE_DISABLE, |
| 110 |
BQ2415X_BOOST_MODE_STATUS, |
| 111 |
BQ2415X_BOOST_MODE_ENABLE, |
| 112 |
BQ2415X_BOOST_MODE_DISABLE, |
| 113 |
|
| 114 |
BQ2415X_OTG_LEVEL, |
| 115 |
BQ2415X_OTG_ACTIVATE_HIGH, |
| 116 |
BQ2415X_OTG_ACTIVATE_LOW, |
| 117 |
BQ2415X_OTG_PIN_STATUS, |
| 118 |
BQ2415X_OTG_PIN_ENABLE, |
| 119 |
BQ2415X_OTG_PIN_DISABLE, |
| 120 |
|
| 121 |
BQ2415X_VENDER_CODE, |
| 122 |
BQ2415X_PART_NUMBER, |
| 123 |
BQ2415X_REVISION, |
| 124 |
}; |
| 125 |
|
| 126 |
enum bq2415x_chip { |
| 127 |
BQUNKNOWN, |
| 128 |
BQ24150, |
| 129 |
BQ24150A, |
| 130 |
BQ24151, |
| 131 |
BQ24151A, |
| 132 |
BQ24152, |
| 133 |
BQ24153, |
| 134 |
BQ24153A, |
| 135 |
BQ24155, |
| 136 |
BQ24156, |
| 137 |
BQ24156A, |
| 138 |
BQ24157S, |
| 139 |
BQ24158, |
| 140 |
}; |
| 141 |
|
| 142 |
static char *bq2415x_chip_name[] = { |
| 143 |
"unknown", |
| 144 |
"bq24150", |
| 145 |
"bq24150a", |
| 146 |
"bq24151", |
| 147 |
"bq24151a", |
| 148 |
"bq24152", |
| 149 |
"bq24153", |
| 150 |
"bq24153a", |
| 151 |
"bq24155", |
| 152 |
"bq24156", |
| 153 |
"bq24156a", |
| 154 |
"bq24157s", |
| 155 |
"bq24158", |
| 156 |
}; |
| 157 |
|
| 158 |
struct bq2415x_device { |
| 159 |
struct device *dev; |
| 160 |
struct bq2415x_platform_data init_data; |
| 161 |
struct power_supply *charger; |
| 162 |
struct power_supply_desc charger_desc; |
| 163 |
struct delayed_work work; |
| 164 |
struct device_node *notify_node; |
| 165 |
struct notifier_block nb; |
| 166 |
enum bq2415x_mode reported_mode;/* mode reported by hook function */ |
| 167 |
enum bq2415x_mode mode; /* currently configured mode */ |
| 168 |
enum bq2415x_chip chip; |
| 169 |
const char *timer_error; |
| 170 |
char *model; |
| 171 |
char *name; |
| 172 |
int autotimer; /* 1 - if driver automatically reset timer, 0 - not */ |
| 173 |
int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */ |
| 174 |
int charge_status; |
| 175 |
int id; |
| 176 |
}; |
| 177 |
|
| 178 |
/* each registered chip must have unique id */ |
| 179 |
static DEFINE_IDR(bq2415x_id); |
| 180 |
|
| 181 |
static DEFINE_MUTEX(bq2415x_id_mutex); |
| 182 |
static DEFINE_MUTEX(bq2415x_timer_mutex); |
| 183 |
static DEFINE_MUTEX(bq2415x_i2c_mutex); |
| 184 |
|
| 185 |
/**** i2c read functions ****/ |
| 186 |
|
| 187 |
/* read value from register */ |
| 188 |
static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg) |
| 189 |
{ |
| 190 |
struct i2c_client *client = to_i2c_client(bq->dev); |
| 191 |
struct i2c_msg msg[2]; |
| 192 |
u8 val; |
| 193 |
int ret; |
| 194 |
|
| 195 |
if (!client->adapter) |
| 196 |
return -ENODEV; |
| 197 |
|
| 198 |
msg[0].addr = client->addr; |
| 199 |
msg[0].flags = 0; |
| 200 |
msg[0].buf = ® |
| 201 |
msg[0].len = sizeof(reg); |
| 202 |
msg[1].addr = client->addr; |
| 203 |
msg[1].flags = I2C_M_RD; |
| 204 |
msg[1].buf = &val; |
| 205 |
msg[1].len = sizeof(val); |
| 206 |
|
| 207 |
mutex_lock(&bq2415x_i2c_mutex); |
| 208 |
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
| 209 |
mutex_unlock(&bq2415x_i2c_mutex); |
| 210 |
|
| 211 |
if (ret < 0) |
| 212 |
return ret; |
| 213 |
|
| 214 |
return val; |
| 215 |
} |
| 216 |
|
| 217 |
/* read value from register, apply mask and right shift it */ |
| 218 |
static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg, |
| 219 |
u8 mask, u8 shift) |
| 220 |
{ |
| 221 |
int ret; |
| 222 |
|
| 223 |
if (shift > 8) |
| 224 |
return -EINVAL; |
| 225 |
|
| 226 |
ret = bq2415x_i2c_read(bq, reg); |
| 227 |
if (ret < 0) |
| 228 |
return ret; |
| 229 |
return (ret & mask) >> shift; |
| 230 |
} |
| 231 |
|
| 232 |
/* read value from register and return one specified bit */ |
| 233 |
static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit) |
| 234 |
{ |
| 235 |
if (bit > 8) |
| 236 |
return -EINVAL; |
| 237 |
return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit); |
| 238 |
} |
| 239 |
|
| 240 |
/**** i2c write functions ****/ |
| 241 |
|
| 242 |
/* write value to register */ |
| 243 |
static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val) |
| 244 |
{ |
| 245 |
struct i2c_client *client = to_i2c_client(bq->dev); |
| 246 |
struct i2c_msg msg[1]; |
| 247 |
u8 data[2]; |
| 248 |
int ret; |
| 249 |
|
| 250 |
data[0] = reg; |
| 251 |
data[1] = val; |
| 252 |
|
| 253 |
msg[0].addr = client->addr; |
| 254 |
msg[0].flags = 0; |
| 255 |
msg[0].buf = data; |
| 256 |
msg[0].len = ARRAY_SIZE(data); |
| 257 |
|
| 258 |
mutex_lock(&bq2415x_i2c_mutex); |
| 259 |
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
| 260 |
mutex_unlock(&bq2415x_i2c_mutex); |
| 261 |
|
| 262 |
/* i2c_transfer returns number of messages transferred */ |
| 263 |
if (ret < 0) |
| 264 |
return ret; |
| 265 |
else if (ret != 1) |
| 266 |
return -EIO; |
| 267 |
|
| 268 |
return 0; |
| 269 |
} |
| 270 |
|
| 271 |
/* read value from register, change it with mask left shifted and write back */ |
| 272 |
static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val, |
| 273 |
u8 mask, u8 shift) |
| 274 |
{ |
| 275 |
int ret; |
| 276 |
|
| 277 |
if (shift > 8) |
| 278 |
return -EINVAL; |
| 279 |
|
| 280 |
ret = bq2415x_i2c_read(bq, reg); |
| 281 |
if (ret < 0) |
| 282 |
return ret; |
| 283 |
|
| 284 |
ret &= ~mask; |
| 285 |
ret |= val << shift; |
| 286 |
|
| 287 |
return bq2415x_i2c_write(bq, reg, ret); |
| 288 |
} |
| 289 |
|
| 290 |
/* change only one bit in register */ |
| 291 |
static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg, |
| 292 |
bool val, u8 bit) |
| 293 |
{ |
| 294 |
if (bit > 8) |
| 295 |
return -EINVAL; |
| 296 |
return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit); |
| 297 |
} |
| 298 |
|
| 299 |
/**** global functions ****/ |
| 300 |
|
| 301 |
/* exec command function */ |
| 302 |
static int bq2415x_exec_command(struct bq2415x_device *bq, |
| 303 |
enum bq2415x_command command) |
| 304 |
{ |
| 305 |
int ret; |
| 306 |
|
| 307 |
switch (command) { |
| 308 |
case BQ2415X_TIMER_RESET: |
| 309 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, |
| 310 |
1, BQ2415X_BIT_TMR_RST); |
| 311 |
case BQ2415X_OTG_STATUS: |
| 312 |
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS, |
| 313 |
BQ2415X_BIT_OTG); |
| 314 |
case BQ2415X_STAT_PIN_STATUS: |
| 315 |
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS, |
| 316 |
BQ2415X_BIT_EN_STAT); |
| 317 |
case BQ2415X_STAT_PIN_ENABLE: |
| 318 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1, |
| 319 |
BQ2415X_BIT_EN_STAT); |
| 320 |
case BQ2415X_STAT_PIN_DISABLE: |
| 321 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0, |
| 322 |
BQ2415X_BIT_EN_STAT); |
| 323 |
case BQ2415X_CHARGE_STATUS: |
| 324 |
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS, |
| 325 |
BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT); |
| 326 |
case BQ2415X_BOOST_STATUS: |
| 327 |
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS, |
| 328 |
BQ2415X_BIT_BOOST); |
| 329 |
case BQ2415X_FAULT_STATUS: |
| 330 |
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS, |
| 331 |
BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT); |
| 332 |
|
| 333 |
case BQ2415X_CHARGE_TERMINATION_STATUS: |
| 334 |
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, |
| 335 |
BQ2415X_BIT_TE); |
| 336 |
case BQ2415X_CHARGE_TERMINATION_ENABLE: |
| 337 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, |
| 338 |
1, BQ2415X_BIT_TE); |
| 339 |
case BQ2415X_CHARGE_TERMINATION_DISABLE: |
| 340 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, |
| 341 |
0, BQ2415X_BIT_TE); |
| 342 |
case BQ2415X_CHARGER_STATUS: |
| 343 |
ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, |
| 344 |
BQ2415X_BIT_CE); |
| 345 |
if (ret < 0) |
| 346 |
return ret; |
| 347 |
return ret > 0 ? 0 : 1; |
| 348 |
case BQ2415X_CHARGER_ENABLE: |
| 349 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, |
| 350 |
0, BQ2415X_BIT_CE); |
| 351 |
case BQ2415X_CHARGER_DISABLE: |
| 352 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, |
| 353 |
1, BQ2415X_BIT_CE); |
| 354 |
case BQ2415X_HIGH_IMPEDANCE_STATUS: |
| 355 |
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, |
| 356 |
BQ2415X_BIT_HZ_MODE); |
| 357 |
case BQ2415X_HIGH_IMPEDANCE_ENABLE: |
| 358 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, |
| 359 |
1, BQ2415X_BIT_HZ_MODE); |
| 360 |
case BQ2415X_HIGH_IMPEDANCE_DISABLE: |
| 361 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, |
| 362 |
0, BQ2415X_BIT_HZ_MODE); |
| 363 |
case BQ2415X_BOOST_MODE_STATUS: |
| 364 |
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, |
| 365 |
BQ2415X_BIT_OPA_MODE); |
| 366 |
case BQ2415X_BOOST_MODE_ENABLE: |
| 367 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, |
| 368 |
1, BQ2415X_BIT_OPA_MODE); |
| 369 |
case BQ2415X_BOOST_MODE_DISABLE: |
| 370 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, |
| 371 |
0, BQ2415X_BIT_OPA_MODE); |
| 372 |
|
| 373 |
case BQ2415X_OTG_LEVEL: |
| 374 |
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE, |
| 375 |
BQ2415X_BIT_OTG_PL); |
| 376 |
case BQ2415X_OTG_ACTIVATE_HIGH: |
| 377 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, |
| 378 |
1, BQ2415X_BIT_OTG_PL); |
| 379 |
case BQ2415X_OTG_ACTIVATE_LOW: |
| 380 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, |
| 381 |
0, BQ2415X_BIT_OTG_PL); |
| 382 |
case BQ2415X_OTG_PIN_STATUS: |
| 383 |
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE, |
| 384 |
BQ2415X_BIT_OTG_EN); |
| 385 |
case BQ2415X_OTG_PIN_ENABLE: |
| 386 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, |
| 387 |
1, BQ2415X_BIT_OTG_EN); |
| 388 |
case BQ2415X_OTG_PIN_DISABLE: |
| 389 |
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, |
| 390 |
0, BQ2415X_BIT_OTG_EN); |
| 391 |
|
| 392 |
case BQ2415X_VENDER_CODE: |
| 393 |
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, |
| 394 |
BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER); |
| 395 |
case BQ2415X_PART_NUMBER: |
| 396 |
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, |
| 397 |
BQ2415X_MASK_PN, BQ2415X_SHIFT_PN); |
| 398 |
case BQ2415X_REVISION: |
| 399 |
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, |
| 400 |
BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION); |
| 401 |
} |
| 402 |
return -EINVAL; |
| 403 |
} |
| 404 |
|
| 405 |
/* detect chip type */ |
| 406 |
static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq) |
| 407 |
{ |
| 408 |
struct i2c_client *client = to_i2c_client(bq->dev); |
| 409 |
int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER); |
| 410 |
|
| 411 |
if (ret < 0) |
| 412 |
return ret; |
| 413 |
|
| 414 |
switch (client->addr) { |
| 415 |
case 0x6b: |
| 416 |
switch (ret) { |
| 417 |
case 0: |
| 418 |
if (bq->chip == BQ24151A) |
| 419 |
return bq->chip; |
| 420 |
return BQ24151; |
| 421 |
case 1: |
| 422 |
if (bq->chip == BQ24150A || |
| 423 |
bq->chip == BQ24152 || |
| 424 |
bq->chip == BQ24155) |
| 425 |
return bq->chip; |
| 426 |
return BQ24150; |
| 427 |
case 2: |
| 428 |
if (bq->chip == BQ24153A) |
| 429 |
return bq->chip; |
| 430 |
return BQ24153; |
| 431 |
default: |
| 432 |
return BQUNKNOWN; |
| 433 |
} |
| 434 |
break; |
| 435 |
|
| 436 |
case 0x6a: |
| 437 |
switch (ret) { |
| 438 |
case 0: |
| 439 |
if (bq->chip == BQ24156A) |
| 440 |
return bq->chip; |
| 441 |
return BQ24156; |
| 442 |
case 2: |
| 443 |
if (bq->chip == BQ24157S) |
| 444 |
return bq->chip; |
| 445 |
return BQ24158; |
| 446 |
default: |
| 447 |
return BQUNKNOWN; |
| 448 |
} |
| 449 |
break; |
| 450 |
} |
| 451 |
|
| 452 |
return BQUNKNOWN; |
| 453 |
} |
| 454 |
|
| 455 |
/* detect chip revision */ |
| 456 |
static int bq2415x_detect_revision(struct bq2415x_device *bq) |
| 457 |
{ |
| 458 |
int ret = bq2415x_exec_command(bq, BQ2415X_REVISION); |
| 459 |
int chip = bq2415x_detect_chip(bq); |
| 460 |
|
| 461 |
if (ret < 0 || chip < 0) |
| 462 |
return -1; |
| 463 |
|
| 464 |
switch (chip) { |
| 465 |
case BQ24150: |
| 466 |
case BQ24150A: |
| 467 |
case BQ24151: |
| 468 |
case BQ24151A: |
| 469 |
case BQ24152: |
| 470 |
if (ret >= 0 && ret <= 3) |
| 471 |
return ret; |
| 472 |
return -1; |
| 473 |
case BQ24153: |
| 474 |
case BQ24153A: |
| 475 |
case BQ24156: |
| 476 |
case BQ24156A: |
| 477 |
case BQ24157S: |
| 478 |
case BQ24158: |
| 479 |
if (ret == 3) |
| 480 |
return 0; |
| 481 |
else if (ret == 1) |
| 482 |
return 1; |
| 483 |
return -1; |
| 484 |
case BQ24155: |
| 485 |
if (ret == 3) |
| 486 |
return 3; |
| 487 |
return -1; |
| 488 |
case BQUNKNOWN: |
| 489 |
return -1; |
| 490 |
} |
| 491 |
|
| 492 |
return -1; |
| 493 |
} |
| 494 |
|
| 495 |
/* return chip vender code */ |
| 496 |
static int bq2415x_get_vender_code(struct bq2415x_device *bq) |
| 497 |
{ |
| 498 |
int ret; |
| 499 |
|
| 500 |
ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE); |
| 501 |
if (ret < 0) |
| 502 |
return 0; |
| 503 |
|
| 504 |
/* convert to binary */ |
| 505 |
return (ret & 0x1) + |
| 506 |
((ret >> 1) & 0x1) * 10 + |
| 507 |
((ret >> 2) & 0x1) * 100; |
| 508 |
} |
| 509 |
|
| 510 |
/* reset all chip registers to default state */ |
| 511 |
static void bq2415x_reset_chip(struct bq2415x_device *bq) |
| 512 |
{ |
| 513 |
bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT); |
| 514 |
bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE); |
| 515 |
bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL); |
| 516 |
bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS); |
| 517 |
bq->timer_error = NULL; |
| 518 |
} |
| 519 |
|
| 520 |
/**** properties functions ****/ |
| 521 |
|
| 522 |
/* set current limit in mA */ |
| 523 |
static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA) |
| 524 |
{ |
| 525 |
int val; |
| 526 |
|
| 527 |
if (mA <= 100) |
| 528 |
val = 0; |
| 529 |
else if (mA <= 500) |
| 530 |
val = 1; |
| 531 |
else if (mA <= 800) |
| 532 |
val = 2; |
| 533 |
else |
| 534 |
val = 3; |
| 535 |
|
| 536 |
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, |
| 537 |
BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); |
| 538 |
} |
| 539 |
|
| 540 |
/* get current limit in mA */ |
| 541 |
static int bq2415x_get_current_limit(struct bq2415x_device *bq) |
| 542 |
{ |
| 543 |
int ret; |
| 544 |
|
| 545 |
ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, |
| 546 |
BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); |
| 547 |
if (ret < 0) |
| 548 |
return ret; |
| 549 |
else if (ret == 0) |
| 550 |
return 100; |
| 551 |
else if (ret == 1) |
| 552 |
return 500; |
| 553 |
else if (ret == 2) |
| 554 |
return 800; |
| 555 |
else if (ret == 3) |
| 556 |
return 1800; |
| 557 |
return -EINVAL; |
| 558 |
} |
| 559 |
|
| 560 |
/* set weak battery voltage in mV */ |
| 561 |
static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV) |
| 562 |
{ |
| 563 |
int val; |
| 564 |
|
| 565 |
/* round to 100mV */ |
| 566 |
if (mV <= 3400 + 50) |
| 567 |
val = 0; |
| 568 |
else if (mV <= 3500 + 50) |
| 569 |
val = 1; |
| 570 |
else if (mV <= 3600 + 50) |
| 571 |
val = 2; |
| 572 |
else |
| 573 |
val = 3; |
| 574 |
|
| 575 |
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, |
| 576 |
BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); |
| 577 |
} |
| 578 |
|
| 579 |
/* get weak battery voltage in mV */ |
| 580 |
static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq) |
| 581 |
{ |
| 582 |
int ret; |
| 583 |
|
| 584 |
ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, |
| 585 |
BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); |
| 586 |
if (ret < 0) |
| 587 |
return ret; |
| 588 |
return 100 * (34 + ret); |
| 589 |
} |
| 590 |
|
| 591 |
/* set battery regulation voltage in mV */ |
| 592 |
static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq, |
| 593 |
int mV) |
| 594 |
{ |
| 595 |
int val = (mV/10 - 350) / 2; |
| 596 |
|
| 597 |
/* |
| 598 |
* According to datasheet, maximum battery regulation voltage is |
| 599 |
* 4440mV which is b101111 = 47. |
| 600 |
*/ |
| 601 |
if (val < 0) |
| 602 |
val = 0; |
| 603 |
else if (val > 47) |
| 604 |
return -EINVAL; |
| 605 |
|
| 606 |
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val, |
| 607 |
BQ2415X_MASK_VO, BQ2415X_SHIFT_VO); |
| 608 |
} |
| 609 |
|
| 610 |
/* get battery regulation voltage in mV */ |
| 611 |
static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq) |
| 612 |
{ |
| 613 |
int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE, |
| 614 |
BQ2415X_MASK_VO, BQ2415X_SHIFT_VO); |
| 615 |
|
| 616 |
if (ret < 0) |
| 617 |
return ret; |
| 618 |
return 10 * (350 + 2*ret); |
| 619 |
} |
| 620 |
|
| 621 |
/* set charge current in mA (platform data must provide resistor sense) */ |
| 622 |
static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA) |
| 623 |
{ |
| 624 |
int val; |
| 625 |
|
| 626 |
if (bq->init_data.resistor_sense <= 0) |
| 627 |
return -EINVAL; |
| 628 |
|
| 629 |
val = (mA * bq->init_data.resistor_sense - 37400) / 6800; |
| 630 |
if (val < 0) |
| 631 |
val = 0; |
| 632 |
else if (val > 7) |
| 633 |
val = 7; |
| 634 |
|
| 635 |
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val, |
| 636 |
BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET, |
| 637 |
BQ2415X_SHIFT_VI_CHRG); |
| 638 |
} |
| 639 |
|
| 640 |
/* get charge current in mA (platform data must provide resistor sense) */ |
| 641 |
static int bq2415x_get_charge_current(struct bq2415x_device *bq) |
| 642 |
{ |
| 643 |
int ret; |
| 644 |
|
| 645 |
if (bq->init_data.resistor_sense <= 0) |
| 646 |
return -EINVAL; |
| 647 |
|
| 648 |
ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT, |
| 649 |
BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG); |
| 650 |
if (ret < 0) |
| 651 |
return ret; |
| 652 |
return (37400 + 6800*ret) / bq->init_data.resistor_sense; |
| 653 |
} |
| 654 |
|
| 655 |
/* set termination current in mA (platform data must provide resistor sense) */ |
| 656 |
static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA) |
| 657 |
{ |
| 658 |
int val; |
| 659 |
|
| 660 |
if (bq->init_data.resistor_sense <= 0) |
| 661 |
return -EINVAL; |
| 662 |
|
| 663 |
val = (mA * bq->init_data.resistor_sense - 3400) / 3400; |
| 664 |
if (val < 0) |
| 665 |
val = 0; |
| 666 |
else if (val > 7) |
| 667 |
val = 7; |
| 668 |
|
| 669 |
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val, |
| 670 |
BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET, |
| 671 |
BQ2415X_SHIFT_VI_TERM); |
| 672 |
} |
| 673 |
|
| 674 |
/* get termination current in mA (platform data must provide resistor sense) */ |
| 675 |
static int bq2415x_get_termination_current(struct bq2415x_device *bq) |
| 676 |
{ |
| 677 |
int ret; |
| 678 |
|
| 679 |
if (bq->init_data.resistor_sense <= 0) |
| 680 |
return -EINVAL; |
| 681 |
|
| 682 |
ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT, |
| 683 |
BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM); |
| 684 |
if (ret < 0) |
| 685 |
return ret; |
| 686 |
return (3400 + 3400*ret) / bq->init_data.resistor_sense; |
| 687 |
} |
| 688 |
|
| 689 |
/* set default value of property */ |
| 690 |
#define bq2415x_set_default_value(bq, prop) \ |
| 691 |
do { \ |
| 692 |
int ret = 0; \ |
| 693 |
if (bq->init_data.prop != -1) \ |
| 694 |
ret = bq2415x_set_##prop(bq, bq->init_data.prop); \ |
| 695 |
if (ret < 0) \ |
| 696 |
return ret; \ |
| 697 |
} while (0) |
| 698 |
|
| 699 |
/* set default values of all properties */ |
| 700 |
static int bq2415x_set_defaults(struct bq2415x_device *bq) |
| 701 |
{ |
| 702 |
bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE); |
| 703 |
bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE); |
| 704 |
bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE); |
| 705 |
|
| 706 |
bq2415x_set_default_value(bq, current_limit); |
| 707 |
bq2415x_set_default_value(bq, weak_battery_voltage); |
| 708 |
bq2415x_set_default_value(bq, battery_regulation_voltage); |
| 709 |
|
| 710 |
if (bq->init_data.resistor_sense > 0) { |
| 711 |
bq2415x_set_default_value(bq, charge_current); |
| 712 |
bq2415x_set_default_value(bq, termination_current); |
| 713 |
bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE); |
| 714 |
} |
| 715 |
|
| 716 |
bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE); |
| 717 |
return 0; |
| 718 |
} |
| 719 |
|
| 720 |
/**** charger mode functions ****/ |
| 721 |
|
| 722 |
/* set charger mode */ |
| 723 |
static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode) |
| 724 |
{ |
| 725 |
int ret = 0; |
| 726 |
int charger = 0; |
| 727 |
int boost = 0; |
| 728 |
|
| 729 |
if (mode == BQ2415X_MODE_BOOST) |
| 730 |
boost = 1; |
| 731 |
else if (mode != BQ2415X_MODE_OFF) |
| 732 |
charger = 1; |
| 733 |
|
| 734 |
if (!charger) |
| 735 |
ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE); |
| 736 |
|
| 737 |
if (!boost) |
| 738 |
ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE); |
| 739 |
|
| 740 |
if (ret < 0) |
| 741 |
return ret; |
| 742 |
|
| 743 |
switch (mode) { |
| 744 |
case BQ2415X_MODE_OFF: |
| 745 |
dev_dbg(bq->dev, "changing mode to: Offline\n"); |
| 746 |
ret = bq2415x_set_current_limit(bq, 100); |
| 747 |
break; |
| 748 |
case BQ2415X_MODE_NONE: |
| 749 |
dev_dbg(bq->dev, "changing mode to: N/A\n"); |
| 750 |
ret = bq2415x_set_current_limit(bq, 100); |
| 751 |
break; |
| 752 |
case BQ2415X_MODE_HOST_CHARGER: |
| 753 |
dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n"); |
| 754 |
ret = bq2415x_set_current_limit(bq, 500); |
| 755 |
break; |
| 756 |
case BQ2415X_MODE_DEDICATED_CHARGER: |
| 757 |
dev_dbg(bq->dev, "changing mode to: Dedicated charger\n"); |
| 758 |
ret = bq2415x_set_current_limit(bq, 1800); |
| 759 |
break; |
| 760 |
case BQ2415X_MODE_BOOST: /* Boost mode */ |
| 761 |
dev_dbg(bq->dev, "changing mode to: Boost\n"); |
| 762 |
ret = bq2415x_set_current_limit(bq, 100); |
| 763 |
break; |
| 764 |
} |
| 765 |
|
| 766 |
if (ret < 0) |
| 767 |
return ret; |
| 768 |
|
| 769 |
if (charger) |
| 770 |
ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE); |
| 771 |
else if (boost) |
| 772 |
ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE); |
| 773 |
|
| 774 |
if (ret < 0) |
| 775 |
return ret; |
| 776 |
|
| 777 |
bq2415x_set_default_value(bq, weak_battery_voltage); |
| 778 |
bq2415x_set_default_value(bq, battery_regulation_voltage); |
| 779 |
|
| 780 |
bq->mode = mode; |
| 781 |
sysfs_notify(&bq->charger->dev.kobj, NULL, "mode"); |
| 782 |
|
| 783 |
return 0; |
| 784 |
|
| 785 |
} |
| 786 |
|
| 787 |
static bool bq2415x_update_reported_mode(struct bq2415x_device *bq, int mA) |
| 788 |
{ |
| 789 |
enum bq2415x_mode mode; |
| 790 |
|
| 791 |
if (mA == 0) |
| 792 |
mode = BQ2415X_MODE_OFF; |
| 793 |
else if (mA < 500) |
| 794 |
mode = BQ2415X_MODE_NONE; |
| 795 |
else if (mA < 1800) |
| 796 |
mode = BQ2415X_MODE_HOST_CHARGER; |
| 797 |
else |
| 798 |
mode = BQ2415X_MODE_DEDICATED_CHARGER; |
| 799 |
|
| 800 |
if (bq->reported_mode == mode) |
| 801 |
return false; |
| 802 |
|
| 803 |
bq->reported_mode = mode; |
| 804 |
return true; |
| 805 |
} |
| 806 |
|
| 807 |
static int bq2415x_notifier_call(struct notifier_block *nb, |
| 808 |
unsigned long val, void *v) |
| 809 |
{ |
| 810 |
struct bq2415x_device *bq = |
| 811 |
container_of(nb, struct bq2415x_device, nb); |
| 812 |
struct power_supply *psy = v; |
| 813 |
union power_supply_propval prop; |
| 814 |
int ret; |
| 815 |
|
| 816 |
if (val != PSY_EVENT_PROP_CHANGED) |
| 817 |
return NOTIFY_OK; |
| 818 |
|
| 819 |
/* Ignore event if it was not send by notify_node/notify_device */ |
| 820 |
if (bq->notify_node) { |
| 821 |
if (!psy->dev.parent || |
| 822 |
psy->dev.parent->of_node != bq->notify_node) |
| 823 |
return NOTIFY_OK; |
| 824 |
} else if (bq->init_data.notify_device) { |
| 825 |
if (strcmp(psy->desc->name, bq->init_data.notify_device) != 0) |
| 826 |
return NOTIFY_OK; |
| 827 |
} |
| 828 |
|
| 829 |
dev_dbg(bq->dev, "notifier call was called\n"); |
| 830 |
|
| 831 |
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, |
| 832 |
&prop); |
| 833 |
if (ret != 0) |
| 834 |
return NOTIFY_OK; |
| 835 |
|
| 836 |
if (!bq2415x_update_reported_mode(bq, prop.intval)) |
| 837 |
return NOTIFY_OK; |
| 838 |
|
| 839 |
power_supply_changed(bq->charger); |
| 840 |
|
| 841 |
/* if automode is not enabled do not tell about reported_mode */ |
| 842 |
if (bq->automode < 1) |
| 843 |
return NOTIFY_OK; |
| 844 |
|
| 845 |
mod_delayed_work(system_percpu_wq, &bq->work, 0); |
| 846 |
|
| 847 |
return NOTIFY_OK; |
| 848 |
} |
| 849 |
|
| 850 |
/**** timer functions ****/ |
| 851 |
|
| 852 |
/* enable/disable auto resetting chip timer */ |
| 853 |
static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state) |
| 854 |
{ |
| 855 |
mutex_lock(&bq2415x_timer_mutex); |
| 856 |
|
| 857 |
if (bq->autotimer == state) { |
| 858 |
mutex_unlock(&bq2415x_timer_mutex); |
| 859 |
return; |
| 860 |
} |
| 861 |
|
| 862 |
bq->autotimer = state; |
| 863 |
|
| 864 |
if (state) { |
| 865 |
schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ); |
| 866 |
bq2415x_exec_command(bq, BQ2415X_TIMER_RESET); |
| 867 |
bq->timer_error = NULL; |
| 868 |
} else { |
| 869 |
cancel_delayed_work_sync(&bq->work); |
| 870 |
} |
| 871 |
|
| 872 |
mutex_unlock(&bq2415x_timer_mutex); |
| 873 |
} |
| 874 |
|
| 875 |
/* called by bq2415x_timer_work on timer error */ |
| 876 |
static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg) |
| 877 |
{ |
| 878 |
bq->timer_error = msg; |
| 879 |
sysfs_notify(&bq->charger->dev.kobj, NULL, "timer"); |
| 880 |
dev_err(bq->dev, "%s\n", msg); |
| 881 |
if (bq->automode > 0) |
| 882 |
bq->automode = 0; |
| 883 |
bq2415x_set_mode(bq, BQ2415X_MODE_OFF); |
| 884 |
bq2415x_set_autotimer(bq, 0); |
| 885 |
} |
| 886 |
|
| 887 |
/* delayed work function for auto resetting chip timer */ |
| 888 |
static void bq2415x_timer_work(struct work_struct *work) |
| 889 |
{ |
| 890 |
struct bq2415x_device *bq = container_of(work, struct bq2415x_device, |
| 891 |
work.work); |
| 892 |
int ret; |
| 893 |
int error; |
| 894 |
int boost; |
| 895 |
int charge; |
| 896 |
|
| 897 |
if (bq->automode > 0 && (bq->reported_mode != bq->mode)) { |
| 898 |
sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode"); |
| 899 |
bq2415x_set_mode(bq, bq->reported_mode); |
| 900 |
} |
| 901 |
|
| 902 |
charge = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS); |
| 903 |
if (bq->charge_status != charge) { |
| 904 |
power_supply_changed(bq->charger); |
| 905 |
bq->charge_status = charge; |
| 906 |
} |
| 907 |
|
| 908 |
if (!bq->autotimer) |
| 909 |
return; |
| 910 |
|
| 911 |
ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET); |
| 912 |
if (ret < 0) { |
| 913 |
bq2415x_timer_error(bq, "Resetting timer failed"); |
| 914 |
return; |
| 915 |
} |
| 916 |
|
| 917 |
boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS); |
| 918 |
if (boost < 0) { |
| 919 |
bq2415x_timer_error(bq, "Unknown error"); |
| 920 |
return; |
| 921 |
} |
| 922 |
|
| 923 |
error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS); |
| 924 |
if (error < 0) { |
| 925 |
bq2415x_timer_error(bq, "Unknown error"); |
| 926 |
return; |
| 927 |
} |
| 928 |
|
| 929 |
if (boost) { |
| 930 |
switch (error) { |
| 931 |
/* Non fatal errors, chip is OK */ |
| 932 |
case 0: /* No error */ |
| 933 |
break; |
| 934 |
case 6: /* Timer expired */ |
| 935 |
dev_err(bq->dev, "Timer expired\n"); |
| 936 |
break; |
| 937 |
case 3: /* Battery voltage too low */ |
| 938 |
dev_err(bq->dev, "Battery voltage to low\n"); |
| 939 |
break; |
| 940 |
|
| 941 |
/* Fatal errors, disable and reset chip */ |
| 942 |
case 1: /* Overvoltage protection (chip fried) */ |
| 943 |
bq2415x_timer_error(bq, |
| 944 |
"Overvoltage protection (chip fried)"); |
| 945 |
return; |
| 946 |
case 2: /* Overload */ |
| 947 |
bq2415x_timer_error(bq, "Overload"); |
| 948 |
return; |
| 949 |
case 4: /* Battery overvoltage protection */ |
| 950 |
bq2415x_timer_error(bq, |
| 951 |
"Battery overvoltage protection"); |
| 952 |
return; |
| 953 |
case 5: /* Thermal shutdown (too hot) */ |
| 954 |
bq2415x_timer_error(bq, |
| 955 |
"Thermal shutdown (too hot)"); |
| 956 |
return; |
| 957 |
case 7: /* N/A */ |
| 958 |
bq2415x_timer_error(bq, "Unknown error"); |
| 959 |
return; |
| 960 |
} |
| 961 |
} else { |
| 962 |
switch (error) { |
| 963 |
/* Non fatal errors, chip is OK */ |
| 964 |
case 0: /* No error */ |
| 965 |
break; |
| 966 |
case 2: /* Sleep mode */ |
| 967 |
dev_err(bq->dev, "Sleep mode\n"); |
| 968 |
break; |
| 969 |
case 3: /* Poor input source */ |
| 970 |
dev_err(bq->dev, "Poor input source\n"); |
| 971 |
break; |
| 972 |
case 6: /* Timer expired */ |
| 973 |
dev_err(bq->dev, "Timer expired\n"); |
| 974 |
break; |
| 975 |
case 7: /* No battery */ |
| 976 |
dev_err(bq->dev, "No battery\n"); |
| 977 |
break; |
| 978 |
|
| 979 |
/* Fatal errors, disable and reset chip */ |
| 980 |
case 1: /* Overvoltage protection (chip fried) */ |
| 981 |
bq2415x_timer_error(bq, |
| 982 |
"Overvoltage protection (chip fried)"); |
| 983 |
return; |
| 984 |
case 4: /* Battery overvoltage protection */ |
| 985 |
bq2415x_timer_error(bq, |
| 986 |
"Battery overvoltage protection"); |
| 987 |
return; |
| 988 |
case 5: /* Thermal shutdown (too hot) */ |
| 989 |
bq2415x_timer_error(bq, |
| 990 |
"Thermal shutdown (too hot)"); |
| 991 |
return; |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ); |
| 996 |
} |
| 997 |
|
| 998 |
/**** power supply interface code ****/ |
| 999 |
|
| 1000 |
static enum power_supply_property bq2415x_power_supply_props[] = { |
| 1001 |
/* TODO: maybe add more power supply properties */ |
| 1002 |
POWER_SUPPLY_PROP_STATUS, |
| 1003 |
POWER_SUPPLY_PROP_MODEL_NAME, |
| 1004 |
POWER_SUPPLY_PROP_ONLINE, |
| 1005 |
}; |
| 1006 |
|
| 1007 |
static int bq2415x_power_supply_get_property(struct power_supply *psy, |
| 1008 |
enum power_supply_property psp, |
| 1009 |
union power_supply_propval *val) |
| 1010 |
{ |
| 1011 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1012 |
int ret; |
| 1013 |
|
| 1014 |
switch (psp) { |
| 1015 |
case POWER_SUPPLY_PROP_STATUS: |
| 1016 |
ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS); |
| 1017 |
if (ret < 0) |
| 1018 |
return ret; |
| 1019 |
else if (ret == 0) /* Ready */ |
| 1020 |
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 1021 |
else if (ret == 1) /* Charge in progress */ |
| 1022 |
val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 1023 |
else if (ret == 2) /* Charge done */ |
| 1024 |
val->intval = POWER_SUPPLY_STATUS_FULL; |
| 1025 |
else |
| 1026 |
val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 1027 |
break; |
| 1028 |
case POWER_SUPPLY_PROP_MODEL_NAME: |
| 1029 |
val->strval = bq->model; |
| 1030 |
break; |
| 1031 |
case POWER_SUPPLY_PROP_ONLINE: |
| 1032 |
/* VBUS is present for all charging and fault states, |
| 1033 |
* except the 'Ready' state. |
| 1034 |
*/ |
| 1035 |
ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS); |
| 1036 |
if (ret < 0) |
| 1037 |
return ret; |
| 1038 |
val->intval = ret > 0; |
| 1039 |
break; |
| 1040 |
default: |
| 1041 |
return -EINVAL; |
| 1042 |
} |
| 1043 |
return 0; |
| 1044 |
} |
| 1045 |
|
| 1046 |
static void bq2415x_power_supply_exit(struct bq2415x_device *bq) |
| 1047 |
{ |
| 1048 |
bq->autotimer = 0; |
| 1049 |
if (bq->automode > 0) |
| 1050 |
bq->automode = 0; |
| 1051 |
cancel_delayed_work_sync(&bq->work); |
| 1052 |
power_supply_unregister(bq->charger); |
| 1053 |
kfree(bq->model); |
| 1054 |
} |
| 1055 |
|
| 1056 |
/**** additional sysfs entries for power supply interface ****/ |
| 1057 |
|
| 1058 |
/* show *_status entries */ |
| 1059 |
static ssize_t bq2415x_sysfs_show_status(struct device *dev, |
| 1060 |
struct device_attribute *attr, |
| 1061 |
char *buf) |
| 1062 |
{ |
| 1063 |
struct power_supply *psy = dev_to_psy(dev); |
| 1064 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1065 |
enum bq2415x_command command; |
| 1066 |
int ret; |
| 1067 |
|
| 1068 |
if (strcmp(attr->attr.name, "otg_status") == 0) |
| 1069 |
command = BQ2415X_OTG_STATUS; |
| 1070 |
else if (strcmp(attr->attr.name, "charge_status") == 0) |
| 1071 |
command = BQ2415X_CHARGE_STATUS; |
| 1072 |
else if (strcmp(attr->attr.name, "boost_status") == 0) |
| 1073 |
command = BQ2415X_BOOST_STATUS; |
| 1074 |
else if (strcmp(attr->attr.name, "fault_status") == 0) |
| 1075 |
command = BQ2415X_FAULT_STATUS; |
| 1076 |
else |
| 1077 |
return -EINVAL; |
| 1078 |
|
| 1079 |
ret = bq2415x_exec_command(bq, command); |
| 1080 |
if (ret < 0) |
| 1081 |
return ret; |
| 1082 |
return sysfs_emit(buf, "%d\n", ret); |
| 1083 |
} |
| 1084 |
|
| 1085 |
/* |
| 1086 |
* set timer entry: |
| 1087 |
* auto - enable auto mode |
| 1088 |
* off - disable auto mode |
| 1089 |
* (other values) - reset chip timer |
| 1090 |
*/ |
| 1091 |
static ssize_t bq2415x_sysfs_set_timer(struct device *dev, |
| 1092 |
struct device_attribute *attr, |
| 1093 |
const char *buf, |
| 1094 |
size_t count) |
| 1095 |
{ |
| 1096 |
struct power_supply *psy = dev_to_psy(dev); |
| 1097 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1098 |
int ret = 0; |
| 1099 |
|
| 1100 |
if (strncmp(buf, "auto", 4) == 0) |
| 1101 |
bq2415x_set_autotimer(bq, 1); |
| 1102 |
else if (strncmp(buf, "off", 3) == 0) |
| 1103 |
bq2415x_set_autotimer(bq, 0); |
| 1104 |
else |
| 1105 |
ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET); |
| 1106 |
|
| 1107 |
if (ret < 0) |
| 1108 |
return ret; |
| 1109 |
return count; |
| 1110 |
} |
| 1111 |
|
| 1112 |
/* show timer entry (auto or off) */ |
| 1113 |
static ssize_t bq2415x_sysfs_show_timer(struct device *dev, |
| 1114 |
struct device_attribute *attr, |
| 1115 |
char *buf) |
| 1116 |
{ |
| 1117 |
struct power_supply *psy = dev_to_psy(dev); |
| 1118 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1119 |
|
| 1120 |
if (bq->timer_error) |
| 1121 |
return sysfs_emit(buf, "%s\n", bq->timer_error); |
| 1122 |
|
| 1123 |
if (bq->autotimer) |
| 1124 |
return sysfs_emit(buf, "auto\n"); |
| 1125 |
return sysfs_emit(buf, "off\n"); |
| 1126 |
} |
| 1127 |
|
| 1128 |
/* |
| 1129 |
* set mode entry: |
| 1130 |
* auto - if automode is supported, enable it and set mode to reported |
| 1131 |
* none - disable charger and boost mode |
| 1132 |
* host - charging mode for host/hub chargers (current limit 500mA) |
| 1133 |
* dedicated - charging mode for dedicated chargers (unlimited current limit) |
| 1134 |
* boost - disable charger and enable boost mode |
| 1135 |
*/ |
| 1136 |
static ssize_t bq2415x_sysfs_set_mode(struct device *dev, |
| 1137 |
struct device_attribute *attr, |
| 1138 |
const char *buf, |
| 1139 |
size_t count) |
| 1140 |
{ |
| 1141 |
struct power_supply *psy = dev_to_psy(dev); |
| 1142 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1143 |
enum bq2415x_mode mode; |
| 1144 |
int ret = 0; |
| 1145 |
|
| 1146 |
if (strncmp(buf, "auto", 4) == 0) { |
| 1147 |
if (bq->automode < 0) |
| 1148 |
return -EINVAL; |
| 1149 |
bq->automode = 1; |
| 1150 |
mode = bq->reported_mode; |
| 1151 |
} else if (strncmp(buf, "off", 3) == 0) { |
| 1152 |
if (bq->automode > 0) |
| 1153 |
bq->automode = 0; |
| 1154 |
mode = BQ2415X_MODE_OFF; |
| 1155 |
} else if (strncmp(buf, "none", 4) == 0) { |
| 1156 |
if (bq->automode > 0) |
| 1157 |
bq->automode = 0; |
| 1158 |
mode = BQ2415X_MODE_NONE; |
| 1159 |
} else if (strncmp(buf, "host", 4) == 0) { |
| 1160 |
if (bq->automode > 0) |
| 1161 |
bq->automode = 0; |
| 1162 |
mode = BQ2415X_MODE_HOST_CHARGER; |
| 1163 |
} else if (strncmp(buf, "dedicated", 9) == 0) { |
| 1164 |
if (bq->automode > 0) |
| 1165 |
bq->automode = 0; |
| 1166 |
mode = BQ2415X_MODE_DEDICATED_CHARGER; |
| 1167 |
} else if (strncmp(buf, "boost", 5) == 0) { |
| 1168 |
if (bq->automode > 0) |
| 1169 |
bq->automode = 0; |
| 1170 |
mode = BQ2415X_MODE_BOOST; |
| 1171 |
} else if (strncmp(buf, "reset", 5) == 0) { |
| 1172 |
bq2415x_reset_chip(bq); |
| 1173 |
bq2415x_set_defaults(bq); |
| 1174 |
if (bq->automode <= 0) |
| 1175 |
return count; |
| 1176 |
bq->automode = 1; |
| 1177 |
mode = bq->reported_mode; |
| 1178 |
} else { |
| 1179 |
return -EINVAL; |
| 1180 |
} |
| 1181 |
|
| 1182 |
ret = bq2415x_set_mode(bq, mode); |
| 1183 |
if (ret < 0) |
| 1184 |
return ret; |
| 1185 |
return count; |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* show mode entry (auto, none, host, dedicated or boost) */ |
| 1189 |
static ssize_t bq2415x_sysfs_show_mode(struct device *dev, |
| 1190 |
struct device_attribute *attr, |
| 1191 |
char *buf) |
| 1192 |
{ |
| 1193 |
struct power_supply *psy = dev_to_psy(dev); |
| 1194 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1195 |
ssize_t ret = 0; |
| 1196 |
|
| 1197 |
if (bq->automode > 0) |
| 1198 |
ret += sysfs_emit_at(buf, ret, "auto ("); |
| 1199 |
|
| 1200 |
switch (bq->mode) { |
| 1201 |
case BQ2415X_MODE_OFF: |
| 1202 |
ret += sysfs_emit_at(buf, ret, "off"); |
| 1203 |
break; |
| 1204 |
case BQ2415X_MODE_NONE: |
| 1205 |
ret += sysfs_emit_at(buf, ret, "none"); |
| 1206 |
break; |
| 1207 |
case BQ2415X_MODE_HOST_CHARGER: |
| 1208 |
ret += sysfs_emit_at(buf, ret, "host"); |
| 1209 |
break; |
| 1210 |
case BQ2415X_MODE_DEDICATED_CHARGER: |
| 1211 |
ret += sysfs_emit_at(buf, ret, "dedicated"); |
| 1212 |
break; |
| 1213 |
case BQ2415X_MODE_BOOST: |
| 1214 |
ret += sysfs_emit_at(buf, ret, "boost"); |
| 1215 |
break; |
| 1216 |
} |
| 1217 |
|
| 1218 |
if (bq->automode > 0) |
| 1219 |
ret += sysfs_emit_at(buf, ret, ")"); |
| 1220 |
|
| 1221 |
ret += sysfs_emit_at(buf, ret, "\n"); |
| 1222 |
return ret; |
| 1223 |
} |
| 1224 |
|
| 1225 |
/* show reported_mode entry (none, host, dedicated or boost) */ |
| 1226 |
static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev, |
| 1227 |
struct device_attribute *attr, |
| 1228 |
char *buf) |
| 1229 |
{ |
| 1230 |
struct power_supply *psy = dev_to_psy(dev); |
| 1231 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1232 |
|
| 1233 |
if (bq->automode < 0) |
| 1234 |
return -EINVAL; |
| 1235 |
|
| 1236 |
switch (bq->reported_mode) { |
| 1237 |
case BQ2415X_MODE_OFF: |
| 1238 |
return sysfs_emit(buf, "off\n"); |
| 1239 |
case BQ2415X_MODE_NONE: |
| 1240 |
return sysfs_emit(buf, "none\n"); |
| 1241 |
case BQ2415X_MODE_HOST_CHARGER: |
| 1242 |
return sysfs_emit(buf, "host\n"); |
| 1243 |
case BQ2415X_MODE_DEDICATED_CHARGER: |
| 1244 |
return sysfs_emit(buf, "dedicated\n"); |
| 1245 |
case BQ2415X_MODE_BOOST: |
| 1246 |
return sysfs_emit(buf, "boost\n"); |
| 1247 |
} |
| 1248 |
|
| 1249 |
return -EINVAL; |
| 1250 |
} |
| 1251 |
|
| 1252 |
/* directly set raw value to chip register, format: 'register value' */ |
| 1253 |
static ssize_t bq2415x_sysfs_set_registers(struct device *dev, |
| 1254 |
struct device_attribute *attr, |
| 1255 |
const char *buf, |
| 1256 |
size_t count) |
| 1257 |
{ |
| 1258 |
struct power_supply *psy = dev_to_psy(dev); |
| 1259 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1260 |
ssize_t ret = 0; |
| 1261 |
unsigned int reg; |
| 1262 |
unsigned int val; |
| 1263 |
|
| 1264 |
if (sscanf(buf, "%x %x", ®, &val) != 2) |
| 1265 |
return -EINVAL; |
| 1266 |
|
| 1267 |
if (reg > 4 || val > 255) |
| 1268 |
return -EINVAL; |
| 1269 |
|
| 1270 |
ret = bq2415x_i2c_write(bq, reg, val); |
| 1271 |
if (ret < 0) |
| 1272 |
return ret; |
| 1273 |
return count; |
| 1274 |
} |
| 1275 |
|
| 1276 |
/* print value of chip register, format: 'register=value' */ |
| 1277 |
static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq, |
| 1278 |
u8 reg, |
| 1279 |
char *buf) |
| 1280 |
{ |
| 1281 |
int ret = bq2415x_i2c_read(bq, reg); |
| 1282 |
|
| 1283 |
if (ret < 0) |
| 1284 |
return sysfs_emit(buf, "%#.2x=error %d\n", reg, ret); |
| 1285 |
return sysfs_emit(buf, "%#.2x=%#.2x\n", reg, ret); |
| 1286 |
} |
| 1287 |
|
| 1288 |
/* show all raw values of chip register, format per line: 'register=value' */ |
| 1289 |
static ssize_t bq2415x_sysfs_show_registers(struct device *dev, |
| 1290 |
struct device_attribute *attr, |
| 1291 |
char *buf) |
| 1292 |
{ |
| 1293 |
struct power_supply *psy = dev_to_psy(dev); |
| 1294 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1295 |
ssize_t ret = 0; |
| 1296 |
|
| 1297 |
ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret); |
| 1298 |
ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret); |
| 1299 |
ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret); |
| 1300 |
ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret); |
| 1301 |
ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret); |
| 1302 |
return ret; |
| 1303 |
} |
| 1304 |
|
| 1305 |
/* set current and voltage limit entries (in mA or mV) */ |
| 1306 |
static ssize_t bq2415x_sysfs_set_limit(struct device *dev, |
| 1307 |
struct device_attribute *attr, |
| 1308 |
const char *buf, |
| 1309 |
size_t count) |
| 1310 |
{ |
| 1311 |
struct power_supply *psy = dev_to_psy(dev); |
| 1312 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1313 |
long val; |
| 1314 |
int ret; |
| 1315 |
|
| 1316 |
if (kstrtol(buf, 10, &val) < 0) |
| 1317 |
return -EINVAL; |
| 1318 |
|
| 1319 |
if (strcmp(attr->attr.name, "current_limit") == 0) |
| 1320 |
ret = bq2415x_set_current_limit(bq, val); |
| 1321 |
else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0) |
| 1322 |
ret = bq2415x_set_weak_battery_voltage(bq, val); |
| 1323 |
else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0) |
| 1324 |
ret = bq2415x_set_battery_regulation_voltage(bq, val); |
| 1325 |
else if (strcmp(attr->attr.name, "charge_current") == 0) |
| 1326 |
ret = bq2415x_set_charge_current(bq, val); |
| 1327 |
else if (strcmp(attr->attr.name, "termination_current") == 0) |
| 1328 |
ret = bq2415x_set_termination_current(bq, val); |
| 1329 |
else |
| 1330 |
return -EINVAL; |
| 1331 |
|
| 1332 |
if (ret < 0) |
| 1333 |
return ret; |
| 1334 |
return count; |
| 1335 |
} |
| 1336 |
|
| 1337 |
/* show current and voltage limit entries (in mA or mV) */ |
| 1338 |
static ssize_t bq2415x_sysfs_show_limit(struct device *dev, |
| 1339 |
struct device_attribute *attr, |
| 1340 |
char *buf) |
| 1341 |
{ |
| 1342 |
struct power_supply *psy = dev_to_psy(dev); |
| 1343 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1344 |
int ret; |
| 1345 |
|
| 1346 |
if (strcmp(attr->attr.name, "current_limit") == 0) |
| 1347 |
ret = bq2415x_get_current_limit(bq); |
| 1348 |
else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0) |
| 1349 |
ret = bq2415x_get_weak_battery_voltage(bq); |
| 1350 |
else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0) |
| 1351 |
ret = bq2415x_get_battery_regulation_voltage(bq); |
| 1352 |
else if (strcmp(attr->attr.name, "charge_current") == 0) |
| 1353 |
ret = bq2415x_get_charge_current(bq); |
| 1354 |
else if (strcmp(attr->attr.name, "termination_current") == 0) |
| 1355 |
ret = bq2415x_get_termination_current(bq); |
| 1356 |
else |
| 1357 |
return -EINVAL; |
| 1358 |
|
| 1359 |
if (ret < 0) |
| 1360 |
return ret; |
| 1361 |
return sysfs_emit(buf, "%d\n", ret); |
| 1362 |
} |
| 1363 |
|
| 1364 |
/* set *_enable entries */ |
| 1365 |
static ssize_t bq2415x_sysfs_set_enable(struct device *dev, |
| 1366 |
struct device_attribute *attr, |
| 1367 |
const char *buf, |
| 1368 |
size_t count) |
| 1369 |
{ |
| 1370 |
struct power_supply *psy = dev_to_psy(dev); |
| 1371 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1372 |
enum bq2415x_command command; |
| 1373 |
long val; |
| 1374 |
int ret; |
| 1375 |
|
| 1376 |
if (kstrtol(buf, 10, &val) < 0) |
| 1377 |
return -EINVAL; |
| 1378 |
|
| 1379 |
if (strcmp(attr->attr.name, "charge_termination_enable") == 0) |
| 1380 |
command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE : |
| 1381 |
BQ2415X_CHARGE_TERMINATION_DISABLE; |
| 1382 |
else if (strcmp(attr->attr.name, "high_impedance_enable") == 0) |
| 1383 |
command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE : |
| 1384 |
BQ2415X_HIGH_IMPEDANCE_DISABLE; |
| 1385 |
else if (strcmp(attr->attr.name, "otg_pin_enable") == 0) |
| 1386 |
command = val ? BQ2415X_OTG_PIN_ENABLE : |
| 1387 |
BQ2415X_OTG_PIN_DISABLE; |
| 1388 |
else if (strcmp(attr->attr.name, "stat_pin_enable") == 0) |
| 1389 |
command = val ? BQ2415X_STAT_PIN_ENABLE : |
| 1390 |
BQ2415X_STAT_PIN_DISABLE; |
| 1391 |
else |
| 1392 |
return -EINVAL; |
| 1393 |
|
| 1394 |
ret = bq2415x_exec_command(bq, command); |
| 1395 |
if (ret < 0) |
| 1396 |
return ret; |
| 1397 |
return count; |
| 1398 |
} |
| 1399 |
|
| 1400 |
/* show *_enable entries */ |
| 1401 |
static ssize_t bq2415x_sysfs_show_enable(struct device *dev, |
| 1402 |
struct device_attribute *attr, |
| 1403 |
char *buf) |
| 1404 |
{ |
| 1405 |
struct power_supply *psy = dev_to_psy(dev); |
| 1406 |
struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
| 1407 |
enum bq2415x_command command; |
| 1408 |
int ret; |
| 1409 |
|
| 1410 |
if (strcmp(attr->attr.name, "charge_termination_enable") == 0) |
| 1411 |
command = BQ2415X_CHARGE_TERMINATION_STATUS; |
| 1412 |
else if (strcmp(attr->attr.name, "high_impedance_enable") == 0) |
| 1413 |
command = BQ2415X_HIGH_IMPEDANCE_STATUS; |
| 1414 |
else if (strcmp(attr->attr.name, "otg_pin_enable") == 0) |
| 1415 |
command = BQ2415X_OTG_PIN_STATUS; |
| 1416 |
else if (strcmp(attr->attr.name, "stat_pin_enable") == 0) |
| 1417 |
command = BQ2415X_STAT_PIN_STATUS; |
| 1418 |
else |
| 1419 |
return -EINVAL; |
| 1420 |
|
| 1421 |
ret = bq2415x_exec_command(bq, command); |
| 1422 |
if (ret < 0) |
| 1423 |
return ret; |
| 1424 |
return sysfs_emit(buf, "%d\n", ret); |
| 1425 |
} |
| 1426 |
|
| 1427 |
static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO, |
| 1428 |
bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); |
| 1429 |
static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO, |
| 1430 |
bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); |
| 1431 |
static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO, |
| 1432 |
bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); |
| 1433 |
static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO, |
| 1434 |
bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); |
| 1435 |
static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO, |
| 1436 |
bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); |
| 1437 |
|
| 1438 |
static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO, |
| 1439 |
bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); |
| 1440 |
static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO, |
| 1441 |
bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); |
| 1442 |
static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO, |
| 1443 |
bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); |
| 1444 |
static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO, |
| 1445 |
bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); |
| 1446 |
|
| 1447 |
static DEVICE_ATTR(reported_mode, S_IRUGO, |
| 1448 |
bq2415x_sysfs_show_reported_mode, NULL); |
| 1449 |
static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, |
| 1450 |
bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode); |
| 1451 |
static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO, |
| 1452 |
bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer); |
| 1453 |
|
| 1454 |
static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO, |
| 1455 |
bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers); |
| 1456 |
|
| 1457 |
static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); |
| 1458 |
static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); |
| 1459 |
static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); |
| 1460 |
static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); |
| 1461 |
|
| 1462 |
static struct attribute *bq2415x_sysfs_attrs[] = { |
| 1463 |
/* |
| 1464 |
* TODO: some (appropriate) of these attrs should be switched to |
| 1465 |
* use power supply class props. |
| 1466 |
*/ |
| 1467 |
&dev_attr_current_limit.attr, |
| 1468 |
&dev_attr_weak_battery_voltage.attr, |
| 1469 |
&dev_attr_battery_regulation_voltage.attr, |
| 1470 |
&dev_attr_charge_current.attr, |
| 1471 |
&dev_attr_termination_current.attr, |
| 1472 |
|
| 1473 |
&dev_attr_charge_termination_enable.attr, |
| 1474 |
&dev_attr_high_impedance_enable.attr, |
| 1475 |
&dev_attr_otg_pin_enable.attr, |
| 1476 |
&dev_attr_stat_pin_enable.attr, |
| 1477 |
|
| 1478 |
&dev_attr_reported_mode.attr, |
| 1479 |
&dev_attr_mode.attr, |
| 1480 |
&dev_attr_timer.attr, |
| 1481 |
|
| 1482 |
&dev_attr_registers.attr, |
| 1483 |
|
| 1484 |
&dev_attr_otg_status.attr, |
| 1485 |
&dev_attr_charge_status.attr, |
| 1486 |
&dev_attr_boost_status.attr, |
| 1487 |
&dev_attr_fault_status.attr, |
| 1488 |
NULL, |
| 1489 |
}; |
| 1490 |
|
| 1491 |
ATTRIBUTE_GROUPS(bq2415x_sysfs); |
| 1492 |
|
| 1493 |
static int bq2415x_power_supply_init(struct bq2415x_device *bq) |
| 1494 |
{ |
| 1495 |
int ret; |
| 1496 |
int chip; |
| 1497 |
char revstr[8]; |
| 1498 |
struct power_supply_config psy_cfg = { |
| 1499 |
.drv_data = bq, |
| 1500 |
.fwnode = dev_fwnode(bq->dev), |
| 1501 |
.attr_grp = bq2415x_sysfs_groups, |
| 1502 |
}; |
| 1503 |
|
| 1504 |
bq->charger_desc.name = bq->name; |
| 1505 |
bq->charger_desc.type = POWER_SUPPLY_TYPE_USB; |
| 1506 |
bq->charger_desc.properties = bq2415x_power_supply_props; |
| 1507 |
bq->charger_desc.num_properties = |
| 1508 |
ARRAY_SIZE(bq2415x_power_supply_props); |
| 1509 |
bq->charger_desc.get_property = bq2415x_power_supply_get_property; |
| 1510 |
|
| 1511 |
ret = bq2415x_detect_chip(bq); |
| 1512 |
if (ret < 0) |
| 1513 |
chip = BQUNKNOWN; |
| 1514 |
else |
| 1515 |
chip = ret; |
| 1516 |
|
| 1517 |
ret = bq2415x_detect_revision(bq); |
| 1518 |
if (ret < 0) |
| 1519 |
strscpy(revstr, "unknown", sizeof(revstr)); |
| 1520 |
else |
| 1521 |
sprintf(revstr, "1.%d", ret); |
| 1522 |
|
| 1523 |
bq->model = kasprintf(GFP_KERNEL, |
| 1524 |
"chip %s, revision %s, vender code %.3d", |
| 1525 |
bq2415x_chip_name[chip], revstr, |
| 1526 |
bq2415x_get_vender_code(bq)); |
| 1527 |
if (!bq->model) { |
| 1528 |
dev_err(bq->dev, "failed to allocate model name\n"); |
| 1529 |
return -ENOMEM; |
| 1530 |
} |
| 1531 |
|
| 1532 |
bq->charger = power_supply_register(bq->dev, &bq->charger_desc, |
| 1533 |
&psy_cfg); |
| 1534 |
if (IS_ERR(bq->charger)) { |
| 1535 |
kfree(bq->model); |
| 1536 |
return PTR_ERR(bq->charger); |
| 1537 |
} |
| 1538 |
|
| 1539 |
return 0; |
| 1540 |
} |
| 1541 |
|
| 1542 |
/* main bq2415x probe function */ |
| 1543 |
static int bq2415x_probe(struct i2c_client *client) |
| 1544 |
{ |
| 1545 |
const struct i2c_device_id *id = i2c_client_get_device_id(client); |
| 1546 |
int ret; |
| 1547 |
int num; |
| 1548 |
char *name = NULL; |
| 1549 |
struct bq2415x_device *bq; |
| 1550 |
struct device_node *np = client->dev.of_node; |
| 1551 |
struct bq2415x_platform_data *pdata = client->dev.platform_data; |
| 1552 |
const struct acpi_device_id *acpi_id = NULL; |
| 1553 |
struct power_supply *notify_psy = NULL; |
| 1554 |
union power_supply_propval prop; |
| 1555 |
|
| 1556 |
if (!np && !pdata && !ACPI_HANDLE(&client->dev)) { |
| 1557 |
dev_err(&client->dev, "Neither devicetree, nor platform data, nor ACPI support\n"); |
| 1558 |
return -ENODEV; |
| 1559 |
} |
| 1560 |
|
| 1561 |
/* Get new ID for the new device */ |
| 1562 |
mutex_lock(&bq2415x_id_mutex); |
| 1563 |
num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL); |
| 1564 |
mutex_unlock(&bq2415x_id_mutex); |
| 1565 |
if (num < 0) |
| 1566 |
return num; |
| 1567 |
|
| 1568 |
if (id) { |
| 1569 |
name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); |
| 1570 |
} else if (ACPI_HANDLE(&client->dev)) { |
| 1571 |
acpi_id = |
| 1572 |
acpi_match_device(client->dev.driver->acpi_match_table, |
| 1573 |
&client->dev); |
| 1574 |
if (!acpi_id) { |
| 1575 |
dev_err(&client->dev, "failed to match device name\n"); |
| 1576 |
ret = -ENODEV; |
| 1577 |
goto error_1; |
| 1578 |
} |
| 1579 |
name = kasprintf(GFP_KERNEL, "%s-%d", acpi_id->id, num); |
| 1580 |
} |
| 1581 |
if (!name) { |
| 1582 |
dev_err(&client->dev, "failed to allocate device name\n"); |
| 1583 |
ret = -ENOMEM; |
| 1584 |
goto error_1; |
| 1585 |
} |
| 1586 |
|
| 1587 |
bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL); |
| 1588 |
if (!bq) { |
| 1589 |
ret = -ENOMEM; |
| 1590 |
goto error_2; |
| 1591 |
} |
| 1592 |
|
| 1593 |
i2c_set_clientdata(client, bq); |
| 1594 |
|
| 1595 |
bq->id = num; |
| 1596 |
bq->dev = &client->dev; |
| 1597 |
if (id) |
| 1598 |
bq->chip = id->driver_data; |
| 1599 |
else if (ACPI_HANDLE(bq->dev)) |
| 1600 |
bq->chip = acpi_id->driver_data; |
| 1601 |
bq->name = name; |
| 1602 |
bq->mode = BQ2415X_MODE_OFF; |
| 1603 |
bq->reported_mode = BQ2415X_MODE_OFF; |
| 1604 |
bq->autotimer = 0; |
| 1605 |
bq->automode = 0; |
| 1606 |
|
| 1607 |
if (np || ACPI_HANDLE(bq->dev)) { |
| 1608 |
ret = device_property_read_u32(bq->dev, |
| 1609 |
"ti,current-limit", |
| 1610 |
&bq->init_data.current_limit); |
| 1611 |
if (ret) |
| 1612 |
goto error_2; |
| 1613 |
ret = device_property_read_u32(bq->dev, |
| 1614 |
"ti,weak-battery-voltage", |
| 1615 |
&bq->init_data.weak_battery_voltage); |
| 1616 |
if (ret) |
| 1617 |
goto error_2; |
| 1618 |
ret = device_property_read_u32(bq->dev, |
| 1619 |
"ti,battery-regulation-voltage", |
| 1620 |
&bq->init_data.battery_regulation_voltage); |
| 1621 |
if (ret) |
| 1622 |
goto error_2; |
| 1623 |
ret = device_property_read_u32(bq->dev, |
| 1624 |
"ti,charge-current", |
| 1625 |
&bq->init_data.charge_current); |
| 1626 |
if (ret) |
| 1627 |
goto error_2; |
| 1628 |
ret = device_property_read_u32(bq->dev, |
| 1629 |
"ti,termination-current", |
| 1630 |
&bq->init_data.termination_current); |
| 1631 |
if (ret) |
| 1632 |
goto error_2; |
| 1633 |
ret = device_property_read_u32(bq->dev, |
| 1634 |
"ti,resistor-sense", |
| 1635 |
&bq->init_data.resistor_sense); |
| 1636 |
if (ret) |
| 1637 |
goto error_2; |
| 1638 |
if (np) |
| 1639 |
bq->notify_node = of_parse_phandle(np, |
| 1640 |
"ti,usb-charger-detection", 0); |
| 1641 |
} else { |
| 1642 |
memcpy(&bq->init_data, pdata, sizeof(bq->init_data)); |
| 1643 |
} |
| 1644 |
|
| 1645 |
bq2415x_reset_chip(bq); |
| 1646 |
|
| 1647 |
ret = bq2415x_power_supply_init(bq); |
| 1648 |
if (ret) { |
| 1649 |
dev_err(bq->dev, "failed to register power supply: %d\n", ret); |
| 1650 |
goto error_2; |
| 1651 |
} |
| 1652 |
|
| 1653 |
ret = bq2415x_set_defaults(bq); |
| 1654 |
if (ret) { |
| 1655 |
dev_err(bq->dev, "failed to set default values: %d\n", ret); |
| 1656 |
goto error_3; |
| 1657 |
} |
| 1658 |
|
| 1659 |
if (bq->notify_node || bq->init_data.notify_device) { |
| 1660 |
bq->nb.notifier_call = bq2415x_notifier_call; |
| 1661 |
ret = power_supply_reg_notifier(&bq->nb); |
| 1662 |
if (ret) { |
| 1663 |
dev_err(bq->dev, "failed to reg notifier: %d\n", ret); |
| 1664 |
goto error_3; |
| 1665 |
} |
| 1666 |
|
| 1667 |
bq->automode = 1; |
| 1668 |
dev_info(bq->dev, "automode supported, waiting for events\n"); |
| 1669 |
} else { |
| 1670 |
bq->automode = -1; |
| 1671 |
dev_info(bq->dev, "automode not supported\n"); |
| 1672 |
} |
| 1673 |
|
| 1674 |
/* Query for initial reported_mode and set it */ |
| 1675 |
if (bq->nb.notifier_call) { |
| 1676 |
if (np) { |
| 1677 |
notify_psy = power_supply_get_by_reference(of_fwnode_handle(np), |
| 1678 |
"ti,usb-charger-detection"); |
| 1679 |
if (IS_ERR(notify_psy)) |
| 1680 |
notify_psy = NULL; |
| 1681 |
} else if (bq->init_data.notify_device) { |
| 1682 |
notify_psy = power_supply_get_by_name( |
| 1683 |
bq->init_data.notify_device); |
| 1684 |
} |
| 1685 |
} |
| 1686 |
if (notify_psy) { |
| 1687 |
ret = power_supply_get_property(notify_psy, |
| 1688 |
POWER_SUPPLY_PROP_CURRENT_MAX, &prop); |
| 1689 |
power_supply_put(notify_psy); |
| 1690 |
|
| 1691 |
if (ret == 0) { |
| 1692 |
bq2415x_update_reported_mode(bq, prop.intval); |
| 1693 |
bq2415x_set_mode(bq, bq->reported_mode); |
| 1694 |
} |
| 1695 |
} |
| 1696 |
|
| 1697 |
INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work); |
| 1698 |
bq2415x_set_autotimer(bq, 1); |
| 1699 |
|
| 1700 |
dev_info(bq->dev, "driver registered\n"); |
| 1701 |
return 0; |
| 1702 |
|
| 1703 |
error_3: |
| 1704 |
bq2415x_power_supply_exit(bq); |
| 1705 |
error_2: |
| 1706 |
if (bq) |
| 1707 |
of_node_put(bq->notify_node); |
| 1708 |
kfree(name); |
| 1709 |
error_1: |
| 1710 |
mutex_lock(&bq2415x_id_mutex); |
| 1711 |
idr_remove(&bq2415x_id, num); |
| 1712 |
mutex_unlock(&bq2415x_id_mutex); |
| 1713 |
|
| 1714 |
return ret; |
| 1715 |
} |
| 1716 |
|
| 1717 |
/* main bq2415x remove function */ |
| 1718 |
|
| 1719 |
static void bq2415x_remove(struct i2c_client *client) |
| 1720 |
{ |
| 1721 |
struct bq2415x_device *bq = i2c_get_clientdata(client); |
| 1722 |
|
| 1723 |
if (bq->nb.notifier_call) |
| 1724 |
power_supply_unreg_notifier(&bq->nb); |
| 1725 |
|
| 1726 |
of_node_put(bq->notify_node); |
| 1727 |
bq2415x_power_supply_exit(bq); |
| 1728 |
|
| 1729 |
bq2415x_reset_chip(bq); |
| 1730 |
|
| 1731 |
mutex_lock(&bq2415x_id_mutex); |
| 1732 |
idr_remove(&bq2415x_id, bq->id); |
| 1733 |
mutex_unlock(&bq2415x_id_mutex); |
| 1734 |
|
| 1735 |
dev_info(bq->dev, "driver unregistered\n"); |
| 1736 |
|
| 1737 |
kfree(bq->name); |
| 1738 |
} |
| 1739 |
|
| 1740 |
static const struct i2c_device_id bq2415x_i2c_id_table[] = { |
| 1741 |
{ "bq2415x", BQUNKNOWN }, |
| 1742 |
{ "bq24150", BQ24150 }, |
| 1743 |
{ "bq24150a", BQ24150A }, |
| 1744 |
{ "bq24151", BQ24151 }, |
| 1745 |
{ "bq24151a", BQ24151A }, |
| 1746 |
{ "bq24152", BQ24152 }, |
| 1747 |
{ "bq24153", BQ24153 }, |
| 1748 |
{ "bq24153a", BQ24153A }, |
| 1749 |
{ "bq24155", BQ24155 }, |
| 1750 |
{ "bq24156", BQ24156 }, |
| 1751 |
{ "bq24156a", BQ24156A }, |
| 1752 |
{ "bq24157s", BQ24157S }, |
| 1753 |
{ "bq24158", BQ24158 }, |
| 1754 |
{}, |
| 1755 |
}; |
| 1756 |
MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table); |
| 1757 |
|
| 1758 |
#ifdef CONFIG_ACPI |
| 1759 |
static const struct acpi_device_id bq2415x_i2c_acpi_match[] = { |
| 1760 |
{ "BQ2415X", BQUNKNOWN }, |
| 1761 |
{ "BQ241500", BQ24150 }, |
| 1762 |
{ "BQA24150", BQ24150A }, |
| 1763 |
{ "BQ241510", BQ24151 }, |
| 1764 |
{ "BQA24151", BQ24151A }, |
| 1765 |
{ "BQ241520", BQ24152 }, |
| 1766 |
{ "BQ241530", BQ24153 }, |
| 1767 |
{ "BQA24153", BQ24153A }, |
| 1768 |
{ "BQ241550", BQ24155 }, |
| 1769 |
{ "BQ241560", BQ24156 }, |
| 1770 |
{ "BQA24156", BQ24156A }, |
| 1771 |
{ "BQS24157", BQ24157S }, |
| 1772 |
{ "BQ241580", BQ24158 }, |
| 1773 |
{}, |
| 1774 |
}; |
| 1775 |
MODULE_DEVICE_TABLE(acpi, bq2415x_i2c_acpi_match); |
| 1776 |
#endif |
| 1777 |
|
| 1778 |
#ifdef CONFIG_OF |
| 1779 |
static const struct of_device_id bq2415x_of_match_table[] = { |
| 1780 |
{ .compatible = "ti,bq24150" }, |
| 1781 |
{ .compatible = "ti,bq24150a" }, |
| 1782 |
{ .compatible = "ti,bq24151" }, |
| 1783 |
{ .compatible = "ti,bq24151a" }, |
| 1784 |
{ .compatible = "ti,bq24152" }, |
| 1785 |
{ .compatible = "ti,bq24153" }, |
| 1786 |
{ .compatible = "ti,bq24153a" }, |
| 1787 |
{ .compatible = "ti,bq24155" }, |
| 1788 |
{ .compatible = "ti,bq24156" }, |
| 1789 |
{ .compatible = "ti,bq24156a" }, |
| 1790 |
{ .compatible = "ti,bq24157s" }, |
| 1791 |
{ .compatible = "ti,bq24158" }, |
| 1792 |
{}, |
| 1793 |
}; |
| 1794 |
MODULE_DEVICE_TABLE(of, bq2415x_of_match_table); |
| 1795 |
#endif |
| 1796 |
|
| 1797 |
static struct i2c_driver bq2415x_driver = { |
| 1798 |
.driver = { |
| 1799 |
.name = "bq2415x-charger", |
| 1800 |
.of_match_table = of_match_ptr(bq2415x_of_match_table), |
| 1801 |
.acpi_match_table = ACPI_PTR(bq2415x_i2c_acpi_match), |
| 1802 |
}, |
| 1803 |
.probe = bq2415x_probe, |
| 1804 |
.remove = bq2415x_remove, |
| 1805 |
.id_table = bq2415x_i2c_id_table, |
| 1806 |
}; |
| 1807 |
module_i2c_driver(bq2415x_driver); |
| 1808 |
|
| 1809 |
MODULE_AUTHOR("Pali Rohár <[email protected]>"); |
| 1810 |
MODULE_DESCRIPTION("bq2415x charger driver"); |
| 1811 |
MODULE_LICENSE("GPL"); |