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