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