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