David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012 Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/gpio.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/debugfs.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/i2c/smb349.h> |
| 25 | #include <linux/power_supply.h> |
| 26 | |
| 27 | #define SMB349_MASK(BITS, POS) ((unsigned char)(((1 << BITS) - 1) << POS)) |
| 28 | |
| 29 | /* Register definitions */ |
| 30 | #define CHG_CURRENT_REG 0x00 |
| 31 | #define CHG_OTHER_CURRENT_REG 0x01 |
| 32 | #define VAR_FUNC_REG 0x02 |
| 33 | #define FLOAT_VOLTAGE_REG 0x03 |
| 34 | #define CHG_CTRL_REG 0x04 |
| 35 | #define STAT_TIMER_REG 0x05 |
| 36 | #define PIN_ENABLE_CTRL_REG 0x06 |
| 37 | #define THERM_CTRL_A_REG 0x07 |
| 38 | #define SYSOK_USB3_SELECT_REG 0x08 |
| 39 | #define CTRL_FUNCTIONS_REG 0x09 |
| 40 | #define OTG_TLIM_THERM_CNTRL_REG 0x0A |
| 41 | #define HARD_SOFT_LIMIT_CELL_TEMP_MONITOR_REG 0x0B |
| 42 | #define FAULT_IRQ_REG 0x0C |
| 43 | #define STATUS_IRQ_REG 0x0D |
| 44 | #define SYSOK_REG 0x0E |
| 45 | #define CMD_A_REG 0x30 |
| 46 | #define CMD_B_REG 0x31 |
| 47 | #define CMD_C_REG 0x33 |
| 48 | #define IRQ_A_REG 0x35 |
| 49 | #define IRQ_B_REG 0x36 |
| 50 | #define IRQ_C_REG 0x37 |
| 51 | #define IRQ_D_REG 0x38 |
| 52 | #define IRQ_E_REG 0x39 |
| 53 | #define IRQ_F_REG 0x3A |
| 54 | #define STATUS_A_REG 0x3B |
| 55 | #define STATUS_B_REG 0x3C |
| 56 | #define STATUS_C_REG 0x3D |
| 57 | #define STATUS_D_REG 0x3E |
| 58 | #define STATUS_E_REG 0x3F |
| 59 | |
| 60 | /* Status bits and masks */ |
| 61 | #define CHG_STATUS_MASK SMB349_MASK(2, 1) |
| 62 | #define CHG_ENABLE_STATUS_BIT BIT(0) |
| 63 | |
| 64 | /* Control bits and masks */ |
| 65 | #define FAST_CHG_CURRENT_MASK SMB349_MASK(4, 4) |
| 66 | #define AC_INPUT_CURRENT_LIMIT_MASK SMB349_MASK(4, 0) |
| 67 | #define PRE_CHG_CURRENT_MASK SMB349_MASK(3, 5) |
| 68 | #define TERMINATION_CURRENT_MASK SMB349_MASK(3, 2) |
| 69 | #define PRE_CHG_TO_FAST_CHG_THRESH_MASK SMB349_MASK(2, 6) |
| 70 | #define FLOAT_VOLTAGE_MASK SMB349_MASK(6, 0) |
| 71 | #define CHG_ENABLE_BIT BIT(1) |
| 72 | #define VOLATILE_W_PERM_BIT BIT(7) |
| 73 | #define USB_SELECTION_BIT BIT(1) |
| 74 | #define SYSTEM_FET_ENABLE_BIT BIT(7) |
| 75 | #define AUTOMATIC_INPUT_CURR_LIMIT_BIT BIT(4) |
| 76 | #define AUTOMATIC_POWER_SOURCE_DETECTION_BIT BIT(2) |
| 77 | #define BATT_OV_END_CHG_BIT BIT(1) |
| 78 | #define VCHG_FUNCTION BIT(0) |
| 79 | #define CURR_TERM_END_CHG_BIT BIT(6) |
| 80 | |
| 81 | struct smb349_struct { |
| 82 | struct i2c_client *client; |
| 83 | bool charging; |
| 84 | bool present; |
| 85 | int chg_current_ma; |
| 86 | |
| 87 | int en_n_gpio; |
| 88 | int chg_susp_gpio; |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 89 | struct dentry *dent; |
| 90 | spinlock_t lock; |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 91 | |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 92 | struct work_struct chg_work; |
| 93 | struct power_supply dc_psy; |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | struct chg_ma_limit_entry { |
| 97 | int fast_chg_ma_limit; |
| 98 | int ac_input_ma_limit; |
| 99 | u8 chg_current_value; |
| 100 | }; |
| 101 | |
| 102 | static struct smb349_struct *the_smb349_chg; |
| 103 | |
| 104 | static int smb349_read_reg(struct i2c_client *client, int reg, |
| 105 | u8 *val) |
| 106 | { |
| 107 | s32 ret; |
| 108 | struct smb349_struct *smb349_chg; |
| 109 | |
| 110 | smb349_chg = i2c_get_clientdata(client); |
| 111 | ret = i2c_smbus_read_byte_data(smb349_chg->client, reg); |
| 112 | if (ret < 0) { |
| 113 | dev_err(&smb349_chg->client->dev, |
| 114 | "i2c read fail: can't read from %02x: %d\n", reg, ret); |
| 115 | return ret; |
| 116 | } else { |
| 117 | *val = ret; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int smb349_write_reg(struct i2c_client *client, int reg, |
| 124 | u8 val) |
| 125 | { |
| 126 | s32 ret; |
| 127 | struct smb349_struct *smb349_chg; |
| 128 | |
| 129 | smb349_chg = i2c_get_clientdata(client); |
| 130 | ret = i2c_smbus_write_byte_data(smb349_chg->client, reg, val); |
| 131 | if (ret < 0) { |
| 132 | dev_err(&smb349_chg->client->dev, |
| 133 | "i2c write fail: can't write %02x to %02x: %d\n", |
| 134 | val, reg, ret); |
| 135 | return ret; |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int smb349_masked_write(struct i2c_client *client, int reg, |
| 141 | u8 mask, u8 val) |
| 142 | { |
| 143 | s32 rc; |
| 144 | u8 temp; |
| 145 | |
| 146 | rc = smb349_read_reg(client, reg, &temp); |
| 147 | if (rc) { |
| 148 | pr_err("smb349_read_reg failed: reg=%03X, rc=%d\n", reg, rc); |
| 149 | return rc; |
| 150 | } |
| 151 | temp &= ~mask; |
| 152 | temp |= val & mask; |
| 153 | rc = smb349_write_reg(client, reg, temp); |
| 154 | if (rc) { |
| 155 | pr_err("smb349_write failed: reg=%03X, rc=%d\n", reg, rc); |
| 156 | return rc; |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static enum power_supply_property pm_power_props[] = { |
| 162 | POWER_SUPPLY_PROP_ONLINE, |
| 163 | POWER_SUPPLY_PROP_CURRENT_MAX, |
| 164 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 165 | }; |
| 166 | |
| 167 | static char *pm_power_supplied_to[] = { |
| 168 | "battery", |
| 169 | }; |
| 170 | |
| 171 | static int get_prop_charge_type(struct smb349_struct *smb349_chg) |
| 172 | { |
| 173 | if (smb349_chg->charging) |
| 174 | return POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 175 | |
| 176 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 177 | } |
| 178 | |
| 179 | static int pm_power_get_property(struct power_supply *psy, |
| 180 | enum power_supply_property psp, |
| 181 | union power_supply_propval *val) |
| 182 | { |
| 183 | struct smb349_struct *smb349_chg = container_of(psy, |
| 184 | struct smb349_struct, |
| 185 | dc_psy); |
| 186 | |
| 187 | switch (psp) { |
| 188 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 189 | val->intval = smb349_chg->chg_current_ma; |
| 190 | break; |
| 191 | case POWER_SUPPLY_PROP_ONLINE: |
| 192 | val->intval = (int)smb349_chg->present; |
| 193 | break; |
| 194 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 195 | val->intval = get_prop_charge_type(smb349_chg); |
| 196 | break; |
| 197 | default: |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | #define SMB349_FAST_CHG_MIN_MA 1000 |
| 204 | #define SMB349_FAST_CHG_STEP_MA 200 |
| 205 | #define SMB349_FAST_CHG_MAX_MA 4000 |
| 206 | #define SMB349_FAST_CHG_SHIFT 4 |
| 207 | static int chg_current_set(struct smb349_struct *smb349_chg) |
| 208 | { |
| 209 | u8 temp; |
| 210 | |
| 211 | if ((smb349_chg->chg_current_ma < SMB349_FAST_CHG_MIN_MA) || |
| 212 | (smb349_chg->chg_current_ma > SMB349_FAST_CHG_MAX_MA)) { |
| 213 | pr_err("bad mA=%d asked to set\n", smb349_chg->chg_current_ma); |
| 214 | return -EINVAL; |
| 215 | } |
| 216 | |
| 217 | temp = (smb349_chg->chg_current_ma - SMB349_FAST_CHG_MIN_MA) |
| 218 | / SMB349_FAST_CHG_STEP_MA; |
| 219 | |
| 220 | temp = temp << SMB349_FAST_CHG_SHIFT; |
| 221 | pr_debug("fastchg limit=%d setting %02x\n", |
| 222 | smb349_chg->chg_current_ma, temp); |
| 223 | return smb349_masked_write(smb349_chg->client, CHG_CURRENT_REG, |
| 224 | FAST_CHG_CURRENT_MASK, temp); |
| 225 | } |
| 226 | |
| 227 | static int set_reg(void *data, u64 val) |
| 228 | { |
| 229 | int addr = (int)data; |
| 230 | int ret; |
| 231 | u8 temp; |
| 232 | |
| 233 | temp = (u16) val; |
| 234 | ret = smb349_write_reg(the_smb349_chg->client, addr, temp); |
| 235 | |
| 236 | if (ret) { |
| 237 | pr_err("smb349_write_reg to %x value =%d errored = %d\n", |
| 238 | addr, temp, ret); |
| 239 | return -EAGAIN; |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | static int get_reg(void *data, u64 *val) |
| 244 | { |
| 245 | int addr = (int)data; |
| 246 | int ret; |
Shashank Mittal | 83302a7 | 2012-06-19 19:47:31 -0700 | [diff] [blame] | 247 | u8 temp = 0; |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 248 | |
| 249 | ret = smb349_read_reg(the_smb349_chg->client, addr, &temp); |
| 250 | if (ret) { |
| 251 | pr_err("smb349_read_reg to %x value =%d errored = %d\n", |
| 252 | addr, temp, ret); |
| 253 | return -EAGAIN; |
| 254 | } |
| 255 | |
| 256 | *val = temp; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n"); |
| 261 | |
| 262 | static void create_debugfs_entries(struct smb349_struct *smb349_chg) |
| 263 | { |
| 264 | struct dentry *file; |
| 265 | smb349_chg->dent = debugfs_create_dir(SMB349_NAME, NULL); |
| 266 | if (IS_ERR(smb349_chg->dent)) { |
| 267 | pr_err("smb349 driver couldn't create debugfs dir\n"); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | file = debugfs_create_file("CHG_CURRENT_REG", 0644, smb349_chg->dent, |
| 272 | (void *) CHG_CURRENT_REG, ®_fops); |
| 273 | if (IS_ERR(file)) { |
| 274 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 275 | return; |
| 276 | } |
| 277 | file = debugfs_create_file("CHG_OTHER_CURRENT_REG", 0644, |
| 278 | smb349_chg->dent, (void *) CHG_OTHER_CURRENT_REG, ®_fops); |
| 279 | if (IS_ERR(file)) { |
| 280 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 281 | return; |
| 282 | } |
| 283 | file = debugfs_create_file("VAR_FUNC_REG", 0644, smb349_chg->dent, |
| 284 | (void *) VAR_FUNC_REG, ®_fops); |
| 285 | if (IS_ERR(file)) { |
| 286 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 287 | return; |
| 288 | } |
| 289 | file = debugfs_create_file("FLOAT_VOLTAGE_REG", 0644, smb349_chg->dent, |
| 290 | (void *) FLOAT_VOLTAGE_REG, ®_fops); |
| 291 | if (IS_ERR(file)) { |
| 292 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 293 | return; |
| 294 | } |
| 295 | file = debugfs_create_file("CHG_CTRL_REG", 0644, smb349_chg->dent, |
| 296 | (void *) CHG_CTRL_REG, ®_fops); |
| 297 | if (IS_ERR(file)) { |
| 298 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 299 | return; |
| 300 | } |
| 301 | file = debugfs_create_file("STAT_TIMER_REG", 0644, smb349_chg->dent, |
| 302 | (void *) STAT_TIMER_REG, ®_fops); |
| 303 | if (IS_ERR(file)) { |
| 304 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 305 | return; |
| 306 | } |
| 307 | file = debugfs_create_file("PIN_ENABLE_CTRL_REG", 0644, |
| 308 | smb349_chg->dent, (void *) PIN_ENABLE_CTRL_REG, ®_fops); |
| 309 | if (IS_ERR(file)) { |
| 310 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 311 | return; |
| 312 | } |
| 313 | file = debugfs_create_file("PIN_ENABLE_CTRL_REG", 0644, |
| 314 | smb349_chg->dent, (void *) PIN_ENABLE_CTRL_REG, ®_fops); |
| 315 | if (IS_ERR(file)) { |
| 316 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 317 | return; |
| 318 | } |
| 319 | file = debugfs_create_file("PIN_ENABLE_CTRL_REG", 0644, |
| 320 | smb349_chg->dent, (void *) PIN_ENABLE_CTRL_REG, ®_fops); |
| 321 | if (IS_ERR(file)) { |
| 322 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 323 | return; |
| 324 | } |
| 325 | file = debugfs_create_file("THERM_CTRL_A_REG", 0644, smb349_chg->dent, |
| 326 | (void *) THERM_CTRL_A_REG, ®_fops); |
| 327 | if (IS_ERR(file)) { |
| 328 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 329 | return; |
| 330 | } |
| 331 | file = debugfs_create_file("SYSOK_USB3_SELECT_REG", 0644, |
| 332 | smb349_chg->dent, (void *) SYSOK_USB3_SELECT_REG, ®_fops); |
| 333 | if (IS_ERR(file)) { |
| 334 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 335 | return; |
| 336 | } |
| 337 | file = debugfs_create_file("CTRL_FUNCTIONS_REG", 0644, |
| 338 | smb349_chg->dent, (void *) CTRL_FUNCTIONS_REG, ®_fops); |
| 339 | if (IS_ERR(file)) { |
| 340 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 341 | return; |
| 342 | } |
| 343 | file = debugfs_create_file("OTG_TLIM_THERM_CNTRL_REG", 0644, |
| 344 | smb349_chg->dent, (void *) OTG_TLIM_THERM_CNTRL_REG, ®_fops); |
| 345 | if (IS_ERR(file)) { |
| 346 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 347 | return; |
| 348 | } |
| 349 | file = debugfs_create_file("HARD_SOFT_LIMIT_CELL_TEMP_MONITOR_REG", |
| 350 | 0644, smb349_chg->dent, |
| 351 | (void *) HARD_SOFT_LIMIT_CELL_TEMP_MONITOR_REG, ®_fops); |
| 352 | if (IS_ERR(file)) { |
| 353 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 354 | return; |
| 355 | } |
| 356 | file = debugfs_create_file("SYSOK_REG", 0644, smb349_chg->dent, |
| 357 | (void *) SYSOK_REG, ®_fops); |
| 358 | if (IS_ERR(file)) { |
| 359 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 360 | return; |
| 361 | } |
| 362 | file = debugfs_create_file("CMD_A_REG", 0644, smb349_chg->dent, |
| 363 | (void *) CMD_A_REG, ®_fops); |
| 364 | if (IS_ERR(file)) { |
| 365 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 366 | return; |
| 367 | } |
| 368 | file = debugfs_create_file("CMD_B_REG", 0644, smb349_chg->dent, |
| 369 | (void *) CMD_B_REG, ®_fops); |
| 370 | if (IS_ERR(file)) { |
| 371 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 372 | return; |
| 373 | } |
| 374 | file = debugfs_create_file("CMD_C_REG", 0644, smb349_chg->dent, |
| 375 | (void *) CMD_C_REG, ®_fops); |
| 376 | if (IS_ERR(file)) { |
| 377 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 378 | return; |
| 379 | } |
| 380 | file = debugfs_create_file("STATUS_A_REG", 0644, smb349_chg->dent, |
| 381 | (void *) STATUS_A_REG, ®_fops); |
| 382 | if (IS_ERR(file)) { |
| 383 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 384 | return; |
| 385 | } |
| 386 | file = debugfs_create_file("STATUS_B_REG", 0644, smb349_chg->dent, |
| 387 | (void *) STATUS_B_REG, ®_fops); |
| 388 | if (IS_ERR(file)) { |
| 389 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 390 | return; |
| 391 | } |
| 392 | file = debugfs_create_file("STATUS_C_REG", 0644, smb349_chg->dent, |
| 393 | (void *) STATUS_C_REG, ®_fops); |
| 394 | if (IS_ERR(file)) { |
| 395 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 396 | return; |
| 397 | } |
| 398 | file = debugfs_create_file("STATUS_D_REG", 0644, smb349_chg->dent, |
| 399 | (void *) STATUS_D_REG, ®_fops); |
| 400 | if (IS_ERR(file)) { |
| 401 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 402 | return; |
| 403 | } |
| 404 | file = debugfs_create_file("STATUS_E_REG", 0644, smb349_chg->dent, |
| 405 | (void *) STATUS_E_REG, ®_fops); |
| 406 | if (IS_ERR(file)) { |
| 407 | pr_err("smb349 driver couldn't create debugfs files\n"); |
| 408 | return; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static void remove_debugfs_entries(struct smb349_struct *smb349_chg) |
| 413 | { |
| 414 | if (smb349_chg->dent) |
| 415 | debugfs_remove_recursive(smb349_chg->dent); |
| 416 | } |
| 417 | |
Stephen Boyd | 197e20b | 2012-04-24 16:21:45 -0700 | [diff] [blame] | 418 | static int smb349_hwinit(struct smb349_struct *smb349_chg) |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 419 | { |
| 420 | int ret; |
| 421 | |
| 422 | ret = smb349_write_reg(smb349_chg->client, CMD_A_REG, |
| 423 | VOLATILE_W_PERM_BIT); |
| 424 | if (ret) { |
| 425 | pr_err("Failed to set VOLATILE_W_PERM_BIT rc=%d\n", ret); |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | ret = smb349_masked_write(smb349_chg->client, CHG_CTRL_REG, |
| 430 | CURR_TERM_END_CHG_BIT, CURR_TERM_END_CHG_BIT); |
| 431 | if (ret) { |
| 432 | pr_err("Failed to set CURR_TERM_END_CHG_BIT rc=%d\n", ret); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | ret = chg_current_set(smb349_chg); |
| 437 | if (ret) { |
| 438 | pr_err("Failed to set FAST_CHG_CURRENT rc=%d\n", ret); |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static int smb349_stop_charging(struct smb349_struct *smb349_chg) |
| 446 | { |
| 447 | unsigned long flags; |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 448 | int rc = 0; |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 449 | |
| 450 | spin_lock_irqsave(&smb349_chg->lock, flags); |
| 451 | pr_debug("stop charging %d\n", smb349_chg->charging); |
| 452 | smb349_chg->charging = 0; |
| 453 | spin_unlock_irqrestore(&smb349_chg->lock, flags); |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 454 | |
| 455 | if (smb349_chg->charging) |
| 456 | rc = schedule_work(&smb349_chg->chg_work); |
| 457 | |
| 458 | return rc; |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static int smb349_start_charging(struct smb349_struct *smb349_chg) |
| 462 | { |
| 463 | unsigned long flags; |
| 464 | int rc; |
| 465 | |
| 466 | rc = 0; |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 467 | spin_lock_irqsave(&smb349_chg->lock, flags); |
| 468 | pr_debug("start charging %d\n", smb349_chg->charging); |
| 469 | smb349_chg->charging = 1; |
| 470 | spin_unlock_irqrestore(&smb349_chg->lock, flags); |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 471 | |
| 472 | if (!smb349_chg->charging) |
| 473 | rc = schedule_work(&smb349_chg->chg_work); |
| 474 | |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 475 | return rc; |
| 476 | } |
| 477 | |
| 478 | static int pm_power_set_property(struct power_supply *psy, |
| 479 | enum power_supply_property psp, |
| 480 | const union power_supply_propval *val) |
| 481 | { |
| 482 | struct smb349_struct *smb349_chg = container_of(psy, |
| 483 | struct smb349_struct, |
| 484 | dc_psy); |
| 485 | |
| 486 | switch (psp) { |
| 487 | case POWER_SUPPLY_PROP_ONLINE: |
| 488 | if (val->intval) { |
| 489 | smb349_chg->present = val->intval; |
| 490 | } else { |
| 491 | smb349_chg->present = 0; |
| 492 | if (smb349_chg->charging) |
| 493 | return smb349_stop_charging(smb349_chg); |
| 494 | } |
| 495 | break; |
| 496 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 497 | if (val->intval) { |
| 498 | if (smb349_chg->chg_current_ma != val->intval) |
| 499 | return -EINVAL; |
| 500 | } |
| 501 | break; |
| 502 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 503 | if (val->intval && smb349_chg->present) { |
| 504 | if (val->intval == POWER_SUPPLY_CHARGE_TYPE_FAST) |
| 505 | return smb349_start_charging(smb349_chg); |
| 506 | if (val->intval == POWER_SUPPLY_CHARGE_TYPE_NONE) |
| 507 | return smb349_stop_charging(smb349_chg); |
| 508 | } else { |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | break; |
| 512 | default: |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | power_supply_changed(&smb349_chg->dc_psy); |
| 516 | return 0; |
| 517 | } |
| 518 | |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 519 | static void chg_worker(struct work_struct *work) |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 520 | { |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 521 | struct smb349_struct *smb349_chg = container_of(work, |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 522 | struct smb349_struct, chg_work); |
| 523 | int ret = 0; |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 524 | |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 525 | gpio_set_value_cansleep(smb349_chg->en_n_gpio, smb349_chg->charging); |
| 526 | |
| 527 | /* |
| 528 | * Write non-default values, charger chip reloads from |
| 529 | * non-volatile memory if it was in suspend mode |
| 530 | * |
| 531 | */ |
| 532 | if (smb349_chg->charging) |
| 533 | ret = smb349_hwinit(smb349_chg); |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 534 | if (ret) |
| 535 | pr_err("Failed to re-initilaze registers\n"); |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 536 | |
| 537 | power_supply_changed(&smb349_chg->dc_psy); |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | static int __devinit smb349_init_ext_chg(struct smb349_struct *smb349_chg) |
| 541 | { |
| 542 | int ret; |
| 543 | |
| 544 | smb349_chg->dc_psy.name = "dc"; |
| 545 | smb349_chg->dc_psy.type = POWER_SUPPLY_TYPE_MAINS; |
| 546 | smb349_chg->dc_psy.supplied_to = pm_power_supplied_to; |
| 547 | smb349_chg->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to); |
| 548 | smb349_chg->dc_psy.properties = pm_power_props; |
| 549 | smb349_chg->dc_psy.num_properties = ARRAY_SIZE(pm_power_props); |
| 550 | smb349_chg->dc_psy.get_property = pm_power_get_property; |
| 551 | smb349_chg->dc_psy.set_property = pm_power_set_property; |
| 552 | |
| 553 | ret = power_supply_register(&smb349_chg->client->dev, |
| 554 | &smb349_chg->dc_psy); |
| 555 | if (ret) { |
| 556 | pr_err("failed to register power_supply. ret=%d.\n", ret); |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int __devinit smb349_probe(struct i2c_client *client, |
| 564 | const struct i2c_device_id *id) |
| 565 | { |
| 566 | const struct smb349_platform_data *pdata; |
| 567 | struct smb349_struct *smb349_chg; |
| 568 | int ret = 0; |
| 569 | |
| 570 | pdata = client->dev.platform_data; |
| 571 | |
| 572 | if (pdata == NULL) { |
| 573 | dev_err(&client->dev, "%s no platform data\n", __func__); |
| 574 | ret = -EINVAL; |
| 575 | goto out; |
| 576 | } |
| 577 | |
| 578 | if (!i2c_check_functionality(client->adapter, |
| 579 | I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 580 | ret = -EIO; |
| 581 | goto out; |
| 582 | } |
| 583 | |
| 584 | smb349_chg = kzalloc(sizeof(*smb349_chg), GFP_KERNEL); |
| 585 | if (!smb349_chg) { |
| 586 | ret = -ENOMEM; |
| 587 | goto out; |
| 588 | } |
| 589 | |
| 590 | smb349_chg->client = client; |
| 591 | smb349_chg->chg_current_ma = pdata->chg_current_ma; |
| 592 | ret = gpio_request(pdata->chg_susp_gpio, "smb349_suspend"); |
| 593 | if (ret) { |
| 594 | dev_err(&client->dev, "%s gpio_request failed for %d ret=%d\n", |
| 595 | __func__, pdata->chg_susp_gpio, ret); |
| 596 | goto free_smb349_chg; |
| 597 | } |
| 598 | smb349_chg->chg_susp_gpio = pdata->chg_susp_gpio; |
| 599 | |
| 600 | ret = gpio_request(pdata->en_n_gpio, "smb349_charger_enable"); |
| 601 | if (ret) { |
| 602 | dev_err(&client->dev, "%s gpio_request failed for %d ret=%d\n", |
| 603 | __func__, pdata->en_n_gpio, ret); |
| 604 | goto chg_susp_gpio_fail; |
| 605 | } |
| 606 | smb349_chg->en_n_gpio = pdata->en_n_gpio; |
| 607 | |
| 608 | i2c_set_clientdata(client, smb349_chg); |
| 609 | |
| 610 | ret = smb349_hwinit(smb349_chg); |
| 611 | if (ret) |
| 612 | goto free_smb349_chg; |
| 613 | |
| 614 | ret = smb349_init_ext_chg(smb349_chg); |
| 615 | if (ret) |
| 616 | goto chg_en_gpio_fail; |
| 617 | |
| 618 | the_smb349_chg = smb349_chg; |
| 619 | |
David Keitel | 4853931 | 2012-06-06 14:57:12 -0700 | [diff] [blame] | 620 | spin_lock_init(&smb349_chg->lock); |
| 621 | |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 622 | create_debugfs_entries(smb349_chg); |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 623 | INIT_WORK(&smb349_chg->chg_work, chg_worker); |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 624 | |
| 625 | pr_info("OK connector present = %d\n", smb349_chg->present); |
| 626 | return 0; |
| 627 | |
| 628 | chg_en_gpio_fail: |
| 629 | gpio_free(smb349_chg->en_n_gpio); |
| 630 | chg_susp_gpio_fail: |
| 631 | gpio_free(smb349_chg->chg_susp_gpio); |
| 632 | free_smb349_chg: |
| 633 | kfree(smb349_chg); |
| 634 | out: |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | static int __devexit smb349_remove(struct i2c_client *client) |
| 639 | { |
| 640 | const struct smb349_platform_data *pdata; |
| 641 | struct smb349_struct *smb349_chg = i2c_get_clientdata(client); |
| 642 | |
David Keitel | e02d056 | 2012-05-09 16:07:00 -0700 | [diff] [blame] | 643 | flush_work(&smb349_chg->chg_work); |
David Keitel | c526ddb | 2012-02-15 11:32:58 -0800 | [diff] [blame] | 644 | pdata = client->dev.platform_data; |
| 645 | power_supply_unregister(&smb349_chg->dc_psy); |
| 646 | gpio_free(pdata->en_n_gpio); |
| 647 | gpio_free(pdata->chg_susp_gpio); |
| 648 | remove_debugfs_entries(smb349_chg); |
| 649 | kfree(smb349_chg); |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static int smb349_suspend(struct device *dev) |
| 654 | { |
| 655 | struct smb349_struct *smb349_chg = dev_get_drvdata(dev); |
| 656 | |
| 657 | pr_debug("suspend\n"); |
| 658 | if (smb349_chg->charging) |
| 659 | return -EBUSY; |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | static int smb349_resume(struct device *dev) |
| 664 | { |
| 665 | pr_debug("resume\n"); |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static const struct dev_pm_ops smb349_pm_ops = { |
| 671 | .suspend = smb349_suspend, |
| 672 | .resume = smb349_resume, |
| 673 | }; |
| 674 | |
| 675 | static const struct i2c_device_id smb349_id[] = { |
| 676 | {SMB349_NAME, 0}, |
| 677 | {}, |
| 678 | }; |
| 679 | MODULE_DEVICE_TABLE(i2c, smb349_id); |
| 680 | |
| 681 | static struct i2c_driver smb349_driver = { |
| 682 | .driver = { |
| 683 | .name = SMB349_NAME, |
| 684 | .owner = THIS_MODULE, |
| 685 | .pm = &smb349_pm_ops, |
| 686 | }, |
| 687 | .probe = smb349_probe, |
| 688 | .remove = __devexit_p(smb349_remove), |
| 689 | .id_table = smb349_id, |
| 690 | }; |
| 691 | |
| 692 | static int __init smb349_init(void) |
| 693 | { |
| 694 | return i2c_add_driver(&smb349_driver); |
| 695 | } |
| 696 | module_init(smb349_init); |
| 697 | |
| 698 | static void __exit smb349_exit(void) |
| 699 | { |
| 700 | return i2c_del_driver(&smb349_driver); |
| 701 | } |
| 702 | module_exit(smb349_exit); |
| 703 | |
| 704 | MODULE_DESCRIPTION("Driver for SMB349 charger chip"); |
| 705 | MODULE_LICENSE("GPL v2"); |
| 706 | MODULE_ALIAS("i2c:" SMB349_NAME); |