Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011 The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 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 | |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/gpio.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/debugfs.h> |
| 20 | #include <linux/workqueue.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/i2c/smb137b.h> |
| 24 | #include <linux/power_supply.h> |
| 25 | #include <linux/msm-charger.h> |
| 26 | |
| 27 | #define SMB137B_MASK(BITS, POS) ((unsigned char)(((1 << BITS) - 1) << POS)) |
| 28 | |
| 29 | #define CHG_CURRENT_REG 0x00 |
| 30 | #define FAST_CHG_CURRENT_MASK SMB137B_MASK(3, 5) |
| 31 | #define PRE_CHG_CURRENT_MASK SMB137B_MASK(2, 3) |
| 32 | #define TERM_CHG_CURRENT_MASK SMB137B_MASK(2, 1) |
| 33 | |
| 34 | #define INPUT_CURRENT_LIMIT_REG 0x01 |
| 35 | #define IN_CURRENT_MASK SMB137B_MASK(3, 5) |
| 36 | #define IN_CURRENT_LIMIT_EN_BIT BIT(2) |
| 37 | #define IN_CURRENT_DET_THRESH_MASK SMB137B_MASK(2, 0) |
| 38 | |
| 39 | #define FLOAT_VOLTAGE_REG 0x02 |
| 40 | #define STAT_OUT_POLARITY_BIT BIT(7) |
| 41 | #define FLOAT_VOLTAGE_MASK SMB137B_MASK(7, 0) |
| 42 | |
| 43 | #define CONTROL_A_REG 0x03 |
| 44 | #define AUTO_RECHARGE_DIS_BIT BIT(7) |
| 45 | #define CURR_CYCLE_TERM_BIT BIT(6) |
| 46 | #define PRE_TO_FAST_V_MASK SMB137B_MASK(3, 3) |
| 47 | #define TEMP_BEHAV_BIT BIT(2) |
| 48 | #define THERM_NTC_CURR_MODE_BIT BIT(1) |
| 49 | #define THERM_NTC_47KOHM_BIT BIT(0) |
| 50 | |
| 51 | #define CONTROL_B_REG 0x04 |
| 52 | #define STAT_OUTPUT_MODE_MASK SMB137B_MASK(2, 6) |
| 53 | #define BATT_OV_ENDS_CYCLE_BIT BIT(5) |
| 54 | #define AUTO_PRE_TO_FAST_DIS_BIT BIT(4) |
| 55 | #define SAFETY_TIMER_EN_BIT BIT(3) |
| 56 | #define OTG_LBR_WD_EN_BIT BIT(2) |
| 57 | #define CHG_WD_TIMER_EN_BIT BIT(1) |
| 58 | #define IRQ_OP_MASK BIT(0) |
| 59 | |
| 60 | #define PIN_CTRL_REG 0x05 |
| 61 | #define AUTO_CHG_EN_BIT BIT(7) |
| 62 | #define AUTO_LBR_EN_BIT BIT(6) |
| 63 | #define OTG_LBR_BIT BIT(5) |
| 64 | #define I2C_PIN_BIT BIT(4) |
| 65 | #define PIN_EN_CTRL_MASK SMB137B_MASK(2, 2) |
| 66 | #define OTG_LBR_PIN_CTRL_MASK SMB137B_MASK(2, 0) |
| 67 | |
| 68 | #define OTG_LBR_CTRL_REG 0x06 |
| 69 | #define BATT_MISSING_DET_EN_BIT BIT(7) |
| 70 | #define AUTO_RECHARGE_THRESH_MASK BIT(6) |
| 71 | #define USB_DP_DN_DET_EN_MASK BIT(5) |
| 72 | #define OTG_LBR_BATT_CURRENT_LIMIT_MASK SMB137B_MASK(2, 3) |
| 73 | #define OTG_LBR_UVLO_THRESH_MASK SMB137B_MASK(3, 0) |
| 74 | |
| 75 | #define FAULT_INTR_REG 0x07 |
| 76 | #define SAFETY_TIMER_EXP_MASK SMB137B_MASK(1, 7) |
| 77 | #define BATT_TEMP_UNSAFE_MASK SMB137B_MASK(1, 6) |
| 78 | #define INPUT_OVLO_IVLO_MASK SMB137B_MASK(1, 5) |
| 79 | #define BATT_OVLO_MASK SMB137B_MASK(1, 4) |
| 80 | #define INTERNAL_OVER_TEMP_MASK SMB137B_MASK(1, 2) |
| 81 | #define ENTER_TAPER_CHG_MASK SMB137B_MASK(1, 1) |
| 82 | #define CHG_MASK SMB137B_MASK(1, 0) |
| 83 | |
| 84 | #define CELL_TEMP_MON_REG 0x08 |
| 85 | #define THERMISTOR_CURR_MASK SMB137B_MASK(2, 6) |
| 86 | #define LOW_TEMP_CHG_INHIBIT_MASK SMB137B_MASK(3, 3) |
| 87 | #define HIGH_TEMP_CHG_INHIBIT_MASK SMB137B_MASK(3, 0) |
| 88 | |
| 89 | #define SAFETY_TIMER_THERMAL_SHUTDOWN_REG 0x09 |
| 90 | #define DCIN_OVLO_SEL_MASK SMB137B_MASK(2, 7) |
| 91 | #define RELOAD_EN_INPUT_VOLTAGE_MASK SMB137B_MASK(1, 6) |
| 92 | #define THERM_SHUTDN_EN_MASK SMB137B_MASK(1, 5) |
| 93 | #define STANDBY_WD_TIMER_EN_MASK SMB137B_MASK(1, 4) |
| 94 | #define COMPLETE_CHG_TMOUT_MASK SMB137B_MASK(2, 2) |
| 95 | #define PRE_CHG_TMOUT_MASK SMB137B_MASK(2, 0) |
| 96 | |
| 97 | #define VSYS_REG 0x0A |
| 98 | #define VSYS_MASK SMB137B_MASK(3, 4) |
| 99 | |
| 100 | #define IRQ_RESET_REG 0x30 |
| 101 | |
| 102 | #define COMMAND_A_REG 0x31 |
| 103 | #define VOLATILE_REGS_WRITE_PERM_BIT BIT(7) |
| 104 | #define POR_BIT BIT(6) |
| 105 | #define FAST_CHG_SETTINGS_BIT BIT(5) |
| 106 | #define BATT_CHG_EN_BIT BIT(4) |
| 107 | #define USBIN_MODE_500_BIT BIT(3) |
| 108 | #define USBIN_MODE_HCMODE_BIT BIT(2) |
| 109 | #define OTG_LBR_EN_BIT BIT(1) |
| 110 | #define STAT_OE_BIT BIT(0) |
| 111 | |
| 112 | #define STATUS_A_REG 0x32 |
| 113 | #define INTERNAL_TEMP_IRQ_STAT BIT(4) |
| 114 | #define DCIN_OV_IRQ_STAT BIT(3) |
| 115 | #define DCIN_UV_IRQ_STAT BIT(2) |
| 116 | #define USBIN_OV_IRQ_STAT BIT(1) |
| 117 | #define USBIN_UV_IRQ_STAT BIT(0) |
| 118 | |
| 119 | #define STATUS_B_REG 0x33 |
| 120 | #define USB_PIN_STAT BIT(7) |
| 121 | #define USB51_MODE_STAT BIT(6) |
| 122 | #define USB51_HC_MODE_STAT BIT(5) |
| 123 | #define INTERNAL_TEMP_LIMIT_B_STAT BIT(4) |
| 124 | #define DC_IN_OV_STAT BIT(3) |
| 125 | #define DC_IN_UV_STAT BIT(2) |
| 126 | #define USB_IN_OV_STAT BIT(1) |
| 127 | #define USB_IN_UV_STAT BIT(0) |
| 128 | |
| 129 | #define STATUS_C_REG 0x34 |
| 130 | #define AUTO_IN_CURR_LIMIT_MASK SMB137B_MASK(4, 4) |
| 131 | #define AUTO_IN_CURR_LIMIT_STAT BIT(3) |
| 132 | #define AUTO_SOURCE_DET_COMP_STAT_MASK SMB137B_MASK(2, 1) |
| 133 | #define AUTO_SOURCE_DET_RESULT_STAT BIT(0) |
| 134 | |
| 135 | #define STATUS_D_REG 0x35 |
| 136 | #define VBATT_LESS_THAN_VSYS_STAT BIT(7) |
| 137 | #define USB_FAIL_STAT BIT(6) |
| 138 | #define BATT_TEMP_STAT_MASK SMB137B_MASK(2, 4) |
| 139 | #define INTERNAL_TEMP_LIMIT_STAT BIT(2) |
| 140 | #define OTG_LBR_MODE_EN_STAT BIT(1) |
| 141 | #define OTG_LBR_VBATT_UVLO_STAT BIT(0) |
| 142 | |
| 143 | #define STATUS_E_REG 0x36 |
| 144 | #define CHARGE_CYCLE_COUNT_STAT BIT(7) |
| 145 | #define CHARGER_TERM_STAT BIT(6) |
| 146 | #define SAFETY_TIMER_STAT_MASK SMB137B_MASK(2, 4) |
| 147 | #define CHARGER_ERROR_STAT BIT(3) |
| 148 | #define CHARGING_STAT_E SMB137B_MASK(2, 1) |
| 149 | #define CHARGING_EN BIT(0) |
| 150 | |
| 151 | #define STATUS_F_REG 0x37 |
| 152 | #define WD_IRQ_ACTIVE_STAT BIT(7) |
| 153 | #define OTG_OVERCURRENT_STAT BIT(6) |
| 154 | #define BATT_PRESENT_STAT BIT(4) |
| 155 | #define BATT_OV_LATCHED_STAT BIT(3) |
| 156 | #define CHARGER_OVLO_STAT BIT(2) |
| 157 | #define CHARGER_UVLO_STAT BIT(1) |
| 158 | #define BATT_LOW_STAT BIT(0) |
| 159 | |
| 160 | #define STATUS_G_REG 0x38 |
| 161 | #define CHARGE_TIMEOUT_IRQ_STAT BIT(7) |
| 162 | #define PRECHARGE_TIMEOUT_IRQ_STAT BIT(6) |
| 163 | #define BATT_HOT_IRQ_STAT BIT(5) |
| 164 | #define BATT_COLD_IRQ_STAT BIT(4) |
| 165 | #define BATT_OV_IRQ_STAT BIT(3) |
| 166 | #define TAPER_CHG_IRQ_STAT BIT(2) |
| 167 | #define FAST_CHG_IRQ_STAT BIT(1) |
| 168 | #define CHARGING_IRQ_STAT BIT(0) |
| 169 | |
| 170 | #define STATUS_H_REG 0x39 |
| 171 | #define CHARGE_TIMEOUT_STAT BIT(7) |
| 172 | #define PRECHARGE_TIMEOUT_STAT BIT(6) |
| 173 | #define BATT_HOT_STAT BIT(5) |
| 174 | #define BATT_COLD_STAT BIT(4) |
| 175 | #define BATT_OV_STAT BIT(3) |
| 176 | #define TAPER_CHG_STAT BIT(2) |
| 177 | #define FAST_CHG_STAT BIT(1) |
| 178 | #define CHARGING_STAT_H BIT(0) |
| 179 | |
| 180 | #define DEV_ID_REG 0x3B |
| 181 | |
| 182 | #define COMMAND_B_REG 0x3C |
| 183 | #define THERM_NTC_CURR_VERRIDE BIT(7) |
| 184 | |
| 185 | #define SMB137B_CHG_PERIOD ((HZ) * 150) |
| 186 | |
| 187 | #define INPUT_CURRENT_REG_DEFAULT 0xE1 |
| 188 | #define INPUT_CURRENT_REG_MIN 0x01 |
| 189 | #define COMMAND_A_REG_DEFAULT 0xA0 |
| 190 | #define COMMAND_A_REG_OTG_MODE 0xA2 |
| 191 | |
| 192 | #define PIN_CTRL_REG_DEFAULT 0x00 |
| 193 | #define PIN_CTRL_REG_CHG_OFF 0x04 |
| 194 | |
| 195 | #define FAST_CHG_E_STATUS 0x2 |
| 196 | |
| 197 | #define SMB137B_DEFAULT_BATT_RATING 950 |
| 198 | struct smb137b_data { |
| 199 | struct i2c_client *client; |
| 200 | struct delayed_work charge_work; |
| 201 | |
| 202 | bool charging; |
| 203 | int chgcurrent; |
| 204 | int cur_charging_mode; |
| 205 | int max_system_voltage; |
| 206 | int min_system_voltage; |
| 207 | |
| 208 | int valid_n_gpio; |
| 209 | |
| 210 | int batt_status; |
| 211 | int batt_chg_type; |
| 212 | int batt_present; |
| 213 | int min_design; |
| 214 | int max_design; |
| 215 | int batt_mah_rating; |
| 216 | |
| 217 | int usb_status; |
| 218 | |
| 219 | u8 dev_id_reg; |
| 220 | struct msm_hardware_charger adapter_hw_chg; |
| 221 | }; |
| 222 | |
| 223 | static unsigned int disabled; |
| 224 | static DEFINE_MUTEX(init_lock); |
| 225 | static unsigned int init_otg_power; |
| 226 | |
| 227 | enum charger_stat { |
| 228 | SMB137B_ABSENT, |
| 229 | SMB137B_PRESENT, |
| 230 | SMB137B_ENUMERATED, |
| 231 | }; |
| 232 | |
| 233 | static struct smb137b_data *usb_smb137b_chg; |
| 234 | |
| 235 | static int smb137b_read_reg(struct i2c_client *client, int reg, |
| 236 | u8 *val) |
| 237 | { |
| 238 | s32 ret; |
| 239 | struct smb137b_data *smb137b_chg; |
| 240 | |
| 241 | smb137b_chg = i2c_get_clientdata(client); |
| 242 | ret = i2c_smbus_read_byte_data(smb137b_chg->client, reg); |
| 243 | if (ret < 0) { |
| 244 | dev_err(&smb137b_chg->client->dev, |
| 245 | "i2c read fail: can't read from %02x: %d\n", reg, ret); |
| 246 | return ret; |
| 247 | } else |
| 248 | *val = ret; |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int smb137b_write_reg(struct i2c_client *client, int reg, |
| 254 | u8 val) |
| 255 | { |
| 256 | s32 ret; |
| 257 | struct smb137b_data *smb137b_chg; |
| 258 | |
| 259 | smb137b_chg = i2c_get_clientdata(client); |
| 260 | ret = i2c_smbus_write_byte_data(smb137b_chg->client, reg, val); |
| 261 | if (ret < 0) { |
| 262 | dev_err(&smb137b_chg->client->dev, |
| 263 | "i2c write fail: can't write %02x to %02x: %d\n", |
| 264 | val, reg, ret); |
| 265 | return ret; |
| 266 | } |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static ssize_t id_reg_show(struct device *dev, struct device_attribute *attr, |
| 271 | char *buf) |
| 272 | { |
| 273 | struct smb137b_data *smb137b_chg; |
| 274 | |
| 275 | smb137b_chg = i2c_get_clientdata(to_i2c_client(dev)); |
| 276 | |
| 277 | return sprintf(buf, "%02x\n", smb137b_chg->dev_id_reg); |
| 278 | } |
| 279 | static DEVICE_ATTR(id_reg, S_IRUGO | S_IWUSR, id_reg_show, NULL); |
| 280 | |
| 281 | #ifdef DEBUG |
| 282 | static void smb137b_dbg_print_status_regs(struct smb137b_data *smb137b_chg) |
| 283 | { |
| 284 | int ret; |
| 285 | u8 temp; |
| 286 | |
| 287 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_A_REG, &temp); |
| 288 | dev_dbg(&smb137b_chg->client->dev, "%s A=0x%x\n", __func__, temp); |
| 289 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_B_REG, &temp); |
| 290 | dev_dbg(&smb137b_chg->client->dev, "%s B=0x%x\n", __func__, temp); |
| 291 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_C_REG, &temp); |
| 292 | dev_dbg(&smb137b_chg->client->dev, "%s C=0x%x\n", __func__, temp); |
| 293 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_D_REG, &temp); |
| 294 | dev_dbg(&smb137b_chg->client->dev, "%s D=0x%x\n", __func__, temp); |
| 295 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_E_REG, &temp); |
| 296 | dev_dbg(&smb137b_chg->client->dev, "%s E=0x%x\n", __func__, temp); |
| 297 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_F_REG, &temp); |
| 298 | dev_dbg(&smb137b_chg->client->dev, "%s F=0x%x\n", __func__, temp); |
| 299 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_G_REG, &temp); |
| 300 | dev_dbg(&smb137b_chg->client->dev, "%s G=0x%x\n", __func__, temp); |
| 301 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_H_REG, &temp); |
| 302 | dev_dbg(&smb137b_chg->client->dev, "%s H=0x%x\n", __func__, temp); |
| 303 | } |
| 304 | #else |
| 305 | static void smb137b_dbg_print_status_regs(struct smb137b_data *smb137b_chg) |
| 306 | { |
| 307 | } |
| 308 | #endif |
| 309 | |
| 310 | static int smb137b_start_charging(struct msm_hardware_charger *hw_chg, |
| 311 | int chg_voltage, int chg_current) |
| 312 | { |
| 313 | int cmd_val = COMMAND_A_REG_DEFAULT; |
| 314 | u8 temp = 0; |
| 315 | int ret = 0; |
| 316 | struct smb137b_data *smb137b_chg; |
| 317 | |
| 318 | smb137b_chg = container_of(hw_chg, struct smb137b_data, adapter_hw_chg); |
| 319 | if (disabled) { |
| 320 | dev_err(&smb137b_chg->client->dev, |
| 321 | "%s called when disabled\n", __func__); |
| 322 | goto out; |
| 323 | } |
| 324 | |
| 325 | if (smb137b_chg->charging == true |
| 326 | && smb137b_chg->chgcurrent == chg_current) |
| 327 | /* we are already charging with the same current*/ |
| 328 | dev_err(&smb137b_chg->client->dev, |
| 329 | "%s charge with same current %d called again\n", |
| 330 | __func__, chg_current); |
| 331 | |
| 332 | dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__); |
| 333 | if (chg_current < 500) |
| 334 | cmd_val &= ~USBIN_MODE_500_BIT; |
| 335 | else if (chg_current == 500) |
| 336 | cmd_val |= USBIN_MODE_500_BIT; |
| 337 | else |
| 338 | cmd_val |= USBIN_MODE_HCMODE_BIT; |
| 339 | |
| 340 | smb137b_chg->chgcurrent = chg_current; |
| 341 | smb137b_chg->cur_charging_mode = cmd_val; |
| 342 | |
| 343 | /* Due to non-volatile reload feature,always enable volatile |
| 344 | * mirror writes before modifying any 00h~09h control register. |
| 345 | * Current mode needs to be programmed according to what's detected |
| 346 | * Otherwise default 100mA mode might cause VOUTL drop and fail |
| 347 | * the system in case of dead battery. |
| 348 | */ |
| 349 | ret = smb137b_write_reg(smb137b_chg->client, |
| 350 | COMMAND_A_REG, cmd_val); |
| 351 | if (ret) { |
| 352 | dev_err(&smb137b_chg->client->dev, |
| 353 | "%s couldn't write to command_reg\n", __func__); |
| 354 | goto out; |
| 355 | } |
| 356 | ret = smb137b_write_reg(smb137b_chg->client, |
| 357 | PIN_CTRL_REG, PIN_CTRL_REG_DEFAULT); |
| 358 | if (ret) { |
| 359 | dev_err(&smb137b_chg->client->dev, |
| 360 | "%s couldn't write to pin ctrl reg\n", __func__); |
| 361 | goto out; |
| 362 | } |
| 363 | smb137b_chg->charging = true; |
| 364 | smb137b_chg->batt_status = POWER_SUPPLY_STATUS_CHARGING; |
| 365 | smb137b_chg->batt_chg_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 366 | |
| 367 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_E_REG, &temp); |
| 368 | if (ret) { |
| 369 | dev_err(&smb137b_chg->client->dev, |
| 370 | "%s couldn't read status e reg %d\n", __func__, ret); |
| 371 | } else { |
| 372 | if (temp & CHARGER_ERROR_STAT) { |
| 373 | dev_err(&smb137b_chg->client->dev, |
| 374 | "%s chg error E=0x%x\n", __func__, temp); |
| 375 | smb137b_dbg_print_status_regs(smb137b_chg); |
| 376 | } |
| 377 | if (((temp & CHARGING_STAT_E) >> 1) >= FAST_CHG_E_STATUS) |
| 378 | smb137b_chg->batt_chg_type |
| 379 | = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 380 | } |
| 381 | /*schedule charge_work to keep track of battery charging state*/ |
| 382 | schedule_delayed_work(&smb137b_chg->charge_work, SMB137B_CHG_PERIOD); |
| 383 | |
| 384 | out: |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | static int smb137b_stop_charging(struct msm_hardware_charger *hw_chg) |
| 389 | { |
| 390 | int ret = 0; |
| 391 | struct smb137b_data *smb137b_chg; |
| 392 | |
| 393 | smb137b_chg = container_of(hw_chg, struct smb137b_data, adapter_hw_chg); |
| 394 | |
| 395 | dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__); |
| 396 | if (smb137b_chg->charging == false) |
| 397 | return 0; |
| 398 | |
| 399 | smb137b_chg->charging = false; |
| 400 | smb137b_chg->batt_status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 401 | smb137b_chg->batt_chg_type = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 402 | |
| 403 | ret = smb137b_write_reg(smb137b_chg->client, COMMAND_A_REG, |
| 404 | smb137b_chg->cur_charging_mode); |
| 405 | if (ret) { |
| 406 | dev_err(&smb137b_chg->client->dev, |
| 407 | "%s couldn't write to command_reg\n", __func__); |
| 408 | goto out; |
| 409 | } |
| 410 | |
| 411 | ret = smb137b_write_reg(smb137b_chg->client, |
| 412 | PIN_CTRL_REG, PIN_CTRL_REG_CHG_OFF); |
| 413 | if (ret) |
| 414 | dev_err(&smb137b_chg->client->dev, |
| 415 | "%s couldn't write to pin ctrl reg\n", __func__); |
| 416 | |
| 417 | out: |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | static int smb137b_charger_switch(struct msm_hardware_charger *hw_chg) |
| 422 | { |
| 423 | struct smb137b_data *smb137b_chg; |
| 424 | |
| 425 | smb137b_chg = container_of(hw_chg, struct smb137b_data, adapter_hw_chg); |
| 426 | dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static irqreturn_t smb137b_valid_handler(int irq, void *dev_id) |
| 431 | { |
| 432 | int val; |
| 433 | struct smb137b_data *smb137b_chg; |
| 434 | struct i2c_client *client = dev_id; |
| 435 | |
| 436 | smb137b_chg = i2c_get_clientdata(client); |
| 437 | |
| 438 | pr_debug("%s Cable Detected USB inserted\n", __func__); |
| 439 | /*extra delay needed to allow CABLE_DET_N settling down and debounce |
| 440 | before trying to sample its correct value*/ |
| 441 | usleep_range(1000, 1200); |
| 442 | val = gpio_get_value_cansleep(smb137b_chg->valid_n_gpio); |
| 443 | if (val < 0) { |
| 444 | dev_err(&smb137b_chg->client->dev, |
| 445 | "%s gpio_get_value failed for %d ret=%d\n", __func__, |
| 446 | smb137b_chg->valid_n_gpio, val); |
| 447 | goto err; |
| 448 | } |
| 449 | dev_dbg(&smb137b_chg->client->dev, "%s val=%d\n", __func__, val); |
| 450 | |
| 451 | if (val) { |
| 452 | if (smb137b_chg->usb_status != SMB137B_ABSENT) { |
| 453 | smb137b_chg->usb_status = SMB137B_ABSENT; |
| 454 | msm_charger_notify_event(&smb137b_chg->adapter_hw_chg, |
| 455 | CHG_REMOVED_EVENT); |
| 456 | } |
| 457 | } else { |
| 458 | if (smb137b_chg->usb_status == SMB137B_ABSENT) { |
| 459 | smb137b_chg->usb_status = SMB137B_PRESENT; |
| 460 | msm_charger_notify_event(&smb137b_chg->adapter_hw_chg, |
| 461 | CHG_INSERTED_EVENT); |
| 462 | } |
| 463 | } |
| 464 | err: |
| 465 | return IRQ_HANDLED; |
| 466 | } |
| 467 | |
| 468 | #ifdef CONFIG_DEBUG_FS |
| 469 | static struct dentry *dent; |
| 470 | static int debug_fs_otg; |
| 471 | static int otg_get(void *data, u64 *value) |
| 472 | { |
| 473 | *value = debug_fs_otg; |
| 474 | return 0; |
| 475 | } |
| 476 | static int otg_set(void *data, u64 value) |
| 477 | { |
| 478 | smb137b_otg_power(debug_fs_otg); |
| 479 | return 0; |
| 480 | } |
| 481 | DEFINE_SIMPLE_ATTRIBUTE(smb137b_otg_fops, otg_get, otg_set, "%llu\n"); |
| 482 | |
| 483 | static void smb137b_create_debugfs_entries(struct smb137b_data *smb137b_chg) |
| 484 | { |
| 485 | dent = debugfs_create_dir("smb137b", NULL); |
| 486 | if (dent) { |
| 487 | debugfs_create_file("otg", 0644, dent, NULL, &smb137b_otg_fops); |
| 488 | } |
| 489 | } |
| 490 | static void smb137b_destroy_debugfs_entries(void) |
| 491 | { |
| 492 | if (dent) |
| 493 | debugfs_remove_recursive(dent); |
| 494 | } |
| 495 | #else |
| 496 | static void smb137b_create_debugfs_entries(struct smb137b_data *smb137b_chg) |
| 497 | { |
| 498 | } |
| 499 | static void smb137b_destroy_debugfs_entries(void) |
| 500 | { |
| 501 | } |
| 502 | #endif |
| 503 | |
| 504 | static int set_disable_status_param(const char *val, struct kernel_param *kp) |
| 505 | { |
| 506 | int ret; |
| 507 | |
| 508 | ret = param_set_int(val, kp); |
| 509 | if (ret) |
| 510 | return ret; |
| 511 | |
| 512 | if (usb_smb137b_chg && disabled) |
| 513 | msm_charger_notify_event(&usb_smb137b_chg->adapter_hw_chg, |
| 514 | CHG_DONE_EVENT); |
| 515 | |
| 516 | pr_debug("%s disabled =%d\n", __func__, disabled); |
| 517 | return 0; |
| 518 | } |
| 519 | module_param_call(disabled, set_disable_status_param, param_get_uint, |
| 520 | &disabled, 0644); |
| 521 | static void smb137b_charge_sm(struct work_struct *smb137b_work) |
| 522 | { |
| 523 | int ret; |
| 524 | struct smb137b_data *smb137b_chg; |
| 525 | u8 temp = 0; |
| 526 | int notify_batt_changed = 0; |
| 527 | |
| 528 | smb137b_chg = container_of(smb137b_work, struct smb137b_data, |
| 529 | charge_work.work); |
| 530 | |
| 531 | /*if not charging, exit smb137b charging state transition*/ |
| 532 | if (!smb137b_chg->charging) |
| 533 | return; |
| 534 | |
| 535 | dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__); |
| 536 | |
| 537 | ret = smb137b_read_reg(smb137b_chg->client, STATUS_F_REG, &temp); |
| 538 | if (ret) { |
| 539 | dev_err(&smb137b_chg->client->dev, |
| 540 | "%s couldn't read status f reg %d\n", __func__, ret); |
| 541 | goto out; |
| 542 | } |
| 543 | if (smb137b_chg->batt_present != !(temp & BATT_PRESENT_STAT)) { |
| 544 | smb137b_chg->batt_present = !(temp & BATT_PRESENT_STAT); |
| 545 | notify_batt_changed = 1; |
| 546 | } |
| 547 | |
| 548 | if (!smb137b_chg->batt_present) |
| 549 | smb137b_chg->batt_chg_type = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 550 | |
| 551 | if (!smb137b_chg->batt_present && smb137b_chg->charging) |
| 552 | msm_charger_notify_event(&smb137b_chg->adapter_hw_chg, |
| 553 | CHG_DONE_EVENT); |
| 554 | |
| 555 | if (smb137b_chg->batt_present |
| 556 | && smb137b_chg->charging |
| 557 | && smb137b_chg->batt_chg_type |
| 558 | != POWER_SUPPLY_CHARGE_TYPE_FAST) { |
| 559 | ret = smb137b_read_reg(smb137b_chg->client, |
| 560 | STATUS_E_REG, &temp); |
| 561 | if (ret) { |
| 562 | dev_err(&smb137b_chg->client->dev, |
| 563 | "%s couldn't read cntrl reg\n", __func__); |
| 564 | goto out; |
| 565 | |
| 566 | } else { |
| 567 | if (temp & CHARGER_ERROR_STAT) { |
| 568 | dev_err(&smb137b_chg->client->dev, |
| 569 | "%s error E=0x%x\n", __func__, temp); |
| 570 | smb137b_dbg_print_status_regs(smb137b_chg); |
| 571 | } |
| 572 | if (((temp & CHARGING_STAT_E) >> 1) |
| 573 | >= FAST_CHG_E_STATUS) { |
| 574 | smb137b_chg->batt_chg_type |
| 575 | = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 576 | notify_batt_changed = 1; |
| 577 | msm_charger_notify_event( |
| 578 | &smb137b_chg->adapter_hw_chg, |
| 579 | CHG_BATT_BEGIN_FAST_CHARGING); |
| 580 | } else { |
| 581 | smb137b_chg->batt_chg_type |
| 582 | = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | out: |
| 588 | schedule_delayed_work(&smb137b_chg->charge_work, |
| 589 | SMB137B_CHG_PERIOD); |
| 590 | } |
| 591 | |
| 592 | static void __smb137b_otg_power(int on) |
| 593 | { |
| 594 | int ret; |
| 595 | |
| 596 | if (on) { |
| 597 | ret = smb137b_write_reg(usb_smb137b_chg->client, |
| 598 | PIN_CTRL_REG, PIN_CTRL_REG_CHG_OFF); |
| 599 | if (ret) { |
| 600 | pr_err("%s turning off charging in pin_ctrl err=%d\n", |
| 601 | __func__, ret); |
| 602 | /* |
| 603 | * don't change the command register if charging in |
| 604 | * pin control cannot be turned off |
| 605 | */ |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | ret = smb137b_write_reg(usb_smb137b_chg->client, |
| 610 | COMMAND_A_REG, COMMAND_A_REG_OTG_MODE); |
| 611 | if (ret) |
| 612 | pr_err("%s failed turning on OTG mode ret=%d\n", |
| 613 | __func__, ret); |
| 614 | } else { |
| 615 | ret = smb137b_write_reg(usb_smb137b_chg->client, |
| 616 | COMMAND_A_REG, COMMAND_A_REG_DEFAULT); |
| 617 | if (ret) |
| 618 | pr_err("%s failed turning off OTG mode ret=%d\n", |
| 619 | __func__, ret); |
| 620 | ret = smb137b_write_reg(usb_smb137b_chg->client, |
| 621 | PIN_CTRL_REG, PIN_CTRL_REG_DEFAULT); |
| 622 | if (ret) |
| 623 | pr_err("%s failed writing to pn_ctrl ret=%d\n", |
| 624 | __func__, ret); |
| 625 | } |
| 626 | } |
| 627 | static int __devinit smb137b_probe(struct i2c_client *client, |
| 628 | const struct i2c_device_id *id) |
| 629 | { |
| 630 | const struct smb137b_platform_data *pdata; |
| 631 | struct smb137b_data *smb137b_chg; |
| 632 | int ret = 0; |
| 633 | |
| 634 | pdata = client->dev.platform_data; |
| 635 | |
| 636 | if (pdata == NULL) { |
| 637 | dev_err(&client->dev, "%s no platform data\n", __func__); |
| 638 | ret = -EINVAL; |
| 639 | goto out; |
| 640 | } |
| 641 | |
| 642 | if (!i2c_check_functionality(client->adapter, |
| 643 | I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 644 | ret = -EIO; |
| 645 | goto out; |
| 646 | } |
| 647 | |
| 648 | smb137b_chg = kzalloc(sizeof(*smb137b_chg), GFP_KERNEL); |
| 649 | if (!smb137b_chg) { |
| 650 | ret = -ENOMEM; |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | INIT_DELAYED_WORK(&smb137b_chg->charge_work, smb137b_charge_sm); |
| 655 | smb137b_chg->client = client; |
| 656 | smb137b_chg->valid_n_gpio = pdata->valid_n_gpio; |
| 657 | smb137b_chg->batt_mah_rating = pdata->batt_mah_rating; |
| 658 | if (smb137b_chg->batt_mah_rating == 0) |
| 659 | smb137b_chg->batt_mah_rating = SMB137B_DEFAULT_BATT_RATING; |
| 660 | |
| 661 | /*It supports USB-WALL charger and PC USB charger */ |
| 662 | smb137b_chg->adapter_hw_chg.type = CHG_TYPE_USB; |
| 663 | smb137b_chg->adapter_hw_chg.rating = pdata->batt_mah_rating; |
| 664 | smb137b_chg->adapter_hw_chg.name = "smb137b-charger"; |
| 665 | smb137b_chg->adapter_hw_chg.start_charging = smb137b_start_charging; |
| 666 | smb137b_chg->adapter_hw_chg.stop_charging = smb137b_stop_charging; |
| 667 | smb137b_chg->adapter_hw_chg.charging_switched = smb137b_charger_switch; |
| 668 | |
| 669 | if (pdata->chg_detection_config) |
| 670 | ret = pdata->chg_detection_config(); |
| 671 | if (ret) { |
| 672 | dev_err(&client->dev, "%s valid config failed ret=%d\n", |
| 673 | __func__, ret); |
| 674 | goto free_smb137b_chg; |
| 675 | } |
| 676 | |
| 677 | ret = gpio_request(pdata->valid_n_gpio, "smb137b_charger_valid"); |
| 678 | if (ret) { |
| 679 | dev_err(&client->dev, "%s gpio_request failed for %d ret=%d\n", |
| 680 | __func__, pdata->valid_n_gpio, ret); |
| 681 | goto free_smb137b_chg; |
| 682 | } |
| 683 | |
| 684 | i2c_set_clientdata(client, smb137b_chg); |
| 685 | |
| 686 | ret = msm_charger_register(&smb137b_chg->adapter_hw_chg); |
| 687 | if (ret) { |
| 688 | dev_err(&client->dev, "%s msm_charger_register\ |
| 689 | failed for ret=%d\n", __func__, ret); |
| 690 | goto free_valid_gpio; |
| 691 | } |
| 692 | |
| 693 | ret = irq_set_irq_wake(client->irq, 1); |
| 694 | if (ret) { |
| 695 | dev_err(&client->dev, "%s failed for irq_set_irq_wake %d ret =%d\n", |
| 696 | __func__, client->irq, ret); |
| 697 | goto unregister_charger; |
| 698 | } |
| 699 | |
| 700 | ret = request_threaded_irq(client->irq, NULL, |
| 701 | smb137b_valid_handler, |
| 702 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 703 | "smb137b_charger_valid", client); |
| 704 | if (ret) { |
| 705 | dev_err(&client->dev, |
| 706 | "%s request_threaded_irq failed for %d ret =%d\n", |
| 707 | __func__, client->irq, ret); |
| 708 | goto disable_irq_wake; |
| 709 | } |
| 710 | |
| 711 | ret = gpio_get_value_cansleep(smb137b_chg->valid_n_gpio); |
| 712 | if (ret < 0) { |
| 713 | dev_err(&client->dev, |
| 714 | "%s gpio_get_value failed for %d ret=%d\n", __func__, |
| 715 | pdata->valid_n_gpio, ret); |
| 716 | /* assume absent */ |
| 717 | ret = 1; |
| 718 | } |
| 719 | if (!ret) { |
| 720 | msm_charger_notify_event(&smb137b_chg->adapter_hw_chg, |
| 721 | CHG_INSERTED_EVENT); |
| 722 | smb137b_chg->usb_status = SMB137B_PRESENT; |
| 723 | } |
| 724 | |
| 725 | ret = smb137b_read_reg(smb137b_chg->client, DEV_ID_REG, |
| 726 | &smb137b_chg->dev_id_reg); |
| 727 | |
| 728 | ret = device_create_file(&smb137b_chg->client->dev, &dev_attr_id_reg); |
| 729 | |
| 730 | /* TODO read min_design and max_design from chip registers */ |
| 731 | smb137b_chg->min_design = 3200; |
| 732 | smb137b_chg->max_design = 4200; |
| 733 | |
| 734 | smb137b_chg->batt_status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 735 | smb137b_chg->batt_chg_type = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 736 | |
| 737 | device_init_wakeup(&client->dev, 1); |
| 738 | |
| 739 | mutex_lock(&init_lock); |
| 740 | usb_smb137b_chg = smb137b_chg; |
| 741 | if (init_otg_power) |
| 742 | __smb137b_otg_power(init_otg_power); |
| 743 | mutex_unlock(&init_lock); |
| 744 | |
| 745 | smb137b_create_debugfs_entries(smb137b_chg); |
| 746 | dev_dbg(&client->dev, |
| 747 | "%s OK device_id = %x chg_state=%d\n", __func__, |
| 748 | smb137b_chg->dev_id_reg, smb137b_chg->usb_status); |
| 749 | return 0; |
| 750 | |
| 751 | disable_irq_wake: |
| 752 | irq_set_irq_wake(client->irq, 0); |
| 753 | unregister_charger: |
| 754 | msm_charger_unregister(&smb137b_chg->adapter_hw_chg); |
| 755 | free_valid_gpio: |
| 756 | gpio_free(pdata->valid_n_gpio); |
| 757 | free_smb137b_chg: |
| 758 | kfree(smb137b_chg); |
| 759 | out: |
| 760 | return ret; |
| 761 | } |
| 762 | |
| 763 | void smb137b_otg_power(int on) |
| 764 | { |
| 765 | pr_debug("%s Enter on=%d\n", __func__, on); |
| 766 | |
| 767 | mutex_lock(&init_lock); |
| 768 | if (!usb_smb137b_chg) { |
| 769 | init_otg_power = !!on; |
| 770 | pr_warning("%s called when not initialized\n", __func__); |
| 771 | mutex_unlock(&init_lock); |
| 772 | return; |
| 773 | } |
| 774 | __smb137b_otg_power(on); |
| 775 | mutex_unlock(&init_lock); |
| 776 | } |
| 777 | |
| 778 | static int __devexit smb137b_remove(struct i2c_client *client) |
| 779 | { |
| 780 | const struct smb137b_platform_data *pdata; |
| 781 | struct smb137b_data *smb137b_chg = i2c_get_clientdata(client); |
| 782 | |
| 783 | pdata = client->dev.platform_data; |
| 784 | device_init_wakeup(&client->dev, 0); |
| 785 | irq_set_irq_wake(client->irq, 0); |
| 786 | free_irq(client->irq, client); |
| 787 | gpio_free(pdata->valid_n_gpio); |
| 788 | cancel_delayed_work_sync(&smb137b_chg->charge_work); |
| 789 | |
| 790 | msm_charger_notify_event(&smb137b_chg->adapter_hw_chg, |
| 791 | CHG_REMOVED_EVENT); |
| 792 | msm_charger_unregister(&smb137b_chg->adapter_hw_chg); |
| 793 | smb137b_destroy_debugfs_entries(); |
| 794 | kfree(smb137b_chg); |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | #ifdef CONFIG_PM |
| 799 | static int smb137b_suspend(struct device *dev) |
| 800 | { |
| 801 | struct smb137b_data *smb137b_chg = dev_get_drvdata(dev); |
| 802 | |
| 803 | dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__); |
| 804 | if (smb137b_chg->charging) |
| 805 | return -EBUSY; |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | static int smb137b_resume(struct device *dev) |
| 810 | { |
| 811 | struct smb137b_data *smb137b_chg = dev_get_drvdata(dev); |
| 812 | |
| 813 | dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__); |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | static const struct dev_pm_ops smb137b_pm_ops = { |
| 818 | .suspend = smb137b_suspend, |
| 819 | .resume = smb137b_resume, |
| 820 | }; |
| 821 | #endif |
| 822 | |
| 823 | static const struct i2c_device_id smb137b_id[] = { |
| 824 | {"smb137b", 0}, |
| 825 | {}, |
| 826 | }; |
| 827 | MODULE_DEVICE_TABLE(i2c, smb137b_id); |
| 828 | |
| 829 | static struct i2c_driver smb137b_driver = { |
| 830 | .driver = { |
| 831 | .name = "smb137b", |
| 832 | .owner = THIS_MODULE, |
| 833 | #ifdef CONFIG_PM |
| 834 | .pm = &smb137b_pm_ops, |
| 835 | #endif |
| 836 | }, |
| 837 | .probe = smb137b_probe, |
| 838 | .remove = __devexit_p(smb137b_remove), |
| 839 | .id_table = smb137b_id, |
| 840 | }; |
| 841 | |
| 842 | static int __init smb137b_init(void) |
| 843 | { |
| 844 | return i2c_add_driver(&smb137b_driver); |
| 845 | } |
| 846 | module_init(smb137b_init); |
| 847 | |
| 848 | static void __exit smb137b_exit(void) |
| 849 | { |
| 850 | return i2c_del_driver(&smb137b_driver); |
| 851 | } |
| 852 | module_exit(smb137b_exit); |
| 853 | |
| 854 | MODULE_AUTHOR("Abhijeet Dharmapurikar <adharmap@codeaurora.org>"); |
| 855 | MODULE_DESCRIPTION("Driver for SMB137B Charger chip"); |
| 856 | MODULE_LICENSE("GPL v2"); |
| 857 | MODULE_ALIAS("i2c:smb137b"); |