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