Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Summit Microelectronics SMB347 Battery Charger Driver |
| 3 | * |
| 4 | * Copyright (C) 2011, Intel Corporation |
| 5 | * |
| 6 | * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com> |
| 7 | * Mika Westerberg <mika.westerberg@linux.intel.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Mika Westerberg | 96facd2 | 2012-05-07 11:26:37 +0300 | [diff] [blame] | 14 | #include <linux/err.h> |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 15 | #include <linux/gpio.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/power_supply.h> |
| 23 | #include <linux/power/smb347-charger.h> |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 24 | #include <linux/regmap.h> |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Configuration registers. These are mirrored to volatile RAM and can be |
| 28 | * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be |
| 29 | * reloaded from non-volatile registers after POR. |
| 30 | */ |
| 31 | #define CFG_CHARGE_CURRENT 0x00 |
| 32 | #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0 |
| 33 | #define CFG_CHARGE_CURRENT_FCC_SHIFT 5 |
| 34 | #define CFG_CHARGE_CURRENT_PCC_MASK 0x18 |
| 35 | #define CFG_CHARGE_CURRENT_PCC_SHIFT 3 |
| 36 | #define CFG_CHARGE_CURRENT_TC_MASK 0x07 |
| 37 | #define CFG_CURRENT_LIMIT 0x01 |
| 38 | #define CFG_CURRENT_LIMIT_DC_MASK 0xf0 |
| 39 | #define CFG_CURRENT_LIMIT_DC_SHIFT 4 |
| 40 | #define CFG_CURRENT_LIMIT_USB_MASK 0x0f |
| 41 | #define CFG_FLOAT_VOLTAGE 0x03 |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 42 | #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 43 | #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0 |
| 44 | #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6 |
| 45 | #define CFG_STAT 0x05 |
| 46 | #define CFG_STAT_DISABLED BIT(5) |
| 47 | #define CFG_STAT_ACTIVE_HIGH BIT(7) |
| 48 | #define CFG_PIN 0x06 |
| 49 | #define CFG_PIN_EN_CTRL_MASK 0x60 |
| 50 | #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40 |
| 51 | #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60 |
| 52 | #define CFG_PIN_EN_APSD_IRQ BIT(1) |
| 53 | #define CFG_PIN_EN_CHARGER_ERROR BIT(2) |
| 54 | #define CFG_THERM 0x07 |
| 55 | #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03 |
| 56 | #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0 |
| 57 | #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c |
| 58 | #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2 |
| 59 | #define CFG_THERM_MONITOR_DISABLED BIT(4) |
| 60 | #define CFG_SYSOK 0x08 |
| 61 | #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2) |
| 62 | #define CFG_OTHER 0x09 |
| 63 | #define CFG_OTHER_RID_MASK 0xc0 |
| 64 | #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0 |
| 65 | #define CFG_OTG 0x0a |
| 66 | #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30 |
| 67 | #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4 |
| 68 | #define CFG_OTG_CC_COMPENSATION_MASK 0xc0 |
| 69 | #define CFG_OTG_CC_COMPENSATION_SHIFT 6 |
| 70 | #define CFG_TEMP_LIMIT 0x0b |
| 71 | #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03 |
| 72 | #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0 |
| 73 | #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c |
| 74 | #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2 |
| 75 | #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30 |
| 76 | #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4 |
| 77 | #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0 |
| 78 | #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6 |
| 79 | #define CFG_FAULT_IRQ 0x0c |
| 80 | #define CFG_FAULT_IRQ_DCIN_UV BIT(2) |
| 81 | #define CFG_STATUS_IRQ 0x0d |
| 82 | #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4) |
| 83 | #define CFG_ADDRESS 0x0e |
| 84 | |
| 85 | /* Command registers */ |
| 86 | #define CMD_A 0x30 |
| 87 | #define CMD_A_CHG_ENABLED BIT(1) |
| 88 | #define CMD_A_SUSPEND_ENABLED BIT(2) |
| 89 | #define CMD_A_ALLOW_WRITE BIT(7) |
| 90 | #define CMD_B 0x31 |
| 91 | #define CMD_C 0x33 |
| 92 | |
| 93 | /* Interrupt Status registers */ |
| 94 | #define IRQSTAT_A 0x35 |
| 95 | #define IRQSTAT_C 0x37 |
| 96 | #define IRQSTAT_C_TERMINATION_STAT BIT(0) |
| 97 | #define IRQSTAT_C_TERMINATION_IRQ BIT(1) |
| 98 | #define IRQSTAT_C_TAPER_IRQ BIT(3) |
| 99 | #define IRQSTAT_E 0x39 |
| 100 | #define IRQSTAT_E_USBIN_UV_STAT BIT(0) |
| 101 | #define IRQSTAT_E_USBIN_UV_IRQ BIT(1) |
| 102 | #define IRQSTAT_E_DCIN_UV_STAT BIT(4) |
| 103 | #define IRQSTAT_E_DCIN_UV_IRQ BIT(5) |
| 104 | #define IRQSTAT_F 0x3a |
| 105 | |
| 106 | /* Status registers */ |
| 107 | #define STAT_A 0x3b |
| 108 | #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f |
| 109 | #define STAT_B 0x3c |
| 110 | #define STAT_C 0x3d |
| 111 | #define STAT_C_CHG_ENABLED BIT(0) |
| 112 | #define STAT_C_CHG_MASK 0x06 |
| 113 | #define STAT_C_CHG_SHIFT 1 |
| 114 | #define STAT_C_CHARGER_ERROR BIT(6) |
| 115 | #define STAT_E 0x3f |
| 116 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 117 | #define SMB347_MAX_REGISTER 0x3f |
| 118 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 119 | /** |
| 120 | * struct smb347_charger - smb347 charger instance |
| 121 | * @lock: protects concurrent access to online variables |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 122 | * @dev: pointer to device |
| 123 | * @regmap: pointer to driver regmap |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 124 | * @mains: power_supply instance for AC/DC power |
| 125 | * @usb: power_supply instance for USB power |
| 126 | * @battery: power_supply instance for battery |
| 127 | * @mains_online: is AC/DC input connected |
| 128 | * @usb_online: is USB input connected |
| 129 | * @charging_enabled: is charging enabled |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 130 | * @pdata: pointer to platform data |
| 131 | */ |
| 132 | struct smb347_charger { |
| 133 | struct mutex lock; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 134 | struct device *dev; |
| 135 | struct regmap *regmap; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 136 | struct power_supply mains; |
| 137 | struct power_supply usb; |
| 138 | struct power_supply battery; |
| 139 | bool mains_online; |
| 140 | bool usb_online; |
| 141 | bool charging_enabled; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 142 | const struct smb347_charger_platform_data *pdata; |
| 143 | }; |
| 144 | |
| 145 | /* Fast charge current in uA */ |
| 146 | static const unsigned int fcc_tbl[] = { |
| 147 | 700000, |
| 148 | 900000, |
| 149 | 1200000, |
| 150 | 1500000, |
| 151 | 1800000, |
| 152 | 2000000, |
| 153 | 2200000, |
| 154 | 2500000, |
| 155 | }; |
| 156 | |
| 157 | /* Pre-charge current in uA */ |
| 158 | static const unsigned int pcc_tbl[] = { |
| 159 | 100000, |
| 160 | 150000, |
| 161 | 200000, |
| 162 | 250000, |
| 163 | }; |
| 164 | |
| 165 | /* Termination current in uA */ |
| 166 | static const unsigned int tc_tbl[] = { |
| 167 | 37500, |
| 168 | 50000, |
| 169 | 100000, |
| 170 | 150000, |
| 171 | 200000, |
| 172 | 250000, |
| 173 | 500000, |
| 174 | 600000, |
| 175 | }; |
| 176 | |
| 177 | /* Input current limit in uA */ |
| 178 | static const unsigned int icl_tbl[] = { |
| 179 | 300000, |
| 180 | 500000, |
| 181 | 700000, |
| 182 | 900000, |
| 183 | 1200000, |
| 184 | 1500000, |
| 185 | 1800000, |
| 186 | 2000000, |
| 187 | 2200000, |
| 188 | 2500000, |
| 189 | }; |
| 190 | |
| 191 | /* Charge current compensation in uA */ |
| 192 | static const unsigned int ccc_tbl[] = { |
| 193 | 250000, |
| 194 | 700000, |
| 195 | 900000, |
| 196 | 1200000, |
| 197 | }; |
| 198 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 199 | /* Convert current to register value using lookup table */ |
| 200 | static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val) |
| 201 | { |
| 202 | size_t i; |
| 203 | |
| 204 | for (i = 0; i < size; i++) |
| 205 | if (val < tbl[i]) |
| 206 | break; |
| 207 | return i > 0 ? i - 1 : -EINVAL; |
| 208 | } |
| 209 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 210 | /** |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 211 | * smb347_update_ps_status - refreshes the power source status |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 212 | * @smb: pointer to smb347 charger instance |
| 213 | * |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 214 | * Function checks whether any power source is connected to the charger and |
| 215 | * updates internal state accordingly. If there is a change to previous state |
| 216 | * function returns %1, otherwise %0 and negative errno in case of errror. |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 217 | */ |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 218 | static int smb347_update_ps_status(struct smb347_charger *smb) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 219 | { |
| 220 | bool usb = false; |
| 221 | bool dc = false; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 222 | unsigned int val; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 223 | int ret; |
| 224 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 225 | ret = regmap_read(smb->regmap, IRQSTAT_E, &val); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 226 | if (ret < 0) |
| 227 | return ret; |
| 228 | |
| 229 | /* |
| 230 | * Dc and usb are set depending on whether they are enabled in |
| 231 | * platform data _and_ whether corresponding undervoltage is set. |
| 232 | */ |
| 233 | if (smb->pdata->use_mains) |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 234 | dc = !(val & IRQSTAT_E_DCIN_UV_STAT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 235 | if (smb->pdata->use_usb) |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 236 | usb = !(val & IRQSTAT_E_USBIN_UV_STAT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 237 | |
| 238 | mutex_lock(&smb->lock); |
| 239 | ret = smb->mains_online != dc || smb->usb_online != usb; |
| 240 | smb->mains_online = dc; |
| 241 | smb->usb_online = usb; |
| 242 | mutex_unlock(&smb->lock); |
| 243 | |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | /* |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 248 | * smb347_is_ps_online - returns whether input power source is connected |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 249 | * @smb: pointer to smb347 charger instance |
| 250 | * |
| 251 | * Returns %true if input power source is connected. Note that this is |
| 252 | * dependent on what platform has configured for usable power sources. For |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 253 | * example if USB is disabled, this will return %false even if the USB cable |
| 254 | * is connected. |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 255 | */ |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 256 | static bool smb347_is_ps_online(struct smb347_charger *smb) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 257 | { |
| 258 | bool ret; |
| 259 | |
| 260 | mutex_lock(&smb->lock); |
| 261 | ret = smb->usb_online || smb->mains_online; |
| 262 | mutex_unlock(&smb->lock); |
| 263 | |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * smb347_charging_status - returns status of charging |
| 269 | * @smb: pointer to smb347 charger instance |
| 270 | * |
| 271 | * Function returns charging status. %0 means no charging is in progress, |
| 272 | * %1 means pre-charging, %2 fast-charging and %3 taper-charging. |
| 273 | */ |
| 274 | static int smb347_charging_status(struct smb347_charger *smb) |
| 275 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 276 | unsigned int val; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 277 | int ret; |
| 278 | |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 279 | if (!smb347_is_ps_online(smb)) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 282 | ret = regmap_read(smb->regmap, STAT_C, &val); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 283 | if (ret < 0) |
| 284 | return 0; |
| 285 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 286 | return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static int smb347_charging_set(struct smb347_charger *smb, bool enable) |
| 290 | { |
| 291 | int ret = 0; |
| 292 | |
| 293 | if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 294 | dev_dbg(smb->dev, "charging enable/disable in SW disabled\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | mutex_lock(&smb->lock); |
| 299 | if (smb->charging_enabled != enable) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 300 | ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED, |
| 301 | enable ? CMD_A_CHG_ENABLED : 0); |
| 302 | if (!ret) |
| 303 | smb->charging_enabled = enable; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 304 | } |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 305 | mutex_unlock(&smb->lock); |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | static inline int smb347_charging_enable(struct smb347_charger *smb) |
| 310 | { |
| 311 | return smb347_charging_set(smb, true); |
| 312 | } |
| 313 | |
| 314 | static inline int smb347_charging_disable(struct smb347_charger *smb) |
| 315 | { |
| 316 | return smb347_charging_set(smb, false); |
| 317 | } |
| 318 | |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 319 | static int smb347_start_stop_charging(struct smb347_charger *smb) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 320 | { |
| 321 | int ret; |
| 322 | |
| 323 | /* |
| 324 | * Depending on whether valid power source is connected or not, we |
| 325 | * disable or enable the charging. We do it manually because it |
| 326 | * depends on how the platform has configured the valid inputs. |
| 327 | */ |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 328 | if (smb347_is_ps_online(smb)) { |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 329 | ret = smb347_charging_enable(smb); |
| 330 | if (ret < 0) |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 331 | dev_err(smb->dev, "failed to enable charging\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 332 | } else { |
| 333 | ret = smb347_charging_disable(smb); |
| 334 | if (ret < 0) |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 335 | dev_err(smb->dev, "failed to disable charging\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static int smb347_set_charge_current(struct smb347_charger *smb) |
| 342 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 343 | int ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 344 | |
| 345 | if (smb->pdata->max_charge_current) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 346 | ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl), |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 347 | smb->pdata->max_charge_current); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 348 | if (ret < 0) |
| 349 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 350 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 351 | ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, |
| 352 | CFG_CHARGE_CURRENT_FCC_MASK, |
| 353 | ret << CFG_CHARGE_CURRENT_FCC_SHIFT); |
| 354 | if (ret < 0) |
| 355 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | if (smb->pdata->pre_charge_current) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 359 | ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl), |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 360 | smb->pdata->pre_charge_current); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 361 | if (ret < 0) |
| 362 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 363 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 364 | ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, |
| 365 | CFG_CHARGE_CURRENT_PCC_MASK, |
| 366 | ret << CFG_CHARGE_CURRENT_PCC_SHIFT); |
| 367 | if (ret < 0) |
| 368 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if (smb->pdata->termination_current) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 372 | ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl), |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 373 | smb->pdata->termination_current); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 374 | if (ret < 0) |
| 375 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 376 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 377 | ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, |
| 378 | CFG_CHARGE_CURRENT_TC_MASK, ret); |
| 379 | if (ret < 0) |
| 380 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 383 | return 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static int smb347_set_current_limits(struct smb347_charger *smb) |
| 387 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 388 | int ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 389 | |
| 390 | if (smb->pdata->mains_current_limit) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 391 | ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 392 | smb->pdata->mains_current_limit); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 393 | if (ret < 0) |
| 394 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 395 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 396 | ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, |
| 397 | CFG_CURRENT_LIMIT_DC_MASK, |
| 398 | ret << CFG_CURRENT_LIMIT_DC_SHIFT); |
| 399 | if (ret < 0) |
| 400 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | if (smb->pdata->usb_hc_current_limit) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 404 | ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 405 | smb->pdata->usb_hc_current_limit); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 406 | if (ret < 0) |
| 407 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 408 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 409 | ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, |
| 410 | CFG_CURRENT_LIMIT_USB_MASK, ret); |
| 411 | if (ret < 0) |
| 412 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 415 | return 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static int smb347_set_voltage_limits(struct smb347_charger *smb) |
| 419 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 420 | int ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 421 | |
| 422 | if (smb->pdata->pre_to_fast_voltage) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 423 | ret = smb->pdata->pre_to_fast_voltage; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 424 | |
| 425 | /* uV */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 426 | ret = clamp_val(ret, 2400000, 3000000) - 2400000; |
| 427 | ret /= 200000; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 428 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 429 | ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, |
| 430 | CFG_FLOAT_VOLTAGE_THRESHOLD_MASK, |
| 431 | ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT); |
| 432 | if (ret < 0) |
| 433 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | if (smb->pdata->max_charge_voltage) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 437 | ret = smb->pdata->max_charge_voltage; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 438 | |
| 439 | /* uV */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 440 | ret = clamp_val(ret, 3500000, 4500000) - 3500000; |
| 441 | ret /= 20000; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 442 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 443 | ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, |
| 444 | CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret); |
| 445 | if (ret < 0) |
| 446 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 449 | return 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static int smb347_set_temp_limits(struct smb347_charger *smb) |
| 453 | { |
| 454 | bool enable_therm_monitor = false; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 455 | int ret = 0; |
| 456 | int val; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 457 | |
| 458 | if (smb->pdata->chip_temp_threshold) { |
| 459 | val = smb->pdata->chip_temp_threshold; |
| 460 | |
| 461 | /* degree C */ |
| 462 | val = clamp_val(val, 100, 130) - 100; |
| 463 | val /= 10; |
| 464 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 465 | ret = regmap_update_bits(smb->regmap, CFG_OTG, |
| 466 | CFG_OTG_TEMP_THRESHOLD_MASK, |
| 467 | val << CFG_OTG_TEMP_THRESHOLD_SHIFT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 468 | if (ret < 0) |
| 469 | return ret; |
| 470 | } |
| 471 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 472 | if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { |
| 473 | val = smb->pdata->soft_cold_temp_limit; |
| 474 | |
| 475 | val = clamp_val(val, 0, 15); |
| 476 | val /= 5; |
| 477 | /* this goes from higher to lower so invert the value */ |
| 478 | val = ~val & 0x3; |
| 479 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 480 | ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, |
| 481 | CFG_TEMP_LIMIT_SOFT_COLD_MASK, |
| 482 | val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT); |
| 483 | if (ret < 0) |
| 484 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 485 | |
| 486 | enable_therm_monitor = true; |
| 487 | } |
| 488 | |
| 489 | if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) { |
| 490 | val = smb->pdata->soft_hot_temp_limit; |
| 491 | |
| 492 | val = clamp_val(val, 40, 55) - 40; |
| 493 | val /= 5; |
| 494 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 495 | ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, |
| 496 | CFG_TEMP_LIMIT_SOFT_HOT_MASK, |
| 497 | val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT); |
| 498 | if (ret < 0) |
| 499 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 500 | |
| 501 | enable_therm_monitor = true; |
| 502 | } |
| 503 | |
| 504 | if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { |
| 505 | val = smb->pdata->hard_cold_temp_limit; |
| 506 | |
| 507 | val = clamp_val(val, -5, 10) + 5; |
| 508 | val /= 5; |
| 509 | /* this goes from higher to lower so invert the value */ |
| 510 | val = ~val & 0x3; |
| 511 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 512 | ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, |
| 513 | CFG_TEMP_LIMIT_HARD_COLD_MASK, |
| 514 | val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT); |
| 515 | if (ret < 0) |
| 516 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 517 | |
| 518 | enable_therm_monitor = true; |
| 519 | } |
| 520 | |
| 521 | if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) { |
| 522 | val = smb->pdata->hard_hot_temp_limit; |
| 523 | |
| 524 | val = clamp_val(val, 50, 65) - 50; |
| 525 | val /= 5; |
| 526 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 527 | ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, |
| 528 | CFG_TEMP_LIMIT_HARD_HOT_MASK, |
| 529 | val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT); |
| 530 | if (ret < 0) |
| 531 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 532 | |
| 533 | enable_therm_monitor = true; |
| 534 | } |
| 535 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 536 | /* |
| 537 | * If any of the temperature limits are set, we also enable the |
| 538 | * thermistor monitoring. |
| 539 | * |
| 540 | * When soft limits are hit, the device will start to compensate |
| 541 | * current and/or voltage depending on the configuration. |
| 542 | * |
| 543 | * When hard limit is hit, the device will suspend charging |
| 544 | * depending on the configuration. |
| 545 | */ |
| 546 | if (enable_therm_monitor) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 547 | ret = regmap_update_bits(smb->regmap, CFG_THERM, |
| 548 | CFG_THERM_MONITOR_DISABLED, 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 549 | if (ret < 0) |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | if (smb->pdata->suspend_on_hard_temp_limit) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 554 | ret = regmap_update_bits(smb->regmap, CFG_SYSOK, |
| 555 | CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 556 | if (ret < 0) |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | if (smb->pdata->soft_temp_limit_compensation != |
| 561 | SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) { |
| 562 | val = smb->pdata->soft_temp_limit_compensation & 0x3; |
| 563 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 564 | ret = regmap_update_bits(smb->regmap, CFG_THERM, |
| 565 | CFG_THERM_SOFT_HOT_COMPENSATION_MASK, |
| 566 | val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 567 | if (ret < 0) |
| 568 | return ret; |
| 569 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 570 | ret = regmap_update_bits(smb->regmap, CFG_THERM, |
| 571 | CFG_THERM_SOFT_COLD_COMPENSATION_MASK, |
| 572 | val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 573 | if (ret < 0) |
| 574 | return ret; |
| 575 | } |
| 576 | |
| 577 | if (smb->pdata->charge_current_compensation) { |
| 578 | val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl), |
| 579 | smb->pdata->charge_current_compensation); |
| 580 | if (val < 0) |
| 581 | return val; |
| 582 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 583 | ret = regmap_update_bits(smb->regmap, CFG_OTG, |
| 584 | CFG_OTG_CC_COMPENSATION_MASK, |
| 585 | (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 586 | if (ret < 0) |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * smb347_set_writable - enables/disables writing to non-volatile registers |
| 595 | * @smb: pointer to smb347 charger instance |
| 596 | * |
| 597 | * You can enable/disable writing to the non-volatile configuration |
| 598 | * registers by calling this function. |
| 599 | * |
| 600 | * Returns %0 on success and negative errno in case of failure. |
| 601 | */ |
| 602 | static int smb347_set_writable(struct smb347_charger *smb, bool writable) |
| 603 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 604 | return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE, |
| 605 | writable ? CMD_A_ALLOW_WRITE : 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static int smb347_hw_init(struct smb347_charger *smb) |
| 609 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 610 | unsigned int val; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 611 | int ret; |
| 612 | |
| 613 | ret = smb347_set_writable(smb, true); |
| 614 | if (ret < 0) |
| 615 | return ret; |
| 616 | |
| 617 | /* |
| 618 | * Program the platform specific configuration values to the device |
| 619 | * first. |
| 620 | */ |
| 621 | ret = smb347_set_charge_current(smb); |
| 622 | if (ret < 0) |
| 623 | goto fail; |
| 624 | |
| 625 | ret = smb347_set_current_limits(smb); |
| 626 | if (ret < 0) |
| 627 | goto fail; |
| 628 | |
| 629 | ret = smb347_set_voltage_limits(smb); |
| 630 | if (ret < 0) |
| 631 | goto fail; |
| 632 | |
| 633 | ret = smb347_set_temp_limits(smb); |
| 634 | if (ret < 0) |
| 635 | goto fail; |
| 636 | |
| 637 | /* If USB charging is disabled we put the USB in suspend mode */ |
| 638 | if (!smb->pdata->use_usb) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 639 | ret = regmap_update_bits(smb->regmap, CMD_A, |
| 640 | CMD_A_SUSPEND_ENABLED, |
| 641 | CMD_A_SUSPEND_ENABLED); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 642 | if (ret < 0) |
| 643 | goto fail; |
| 644 | } |
| 645 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 646 | /* |
| 647 | * If configured by platform data, we enable hardware Auto-OTG |
| 648 | * support for driving VBUS. Otherwise we disable it. |
| 649 | */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 650 | ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK, |
| 651 | smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 652 | if (ret < 0) |
| 653 | goto fail; |
| 654 | |
| 655 | /* |
| 656 | * Make the charging functionality controllable by a write to the |
| 657 | * command register unless pin control is specified in the platform |
| 658 | * data. |
| 659 | */ |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 660 | switch (smb->pdata->enable_control) { |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 661 | case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW: |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 662 | val = CFG_PIN_EN_CTRL_ACTIVE_LOW; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 663 | break; |
| 664 | case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH: |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 665 | val = CFG_PIN_EN_CTRL_ACTIVE_HIGH; |
| 666 | break; |
| 667 | default: |
| 668 | val = 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 669 | break; |
| 670 | } |
| 671 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 672 | ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK, |
| 673 | val); |
| 674 | if (ret < 0) |
| 675 | goto fail; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 676 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 677 | /* Disable Automatic Power Source Detection (APSD) interrupt. */ |
| 678 | ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 679 | if (ret < 0) |
| 680 | goto fail; |
| 681 | |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 682 | ret = smb347_update_ps_status(smb); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 683 | if (ret < 0) |
| 684 | goto fail; |
| 685 | |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 686 | ret = smb347_start_stop_charging(smb); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 687 | |
| 688 | fail: |
| 689 | smb347_set_writable(smb, false); |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | static irqreturn_t smb347_interrupt(int irq, void *data) |
| 694 | { |
| 695 | struct smb347_charger *smb = data; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 696 | unsigned int stat_c, irqstat_e, irqstat_c; |
| 697 | bool handled = false; |
| 698 | int ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 699 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 700 | ret = regmap_read(smb->regmap, STAT_C, &stat_c); |
| 701 | if (ret < 0) { |
| 702 | dev_warn(smb->dev, "reading STAT_C failed\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 703 | return IRQ_NONE; |
| 704 | } |
| 705 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 706 | ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c); |
| 707 | if (ret < 0) { |
| 708 | dev_warn(smb->dev, "reading IRQSTAT_C failed\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 709 | return IRQ_NONE; |
| 710 | } |
| 711 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 712 | ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e); |
| 713 | if (ret < 0) { |
| 714 | dev_warn(smb->dev, "reading IRQSTAT_E failed\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 715 | return IRQ_NONE; |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * If we get charger error we report the error back to user and |
| 720 | * disable charging. |
| 721 | */ |
| 722 | if (stat_c & STAT_C_CHARGER_ERROR) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 723 | dev_err(smb->dev, "error in charger, disabling charging\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 724 | |
| 725 | smb347_charging_disable(smb); |
| 726 | power_supply_changed(&smb->battery); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 727 | handled = true; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /* |
| 731 | * If we reached the termination current the battery is charged and |
| 732 | * we can update the status now. Charging is automatically |
| 733 | * disabled by the hardware. |
| 734 | */ |
| 735 | if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) { |
| 736 | if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) |
| 737 | power_supply_changed(&smb->battery); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 738 | handled = true; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* |
| 742 | * If we got an under voltage interrupt it means that AC/USB input |
| 743 | * was connected or disconnected. |
| 744 | */ |
| 745 | if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) { |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 746 | if (smb347_update_ps_status(smb) > 0) { |
| 747 | smb347_start_stop_charging(smb); |
Ramakrishna Pallala | e6fe359 | 2012-04-19 10:00:18 +0530 | [diff] [blame] | 748 | if (smb->pdata->use_mains) |
| 749 | power_supply_changed(&smb->mains); |
| 750 | if (smb->pdata->use_usb) |
| 751 | power_supply_changed(&smb->usb); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 752 | } |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 753 | handled = true; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 756 | return handled ? IRQ_HANDLED : IRQ_NONE; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static int smb347_irq_set(struct smb347_charger *smb, bool enable) |
| 760 | { |
| 761 | int ret; |
| 762 | |
| 763 | ret = smb347_set_writable(smb, true); |
| 764 | if (ret < 0) |
| 765 | return ret; |
| 766 | |
| 767 | /* |
| 768 | * Enable/disable interrupts for: |
| 769 | * - under voltage |
| 770 | * - termination current reached |
| 771 | * - charger error |
| 772 | */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 773 | ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff, |
| 774 | enable ? CFG_FAULT_IRQ_DCIN_UV : 0); |
| 775 | if (ret < 0) |
| 776 | goto fail; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 777 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 778 | ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff, |
| 779 | enable ? CFG_STATUS_IRQ_TERMINATION_OR_TAPER : 0); |
| 780 | if (ret < 0) |
| 781 | goto fail; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 782 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 783 | ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR, |
| 784 | enable ? CFG_PIN_EN_CHARGER_ERROR : 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 785 | fail: |
| 786 | smb347_set_writable(smb, false); |
| 787 | return ret; |
| 788 | } |
| 789 | |
| 790 | static inline int smb347_irq_enable(struct smb347_charger *smb) |
| 791 | { |
| 792 | return smb347_irq_set(smb, true); |
| 793 | } |
| 794 | |
| 795 | static inline int smb347_irq_disable(struct smb347_charger *smb) |
| 796 | { |
| 797 | return smb347_irq_set(smb, false); |
| 798 | } |
| 799 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 800 | static int smb347_irq_init(struct smb347_charger *smb, |
| 801 | struct i2c_client *client) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 802 | { |
| 803 | const struct smb347_charger_platform_data *pdata = smb->pdata; |
| 804 | int ret, irq = gpio_to_irq(pdata->irq_gpio); |
| 805 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 806 | ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 807 | if (ret < 0) |
| 808 | goto fail; |
| 809 | |
| 810 | ret = request_threaded_irq(irq, NULL, smb347_interrupt, |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 811 | IRQF_TRIGGER_FALLING, client->name, smb); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 812 | if (ret < 0) |
| 813 | goto fail_gpio; |
| 814 | |
| 815 | ret = smb347_set_writable(smb, true); |
| 816 | if (ret < 0) |
| 817 | goto fail_irq; |
| 818 | |
| 819 | /* |
| 820 | * Configure the STAT output to be suitable for interrupts: disable |
| 821 | * all other output (except interrupts) and make it active low. |
| 822 | */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 823 | ret = regmap_update_bits(smb->regmap, CFG_STAT, |
| 824 | CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED, |
| 825 | CFG_STAT_DISABLED); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 826 | if (ret < 0) |
| 827 | goto fail_readonly; |
| 828 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 829 | smb347_set_writable(smb, false); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 830 | client->irq = irq; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 831 | return 0; |
| 832 | |
| 833 | fail_readonly: |
| 834 | smb347_set_writable(smb, false); |
| 835 | fail_irq: |
| 836 | free_irq(irq, smb); |
| 837 | fail_gpio: |
| 838 | gpio_free(pdata->irq_gpio); |
| 839 | fail: |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 840 | client->irq = 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 841 | return ret; |
| 842 | } |
| 843 | |
| 844 | static int smb347_mains_get_property(struct power_supply *psy, |
| 845 | enum power_supply_property prop, |
| 846 | union power_supply_propval *val) |
| 847 | { |
| 848 | struct smb347_charger *smb = |
| 849 | container_of(psy, struct smb347_charger, mains); |
| 850 | |
| 851 | if (prop == POWER_SUPPLY_PROP_ONLINE) { |
| 852 | val->intval = smb->mains_online; |
| 853 | return 0; |
| 854 | } |
| 855 | return -EINVAL; |
| 856 | } |
| 857 | |
| 858 | static enum power_supply_property smb347_mains_properties[] = { |
| 859 | POWER_SUPPLY_PROP_ONLINE, |
| 860 | }; |
| 861 | |
| 862 | static int smb347_usb_get_property(struct power_supply *psy, |
| 863 | enum power_supply_property prop, |
| 864 | union power_supply_propval *val) |
| 865 | { |
| 866 | struct smb347_charger *smb = |
| 867 | container_of(psy, struct smb347_charger, usb); |
| 868 | |
| 869 | if (prop == POWER_SUPPLY_PROP_ONLINE) { |
| 870 | val->intval = smb->usb_online; |
| 871 | return 0; |
| 872 | } |
| 873 | return -EINVAL; |
| 874 | } |
| 875 | |
| 876 | static enum power_supply_property smb347_usb_properties[] = { |
| 877 | POWER_SUPPLY_PROP_ONLINE, |
| 878 | }; |
| 879 | |
| 880 | static int smb347_battery_get_property(struct power_supply *psy, |
| 881 | enum power_supply_property prop, |
| 882 | union power_supply_propval *val) |
| 883 | { |
| 884 | struct smb347_charger *smb = |
| 885 | container_of(psy, struct smb347_charger, battery); |
| 886 | const struct smb347_charger_platform_data *pdata = smb->pdata; |
| 887 | int ret; |
| 888 | |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 889 | ret = smb347_update_ps_status(smb); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 890 | if (ret < 0) |
| 891 | return ret; |
| 892 | |
| 893 | switch (prop) { |
| 894 | case POWER_SUPPLY_PROP_STATUS: |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 895 | if (!smb347_is_ps_online(smb)) { |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 896 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 897 | break; |
| 898 | } |
| 899 | if (smb347_charging_status(smb)) |
| 900 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 901 | else |
| 902 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 903 | break; |
| 904 | |
| 905 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 906 | if (!smb347_is_ps_online(smb)) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 907 | return -ENODATA; |
| 908 | |
| 909 | /* |
| 910 | * We handle trickle and pre-charging the same, and taper |
| 911 | * and none the same. |
| 912 | */ |
| 913 | switch (smb347_charging_status(smb)) { |
| 914 | case 1: |
| 915 | val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 916 | break; |
| 917 | case 2: |
| 918 | val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 919 | break; |
| 920 | default: |
| 921 | val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 922 | break; |
| 923 | } |
| 924 | break; |
| 925 | |
| 926 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 927 | val->intval = pdata->battery_info.technology; |
| 928 | break; |
| 929 | |
| 930 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
| 931 | val->intval = pdata->battery_info.voltage_min_design; |
| 932 | break; |
| 933 | |
| 934 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 935 | val->intval = pdata->battery_info.voltage_max_design; |
| 936 | break; |
| 937 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 938 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 939 | val->intval = pdata->battery_info.charge_full_design; |
| 940 | break; |
| 941 | |
| 942 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 943 | val->strval = pdata->battery_info.name; |
| 944 | break; |
| 945 | |
| 946 | default: |
| 947 | return -EINVAL; |
| 948 | } |
| 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | static enum power_supply_property smb347_battery_properties[] = { |
| 954 | POWER_SUPPLY_PROP_STATUS, |
| 955 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 956 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 957 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 958 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 959 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 960 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 961 | }; |
| 962 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 963 | static bool smb347_volatile_reg(struct device *dev, unsigned int reg) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 964 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 965 | switch (reg) { |
| 966 | case IRQSTAT_A: |
| 967 | case IRQSTAT_C: |
| 968 | case IRQSTAT_E: |
| 969 | case IRQSTAT_F: |
| 970 | case STAT_A: |
| 971 | case STAT_B: |
| 972 | case STAT_C: |
| 973 | case STAT_E: |
| 974 | return true; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 977 | return false; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 980 | static bool smb347_readable_reg(struct device *dev, unsigned int reg) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 981 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 982 | switch (reg) { |
| 983 | case CFG_CHARGE_CURRENT: |
| 984 | case CFG_CURRENT_LIMIT: |
| 985 | case CFG_FLOAT_VOLTAGE: |
| 986 | case CFG_STAT: |
| 987 | case CFG_PIN: |
| 988 | case CFG_THERM: |
| 989 | case CFG_SYSOK: |
| 990 | case CFG_OTHER: |
| 991 | case CFG_OTG: |
| 992 | case CFG_TEMP_LIMIT: |
| 993 | case CFG_FAULT_IRQ: |
| 994 | case CFG_STATUS_IRQ: |
| 995 | case CFG_ADDRESS: |
| 996 | case CMD_A: |
| 997 | case CMD_B: |
| 998 | case CMD_C: |
| 999 | return true; |
| 1000 | } |
| 1001 | |
| 1002 | return smb347_volatile_reg(dev, reg); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1005 | static const struct regmap_config smb347_regmap = { |
| 1006 | .reg_bits = 8, |
| 1007 | .val_bits = 8, |
| 1008 | .max_register = SMB347_MAX_REGISTER, |
| 1009 | .volatile_reg = smb347_volatile_reg, |
| 1010 | .readable_reg = smb347_readable_reg, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1011 | }; |
| 1012 | |
| 1013 | static int smb347_probe(struct i2c_client *client, |
| 1014 | const struct i2c_device_id *id) |
| 1015 | { |
| 1016 | static char *battery[] = { "smb347-battery" }; |
| 1017 | const struct smb347_charger_platform_data *pdata; |
| 1018 | struct device *dev = &client->dev; |
| 1019 | struct smb347_charger *smb; |
| 1020 | int ret; |
| 1021 | |
| 1022 | pdata = dev->platform_data; |
| 1023 | if (!pdata) |
| 1024 | return -EINVAL; |
| 1025 | |
| 1026 | if (!pdata->use_mains && !pdata->use_usb) |
| 1027 | return -EINVAL; |
| 1028 | |
| 1029 | smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL); |
| 1030 | if (!smb) |
| 1031 | return -ENOMEM; |
| 1032 | |
| 1033 | i2c_set_clientdata(client, smb); |
| 1034 | |
| 1035 | mutex_init(&smb->lock); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1036 | smb->dev = &client->dev; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1037 | smb->pdata = pdata; |
| 1038 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1039 | smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap); |
| 1040 | if (IS_ERR(smb->regmap)) |
| 1041 | return PTR_ERR(smb->regmap); |
| 1042 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1043 | ret = smb347_hw_init(smb); |
| 1044 | if (ret < 0) |
| 1045 | return ret; |
| 1046 | |
Ramakrishna Pallala | e6fe359 | 2012-04-19 10:00:18 +0530 | [diff] [blame] | 1047 | if (smb->pdata->use_mains) { |
| 1048 | smb->mains.name = "smb347-mains"; |
| 1049 | smb->mains.type = POWER_SUPPLY_TYPE_MAINS; |
| 1050 | smb->mains.get_property = smb347_mains_get_property; |
| 1051 | smb->mains.properties = smb347_mains_properties; |
| 1052 | smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties); |
| 1053 | smb->mains.supplied_to = battery; |
| 1054 | smb->mains.num_supplicants = ARRAY_SIZE(battery); |
| 1055 | ret = power_supply_register(dev, &smb->mains); |
| 1056 | if (ret < 0) |
| 1057 | return ret; |
| 1058 | } |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1059 | |
Ramakrishna Pallala | e6fe359 | 2012-04-19 10:00:18 +0530 | [diff] [blame] | 1060 | if (smb->pdata->use_usb) { |
| 1061 | smb->usb.name = "smb347-usb"; |
| 1062 | smb->usb.type = POWER_SUPPLY_TYPE_USB; |
| 1063 | smb->usb.get_property = smb347_usb_get_property; |
| 1064 | smb->usb.properties = smb347_usb_properties; |
| 1065 | smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties); |
| 1066 | smb->usb.supplied_to = battery; |
| 1067 | smb->usb.num_supplicants = ARRAY_SIZE(battery); |
| 1068 | ret = power_supply_register(dev, &smb->usb); |
| 1069 | if (ret < 0) { |
| 1070 | if (smb->pdata->use_mains) |
| 1071 | power_supply_unregister(&smb->mains); |
| 1072 | return ret; |
| 1073 | } |
| 1074 | } |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1075 | |
| 1076 | smb->battery.name = "smb347-battery"; |
| 1077 | smb->battery.type = POWER_SUPPLY_TYPE_BATTERY; |
| 1078 | smb->battery.get_property = smb347_battery_get_property; |
| 1079 | smb->battery.properties = smb347_battery_properties; |
| 1080 | smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties); |
| 1081 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1082 | |
| 1083 | ret = power_supply_register(dev, &smb->battery); |
| 1084 | if (ret < 0) { |
Ramakrishna Pallala | e6fe359 | 2012-04-19 10:00:18 +0530 | [diff] [blame] | 1085 | if (smb->pdata->use_usb) |
| 1086 | power_supply_unregister(&smb->usb); |
| 1087 | if (smb->pdata->use_mains) |
| 1088 | power_supply_unregister(&smb->mains); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1089 | return ret; |
| 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * Interrupt pin is optional. If it is connected, we setup the |
| 1094 | * interrupt support here. |
| 1095 | */ |
| 1096 | if (pdata->irq_gpio >= 0) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1097 | ret = smb347_irq_init(smb, client); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1098 | if (ret < 0) { |
| 1099 | dev_warn(dev, "failed to initialize IRQ: %d\n", ret); |
| 1100 | dev_warn(dev, "disabling IRQ support\n"); |
Mika Westerberg | d72bade | 2012-04-16 11:48:40 +0300 | [diff] [blame] | 1101 | } else { |
| 1102 | smb347_irq_enable(smb); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | static int smb347_remove(struct i2c_client *client) |
| 1110 | { |
| 1111 | struct smb347_charger *smb = i2c_get_clientdata(client); |
| 1112 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1113 | if (client->irq) { |
| 1114 | smb347_irq_disable(smb); |
| 1115 | free_irq(client->irq, smb); |
| 1116 | gpio_free(smb->pdata->irq_gpio); |
| 1117 | } |
| 1118 | |
| 1119 | power_supply_unregister(&smb->battery); |
Ramakrishna Pallala | e6fe359 | 2012-04-19 10:00:18 +0530 | [diff] [blame] | 1120 | if (smb->pdata->use_usb) |
| 1121 | power_supply_unregister(&smb->usb); |
| 1122 | if (smb->pdata->use_mains) |
| 1123 | power_supply_unregister(&smb->mains); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static const struct i2c_device_id smb347_id[] = { |
| 1128 | { "smb347", 0 }, |
| 1129 | { } |
| 1130 | }; |
| 1131 | MODULE_DEVICE_TABLE(i2c, smb347_id); |
| 1132 | |
| 1133 | static struct i2c_driver smb347_driver = { |
| 1134 | .driver = { |
| 1135 | .name = "smb347", |
| 1136 | }, |
| 1137 | .probe = smb347_probe, |
| 1138 | .remove = __devexit_p(smb347_remove), |
| 1139 | .id_table = smb347_id, |
| 1140 | }; |
| 1141 | |
Mika Westerberg | b75ef1d | 2012-04-16 11:48:38 +0300 | [diff] [blame] | 1142 | module_i2c_driver(smb347_driver); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1143 | |
| 1144 | MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>"); |
| 1145 | MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); |
| 1146 | MODULE_DESCRIPTION("SMB347 battery charger driver"); |
| 1147 | MODULE_LICENSE("GPL"); |
| 1148 | MODULE_ALIAS("i2c:smb347"); |