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