Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Linux Foundation. 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 | |
| 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/debugfs.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/power_supply.h> |
| 26 | #include <linux/i2c/smb350.h> |
| 27 | #include <linux/bitops.h> |
| 28 | #include <linux/of.h> |
| 29 | #include <linux/of_device.h> |
| 30 | #include <linux/of_gpio.h> |
| 31 | #include <linux/printk.h> |
| 32 | |
| 33 | /* Register definitions */ |
| 34 | #define CHG_CURRENT_REG 0x00 /* Non-Volatile + mirror */ |
| 35 | #define CHG_OTHER_CURRENT_REG 0x01 /* Non-Volatile + mirror */ |
| 36 | #define VAR_FUNC_REG 0x02 /* Non-Volatile + mirror */ |
| 37 | #define FLOAT_VOLTAGE_REG 0x03 /* Non-Volatile + mirror */ |
| 38 | #define CHG_CTRL_REG 0x04 /* Non-Volatile + mirror */ |
| 39 | #define STAT_TIMER_REG 0x05 /* Non-Volatile + mirror */ |
| 40 | #define PIN_ENABLE_CTRL_REG 0x06 /* Non-Volatile + mirror */ |
| 41 | #define THERM_CTRL_A_REG 0x07 /* Non-Volatile + mirror */ |
| 42 | #define SYSOK_USB3_SELECT_REG 0x08 /* Non-Volatile + mirror */ |
| 43 | #define CTRL_FUNCTIONS_REG 0x09 /* Non-Volatile + mirror */ |
| 44 | #define OTG_TLIM_THERM_CNTRL_REG 0x0A /* Non-Volatile + mirror */ |
| 45 | #define TEMP_MONITOR_REG 0x0B /* Non-Volatile + mirror */ |
| 46 | #define FAULT_IRQ_REG 0x0C /* Non-Volatile */ |
| 47 | #define IRQ_ENABLE_REG 0x0D /* Non-Volatile */ |
| 48 | #define SYSOK_REG 0x0E /* Non-Volatile + mirror */ |
| 49 | |
| 50 | #define AUTO_INPUT_VOLT_DETECT_REG 0x10 /* Non-Volatile Read-Only */ |
| 51 | #define STATUS_IRQ_REG 0x11 /* Non-Volatile Read-Only */ |
| 52 | #define I2C_SLAVE_ADDR_REG 0x12 /* Non-Volatile Read-Only */ |
| 53 | |
| 54 | #define CMD_A_REG 0x30 /* Volatile Read-Write */ |
| 55 | #define CMD_B_REG 0x31 /* Volatile Read-Write */ |
| 56 | #define CMD_C_REG 0x33 /* Volatile Read-Write */ |
| 57 | |
Amir Samuelov | 094534d | 2012-11-12 14:27:39 +0200 | [diff] [blame] | 58 | #define HW_VERSION_REG 0x34 /* Volatile Read-Only */ |
| 59 | |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 60 | #define IRQ_STATUS_A_REG 0x35 /* Volatile Read-Only */ |
| 61 | #define IRQ_STATUS_B_REG 0x36 /* Volatile Read-Only */ |
| 62 | #define IRQ_STATUS_C_REG 0x37 /* Volatile Read-Only */ |
| 63 | #define IRQ_STATUS_D_REG 0x38 /* Volatile Read-Only */ |
| 64 | #define IRQ_STATUS_E_REG 0x39 /* Volatile Read-Only */ |
| 65 | #define IRQ_STATUS_F_REG 0x3A /* Volatile Read-Only */ |
| 66 | |
| 67 | #define STATUS_A_REG 0x3B /* Volatile Read-Only */ |
| 68 | #define STATUS_B_REG 0x3D /* Volatile Read-Only */ |
| 69 | /* Note: STATUS_C_REG was removed from SMB349 to SMB350 */ |
| 70 | #define STATUS_D_REG 0x3E /* Volatile Read-Only */ |
| 71 | #define STATUS_E_REG 0x3F /* Volatile Read-Only */ |
| 72 | |
| 73 | #define IRQ_STATUS_NUM (IRQ_STATUS_F_REG - IRQ_STATUS_A_REG + 1) |
| 74 | |
| 75 | /* Status bits and masks */ |
| 76 | #define SMB350_MASK(BITS, POS) ((u8)(((1 << BITS) - 1) << POS)) |
| 77 | #define FAST_CHG_CURRENT_MASK SMB350_MASK(4, 4) |
| 78 | |
| 79 | #define SMB350_FAST_CHG_MIN_MA 1000 |
| 80 | #define SMB350_FAST_CHG_STEP_MA 200 |
| 81 | #define SMB350_FAST_CHG_MAX_MA 3600 |
| 82 | |
| 83 | #define TERM_CURRENT_MASK SMB350_MASK(3, 2) |
| 84 | |
| 85 | #define SMB350_TERM_CUR_MIN_MA 200 |
| 86 | #define SMB350_TERM_CUR_STEP_MA 100 |
| 87 | #define SMB350_TERM_CUR_MAX_MA 700 |
| 88 | |
| 89 | #define CMD_A_VOLATILE_WR_PERM BIT(7) |
| 90 | #define CHG_CTRL_CURR_TERM_END_CHG BIT(6) |
| 91 | |
| 92 | enum smb350_chg_status { |
| 93 | SMB_CHG_STATUS_NONE = 0, |
| 94 | SMB_CHG_STATUS_PRE_CHARGE = 1, |
| 95 | SMB_CHG_STATUS_FAST_CHARGE = 2, |
| 96 | SMB_CHG_STATUS_TAPER_CHARGE = 3, |
| 97 | }; |
| 98 | |
| 99 | static const char * const smb350_chg_status[] = { |
| 100 | "none", |
| 101 | "pre-charge", |
| 102 | "fast-charge", |
| 103 | "taper-charge" |
| 104 | }; |
| 105 | |
| 106 | struct smb350_device { |
| 107 | /* setup */ |
| 108 | int chg_current_ma; |
| 109 | int term_current_ma; |
| 110 | int chg_en_n_gpio; |
| 111 | int chg_susp_n_gpio; |
| 112 | int stat_gpio; |
| 113 | int irq; |
| 114 | /* internal */ |
| 115 | enum smb350_chg_status chg_status; |
| 116 | struct i2c_client *client; |
| 117 | struct delayed_work irq_work; |
| 118 | struct dentry *dent; |
| 119 | struct wake_lock chg_wake_lock; |
| 120 | struct power_supply dc_psy; |
| 121 | }; |
| 122 | |
| 123 | static struct smb350_device *smb350_dev; |
| 124 | |
| 125 | struct debug_reg { |
| 126 | char *name; |
| 127 | u8 reg; |
| 128 | }; |
| 129 | |
| 130 | #define SMB350_DEBUG_REG(x) {#x, x##_REG} |
| 131 | |
| 132 | static struct debug_reg smb350_debug_regs[] = { |
| 133 | SMB350_DEBUG_REG(CHG_CURRENT), |
| 134 | SMB350_DEBUG_REG(CHG_OTHER_CURRENT), |
| 135 | SMB350_DEBUG_REG(VAR_FUNC), |
| 136 | SMB350_DEBUG_REG(FLOAT_VOLTAGE), |
| 137 | SMB350_DEBUG_REG(CHG_CTRL), |
| 138 | SMB350_DEBUG_REG(STAT_TIMER), |
| 139 | SMB350_DEBUG_REG(PIN_ENABLE_CTRL), |
| 140 | SMB350_DEBUG_REG(THERM_CTRL_A), |
| 141 | SMB350_DEBUG_REG(SYSOK_USB3_SELECT), |
| 142 | SMB350_DEBUG_REG(CTRL_FUNCTIONS), |
| 143 | SMB350_DEBUG_REG(OTG_TLIM_THERM_CNTRL), |
| 144 | SMB350_DEBUG_REG(TEMP_MONITOR), |
| 145 | SMB350_DEBUG_REG(FAULT_IRQ), |
| 146 | SMB350_DEBUG_REG(IRQ_ENABLE), |
| 147 | SMB350_DEBUG_REG(SYSOK), |
| 148 | SMB350_DEBUG_REG(AUTO_INPUT_VOLT_DETECT), |
| 149 | SMB350_DEBUG_REG(STATUS_IRQ), |
| 150 | SMB350_DEBUG_REG(I2C_SLAVE_ADDR), |
| 151 | SMB350_DEBUG_REG(CMD_A), |
| 152 | SMB350_DEBUG_REG(CMD_B), |
| 153 | SMB350_DEBUG_REG(CMD_C), |
Amir Samuelov | 094534d | 2012-11-12 14:27:39 +0200 | [diff] [blame] | 154 | SMB350_DEBUG_REG(HW_VERSION), |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 155 | SMB350_DEBUG_REG(IRQ_STATUS_A), |
| 156 | SMB350_DEBUG_REG(IRQ_STATUS_B), |
| 157 | SMB350_DEBUG_REG(IRQ_STATUS_C), |
| 158 | SMB350_DEBUG_REG(IRQ_STATUS_D), |
| 159 | SMB350_DEBUG_REG(IRQ_STATUS_E), |
| 160 | SMB350_DEBUG_REG(IRQ_STATUS_F), |
| 161 | SMB350_DEBUG_REG(STATUS_A), |
| 162 | SMB350_DEBUG_REG(STATUS_B), |
| 163 | SMB350_DEBUG_REG(STATUS_D), |
| 164 | SMB350_DEBUG_REG(STATUS_E), |
| 165 | }; |
| 166 | |
| 167 | /* |
| 168 | * Read 8-bit register value. return negative value on error. |
| 169 | */ |
| 170 | static int smb350_read_reg(struct i2c_client *client, u8 reg) |
| 171 | { |
| 172 | int val; |
| 173 | |
| 174 | val = i2c_smbus_read_byte_data(client, reg); |
| 175 | if (val < 0) |
| 176 | pr_err("i2c read fail. reg=0x%x.ret=%d.\n", reg, val); |
| 177 | else |
| 178 | pr_debug("reg=0x%02X.val=0x%02X.\n", reg , val); |
| 179 | |
| 180 | return val; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Write 8-bit register value. return negative value on error. |
| 185 | */ |
| 186 | static int smb350_write_reg(struct i2c_client *client, u8 reg, u8 val) |
| 187 | { |
| 188 | int ret; |
| 189 | |
| 190 | ret = i2c_smbus_write_byte_data(client, reg, val); |
| 191 | if (ret < 0) |
| 192 | pr_err("i2c read fail. reg=0x%x.val=0x%x.ret=%d.\n", |
| 193 | reg, val, ret); |
| 194 | else |
| 195 | pr_debug("reg=0x%02X.val=0x%02X.\n", reg , val); |
| 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | static int smb350_masked_write(struct i2c_client *client, int reg, u8 mask, |
| 201 | u8 val) |
| 202 | { |
| 203 | int ret; |
| 204 | int temp; |
| 205 | int shift = find_first_bit((unsigned long *) &mask, 8); |
| 206 | |
| 207 | temp = smb350_read_reg(client, reg); |
| 208 | if (temp < 0) |
| 209 | return temp; |
| 210 | |
| 211 | temp &= ~mask; |
| 212 | temp |= (val << shift) & mask; |
| 213 | ret = smb350_write_reg(client, reg, temp); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 218 | static bool smb350_is_dc_present(struct i2c_client *client) |
| 219 | { |
| 220 | u16 irq_status_f = smb350_read_reg(client, IRQ_STATUS_F_REG); |
| 221 | bool power_ok = irq_status_f & 0x01; |
| 222 | |
| 223 | /* Power-ok , IRQ_STATUS_F_REG bit#0 */ |
| 224 | if (power_ok) |
| 225 | pr_debug("DC is present.\n"); |
| 226 | else |
| 227 | pr_debug("DC is missing.\n"); |
| 228 | |
| 229 | return power_ok; |
| 230 | } |
| 231 | |
Amir Samuelov | 1c7efb5 | 2012-11-20 10:48:59 +0200 | [diff] [blame] | 232 | static bool smb350_is_charger_present(struct i2c_client *client) |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 233 | { |
| 234 | int val; |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 235 | |
Amir Samuelov | 1c7efb5 | 2012-11-20 10:48:59 +0200 | [diff] [blame] | 236 | /* Normally the device is non-removable and embedded on the board. |
| 237 | * Verify that charger is present by getting I2C response. |
| 238 | */ |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 239 | val = smb350_read_reg(client, STATUS_B_REG); |
| 240 | if (val < 0) |
| 241 | return false; |
| 242 | |
Amir Samuelov | 1c7efb5 | 2012-11-20 10:48:59 +0200 | [diff] [blame] | 243 | return true; |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static int smb350_get_prop_charge_type(struct smb350_device *dev) |
| 247 | { |
| 248 | int status_b; |
| 249 | enum smb350_chg_status status; |
| 250 | int chg_type = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; |
| 251 | bool chg_enabled; |
| 252 | bool charger_err; |
| 253 | struct i2c_client *client = dev->client; |
| 254 | |
| 255 | status_b = smb350_read_reg(client, STATUS_B_REG); |
| 256 | if (status_b < 0) { |
| 257 | pr_err("failed to read STATUS_B_REG.\n"); |
| 258 | return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; |
| 259 | } |
| 260 | |
| 261 | chg_enabled = (bool) (status_b & 0x01); |
| 262 | charger_err = (bool) (status_b & (1<<6)); |
| 263 | |
| 264 | if (!chg_enabled) { |
| 265 | pr_warn("Charging not enabled.\n"); |
| 266 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 267 | } |
| 268 | |
| 269 | if (charger_err) { |
| 270 | pr_warn("Charger error detected.\n"); |
| 271 | return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 272 | } |
| 273 | |
| 274 | status = (status_b >> 1) & 0x3; |
| 275 | |
| 276 | if (status == SMB_CHG_STATUS_NONE) |
| 277 | chg_type = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 278 | else if (status == SMB_CHG_STATUS_FAST_CHARGE) /* constant current */ |
| 279 | chg_type = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 280 | else if (status == SMB_CHG_STATUS_TAPER_CHARGE) /* constant voltage */ |
| 281 | chg_type = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 282 | else if (status == SMB_CHG_STATUS_PRE_CHARGE) |
| 283 | chg_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 284 | |
| 285 | pr_debug("smb-chg-status=%d=%s.\n", status, smb350_chg_status[status]); |
| 286 | |
| 287 | if (dev->chg_status != status) { /* Status changed */ |
| 288 | if (status == SMB_CHG_STATUS_NONE) { |
| 289 | pr_debug("Charging stopped.\n"); |
| 290 | wake_unlock(&dev->chg_wake_lock); |
| 291 | } else { |
| 292 | pr_debug("Charging started.\n"); |
| 293 | wake_lock(&dev->chg_wake_lock); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | dev->chg_status = status; |
| 298 | |
| 299 | return chg_type; |
| 300 | } |
| 301 | |
| 302 | static void smb350_enable_charging(struct smb350_device *dev, bool enable) |
| 303 | { |
| 304 | int val = !enable; /* active low */ |
| 305 | |
| 306 | pr_debug("enable=%d.\n", enable); |
| 307 | |
| 308 | gpio_set_value_cansleep(dev->chg_en_n_gpio, val); |
| 309 | } |
| 310 | |
| 311 | /* When the status bit of a certain condition is read, |
| 312 | * the corresponding IRQ signal is cleared. |
| 313 | */ |
| 314 | static int smb350_clear_irq(struct i2c_client *client) |
| 315 | { |
| 316 | int ret; |
| 317 | |
| 318 | ret = smb350_read_reg(client, IRQ_STATUS_A_REG); |
| 319 | if (ret < 0) |
| 320 | return ret; |
| 321 | ret = smb350_read_reg(client, IRQ_STATUS_B_REG); |
| 322 | if (ret < 0) |
| 323 | return ret; |
| 324 | ret = smb350_read_reg(client, IRQ_STATUS_C_REG); |
| 325 | if (ret < 0) |
| 326 | return ret; |
| 327 | ret = smb350_read_reg(client, IRQ_STATUS_D_REG); |
| 328 | if (ret < 0) |
| 329 | return ret; |
| 330 | ret = smb350_read_reg(client, IRQ_STATUS_E_REG); |
| 331 | if (ret < 0) |
| 332 | return ret; |
| 333 | ret = smb350_read_reg(client, IRQ_STATUS_F_REG); |
| 334 | if (ret < 0) |
| 335 | return ret; |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Do the IRQ work from a thread context rather than interrupt context. |
| 342 | * Read status registers to clear interrupt source. |
| 343 | * Notify the power-supply driver about change detected. |
| 344 | * Relevant events for start/stop charging: |
| 345 | * 1. DC insert/remove |
| 346 | * 2. End-Of-Charging |
| 347 | * 3. Battery insert/remove |
| 348 | * 4. Temperture too hot/cold |
| 349 | * 5. Charging timeout expired. |
| 350 | */ |
| 351 | static void smb350_irq_worker(struct work_struct *work) |
| 352 | { |
| 353 | int ret = 0; |
| 354 | struct smb350_device *dev = |
| 355 | container_of(work, struct smb350_device, irq_work.work); |
| 356 | |
| 357 | ret = smb350_clear_irq(dev->client); |
| 358 | if (ret == 0) { /* Cleared ok */ |
| 359 | /* Notify Battery-psy about status changed */ |
| 360 | pr_debug("Notify power_supply_changed.\n"); |
| 361 | power_supply_changed(&dev->dc_psy); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * The STAT pin is low when charging and high when not charging. |
| 367 | * When the smb350 start/stop charging the STAT pin triggers an interrupt. |
| 368 | * Interrupt is triggered on both rising or falling edge. |
| 369 | */ |
| 370 | static irqreturn_t smb350_irq(int irq, void *dev_id) |
| 371 | { |
| 372 | struct smb350_device *dev = dev_id; |
| 373 | |
| 374 | pr_debug("\n"); |
| 375 | |
| 376 | /* I2C transfers API should not run in interrupt context */ |
| 377 | schedule_delayed_work(&dev->irq_work, msecs_to_jiffies(100)); |
| 378 | |
| 379 | return IRQ_HANDLED; |
| 380 | } |
| 381 | |
| 382 | static enum power_supply_property pm_power_props[] = { |
| 383 | /* real time */ |
| 384 | POWER_SUPPLY_PROP_PRESENT, |
| 385 | POWER_SUPPLY_PROP_ONLINE, |
| 386 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 387 | /* fixed */ |
| 388 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 389 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 390 | POWER_SUPPLY_PROP_CURRENT_MAX, |
| 391 | }; |
| 392 | |
| 393 | static char *pm_power_supplied_to[] = { |
| 394 | "battery", |
| 395 | }; |
| 396 | |
| 397 | static int smb350_get_property(struct power_supply *psy, |
| 398 | enum power_supply_property psp, |
| 399 | union power_supply_propval *val) |
| 400 | { |
| 401 | int ret = 0; |
| 402 | struct smb350_device *dev = container_of(psy, |
| 403 | struct smb350_device, |
| 404 | dc_psy); |
| 405 | struct i2c_client *client = dev->client; |
| 406 | |
| 407 | switch (psp) { |
| 408 | case POWER_SUPPLY_PROP_PRESENT: |
Amir Samuelov | 1c7efb5 | 2012-11-20 10:48:59 +0200 | [diff] [blame] | 409 | val->intval = smb350_is_charger_present(client); |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 410 | break; |
| 411 | case POWER_SUPPLY_PROP_ONLINE: |
Amir Samuelov | 1c7efb5 | 2012-11-20 10:48:59 +0200 | [diff] [blame] | 412 | val->intval = smb350_is_dc_present(client); |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 413 | break; |
| 414 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 415 | val->intval = smb350_get_prop_charge_type(dev); |
| 416 | break; |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 417 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 418 | val->strval = SMB350_NAME; |
| 419 | break; |
| 420 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 421 | val->strval = "Summit Microelectronics"; |
| 422 | break; |
| 423 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 424 | val->intval = dev->chg_current_ma; |
| 425 | break; |
| 426 | default: |
| 427 | pr_err("Invalid prop = %d.\n", psp); |
| 428 | ret = -EINVAL; |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static int smb350_set_property(struct power_supply *psy, |
| 436 | enum power_supply_property psp, |
| 437 | const union power_supply_propval *val) |
| 438 | { |
| 439 | int ret = 0; |
| 440 | struct smb350_device *dev = |
| 441 | container_of(psy, struct smb350_device, dc_psy); |
| 442 | |
| 443 | switch (psp) { |
| 444 | /* |
| 445 | * Allow a smart battery to Start/Stop charging. |
| 446 | * i.e. when End-Of-Charging detected. |
| 447 | * The SMB350 can be configured to terminate charging |
| 448 | * when charge-current reaching Termination-Current. |
| 449 | */ |
| 450 | case POWER_SUPPLY_PROP_ONLINE: |
| 451 | smb350_enable_charging(dev, val->intval); |
| 452 | break; |
| 453 | default: |
| 454 | pr_err("Invalid prop = %d.\n", psp); |
| 455 | ret = -EINVAL; |
| 456 | } |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | static int smb350_set_chg_current(struct i2c_client *client, int current_ma) |
| 462 | { |
| 463 | int ret; |
| 464 | u8 temp; |
| 465 | |
| 466 | if ((current_ma < SMB350_FAST_CHG_MIN_MA) || |
| 467 | (current_ma > SMB350_FAST_CHG_MAX_MA)) { |
| 468 | pr_err("invalid current %d mA.\n", current_ma); |
| 469 | return -EINVAL; |
| 470 | } |
| 471 | |
| 472 | temp = (current_ma - SMB350_FAST_CHG_MIN_MA) / SMB350_FAST_CHG_STEP_MA; |
| 473 | |
| 474 | pr_debug("fast-chg-current=%d mA setting %02x\n", current_ma, temp); |
| 475 | |
| 476 | ret = smb350_masked_write(client, CHG_CURRENT_REG, |
| 477 | FAST_CHG_CURRENT_MASK, temp); |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static int smb350_set_term_current(struct i2c_client *client, int current_ma) |
| 483 | { |
| 484 | int ret; |
| 485 | u8 temp; |
| 486 | |
| 487 | if ((current_ma < SMB350_TERM_CUR_MIN_MA) || |
| 488 | (current_ma > SMB350_TERM_CUR_MAX_MA)) { |
| 489 | pr_err("invalid current %d mA to set\n", current_ma); |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | temp = (current_ma - SMB350_TERM_CUR_MIN_MA) / SMB350_TERM_CUR_STEP_MA; |
| 494 | |
| 495 | pr_debug("term-current=%d mA setting %02x\n", current_ma, temp); |
| 496 | |
| 497 | ret = smb350_masked_write(client, CHG_OTHER_CURRENT_REG, |
| 498 | TERM_CURRENT_MASK, temp); |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
| 503 | static int smb350_set_reg(void *data, u64 val) |
| 504 | { |
| 505 | u32 addr = (u32) data; |
| 506 | int ret; |
| 507 | struct i2c_client *client = smb350_dev->client; |
| 508 | |
| 509 | ret = smb350_write_reg(client, addr, (u8) val); |
| 510 | |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | static int smb350_get_reg(void *data, u64 *val) |
| 515 | { |
| 516 | u32 addr = (u32) data; |
| 517 | int ret; |
| 518 | struct i2c_client *client = smb350_dev->client; |
| 519 | |
| 520 | ret = smb350_read_reg(client, addr); |
| 521 | if (ret < 0) |
| 522 | return ret; |
| 523 | |
| 524 | *val = ret; |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, smb350_get_reg, smb350_set_reg, "0x%02llx\n"); |
| 530 | |
| 531 | static int smb350_create_debugfs_entries(struct smb350_device *dev) |
| 532 | { |
| 533 | int i; |
| 534 | dev->dent = debugfs_create_dir(SMB350_NAME, NULL); |
| 535 | if (IS_ERR(dev->dent)) { |
| 536 | pr_err("smb350 driver couldn't create debugfs dir\n"); |
| 537 | return -EFAULT; |
| 538 | } |
| 539 | |
| 540 | for (i = 0 ; i < ARRAY_SIZE(smb350_debug_regs) ; i++) { |
| 541 | char *name = smb350_debug_regs[i].name; |
| 542 | u32 reg = smb350_debug_regs[i].reg; |
| 543 | struct dentry *file; |
| 544 | |
| 545 | file = debugfs_create_file(name, 0644, dev->dent, |
| 546 | (void *) reg, ®_fops); |
| 547 | if (IS_ERR(file)) { |
| 548 | pr_err("debugfs_create_file %s failed.\n", name); |
| 549 | return -EFAULT; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int smb350_set_volatile_params(struct smb350_device *dev) |
| 557 | { |
| 558 | int ret; |
| 559 | struct i2c_client *client = dev->client; |
| 560 | |
| 561 | pr_debug("\n"); |
| 562 | |
| 563 | ret = smb350_write_reg(client, CMD_A_REG, CMD_A_VOLATILE_WR_PERM); |
| 564 | if (ret) { |
| 565 | pr_err("Failed to set VOLATILE_WR_PERM ret=%d\n", ret); |
| 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | /* Disable SMB350 pulse-IRQ mechanism, |
| 570 | * we use interrupts based on charging-status-transition |
| 571 | */ |
| 572 | /* Enable STATUS output (regardless of IRQ-pulses) */ |
| 573 | smb350_masked_write(client, CMD_A_REG, BIT(0), 0); |
| 574 | |
| 575 | /* Disable LED blinking - avoid periodic irq */ |
| 576 | smb350_masked_write(client, PIN_ENABLE_CTRL_REG, BIT(7), 0); |
| 577 | |
| 578 | /* Disable Failure SMB-IRQ */ |
| 579 | ret = smb350_write_reg(client, FAULT_IRQ_REG, 0x00); |
| 580 | if (ret) { |
| 581 | pr_err("Failed to set FAULT_IRQ_REG ret=%d\n", ret); |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | /* Disable Event IRQ */ |
| 586 | ret = smb350_write_reg(client, IRQ_ENABLE_REG, 0x00); |
| 587 | if (ret) { |
| 588 | pr_err("Failed to set IRQ_ENABLE_REG ret=%d\n", ret); |
| 589 | return ret; |
| 590 | } |
| 591 | |
| 592 | /* Enable charging/not-charging status output via STAT pin */ |
| 593 | smb350_masked_write(client, STAT_TIMER_REG, BIT(5), 0); |
| 594 | |
| 595 | /* Disable Automatic Recharge */ |
| 596 | smb350_masked_write(client, CHG_CTRL_REG, BIT(7), 1); |
| 597 | |
| 598 | /* Set fast-charge current */ |
| 599 | ret = smb350_set_chg_current(client, dev->chg_current_ma); |
| 600 | if (ret) { |
| 601 | pr_err("Failed to set FAST_CHG_CURRENT ret=%d\n", ret); |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | if (dev->term_current_ma > 0) { |
| 606 | /* Enable Current Termination */ |
| 607 | smb350_masked_write(client, CHG_CTRL_REG, BIT(6), 0); |
| 608 | |
| 609 | /* Set Termination current */ |
| 610 | smb350_set_term_current(client, dev->term_current_ma); |
| 611 | } else { |
| 612 | /* Disable Current Termination */ |
| 613 | smb350_masked_write(client, CHG_CTRL_REG, BIT(6), 1); |
| 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int __devinit smb350_register_psy(struct smb350_device *dev) |
| 620 | { |
| 621 | int ret; |
| 622 | |
| 623 | dev->dc_psy.name = "dc"; |
| 624 | dev->dc_psy.type = POWER_SUPPLY_TYPE_MAINS; |
| 625 | dev->dc_psy.supplied_to = pm_power_supplied_to; |
| 626 | dev->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to); |
| 627 | dev->dc_psy.properties = pm_power_props; |
| 628 | dev->dc_psy.num_properties = ARRAY_SIZE(pm_power_props); |
| 629 | dev->dc_psy.get_property = smb350_get_property; |
| 630 | dev->dc_psy.set_property = smb350_set_property; |
| 631 | |
| 632 | ret = power_supply_register(&dev->client->dev, |
| 633 | &dev->dc_psy); |
| 634 | if (ret) { |
| 635 | pr_err("failed to register power_supply. ret=%d.\n", ret); |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static int __devinit smb350_probe(struct i2c_client *client, |
| 643 | const struct i2c_device_id *id) |
| 644 | { |
| 645 | int ret = 0; |
| 646 | const struct smb350_platform_data *pdata; |
| 647 | struct device_node *dev_node = client->dev.of_node; |
| 648 | struct smb350_device *dev; |
Amir Samuelov | 094534d | 2012-11-12 14:27:39 +0200 | [diff] [blame] | 649 | u8 version; |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 650 | |
| 651 | /* STAT pin change on start/stop charging */ |
| 652 | u32 irq_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING; |
| 653 | |
| 654 | if (!i2c_check_functionality(client->adapter, |
| 655 | I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 656 | pr_err("i2c func fail.\n"); |
| 657 | return -EIO; |
| 658 | } |
| 659 | |
| 660 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 661 | if (!dev) { |
| 662 | pr_err("alloc fail.\n"); |
| 663 | return -ENOMEM; |
| 664 | } |
| 665 | |
| 666 | smb350_dev = dev; |
| 667 | dev->client = client; |
| 668 | |
| 669 | if (dev_node) { |
| 670 | dev->chg_en_n_gpio = |
| 671 | of_get_named_gpio(dev_node, "summit,chg-en-n-gpio", 0); |
| 672 | pr_debug("chg_en_n_gpio = %d.\n", dev->chg_en_n_gpio); |
| 673 | |
| 674 | dev->chg_susp_n_gpio = |
| 675 | of_get_named_gpio(dev_node, |
| 676 | "summit,chg-susp-n-gpio", 0); |
| 677 | pr_debug("chg_susp_n_gpio = %d.\n", dev->chg_susp_n_gpio); |
| 678 | |
| 679 | dev->stat_gpio = |
| 680 | of_get_named_gpio(dev_node, "summit,stat-gpio", 0); |
| 681 | pr_debug("stat_gpio = %d.\n", dev->stat_gpio); |
| 682 | |
| 683 | ret = of_property_read_u32(dev_node, "summit,chg-current-ma", |
| 684 | &(dev->chg_current_ma)); |
| 685 | pr_debug("chg_current_ma = %d.\n", dev->chg_current_ma); |
| 686 | if (ret) { |
| 687 | pr_err("Unable to read chg_current.\n"); |
| 688 | return ret; |
| 689 | } |
| 690 | ret = of_property_read_u32(dev_node, "summit,term-current-ma", |
| 691 | &(dev->term_current_ma)); |
| 692 | pr_debug("term_current_ma = %d.\n", dev->term_current_ma); |
| 693 | if (ret) { |
| 694 | pr_err("Unable to read term_current_ma.\n"); |
| 695 | return ret; |
| 696 | } |
| 697 | } else { |
| 698 | pdata = client->dev.platform_data; |
| 699 | |
| 700 | if (pdata == NULL) { |
| 701 | pr_err("no platform data.\n"); |
| 702 | return -EINVAL; |
| 703 | } |
| 704 | |
| 705 | dev->chg_en_n_gpio = pdata->chg_en_n_gpio; |
| 706 | dev->chg_susp_n_gpio = pdata->chg_susp_n_gpio; |
| 707 | dev->stat_gpio = pdata->stat_gpio; |
| 708 | |
| 709 | dev->chg_current_ma = pdata->chg_current_ma; |
| 710 | dev->term_current_ma = pdata->term_current_ma; |
| 711 | } |
| 712 | |
| 713 | ret = gpio_request(dev->stat_gpio, "smb350_stat"); |
| 714 | if (ret) { |
| 715 | pr_err("gpio_request failed for %d ret=%d\n", |
| 716 | dev->stat_gpio, ret); |
| 717 | goto err_stat_gpio; |
| 718 | } |
| 719 | dev->irq = gpio_to_irq(dev->stat_gpio); |
| 720 | pr_debug("irq#=%d.\n", dev->irq); |
| 721 | |
| 722 | ret = gpio_request(dev->chg_susp_n_gpio, "smb350_suspend"); |
| 723 | if (ret) { |
| 724 | pr_err("gpio_request failed for %d ret=%d\n", |
| 725 | dev->chg_susp_n_gpio, ret); |
| 726 | goto err_susp_gpio; |
| 727 | } |
| 728 | |
| 729 | ret = gpio_request(dev->chg_en_n_gpio, "smb350_charger_enable"); |
| 730 | if (ret) { |
| 731 | pr_err("gpio_request failed for %d ret=%d\n", |
| 732 | dev->chg_en_n_gpio, ret); |
| 733 | goto err_en_gpio; |
| 734 | } |
| 735 | |
| 736 | i2c_set_clientdata(client, dev); |
| 737 | |
Amir Samuelov | fef0217 | 2012-11-01 20:19:22 +0200 | [diff] [blame] | 738 | /* Disable battery charging by default on power up. |
| 739 | * Battery charging is enabled by BMS or Battery-Gauge |
| 740 | * by using the set_property callback. |
| 741 | */ |
| 742 | smb350_enable_charging(dev, false); |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 743 | msleep(100); |
| 744 | gpio_set_value_cansleep(dev->chg_susp_n_gpio, 1); /* Normal */ |
| 745 | msleep(100); /* Allow the device to exist shutdown */ |
| 746 | |
Amir Samuelov | fef0217 | 2012-11-01 20:19:22 +0200 | [diff] [blame] | 747 | /* I2C transaction allowed only after device exit suspend */ |
| 748 | ret = smb350_read_reg(client, I2C_SLAVE_ADDR_REG); |
| 749 | if ((ret>>1) != client->addr) { |
| 750 | pr_err("No device.\n"); |
| 751 | ret = -ENODEV; |
| 752 | goto err_no_dev; |
| 753 | } |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 754 | |
Amir Samuelov | 094534d | 2012-11-12 14:27:39 +0200 | [diff] [blame] | 755 | version = smb350_read_reg(client, HW_VERSION_REG); |
| 756 | version &= 0x0F; /* bits 0..3 */ |
| 757 | |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 758 | ret = smb350_set_volatile_params(dev); |
| 759 | if (ret) |
| 760 | goto err_set_params; |
| 761 | |
| 762 | ret = smb350_register_psy(dev); |
| 763 | if (ret) |
| 764 | goto err_set_params; |
| 765 | |
| 766 | ret = smb350_create_debugfs_entries(dev); |
| 767 | if (ret) |
| 768 | goto err_debugfs; |
| 769 | |
| 770 | INIT_DELAYED_WORK(&dev->irq_work, smb350_irq_worker); |
| 771 | wake_lock_init(&dev->chg_wake_lock, |
| 772 | WAKE_LOCK_SUSPEND, SMB350_NAME); |
| 773 | |
| 774 | ret = request_irq(dev->irq, smb350_irq, irq_flags, |
| 775 | "smb350_irq", dev); |
| 776 | if (ret) { |
| 777 | pr_err("request_irq %d failed.ret=%d\n", dev->irq, ret); |
| 778 | goto err_irq; |
| 779 | } |
| 780 | |
Amir Samuelov | 094534d | 2012-11-12 14:27:39 +0200 | [diff] [blame] | 781 | pr_info("HW Version = 0x%X.\n", version); |
| 782 | |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 783 | return 0; |
| 784 | |
| 785 | err_irq: |
| 786 | err_debugfs: |
| 787 | if (dev->dent) |
| 788 | debugfs_remove_recursive(dev->dent); |
Amir Samuelov | fef0217 | 2012-11-01 20:19:22 +0200 | [diff] [blame] | 789 | err_no_dev: |
Amir Samuelov | caaa8f0 | 2012-10-25 11:11:57 +0200 | [diff] [blame] | 790 | err_set_params: |
| 791 | gpio_free(dev->chg_en_n_gpio); |
| 792 | err_en_gpio: |
| 793 | gpio_free(dev->chg_susp_n_gpio); |
| 794 | err_susp_gpio: |
| 795 | gpio_free(dev->stat_gpio); |
| 796 | err_stat_gpio: |
| 797 | kfree(smb350_dev); |
| 798 | smb350_dev = NULL; |
| 799 | |
| 800 | pr_info("FAIL.\n"); |
| 801 | |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | static int __devexit smb350_remove(struct i2c_client *client) |
| 806 | { |
| 807 | struct smb350_device *dev = i2c_get_clientdata(client); |
| 808 | |
| 809 | power_supply_unregister(&dev->dc_psy); |
| 810 | gpio_free(dev->chg_en_n_gpio); |
| 811 | gpio_free(dev->chg_susp_n_gpio); |
| 812 | if (dev->stat_gpio) |
| 813 | gpio_free(dev->stat_gpio); |
| 814 | if (dev->irq) |
| 815 | free_irq(dev->irq, dev); |
| 816 | if (dev->dent) |
| 817 | debugfs_remove_recursive(dev->dent); |
| 818 | kfree(smb350_dev); |
| 819 | smb350_dev = NULL; |
| 820 | |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | static const struct i2c_device_id smb350_id[] = { |
| 825 | {SMB350_NAME, 0}, |
| 826 | {}, |
| 827 | }; |
| 828 | MODULE_DEVICE_TABLE(i2c, smb350_id); |
| 829 | |
| 830 | static const struct of_device_id smb350_match[] = { |
| 831 | { .compatible = "summit,smb350-charger", }, |
| 832 | { }, |
| 833 | }; |
| 834 | |
| 835 | static struct i2c_driver smb350_driver = { |
| 836 | .driver = { |
| 837 | .name = SMB350_NAME, |
| 838 | .owner = THIS_MODULE, |
| 839 | .of_match_table = of_match_ptr(smb350_match), |
| 840 | }, |
| 841 | .probe = smb350_probe, |
| 842 | .remove = __devexit_p(smb350_remove), |
| 843 | .id_table = smb350_id, |
| 844 | }; |
| 845 | |
| 846 | static int __init smb350_init(void) |
| 847 | { |
| 848 | return i2c_add_driver(&smb350_driver); |
| 849 | } |
| 850 | module_init(smb350_init); |
| 851 | |
| 852 | static void __exit smb350_exit(void) |
| 853 | { |
| 854 | return i2c_del_driver(&smb350_driver); |
| 855 | } |
| 856 | module_exit(smb350_exit); |
| 857 | |
| 858 | MODULE_DESCRIPTION("Driver for SMB350 charger chip"); |
| 859 | MODULE_LICENSE("GPL v2"); |
| 860 | MODULE_ALIAS("i2c:" SMB350_NAME); |