| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Summit Microelectronics SMB347 Battery Charger Driver |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com> |
|---|
| 7 | 8 | * 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 | 9 | */ |
|---|
| 13 | 10 | |
|---|
| 11 | +#include <linux/delay.h> |
|---|
| 14 | 12 | #include <linux/err.h> |
|---|
| 15 | 13 | #include <linux/gpio.h> |
|---|
| 16 | 14 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 18 | 16 | #include <linux/init.h> |
|---|
| 19 | 17 | #include <linux/interrupt.h> |
|---|
| 20 | 18 | #include <linux/i2c.h> |
|---|
| 21 | | -#include <linux/mutex.h> |
|---|
| 22 | 19 | #include <linux/power_supply.h> |
|---|
| 23 | | -#include <linux/power/smb347-charger.h> |
|---|
| 20 | +#include <linux/property.h> |
|---|
| 24 | 21 | #include <linux/regmap.h> |
|---|
| 22 | + |
|---|
| 23 | +#include <dt-bindings/power/summit,smb347-charger.h> |
|---|
| 24 | + |
|---|
| 25 | +/* Use the default compensation method */ |
|---|
| 26 | +#define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1 |
|---|
| 27 | + |
|---|
| 28 | +/* Use default factory programmed value for hard/soft temperature limit */ |
|---|
| 29 | +#define SMB3XX_TEMP_USE_DEFAULT -273 |
|---|
| 25 | 30 | |
|---|
| 26 | 31 | /* |
|---|
| 27 | 32 | * Configuration registers. These are mirrored to volatile RAM and can be |
|---|
| .. | .. |
|---|
| 51 | 56 | #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60 |
|---|
| 52 | 57 | #define CFG_PIN_EN_APSD_IRQ BIT(1) |
|---|
| 53 | 58 | #define CFG_PIN_EN_CHARGER_ERROR BIT(2) |
|---|
| 59 | +#define CFG_PIN_EN_CTRL BIT(4) |
|---|
| 54 | 60 | #define CFG_THERM 0x07 |
|---|
| 55 | 61 | #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03 |
|---|
| 56 | 62 | #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0 |
|---|
| .. | .. |
|---|
| 124 | 130 | |
|---|
| 125 | 131 | /** |
|---|
| 126 | 132 | * struct smb347_charger - smb347 charger instance |
|---|
| 127 | | - * @lock: protects concurrent access to online variables |
|---|
| 128 | 133 | * @dev: pointer to device |
|---|
| 129 | 134 | * @regmap: pointer to driver regmap |
|---|
| 130 | 135 | * @mains: power_supply instance for AC/DC power |
|---|
| 131 | 136 | * @usb: power_supply instance for USB power |
|---|
| 132 | | - * @battery: power_supply instance for battery |
|---|
| 137 | + * @id: SMB charger ID |
|---|
| 133 | 138 | * @mains_online: is AC/DC input connected |
|---|
| 134 | 139 | * @usb_online: is USB input connected |
|---|
| 135 | 140 | * @charging_enabled: is charging enabled |
|---|
| 136 | | - * @pdata: pointer to platform data |
|---|
| 141 | + * @irq_unsupported: is interrupt unsupported by SMB hardware |
|---|
| 142 | + * @max_charge_current: maximum current (in uA) the battery can be charged |
|---|
| 143 | + * @max_charge_voltage: maximum voltage (in uV) the battery can be charged |
|---|
| 144 | + * @pre_charge_current: current (in uA) to use in pre-charging phase |
|---|
| 145 | + * @termination_current: current (in uA) used to determine when the |
|---|
| 146 | + * charging cycle terminates |
|---|
| 147 | + * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to |
|---|
| 148 | + * pre-charge to fast charge mode |
|---|
| 149 | + * @mains_current_limit: maximum input current drawn from AC/DC input (in uA) |
|---|
| 150 | + * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB |
|---|
| 151 | + * input |
|---|
| 152 | + * @chip_temp_threshold: die temperature where device starts limiting charge |
|---|
| 153 | + * current [%100 - %130] (in degree C) |
|---|
| 154 | + * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C), |
|---|
| 155 | + * granularity is 5 deg C. |
|---|
| 156 | + * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree C), |
|---|
| 157 | + * granularity is 5 deg C. |
|---|
| 158 | + * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C), |
|---|
| 159 | + * granularity is 5 deg C. |
|---|
| 160 | + * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C), |
|---|
| 161 | + * granularity is 5 deg C. |
|---|
| 162 | + * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit |
|---|
| 163 | + * @soft_temp_limit_compensation: compensation method when soft temperature |
|---|
| 164 | + * limit is hit |
|---|
| 165 | + * @charge_current_compensation: current (in uA) for charging compensation |
|---|
| 166 | + * current when temperature hits soft limits |
|---|
| 167 | + * @use_mains: AC/DC input can be used |
|---|
| 168 | + * @use_usb: USB input can be used |
|---|
| 169 | + * @use_usb_otg: USB OTG output can be used (not implemented yet) |
|---|
| 170 | + * @enable_control: how charging enable/disable is controlled |
|---|
| 171 | + * (driver/pin controls) |
|---|
| 172 | + * |
|---|
| 173 | + * @use_main, @use_usb, and @use_usb_otg are means to enable/disable |
|---|
| 174 | + * hardware support for these. This is useful when we want to have for |
|---|
| 175 | + * example OTG charging controlled via OTG transceiver driver and not by |
|---|
| 176 | + * the SMB347 hardware. |
|---|
| 177 | + * |
|---|
| 178 | + * Hard and soft temperature limit values are given as described in the |
|---|
| 179 | + * device data sheet and assuming NTC beta value is %3750. Even if this is |
|---|
| 180 | + * not the case, these values should be used. They can be mapped to the |
|---|
| 181 | + * corresponding NTC beta values with the help of table %2 in the data |
|---|
| 182 | + * sheet. So for example if NTC beta is %3375 and we want to program hard |
|---|
| 183 | + * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50. |
|---|
| 184 | + * |
|---|
| 185 | + * If zero value is given in any of the current and voltage values, the |
|---|
| 186 | + * factory programmed default will be used. For soft/hard temperature |
|---|
| 187 | + * values, pass in %SMB3XX_TEMP_USE_DEFAULT instead. |
|---|
| 137 | 188 | */ |
|---|
| 138 | 189 | struct smb347_charger { |
|---|
| 139 | | - struct mutex lock; |
|---|
| 140 | 190 | struct device *dev; |
|---|
| 141 | 191 | struct regmap *regmap; |
|---|
| 142 | 192 | struct power_supply *mains; |
|---|
| 143 | 193 | struct power_supply *usb; |
|---|
| 144 | | - struct power_supply *battery; |
|---|
| 194 | + unsigned int id; |
|---|
| 145 | 195 | bool mains_online; |
|---|
| 146 | 196 | bool usb_online; |
|---|
| 147 | 197 | bool charging_enabled; |
|---|
| 148 | | - const struct smb347_charger_platform_data *pdata; |
|---|
| 198 | + bool irq_unsupported; |
|---|
| 199 | + |
|---|
| 200 | + unsigned int max_charge_current; |
|---|
| 201 | + unsigned int max_charge_voltage; |
|---|
| 202 | + unsigned int pre_charge_current; |
|---|
| 203 | + unsigned int termination_current; |
|---|
| 204 | + unsigned int pre_to_fast_voltage; |
|---|
| 205 | + unsigned int mains_current_limit; |
|---|
| 206 | + unsigned int usb_hc_current_limit; |
|---|
| 207 | + unsigned int chip_temp_threshold; |
|---|
| 208 | + int soft_cold_temp_limit; |
|---|
| 209 | + int soft_hot_temp_limit; |
|---|
| 210 | + int hard_cold_temp_limit; |
|---|
| 211 | + int hard_hot_temp_limit; |
|---|
| 212 | + bool suspend_on_hard_temp_limit; |
|---|
| 213 | + unsigned int soft_temp_limit_compensation; |
|---|
| 214 | + unsigned int charge_current_compensation; |
|---|
| 215 | + bool use_mains; |
|---|
| 216 | + bool use_usb; |
|---|
| 217 | + bool use_usb_otg; |
|---|
| 218 | + unsigned int enable_control; |
|---|
| 219 | +}; |
|---|
| 220 | + |
|---|
| 221 | +enum smb_charger_chipid { |
|---|
| 222 | + SMB345, |
|---|
| 223 | + SMB347, |
|---|
| 224 | + SMB358, |
|---|
| 225 | + NUM_CHIP_TYPES, |
|---|
| 149 | 226 | }; |
|---|
| 150 | 227 | |
|---|
| 151 | 228 | /* 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, |
|---|
| 229 | +static const unsigned int fcc_tbl[NUM_CHIP_TYPES][8] = { |
|---|
| 230 | + [SMB345] = { 200000, 450000, 600000, 900000, |
|---|
| 231 | + 1300000, 1500000, 1800000, 2000000 }, |
|---|
| 232 | + [SMB347] = { 700000, 900000, 1200000, 1500000, |
|---|
| 233 | + 1800000, 2000000, 2200000, 2500000 }, |
|---|
| 234 | + [SMB358] = { 200000, 450000, 600000, 900000, |
|---|
| 235 | + 1300000, 1500000, 1800000, 2000000 }, |
|---|
| 161 | 236 | }; |
|---|
| 162 | | - |
|---|
| 163 | 237 | /* Pre-charge current in uA */ |
|---|
| 164 | | -static const unsigned int pcc_tbl[] = { |
|---|
| 165 | | - 100000, |
|---|
| 166 | | - 150000, |
|---|
| 167 | | - 200000, |
|---|
| 168 | | - 250000, |
|---|
| 238 | +static const unsigned int pcc_tbl[NUM_CHIP_TYPES][4] = { |
|---|
| 239 | + [SMB345] = { 150000, 250000, 350000, 450000 }, |
|---|
| 240 | + [SMB347] = { 100000, 150000, 200000, 250000 }, |
|---|
| 241 | + [SMB358] = { 150000, 250000, 350000, 450000 }, |
|---|
| 169 | 242 | }; |
|---|
| 170 | 243 | |
|---|
| 171 | 244 | /* 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, |
|---|
| 245 | +static const unsigned int tc_tbl[NUM_CHIP_TYPES][8] = { |
|---|
| 246 | + [SMB345] = { 30000, 40000, 60000, 80000, |
|---|
| 247 | + 100000, 125000, 150000, 200000 }, |
|---|
| 248 | + [SMB347] = { 37500, 50000, 100000, 150000, |
|---|
| 249 | + 200000, 250000, 500000, 600000 }, |
|---|
| 250 | + [SMB358] = { 30000, 40000, 60000, 80000, |
|---|
| 251 | + 100000, 125000, 150000, 200000 }, |
|---|
| 181 | 252 | }; |
|---|
| 182 | 253 | |
|---|
| 183 | 254 | /* 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, |
|---|
| 255 | +static const unsigned int icl_tbl[NUM_CHIP_TYPES][10] = { |
|---|
| 256 | + [SMB345] = { 300000, 500000, 700000, 1000000, 1500000, |
|---|
| 257 | + 1800000, 2000000, 2000000, 2000000, 2000000 }, |
|---|
| 258 | + [SMB347] = { 300000, 500000, 700000, 900000, 1200000, |
|---|
| 259 | + 1500000, 1800000, 2000000, 2200000, 2500000 }, |
|---|
| 260 | + [SMB358] = { 300000, 500000, 700000, 1000000, 1500000, |
|---|
| 261 | + 1800000, 2000000, 2000000, 2000000, 2000000 }, |
|---|
| 195 | 262 | }; |
|---|
| 196 | 263 | |
|---|
| 197 | 264 | /* Charge current compensation in uA */ |
|---|
| 198 | | -static const unsigned int ccc_tbl[] = { |
|---|
| 199 | | - 250000, |
|---|
| 200 | | - 700000, |
|---|
| 201 | | - 900000, |
|---|
| 202 | | - 1200000, |
|---|
| 265 | +static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = { |
|---|
| 266 | + [SMB345] = { 200000, 450000, 600000, 900000 }, |
|---|
| 267 | + [SMB347] = { 250000, 700000, 900000, 1200000 }, |
|---|
| 268 | + [SMB358] = { 200000, 450000, 600000, 900000 }, |
|---|
| 203 | 269 | }; |
|---|
| 204 | 270 | |
|---|
| 205 | 271 | /* Convert register value to current using lookup table */ |
|---|
| .. | .. |
|---|
| 244 | 310 | * Dc and usb are set depending on whether they are enabled in |
|---|
| 245 | 311 | * platform data _and_ whether corresponding undervoltage is set. |
|---|
| 246 | 312 | */ |
|---|
| 247 | | - if (smb->pdata->use_mains) |
|---|
| 313 | + if (smb->use_mains) |
|---|
| 248 | 314 | dc = !(val & IRQSTAT_E_DCIN_UV_STAT); |
|---|
| 249 | | - if (smb->pdata->use_usb) |
|---|
| 315 | + if (smb->use_usb) |
|---|
| 250 | 316 | usb = !(val & IRQSTAT_E_USBIN_UV_STAT); |
|---|
| 251 | 317 | |
|---|
| 252 | | - mutex_lock(&smb->lock); |
|---|
| 253 | 318 | ret = smb->mains_online != dc || smb->usb_online != usb; |
|---|
| 254 | 319 | smb->mains_online = dc; |
|---|
| 255 | 320 | smb->usb_online = usb; |
|---|
| 256 | | - mutex_unlock(&smb->lock); |
|---|
| 257 | 321 | |
|---|
| 258 | 322 | return ret; |
|---|
| 259 | 323 | } |
|---|
| .. | .. |
|---|
| 269 | 333 | */ |
|---|
| 270 | 334 | static bool smb347_is_ps_online(struct smb347_charger *smb) |
|---|
| 271 | 335 | { |
|---|
| 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; |
|---|
| 336 | + return smb->usb_online || smb->mains_online; |
|---|
| 279 | 337 | } |
|---|
| 280 | 338 | |
|---|
| 281 | 339 | /** |
|---|
| .. | .. |
|---|
| 304 | 362 | { |
|---|
| 305 | 363 | int ret = 0; |
|---|
| 306 | 364 | |
|---|
| 307 | | - if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) { |
|---|
| 365 | + if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) { |
|---|
| 308 | 366 | dev_dbg(smb->dev, "charging enable/disable in SW disabled\n"); |
|---|
| 309 | 367 | return 0; |
|---|
| 310 | 368 | } |
|---|
| 311 | 369 | |
|---|
| 312 | | - mutex_lock(&smb->lock); |
|---|
| 313 | 370 | if (smb->charging_enabled != enable) { |
|---|
| 314 | 371 | ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED, |
|---|
| 315 | 372 | enable ? CMD_A_CHG_ENABLED : 0); |
|---|
| 316 | 373 | if (!ret) |
|---|
| 317 | 374 | smb->charging_enabled = enable; |
|---|
| 318 | 375 | } |
|---|
| 319 | | - mutex_unlock(&smb->lock); |
|---|
| 376 | + |
|---|
| 320 | 377 | return ret; |
|---|
| 321 | 378 | } |
|---|
| 322 | 379 | |
|---|
| .. | .. |
|---|
| 354 | 411 | |
|---|
| 355 | 412 | static int smb347_set_charge_current(struct smb347_charger *smb) |
|---|
| 356 | 413 | { |
|---|
| 414 | + unsigned int id = smb->id; |
|---|
| 357 | 415 | int ret; |
|---|
| 358 | 416 | |
|---|
| 359 | | - if (smb->pdata->max_charge_current) { |
|---|
| 360 | | - ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl), |
|---|
| 361 | | - smb->pdata->max_charge_current); |
|---|
| 417 | + if (smb->max_charge_current) { |
|---|
| 418 | + ret = current_to_hw(fcc_tbl[id], ARRAY_SIZE(fcc_tbl[id]), |
|---|
| 419 | + smb->max_charge_current); |
|---|
| 362 | 420 | if (ret < 0) |
|---|
| 363 | 421 | return ret; |
|---|
| 364 | 422 | |
|---|
| .. | .. |
|---|
| 369 | 427 | return ret; |
|---|
| 370 | 428 | } |
|---|
| 371 | 429 | |
|---|
| 372 | | - if (smb->pdata->pre_charge_current) { |
|---|
| 373 | | - ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl), |
|---|
| 374 | | - smb->pdata->pre_charge_current); |
|---|
| 430 | + if (smb->pre_charge_current) { |
|---|
| 431 | + ret = current_to_hw(pcc_tbl[id], ARRAY_SIZE(pcc_tbl[id]), |
|---|
| 432 | + smb->pre_charge_current); |
|---|
| 375 | 433 | if (ret < 0) |
|---|
| 376 | 434 | return ret; |
|---|
| 377 | 435 | |
|---|
| .. | .. |
|---|
| 382 | 440 | return ret; |
|---|
| 383 | 441 | } |
|---|
| 384 | 442 | |
|---|
| 385 | | - if (smb->pdata->termination_current) { |
|---|
| 386 | | - ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl), |
|---|
| 387 | | - smb->pdata->termination_current); |
|---|
| 443 | + if (smb->termination_current) { |
|---|
| 444 | + ret = current_to_hw(tc_tbl[id], ARRAY_SIZE(tc_tbl[id]), |
|---|
| 445 | + smb->termination_current); |
|---|
| 388 | 446 | if (ret < 0) |
|---|
| 389 | 447 | return ret; |
|---|
| 390 | 448 | |
|---|
| .. | .. |
|---|
| 399 | 457 | |
|---|
| 400 | 458 | static int smb347_set_current_limits(struct smb347_charger *smb) |
|---|
| 401 | 459 | { |
|---|
| 460 | + unsigned int id = smb->id; |
|---|
| 402 | 461 | int ret; |
|---|
| 403 | 462 | |
|---|
| 404 | | - if (smb->pdata->mains_current_limit) { |
|---|
| 405 | | - ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), |
|---|
| 406 | | - smb->pdata->mains_current_limit); |
|---|
| 463 | + if (smb->mains_current_limit) { |
|---|
| 464 | + ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]), |
|---|
| 465 | + smb->mains_current_limit); |
|---|
| 407 | 466 | if (ret < 0) |
|---|
| 408 | 467 | return ret; |
|---|
| 409 | 468 | |
|---|
| .. | .. |
|---|
| 414 | 473 | return ret; |
|---|
| 415 | 474 | } |
|---|
| 416 | 475 | |
|---|
| 417 | | - if (smb->pdata->usb_hc_current_limit) { |
|---|
| 418 | | - ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), |
|---|
| 419 | | - smb->pdata->usb_hc_current_limit); |
|---|
| 476 | + if (smb->usb_hc_current_limit) { |
|---|
| 477 | + ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]), |
|---|
| 478 | + smb->usb_hc_current_limit); |
|---|
| 420 | 479 | if (ret < 0) |
|---|
| 421 | 480 | return ret; |
|---|
| 422 | 481 | |
|---|
| .. | .. |
|---|
| 433 | 492 | { |
|---|
| 434 | 493 | int ret; |
|---|
| 435 | 494 | |
|---|
| 436 | | - if (smb->pdata->pre_to_fast_voltage) { |
|---|
| 437 | | - ret = smb->pdata->pre_to_fast_voltage; |
|---|
| 495 | + if (smb->pre_to_fast_voltage) { |
|---|
| 496 | + ret = smb->pre_to_fast_voltage; |
|---|
| 438 | 497 | |
|---|
| 439 | 498 | /* uV */ |
|---|
| 440 | 499 | ret = clamp_val(ret, 2400000, 3000000) - 2400000; |
|---|
| .. | .. |
|---|
| 447 | 506 | return ret; |
|---|
| 448 | 507 | } |
|---|
| 449 | 508 | |
|---|
| 450 | | - if (smb->pdata->max_charge_voltage) { |
|---|
| 451 | | - ret = smb->pdata->max_charge_voltage; |
|---|
| 509 | + if (smb->max_charge_voltage) { |
|---|
| 510 | + ret = smb->max_charge_voltage; |
|---|
| 452 | 511 | |
|---|
| 453 | 512 | /* uV */ |
|---|
| 454 | 513 | ret = clamp_val(ret, 3500000, 4500000) - 3500000; |
|---|
| .. | .. |
|---|
| 465 | 524 | |
|---|
| 466 | 525 | static int smb347_set_temp_limits(struct smb347_charger *smb) |
|---|
| 467 | 526 | { |
|---|
| 527 | + unsigned int id = smb->id; |
|---|
| 468 | 528 | bool enable_therm_monitor = false; |
|---|
| 469 | 529 | int ret = 0; |
|---|
| 470 | 530 | int val; |
|---|
| 471 | 531 | |
|---|
| 472 | | - if (smb->pdata->chip_temp_threshold) { |
|---|
| 473 | | - val = smb->pdata->chip_temp_threshold; |
|---|
| 532 | + if (smb->chip_temp_threshold) { |
|---|
| 533 | + val = smb->chip_temp_threshold; |
|---|
| 474 | 534 | |
|---|
| 475 | 535 | /* degree C */ |
|---|
| 476 | 536 | val = clamp_val(val, 100, 130) - 100; |
|---|
| .. | .. |
|---|
| 483 | 543 | return ret; |
|---|
| 484 | 544 | } |
|---|
| 485 | 545 | |
|---|
| 486 | | - if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { |
|---|
| 487 | | - val = smb->pdata->soft_cold_temp_limit; |
|---|
| 546 | + if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { |
|---|
| 547 | + val = smb->soft_cold_temp_limit; |
|---|
| 488 | 548 | |
|---|
| 489 | 549 | val = clamp_val(val, 0, 15); |
|---|
| 490 | 550 | val /= 5; |
|---|
| .. | .. |
|---|
| 500 | 560 | enable_therm_monitor = true; |
|---|
| 501 | 561 | } |
|---|
| 502 | 562 | |
|---|
| 503 | | - if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) { |
|---|
| 504 | | - val = smb->pdata->soft_hot_temp_limit; |
|---|
| 563 | + if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { |
|---|
| 564 | + val = smb->soft_hot_temp_limit; |
|---|
| 505 | 565 | |
|---|
| 506 | 566 | val = clamp_val(val, 40, 55) - 40; |
|---|
| 507 | 567 | val /= 5; |
|---|
| .. | .. |
|---|
| 515 | 575 | enable_therm_monitor = true; |
|---|
| 516 | 576 | } |
|---|
| 517 | 577 | |
|---|
| 518 | | - if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { |
|---|
| 519 | | - val = smb->pdata->hard_cold_temp_limit; |
|---|
| 578 | + if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { |
|---|
| 579 | + val = smb->hard_cold_temp_limit; |
|---|
| 520 | 580 | |
|---|
| 521 | 581 | val = clamp_val(val, -5, 10) + 5; |
|---|
| 522 | 582 | val /= 5; |
|---|
| .. | .. |
|---|
| 532 | 592 | enable_therm_monitor = true; |
|---|
| 533 | 593 | } |
|---|
| 534 | 594 | |
|---|
| 535 | | - if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) { |
|---|
| 536 | | - val = smb->pdata->hard_hot_temp_limit; |
|---|
| 595 | + if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { |
|---|
| 596 | + val = smb->hard_hot_temp_limit; |
|---|
| 537 | 597 | |
|---|
| 538 | 598 | val = clamp_val(val, 50, 65) - 50; |
|---|
| 539 | 599 | val /= 5; |
|---|
| .. | .. |
|---|
| 564 | 624 | return ret; |
|---|
| 565 | 625 | } |
|---|
| 566 | 626 | |
|---|
| 567 | | - if (smb->pdata->suspend_on_hard_temp_limit) { |
|---|
| 627 | + if (smb->suspend_on_hard_temp_limit) { |
|---|
| 568 | 628 | ret = regmap_update_bits(smb->regmap, CFG_SYSOK, |
|---|
| 569 | 629 | CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0); |
|---|
| 570 | 630 | if (ret < 0) |
|---|
| 571 | 631 | return ret; |
|---|
| 572 | 632 | } |
|---|
| 573 | 633 | |
|---|
| 574 | | - if (smb->pdata->soft_temp_limit_compensation != |
|---|
| 575 | | - SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) { |
|---|
| 576 | | - val = smb->pdata->soft_temp_limit_compensation & 0x3; |
|---|
| 634 | + if (smb->soft_temp_limit_compensation != |
|---|
| 635 | + SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT) { |
|---|
| 636 | + val = smb->soft_temp_limit_compensation & 0x3; |
|---|
| 577 | 637 | |
|---|
| 578 | 638 | ret = regmap_update_bits(smb->regmap, CFG_THERM, |
|---|
| 579 | 639 | CFG_THERM_SOFT_HOT_COMPENSATION_MASK, |
|---|
| .. | .. |
|---|
| 588 | 648 | return ret; |
|---|
| 589 | 649 | } |
|---|
| 590 | 650 | |
|---|
| 591 | | - if (smb->pdata->charge_current_compensation) { |
|---|
| 592 | | - val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl), |
|---|
| 593 | | - smb->pdata->charge_current_compensation); |
|---|
| 651 | + if (smb->charge_current_compensation) { |
|---|
| 652 | + val = current_to_hw(ccc_tbl[id], ARRAY_SIZE(ccc_tbl[id]), |
|---|
| 653 | + smb->charge_current_compensation); |
|---|
| 594 | 654 | if (val < 0) |
|---|
| 595 | 655 | return val; |
|---|
| 596 | 656 | |
|---|
| .. | .. |
|---|
| 649 | 709 | goto fail; |
|---|
| 650 | 710 | |
|---|
| 651 | 711 | /* If USB charging is disabled we put the USB in suspend mode */ |
|---|
| 652 | | - if (!smb->pdata->use_usb) { |
|---|
| 712 | + if (!smb->use_usb) { |
|---|
| 653 | 713 | ret = regmap_update_bits(smb->regmap, CMD_A, |
|---|
| 654 | 714 | CMD_A_SUSPEND_ENABLED, |
|---|
| 655 | 715 | CMD_A_SUSPEND_ENABLED); |
|---|
| .. | .. |
|---|
| 662 | 722 | * support for driving VBUS. Otherwise we disable it. |
|---|
| 663 | 723 | */ |
|---|
| 664 | 724 | 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); |
|---|
| 725 | + smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0); |
|---|
| 666 | 726 | if (ret < 0) |
|---|
| 667 | 727 | goto fail; |
|---|
| 728 | + |
|---|
| 729 | + /* Activate pin control, making it writable. */ |
|---|
| 730 | + switch (smb->enable_control) { |
|---|
| 731 | + case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW: |
|---|
| 732 | + case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH: |
|---|
| 733 | + ret = regmap_set_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL); |
|---|
| 734 | + if (ret < 0) |
|---|
| 735 | + goto fail; |
|---|
| 736 | + } |
|---|
| 668 | 737 | |
|---|
| 669 | 738 | /* |
|---|
| 670 | 739 | * Make the charging functionality controllable by a write to the |
|---|
| 671 | 740 | * command register unless pin control is specified in the platform |
|---|
| 672 | 741 | * data. |
|---|
| 673 | 742 | */ |
|---|
| 674 | | - switch (smb->pdata->enable_control) { |
|---|
| 675 | | - case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW: |
|---|
| 743 | + switch (smb->enable_control) { |
|---|
| 744 | + case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW: |
|---|
| 676 | 745 | val = CFG_PIN_EN_CTRL_ACTIVE_LOW; |
|---|
| 677 | 746 | break; |
|---|
| 678 | | - case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH: |
|---|
| 747 | + case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH: |
|---|
| 679 | 748 | val = CFG_PIN_EN_CTRL_ACTIVE_HIGH; |
|---|
| 680 | 749 | break; |
|---|
| 681 | 750 | default: |
|---|
| .. | .. |
|---|
| 711 | 780 | bool handled = false; |
|---|
| 712 | 781 | int ret; |
|---|
| 713 | 782 | |
|---|
| 783 | + /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */ |
|---|
| 784 | + usleep_range(25000, 35000); |
|---|
| 785 | + |
|---|
| 714 | 786 | ret = regmap_read(smb->regmap, STAT_C, &stat_c); |
|---|
| 715 | 787 | if (ret < 0) { |
|---|
| 716 | 788 | dev_warn(smb->dev, "reading STAT_C failed\n"); |
|---|
| .. | .. |
|---|
| 741 | 813 | */ |
|---|
| 742 | 814 | if (stat_c & STAT_C_CHARGER_ERROR) { |
|---|
| 743 | 815 | dev_err(smb->dev, "charging stopped due to charger error\n"); |
|---|
| 744 | | - power_supply_changed(smb->battery); |
|---|
| 816 | + if (smb->use_mains) |
|---|
| 817 | + power_supply_changed(smb->mains); |
|---|
| 818 | + if (smb->use_usb) |
|---|
| 819 | + power_supply_changed(smb->usb); |
|---|
| 745 | 820 | handled = true; |
|---|
| 746 | 821 | } |
|---|
| 747 | 822 | |
|---|
| .. | .. |
|---|
| 751 | 826 | * disabled by the hardware. |
|---|
| 752 | 827 | */ |
|---|
| 753 | 828 | if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) { |
|---|
| 754 | | - if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) |
|---|
| 755 | | - power_supply_changed(smb->battery); |
|---|
| 829 | + if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) { |
|---|
| 830 | + if (smb->use_mains) |
|---|
| 831 | + power_supply_changed(smb->mains); |
|---|
| 832 | + if (smb->use_usb) |
|---|
| 833 | + power_supply_changed(smb->usb); |
|---|
| 834 | + } |
|---|
| 756 | 835 | dev_dbg(smb->dev, "going to HW maintenance mode\n"); |
|---|
| 757 | 836 | handled = true; |
|---|
| 758 | 837 | } |
|---|
| .. | .. |
|---|
| 766 | 845 | |
|---|
| 767 | 846 | if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT) |
|---|
| 768 | 847 | dev_warn(smb->dev, "charging stopped due to timeout\n"); |
|---|
| 769 | | - power_supply_changed(smb->battery); |
|---|
| 848 | + if (smb->use_mains) |
|---|
| 849 | + power_supply_changed(smb->mains); |
|---|
| 850 | + if (smb->use_usb) |
|---|
| 851 | + power_supply_changed(smb->usb); |
|---|
| 770 | 852 | handled = true; |
|---|
| 771 | 853 | } |
|---|
| 772 | 854 | |
|---|
| .. | .. |
|---|
| 777 | 859 | if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) { |
|---|
| 778 | 860 | if (smb347_update_ps_status(smb) > 0) { |
|---|
| 779 | 861 | smb347_start_stop_charging(smb); |
|---|
| 780 | | - if (smb->pdata->use_mains) |
|---|
| 862 | + if (smb->use_mains) |
|---|
| 781 | 863 | power_supply_changed(smb->mains); |
|---|
| 782 | | - if (smb->pdata->use_usb) |
|---|
| 864 | + if (smb->use_usb) |
|---|
| 783 | 865 | power_supply_changed(smb->usb); |
|---|
| 784 | 866 | } |
|---|
| 785 | 867 | handled = true; |
|---|
| .. | .. |
|---|
| 791 | 873 | static int smb347_irq_set(struct smb347_charger *smb, bool enable) |
|---|
| 792 | 874 | { |
|---|
| 793 | 875 | int ret; |
|---|
| 876 | + |
|---|
| 877 | + if (smb->irq_unsupported) |
|---|
| 878 | + return 0; |
|---|
| 794 | 879 | |
|---|
| 795 | 880 | ret = smb347_set_writable(smb, true); |
|---|
| 796 | 881 | if (ret < 0) |
|---|
| .. | .. |
|---|
| 834 | 919 | static int smb347_irq_init(struct smb347_charger *smb, |
|---|
| 835 | 920 | struct i2c_client *client) |
|---|
| 836 | 921 | { |
|---|
| 837 | | - const struct smb347_charger_platform_data *pdata = smb->pdata; |
|---|
| 838 | | - int ret, irq = gpio_to_irq(pdata->irq_gpio); |
|---|
| 922 | + int ret; |
|---|
| 839 | 923 | |
|---|
| 840 | | - ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name); |
|---|
| 924 | + ret = devm_request_threaded_irq(smb->dev, client->irq, NULL, |
|---|
| 925 | + smb347_interrupt, IRQF_ONESHOT, |
|---|
| 926 | + client->name, smb); |
|---|
| 841 | 927 | if (ret < 0) |
|---|
| 842 | | - goto fail; |
|---|
| 843 | | - |
|---|
| 844 | | - ret = request_threaded_irq(irq, NULL, smb347_interrupt, |
|---|
| 845 | | - IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
|---|
| 846 | | - client->name, smb); |
|---|
| 847 | | - if (ret < 0) |
|---|
| 848 | | - goto fail_gpio; |
|---|
| 928 | + return ret; |
|---|
| 849 | 929 | |
|---|
| 850 | 930 | ret = smb347_set_writable(smb, true); |
|---|
| 851 | 931 | if (ret < 0) |
|---|
| 852 | | - goto fail_irq; |
|---|
| 932 | + return ret; |
|---|
| 853 | 933 | |
|---|
| 854 | 934 | /* |
|---|
| 855 | 935 | * Configure the STAT output to be suitable for interrupts: disable |
|---|
| .. | .. |
|---|
| 858 | 938 | ret = regmap_update_bits(smb->regmap, CFG_STAT, |
|---|
| 859 | 939 | CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED, |
|---|
| 860 | 940 | CFG_STAT_DISABLED); |
|---|
| 861 | | - if (ret < 0) |
|---|
| 862 | | - goto fail_readonly; |
|---|
| 863 | 941 | |
|---|
| 864 | 942 | smb347_set_writable(smb, false); |
|---|
| 865 | | - client->irq = irq; |
|---|
| 866 | | - return 0; |
|---|
| 867 | 943 | |
|---|
| 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: |
|---|
| 875 | | - client->irq = 0; |
|---|
| 876 | 944 | return ret; |
|---|
| 877 | 945 | } |
|---|
| 878 | 946 | |
|---|
| .. | .. |
|---|
| 882 | 950 | */ |
|---|
| 883 | 951 | static int get_const_charge_current(struct smb347_charger *smb) |
|---|
| 884 | 952 | { |
|---|
| 953 | + unsigned int id = smb->id; |
|---|
| 885 | 954 | int ret, intval; |
|---|
| 886 | 955 | unsigned int v; |
|---|
| 887 | 956 | |
|---|
| .. | .. |
|---|
| 897 | 966 | * and we can detect which table to use from bit 5. |
|---|
| 898 | 967 | */ |
|---|
| 899 | 968 | if (v & 0x20) { |
|---|
| 900 | | - intval = hw_to_current(fcc_tbl, ARRAY_SIZE(fcc_tbl), v & 7); |
|---|
| 969 | + intval = hw_to_current(fcc_tbl[id], |
|---|
| 970 | + ARRAY_SIZE(fcc_tbl[id]), v & 7); |
|---|
| 901 | 971 | } else { |
|---|
| 902 | 972 | v >>= 3; |
|---|
| 903 | | - intval = hw_to_current(pcc_tbl, ARRAY_SIZE(pcc_tbl), v & 7); |
|---|
| 973 | + intval = hw_to_current(pcc_tbl[id], |
|---|
| 974 | + ARRAY_SIZE(pcc_tbl[id]), v & 7); |
|---|
| 904 | 975 | } |
|---|
| 905 | 976 | |
|---|
| 906 | 977 | return intval; |
|---|
| .. | .. |
|---|
| 931 | 1002 | return intval; |
|---|
| 932 | 1003 | } |
|---|
| 933 | 1004 | |
|---|
| 934 | | -static int smb347_mains_get_property(struct power_supply *psy, |
|---|
| 935 | | - enum power_supply_property prop, |
|---|
| 936 | | - union power_supply_propval *val) |
|---|
| 937 | | -{ |
|---|
| 938 | | - struct smb347_charger *smb = power_supply_get_drvdata(psy); |
|---|
| 939 | | - int ret; |
|---|
| 940 | | - |
|---|
| 941 | | - switch (prop) { |
|---|
| 942 | | - case POWER_SUPPLY_PROP_ONLINE: |
|---|
| 943 | | - val->intval = smb->mains_online; |
|---|
| 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; |
|---|
| 964 | | - } |
|---|
| 965 | | - |
|---|
| 966 | | - return 0; |
|---|
| 967 | | -} |
|---|
| 968 | | - |
|---|
| 969 | | -static enum power_supply_property smb347_mains_properties[] = { |
|---|
| 970 | | - POWER_SUPPLY_PROP_ONLINE, |
|---|
| 971 | | - POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, |
|---|
| 972 | | - POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, |
|---|
| 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 | | -{ |
|---|
| 979 | | - struct smb347_charger *smb = power_supply_get_drvdata(psy); |
|---|
| 980 | | - int ret; |
|---|
| 981 | | - |
|---|
| 982 | | - switch (prop) { |
|---|
| 983 | | - case POWER_SUPPLY_PROP_ONLINE: |
|---|
| 984 | | - val->intval = smb->usb_online; |
|---|
| 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; |
|---|
| 1005 | | - } |
|---|
| 1006 | | - |
|---|
| 1007 | | - return 0; |
|---|
| 1008 | | -} |
|---|
| 1009 | | - |
|---|
| 1010 | | -static enum power_supply_property smb347_usb_properties[] = { |
|---|
| 1011 | | - POWER_SUPPLY_PROP_ONLINE, |
|---|
| 1012 | | - POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, |
|---|
| 1013 | | - POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, |
|---|
| 1014 | | -}; |
|---|
| 1015 | | - |
|---|
| 1016 | | -static int smb347_get_charging_status(struct smb347_charger *smb) |
|---|
| 1005 | +static int smb347_get_charging_status(struct smb347_charger *smb, |
|---|
| 1006 | + struct power_supply *psy) |
|---|
| 1017 | 1007 | { |
|---|
| 1018 | 1008 | int ret, status; |
|---|
| 1019 | 1009 | unsigned int val; |
|---|
| 1020 | 1010 | |
|---|
| 1021 | | - if (!smb347_is_ps_online(smb)) |
|---|
| 1022 | | - return POWER_SUPPLY_STATUS_DISCHARGING; |
|---|
| 1011 | + if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { |
|---|
| 1012 | + if (!smb->usb_online) |
|---|
| 1013 | + return POWER_SUPPLY_STATUS_DISCHARGING; |
|---|
| 1014 | + } else { |
|---|
| 1015 | + if (!smb->mains_online) |
|---|
| 1016 | + return POWER_SUPPLY_STATUS_DISCHARGING; |
|---|
| 1017 | + } |
|---|
| 1023 | 1018 | |
|---|
| 1024 | 1019 | ret = regmap_read(smb->regmap, STAT_C, &val); |
|---|
| 1025 | 1020 | if (ret < 0) |
|---|
| .. | .. |
|---|
| 1058 | 1053 | return status; |
|---|
| 1059 | 1054 | } |
|---|
| 1060 | 1055 | |
|---|
| 1061 | | -static int smb347_battery_get_property(struct power_supply *psy, |
|---|
| 1062 | | - enum power_supply_property prop, |
|---|
| 1063 | | - union power_supply_propval *val) |
|---|
| 1056 | +static int smb347_get_property_locked(struct power_supply *psy, |
|---|
| 1057 | + enum power_supply_property prop, |
|---|
| 1058 | + union power_supply_propval *val) |
|---|
| 1064 | 1059 | { |
|---|
| 1065 | 1060 | struct smb347_charger *smb = power_supply_get_drvdata(psy); |
|---|
| 1066 | | - const struct smb347_charger_platform_data *pdata = smb->pdata; |
|---|
| 1067 | 1061 | int ret; |
|---|
| 1068 | | - |
|---|
| 1069 | | - ret = smb347_update_ps_status(smb); |
|---|
| 1070 | | - if (ret < 0) |
|---|
| 1071 | | - return ret; |
|---|
| 1072 | 1062 | |
|---|
| 1073 | 1063 | switch (prop) { |
|---|
| 1074 | 1064 | case POWER_SUPPLY_PROP_STATUS: |
|---|
| 1075 | | - ret = smb347_get_charging_status(smb); |
|---|
| 1065 | + ret = smb347_get_charging_status(smb, psy); |
|---|
| 1076 | 1066 | if (ret < 0) |
|---|
| 1077 | 1067 | return ret; |
|---|
| 1078 | 1068 | val->intval = ret; |
|---|
| 1079 | 1069 | break; |
|---|
| 1080 | 1070 | |
|---|
| 1081 | 1071 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
|---|
| 1082 | | - if (!smb347_is_ps_online(smb)) |
|---|
| 1083 | | - return -ENODATA; |
|---|
| 1072 | + if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { |
|---|
| 1073 | + if (!smb->usb_online) |
|---|
| 1074 | + return -ENODATA; |
|---|
| 1075 | + } else { |
|---|
| 1076 | + if (!smb->mains_online) |
|---|
| 1077 | + return -ENODATA; |
|---|
| 1078 | + } |
|---|
| 1084 | 1079 | |
|---|
| 1085 | 1080 | /* |
|---|
| 1086 | 1081 | * We handle trickle and pre-charging the same, and taper |
|---|
| .. | .. |
|---|
| 1099 | 1094 | } |
|---|
| 1100 | 1095 | break; |
|---|
| 1101 | 1096 | |
|---|
| 1102 | | - case POWER_SUPPLY_PROP_TECHNOLOGY: |
|---|
| 1103 | | - val->intval = pdata->battery_info.technology; |
|---|
| 1097 | + case POWER_SUPPLY_PROP_ONLINE: |
|---|
| 1098 | + if (psy->desc->type == POWER_SUPPLY_TYPE_USB) |
|---|
| 1099 | + val->intval = smb->usb_online; |
|---|
| 1100 | + else |
|---|
| 1101 | + val->intval = smb->mains_online; |
|---|
| 1104 | 1102 | break; |
|---|
| 1105 | 1103 | |
|---|
| 1106 | | - case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
|---|
| 1107 | | - val->intval = pdata->battery_info.voltage_min_design; |
|---|
| 1104 | + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: |
|---|
| 1105 | + ret = get_const_charge_voltage(smb); |
|---|
| 1106 | + if (ret < 0) |
|---|
| 1107 | + return ret; |
|---|
| 1108 | + val->intval = ret; |
|---|
| 1108 | 1109 | break; |
|---|
| 1109 | 1110 | |
|---|
| 1110 | | - case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
|---|
| 1111 | | - val->intval = pdata->battery_info.voltage_max_design; |
|---|
| 1112 | | - break; |
|---|
| 1113 | | - |
|---|
| 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; |
|---|
| 1111 | + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: |
|---|
| 1112 | + ret = get_const_charge_current(smb); |
|---|
| 1113 | + if (ret < 0) |
|---|
| 1114 | + return ret; |
|---|
| 1115 | + val->intval = ret; |
|---|
| 1120 | 1116 | break; |
|---|
| 1121 | 1117 | |
|---|
| 1122 | 1118 | default: |
|---|
| .. | .. |
|---|
| 1126 | 1122 | return 0; |
|---|
| 1127 | 1123 | } |
|---|
| 1128 | 1124 | |
|---|
| 1129 | | -static enum power_supply_property smb347_battery_properties[] = { |
|---|
| 1125 | +static int smb347_get_property(struct power_supply *psy, |
|---|
| 1126 | + enum power_supply_property prop, |
|---|
| 1127 | + union power_supply_propval *val) |
|---|
| 1128 | +{ |
|---|
| 1129 | + struct smb347_charger *smb = power_supply_get_drvdata(psy); |
|---|
| 1130 | + struct i2c_client *client = to_i2c_client(smb->dev); |
|---|
| 1131 | + int ret; |
|---|
| 1132 | + |
|---|
| 1133 | + disable_irq(client->irq); |
|---|
| 1134 | + ret = smb347_get_property_locked(psy, prop, val); |
|---|
| 1135 | + enable_irq(client->irq); |
|---|
| 1136 | + |
|---|
| 1137 | + return ret; |
|---|
| 1138 | +} |
|---|
| 1139 | + |
|---|
| 1140 | +static enum power_supply_property smb347_properties[] = { |
|---|
| 1130 | 1141 | POWER_SUPPLY_PROP_STATUS, |
|---|
| 1131 | 1142 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
|---|
| 1132 | | - POWER_SUPPLY_PROP_TECHNOLOGY, |
|---|
| 1133 | | - POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
|---|
| 1134 | | - POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
|---|
| 1135 | | - POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
|---|
| 1136 | | - POWER_SUPPLY_PROP_MODEL_NAME, |
|---|
| 1143 | + POWER_SUPPLY_PROP_ONLINE, |
|---|
| 1144 | + POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, |
|---|
| 1145 | + POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, |
|---|
| 1137 | 1146 | }; |
|---|
| 1138 | 1147 | |
|---|
| 1139 | 1148 | static bool smb347_volatile_reg(struct device *dev, unsigned int reg) |
|---|
| .. | .. |
|---|
| 1179 | 1188 | return smb347_volatile_reg(dev, reg); |
|---|
| 1180 | 1189 | } |
|---|
| 1181 | 1190 | |
|---|
| 1191 | +static void smb347_dt_parse_dev_info(struct smb347_charger *smb) |
|---|
| 1192 | +{ |
|---|
| 1193 | + struct device *dev = smb->dev; |
|---|
| 1194 | + |
|---|
| 1195 | + smb->soft_temp_limit_compensation = |
|---|
| 1196 | + SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT; |
|---|
| 1197 | + /* |
|---|
| 1198 | + * These properties come from the battery info, still we need to |
|---|
| 1199 | + * pre-initialize the values. See smb347_get_battery_info() below. |
|---|
| 1200 | + */ |
|---|
| 1201 | + smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT; |
|---|
| 1202 | + smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT; |
|---|
| 1203 | + smb->soft_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT; |
|---|
| 1204 | + smb->hard_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT; |
|---|
| 1205 | + |
|---|
| 1206 | + /* Charging constraints */ |
|---|
| 1207 | + device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt", |
|---|
| 1208 | + &smb->pre_to_fast_voltage); |
|---|
| 1209 | + device_property_read_u32(dev, "summit,mains-current-limit-microamp", |
|---|
| 1210 | + &smb->mains_current_limit); |
|---|
| 1211 | + device_property_read_u32(dev, "summit,usb-current-limit-microamp", |
|---|
| 1212 | + &smb->usb_hc_current_limit); |
|---|
| 1213 | + |
|---|
| 1214 | + /* For thermometer monitoring */ |
|---|
| 1215 | + device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius", |
|---|
| 1216 | + &smb->chip_temp_threshold); |
|---|
| 1217 | + device_property_read_u32(dev, "summit,soft-compensation-method", |
|---|
| 1218 | + &smb->soft_temp_limit_compensation); |
|---|
| 1219 | + device_property_read_u32(dev, "summit,charge-current-compensation-microamp", |
|---|
| 1220 | + &smb->charge_current_compensation); |
|---|
| 1221 | + |
|---|
| 1222 | + /* Supported charging mode */ |
|---|
| 1223 | + smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging"); |
|---|
| 1224 | + smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging"); |
|---|
| 1225 | + smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging"); |
|---|
| 1226 | + |
|---|
| 1227 | + /* Select charging control */ |
|---|
| 1228 | + device_property_read_u32(dev, "summit,enable-charge-control", |
|---|
| 1229 | + &smb->enable_control); |
|---|
| 1230 | +} |
|---|
| 1231 | + |
|---|
| 1232 | +static int smb347_get_battery_info(struct smb347_charger *smb) |
|---|
| 1233 | +{ |
|---|
| 1234 | + struct power_supply_battery_info info = {}; |
|---|
| 1235 | + struct power_supply *supply; |
|---|
| 1236 | + int err; |
|---|
| 1237 | + |
|---|
| 1238 | + if (smb->mains) |
|---|
| 1239 | + supply = smb->mains; |
|---|
| 1240 | + else |
|---|
| 1241 | + supply = smb->usb; |
|---|
| 1242 | + |
|---|
| 1243 | + err = power_supply_get_battery_info(supply, &info); |
|---|
| 1244 | + if (err == -ENXIO || err == -ENODEV) |
|---|
| 1245 | + return 0; |
|---|
| 1246 | + if (err) |
|---|
| 1247 | + return err; |
|---|
| 1248 | + |
|---|
| 1249 | + if (info.constant_charge_current_max_ua != -EINVAL) |
|---|
| 1250 | + smb->max_charge_current = info.constant_charge_current_max_ua; |
|---|
| 1251 | + |
|---|
| 1252 | + if (info.constant_charge_voltage_max_uv != -EINVAL) |
|---|
| 1253 | + smb->max_charge_voltage = info.constant_charge_voltage_max_uv; |
|---|
| 1254 | + |
|---|
| 1255 | + if (info.precharge_current_ua != -EINVAL) |
|---|
| 1256 | + smb->pre_charge_current = info.precharge_current_ua; |
|---|
| 1257 | + |
|---|
| 1258 | + if (info.charge_term_current_ua != -EINVAL) |
|---|
| 1259 | + smb->termination_current = info.charge_term_current_ua; |
|---|
| 1260 | + |
|---|
| 1261 | + if (info.temp_alert_min != INT_MIN) |
|---|
| 1262 | + smb->soft_cold_temp_limit = info.temp_alert_min; |
|---|
| 1263 | + |
|---|
| 1264 | + if (info.temp_alert_max != INT_MAX) |
|---|
| 1265 | + smb->soft_hot_temp_limit = info.temp_alert_max; |
|---|
| 1266 | + |
|---|
| 1267 | + if (info.temp_min != INT_MIN) |
|---|
| 1268 | + smb->hard_cold_temp_limit = info.temp_min; |
|---|
| 1269 | + |
|---|
| 1270 | + if (info.temp_max != INT_MAX) |
|---|
| 1271 | + smb->hard_hot_temp_limit = info.temp_max; |
|---|
| 1272 | + |
|---|
| 1273 | + /* Suspend when battery temperature is outside hard limits */ |
|---|
| 1274 | + if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT || |
|---|
| 1275 | + smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) |
|---|
| 1276 | + smb->suspend_on_hard_temp_limit = true; |
|---|
| 1277 | + |
|---|
| 1278 | + return 0; |
|---|
| 1279 | +} |
|---|
| 1280 | + |
|---|
| 1182 | 1281 | static const struct regmap_config smb347_regmap = { |
|---|
| 1183 | 1282 | .reg_bits = 8, |
|---|
| 1184 | 1283 | .val_bits = 8, |
|---|
| .. | .. |
|---|
| 1190 | 1289 | static const struct power_supply_desc smb347_mains_desc = { |
|---|
| 1191 | 1290 | .name = "smb347-mains", |
|---|
| 1192 | 1291 | .type = POWER_SUPPLY_TYPE_MAINS, |
|---|
| 1193 | | - .get_property = smb347_mains_get_property, |
|---|
| 1194 | | - .properties = smb347_mains_properties, |
|---|
| 1195 | | - .num_properties = ARRAY_SIZE(smb347_mains_properties), |
|---|
| 1292 | + .get_property = smb347_get_property, |
|---|
| 1293 | + .properties = smb347_properties, |
|---|
| 1294 | + .num_properties = ARRAY_SIZE(smb347_properties), |
|---|
| 1196 | 1295 | }; |
|---|
| 1197 | 1296 | |
|---|
| 1198 | 1297 | static const struct power_supply_desc smb347_usb_desc = { |
|---|
| 1199 | 1298 | .name = "smb347-usb", |
|---|
| 1200 | 1299 | .type = POWER_SUPPLY_TYPE_USB, |
|---|
| 1201 | | - .get_property = smb347_usb_get_property, |
|---|
| 1202 | | - .properties = smb347_usb_properties, |
|---|
| 1203 | | - .num_properties = ARRAY_SIZE(smb347_usb_properties), |
|---|
| 1204 | | -}; |
|---|
| 1205 | | - |
|---|
| 1206 | | -static const struct power_supply_desc smb347_battery_desc = { |
|---|
| 1207 | | - .name = "smb347-battery", |
|---|
| 1208 | | - .type = POWER_SUPPLY_TYPE_BATTERY, |
|---|
| 1209 | | - .get_property = smb347_battery_get_property, |
|---|
| 1210 | | - .properties = smb347_battery_properties, |
|---|
| 1211 | | - .num_properties = ARRAY_SIZE(smb347_battery_properties), |
|---|
| 1300 | + .get_property = smb347_get_property, |
|---|
| 1301 | + .properties = smb347_properties, |
|---|
| 1302 | + .num_properties = ARRAY_SIZE(smb347_properties), |
|---|
| 1212 | 1303 | }; |
|---|
| 1213 | 1304 | |
|---|
| 1214 | 1305 | static int smb347_probe(struct i2c_client *client, |
|---|
| 1215 | 1306 | const struct i2c_device_id *id) |
|---|
| 1216 | 1307 | { |
|---|
| 1217 | | - static char *battery[] = { "smb347-battery" }; |
|---|
| 1218 | | - const struct smb347_charger_platform_data *pdata; |
|---|
| 1219 | | - struct power_supply_config mains_usb_cfg = {}, battery_cfg = {}; |
|---|
| 1308 | + struct power_supply_config mains_usb_cfg = {}; |
|---|
| 1220 | 1309 | struct device *dev = &client->dev; |
|---|
| 1221 | 1310 | struct smb347_charger *smb; |
|---|
| 1222 | 1311 | int ret; |
|---|
| 1223 | 1312 | |
|---|
| 1224 | | - pdata = dev->platform_data; |
|---|
| 1225 | | - if (!pdata) |
|---|
| 1226 | | - return -EINVAL; |
|---|
| 1227 | | - |
|---|
| 1228 | | - if (!pdata->use_mains && !pdata->use_usb) |
|---|
| 1229 | | - return -EINVAL; |
|---|
| 1230 | | - |
|---|
| 1231 | 1313 | smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL); |
|---|
| 1232 | 1314 | if (!smb) |
|---|
| 1233 | 1315 | return -ENOMEM; |
|---|
| 1234 | | - |
|---|
| 1316 | + smb->dev = &client->dev; |
|---|
| 1317 | + smb->id = id->driver_data; |
|---|
| 1235 | 1318 | i2c_set_clientdata(client, smb); |
|---|
| 1236 | 1319 | |
|---|
| 1237 | | - mutex_init(&smb->lock); |
|---|
| 1238 | | - smb->dev = &client->dev; |
|---|
| 1239 | | - smb->pdata = pdata; |
|---|
| 1320 | + smb347_dt_parse_dev_info(smb); |
|---|
| 1321 | + if (!smb->use_mains && !smb->use_usb) |
|---|
| 1322 | + return -EINVAL; |
|---|
| 1240 | 1323 | |
|---|
| 1241 | 1324 | smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap); |
|---|
| 1242 | 1325 | if (IS_ERR(smb->regmap)) |
|---|
| 1243 | 1326 | return PTR_ERR(smb->regmap); |
|---|
| 1244 | 1327 | |
|---|
| 1245 | | - ret = smb347_hw_init(smb); |
|---|
| 1246 | | - if (ret < 0) |
|---|
| 1247 | | - return ret; |
|---|
| 1248 | | - |
|---|
| 1249 | | - mains_usb_cfg.supplied_to = battery; |
|---|
| 1250 | | - mains_usb_cfg.num_supplicants = ARRAY_SIZE(battery); |
|---|
| 1251 | 1328 | mains_usb_cfg.drv_data = smb; |
|---|
| 1252 | | - if (smb->pdata->use_mains) { |
|---|
| 1253 | | - smb->mains = power_supply_register(dev, &smb347_mains_desc, |
|---|
| 1254 | | - &mains_usb_cfg); |
|---|
| 1329 | + mains_usb_cfg.of_node = dev->of_node; |
|---|
| 1330 | + if (smb->use_mains) { |
|---|
| 1331 | + smb->mains = devm_power_supply_register(dev, &smb347_mains_desc, |
|---|
| 1332 | + &mains_usb_cfg); |
|---|
| 1255 | 1333 | if (IS_ERR(smb->mains)) |
|---|
| 1256 | 1334 | return PTR_ERR(smb->mains); |
|---|
| 1257 | 1335 | } |
|---|
| 1258 | 1336 | |
|---|
| 1259 | | - if (smb->pdata->use_usb) { |
|---|
| 1260 | | - smb->usb = power_supply_register(dev, &smb347_usb_desc, |
|---|
| 1261 | | - &mains_usb_cfg); |
|---|
| 1262 | | - if (IS_ERR(smb->usb)) { |
|---|
| 1263 | | - if (smb->pdata->use_mains) |
|---|
| 1264 | | - power_supply_unregister(smb->mains); |
|---|
| 1337 | + if (smb->use_usb) { |
|---|
| 1338 | + smb->usb = devm_power_supply_register(dev, &smb347_usb_desc, |
|---|
| 1339 | + &mains_usb_cfg); |
|---|
| 1340 | + if (IS_ERR(smb->usb)) |
|---|
| 1265 | 1341 | return PTR_ERR(smb->usb); |
|---|
| 1266 | | - } |
|---|
| 1267 | 1342 | } |
|---|
| 1268 | 1343 | |
|---|
| 1269 | | - battery_cfg.drv_data = smb; |
|---|
| 1270 | | - smb->battery = power_supply_register(dev, &smb347_battery_desc, |
|---|
| 1271 | | - &battery_cfg); |
|---|
| 1272 | | - if (IS_ERR(smb->battery)) { |
|---|
| 1273 | | - if (smb->pdata->use_usb) |
|---|
| 1274 | | - power_supply_unregister(smb->usb); |
|---|
| 1275 | | - if (smb->pdata->use_mains) |
|---|
| 1276 | | - power_supply_unregister(smb->mains); |
|---|
| 1277 | | - return PTR_ERR(smb->battery); |
|---|
| 1278 | | - } |
|---|
| 1344 | + ret = smb347_get_battery_info(smb); |
|---|
| 1345 | + if (ret) |
|---|
| 1346 | + return ret; |
|---|
| 1347 | + |
|---|
| 1348 | + ret = smb347_hw_init(smb); |
|---|
| 1349 | + if (ret < 0) |
|---|
| 1350 | + return ret; |
|---|
| 1279 | 1351 | |
|---|
| 1280 | 1352 | /* |
|---|
| 1281 | 1353 | * Interrupt pin is optional. If it is connected, we setup the |
|---|
| 1282 | 1354 | * interrupt support here. |
|---|
| 1283 | 1355 | */ |
|---|
| 1284 | | - if (pdata->irq_gpio >= 0) { |
|---|
| 1356 | + if (client->irq) { |
|---|
| 1285 | 1357 | ret = smb347_irq_init(smb, client); |
|---|
| 1286 | 1358 | if (ret < 0) { |
|---|
| 1287 | 1359 | dev_warn(dev, "failed to initialize IRQ: %d\n", ret); |
|---|
| 1288 | 1360 | dev_warn(dev, "disabling IRQ support\n"); |
|---|
| 1361 | + smb->irq_unsupported = true; |
|---|
| 1289 | 1362 | } else { |
|---|
| 1290 | 1363 | smb347_irq_enable(smb); |
|---|
| 1291 | 1364 | } |
|---|
| .. | .. |
|---|
| 1298 | 1371 | { |
|---|
| 1299 | 1372 | struct smb347_charger *smb = i2c_get_clientdata(client); |
|---|
| 1300 | 1373 | |
|---|
| 1301 | | - if (client->irq) { |
|---|
| 1302 | | - smb347_irq_disable(smb); |
|---|
| 1303 | | - free_irq(client->irq, smb); |
|---|
| 1304 | | - gpio_free(smb->pdata->irq_gpio); |
|---|
| 1305 | | - } |
|---|
| 1374 | + smb347_irq_disable(smb); |
|---|
| 1306 | 1375 | |
|---|
| 1307 | | - power_supply_unregister(smb->battery); |
|---|
| 1308 | | - if (smb->pdata->use_usb) |
|---|
| 1309 | | - power_supply_unregister(smb->usb); |
|---|
| 1310 | | - if (smb->pdata->use_mains) |
|---|
| 1311 | | - power_supply_unregister(smb->mains); |
|---|
| 1312 | 1376 | return 0; |
|---|
| 1313 | 1377 | } |
|---|
| 1314 | 1378 | |
|---|
| 1315 | 1379 | static const struct i2c_device_id smb347_id[] = { |
|---|
| 1316 | | - { "smb347", 0 }, |
|---|
| 1317 | | - { } |
|---|
| 1380 | + { "smb345", SMB345 }, |
|---|
| 1381 | + { "smb347", SMB347 }, |
|---|
| 1382 | + { "smb358", SMB358 }, |
|---|
| 1383 | + { }, |
|---|
| 1318 | 1384 | }; |
|---|
| 1319 | 1385 | MODULE_DEVICE_TABLE(i2c, smb347_id); |
|---|
| 1386 | + |
|---|
| 1387 | +static const struct of_device_id smb3xx_of_match[] = { |
|---|
| 1388 | + { .compatible = "summit,smb345" }, |
|---|
| 1389 | + { .compatible = "summit,smb347" }, |
|---|
| 1390 | + { .compatible = "summit,smb358" }, |
|---|
| 1391 | + { }, |
|---|
| 1392 | +}; |
|---|
| 1393 | +MODULE_DEVICE_TABLE(of, smb3xx_of_match); |
|---|
| 1320 | 1394 | |
|---|
| 1321 | 1395 | static struct i2c_driver smb347_driver = { |
|---|
| 1322 | 1396 | .driver = { |
|---|
| 1323 | 1397 | .name = "smb347", |
|---|
| 1398 | + .of_match_table = smb3xx_of_match, |
|---|
| 1324 | 1399 | }, |
|---|
| 1325 | 1400 | .probe = smb347_probe, |
|---|
| 1326 | 1401 | .remove = smb347_remove, |
|---|