| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved. |
| 4 | * Copyright (c) 2022, Linaro Ltd |
| 5 | * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 6 | */ |
| 7 | #include <linux/auxiliary_bus.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/mutex.h> |
| 10 | #include <linux/nvmem-consumer.h> |
| 11 | #include <linux/of_device.h> |
| 12 | #include <linux/power_supply.h> |
| 13 | #include <linux/property.h> |
| 14 | #include <linux/soc/qcom/pdr.h> |
| 15 | #include <linux/soc/qcom/pmic_glink.h> |
| 16 | #include <linux/math.h> |
| 17 | #include <linux/units.h> |
| 18 | |
| 19 | #define BATTMGR_CHEMISTRY_LEN 4 |
| 20 | #define BATTMGR_STRING_LEN 128 |
| 21 | |
| 22 | enum qcom_battmgr_variant { |
| 23 | QCOM_BATTMGR_SC8280XP, |
| 24 | QCOM_BATTMGR_SM8350, |
| 25 | QCOM_BATTMGR_SM8550, |
| 26 | QCOM_BATTMGR_X1E80100, |
| 27 | }; |
| 28 | |
| 29 | #define BATTMGR_BAT_STATUS 0x1 |
| 30 | |
| 31 | #define BATTMGR_REQUEST_NOTIFICATION 0x4 |
| 32 | |
| 33 | #define BATTMGR_NOTIFICATION 0x7 |
| 34 | #define NOTIF_BAT_PROPERTY 0x30 |
| 35 | #define NOTIF_USB_PROPERTY 0x32 |
| 36 | #define NOTIF_WLS_PROPERTY 0x34 |
| 37 | #define NOTIF_BAT_STATUS 0x80 |
| 38 | #define NOTIF_BAT_INFO 0x81 |
| 39 | #define NOTIF_BAT_CHARGING_STATE 0x83 |
| 40 | |
| 41 | #define BATTMGR_BAT_INFO 0x9 |
| 42 | |
| 43 | #define BATTMGR_BAT_DISCHARGE_TIME 0xc |
| 44 | |
| 45 | #define BATTMGR_BAT_CHARGE_TIME 0xd |
| 46 | |
| 47 | #define BATTMGR_BAT_PROPERTY_GET 0x30 |
| 48 | #define BATTMGR_BAT_PROPERTY_SET 0x31 |
| 49 | #define BATT_STATUS 0 |
| 50 | #define BATT_HEALTH 1 |
| 51 | #define BATT_PRESENT 2 |
| 52 | #define BATT_CHG_TYPE 3 |
| 53 | #define BATT_CAPACITY 4 |
| 54 | #define BATT_SOH 5 |
| 55 | #define BATT_VOLT_OCV 6 |
| 56 | #define BATT_VOLT_NOW 7 |
| 57 | #define BATT_VOLT_MAX 8 |
| 58 | #define BATT_CURR_NOW 9 |
| 59 | #define BATT_CHG_CTRL_LIM 10 |
| 60 | #define BATT_CHG_CTRL_LIM_MAX 11 |
| 61 | #define BATT_TEMP 12 |
| 62 | #define BATT_TECHNOLOGY 13 |
| 63 | #define BATT_CHG_COUNTER 14 |
| 64 | #define BATT_CYCLE_COUNT 15 |
| 65 | #define BATT_CHG_FULL_DESIGN 16 |
| 66 | #define BATT_CHG_FULL 17 |
| 67 | #define BATT_MODEL_NAME 18 |
| 68 | #define BATT_TTF_AVG 19 |
| 69 | #define BATT_TTE_AVG 20 |
| 70 | #define BATT_RESISTANCE 21 |
| 71 | #define BATT_POWER_NOW 22 |
| 72 | #define BATT_POWER_AVG 23 |
| 73 | #define BATT_CHG_CTRL_EN 24 |
| 74 | #define BATT_CHG_CTRL_START_THR 25 |
| 75 | #define BATT_CHG_CTRL_END_THR 26 |
| 76 | |
| 77 | #define BATTMGR_USB_PROPERTY_GET 0x32 |
| 78 | #define BATTMGR_USB_PROPERTY_SET 0x33 |
| 79 | #define USB_ONLINE 0 |
| 80 | #define USB_VOLT_NOW 1 |
| 81 | #define USB_VOLT_MAX 2 |
| 82 | #define USB_CURR_NOW 3 |
| 83 | #define USB_CURR_MAX 4 |
| 84 | #define USB_INPUT_CURR_LIMIT 5 |
| 85 | #define USB_TYPE 6 |
| 86 | #define USB_ADAP_TYPE 7 |
| 87 | #define USB_MOISTURE_DET_EN 8 |
| 88 | #define USB_MOISTURE_DET_STS 9 |
| 89 | |
| 90 | #define BATTMGR_WLS_PROPERTY_GET 0x34 |
| 91 | #define BATTMGR_WLS_PROPERTY_SET 0x35 |
| 92 | #define WLS_ONLINE 0 |
| 93 | #define WLS_VOLT_NOW 1 |
| 94 | #define WLS_VOLT_MAX 2 |
| 95 | #define WLS_CURR_NOW 3 |
| 96 | #define WLS_CURR_MAX 4 |
| 97 | #define WLS_TYPE 5 |
| 98 | #define WLS_BOOST_EN 6 |
| 99 | |
| 100 | #define BATTMGR_CHG_CTRL_LIMIT_EN 0x48 |
| 101 | #define CHARGE_CTRL_START_THR_MIN 50 |
| 102 | #define CHARGE_CTRL_START_THR_MAX 95 |
| 103 | #define CHARGE_CTRL_END_THR_MIN 55 |
| 104 | #define CHARGE_CTRL_END_THR_MAX 100 |
| 105 | #define CHARGE_CTRL_DELTA_SOC 5 |
| 106 | |
| 107 | struct qcom_battmgr_enable_request { |
| 108 | struct pmic_glink_hdr hdr; |
| 109 | __le32 battery_id; |
| 110 | __le32 power_state; |
| 111 | __le32 low_capacity; |
| 112 | __le32 high_capacity; |
| 113 | }; |
| 114 | |
| 115 | struct qcom_battmgr_property_request { |
| 116 | struct pmic_glink_hdr hdr; |
| 117 | __le32 battery; |
| 118 | __le32 property; |
| 119 | __le32 value; |
| 120 | }; |
| 121 | |
| 122 | struct qcom_battmgr_update_request { |
| 123 | struct pmic_glink_hdr hdr; |
| 124 | __le32 battery_id; |
| 125 | }; |
| 126 | |
| 127 | struct qcom_battmgr_charge_time_request { |
| 128 | struct pmic_glink_hdr hdr; |
| 129 | __le32 battery_id; |
| 130 | __le32 percent; |
| 131 | __le32 reserved; |
| 132 | }; |
| 133 | |
| 134 | struct qcom_battmgr_discharge_time_request { |
| 135 | struct pmic_glink_hdr hdr; |
| 136 | __le32 battery_id; |
| 137 | __le32 rate; /* 0 for current rate */ |
| 138 | __le32 reserved; |
| 139 | }; |
| 140 | |
| 141 | struct qcom_battmgr_charge_ctrl_request { |
| 142 | struct pmic_glink_hdr hdr; |
| 143 | __le32 enable; |
| 144 | __le32 target_soc; |
| 145 | __le32 delta_soc; |
| 146 | }; |
| 147 | |
| 148 | struct qcom_battmgr_message { |
| 149 | struct pmic_glink_hdr hdr; |
| 150 | union { |
| 151 | struct { |
| 152 | __le32 property; |
| 153 | __le32 value; |
| 154 | __le32 result; |
| 155 | } intval; |
| 156 | struct { |
| 157 | __le32 property; |
| 158 | char model[BATTMGR_STRING_LEN]; |
| 159 | } strval; |
| 160 | struct { |
| 161 | /* |
| 162 | * 0: mWh |
| 163 | * 1: mAh |
| 164 | */ |
| 165 | __le32 power_unit; |
| 166 | __le32 design_capacity; |
| 167 | __le32 last_full_capacity; |
| 168 | /* |
| 169 | * 0 nonrechargable |
| 170 | * 1 rechargable |
| 171 | */ |
| 172 | __le32 battery_tech; |
| 173 | __le32 design_voltage; /* mV */ |
| 174 | __le32 capacity_low; |
| 175 | __le32 capacity_warning; |
| 176 | __le32 cycle_count; |
| 177 | /* thousandth of percent */ |
| 178 | __le32 accuracy; |
| 179 | __le32 max_sample_time_ms; |
| 180 | __le32 min_sample_time_ms; |
| 181 | __le32 max_average_interval_ms; |
| 182 | __le32 min_average_interval_ms; |
| 183 | /* granularity between low and warning */ |
| 184 | __le32 capacity_granularity1; |
| 185 | /* granularity between warning and full */ |
| 186 | __le32 capacity_granularity2; |
| 187 | /* |
| 188 | * 0: no |
| 189 | * 1: cold |
| 190 | * 2: hot |
| 191 | */ |
| 192 | __le32 swappable; |
| 193 | __le32 capabilities; |
| 194 | char model_number[BATTMGR_STRING_LEN]; |
| 195 | char serial_number[BATTMGR_STRING_LEN]; |
| 196 | char battery_type[BATTMGR_STRING_LEN]; |
| 197 | char oem_info[BATTMGR_STRING_LEN]; |
| 198 | char battery_chemistry[BATTMGR_CHEMISTRY_LEN]; |
| 199 | char uid[BATTMGR_STRING_LEN]; |
| 200 | __le32 critical_bias; |
| 201 | u8 day; |
| 202 | u8 month; |
| 203 | __le16 year; |
| 204 | __le32 battery_id; |
| 205 | } info; |
| 206 | struct { |
| 207 | /* |
| 208 | * BIT(0) discharging |
| 209 | * BIT(1) charging |
| 210 | * BIT(2) critical low |
| 211 | */ |
| 212 | __le32 battery_state; |
| 213 | /* mWh or mAh, based on info->power_unit */ |
| 214 | __le32 capacity; |
| 215 | __le32 rate; |
| 216 | /* mv */ |
| 217 | __le32 battery_voltage; |
| 218 | /* |
| 219 | * BIT(0) power online |
| 220 | * BIT(1) discharging |
| 221 | * BIT(2) charging |
| 222 | * BIT(3) battery critical |
| 223 | */ |
| 224 | __le32 power_state; |
| 225 | /* |
| 226 | * 1: AC |
| 227 | * 2: USB |
| 228 | * 3: Wireless |
| 229 | */ |
| 230 | __le32 charging_source; |
| 231 | __le32 temperature; |
| 232 | } status; |
| 233 | __le32 time; |
| 234 | __le32 notification; |
| 235 | }; |
| 236 | }; |
| 237 | |
| 238 | #define BATTMGR_CHARGING_SOURCE_AC 1 |
| 239 | #define BATTMGR_CHARGING_SOURCE_USB 2 |
| 240 | #define BATTMGR_CHARGING_SOURCE_WIRELESS 3 |
| 241 | |
| 242 | enum qcom_battmgr_unit { |
| 243 | QCOM_BATTMGR_UNIT_mWh = 0, |
| 244 | QCOM_BATTMGR_UNIT_mAh = 1 |
| 245 | }; |
| 246 | |
| 247 | struct qcom_battmgr_info { |
| 248 | bool valid; |
| 249 | |
| 250 | bool present; |
| 251 | unsigned int charge_type; |
| 252 | unsigned int design_capacity; |
| 253 | unsigned int last_full_capacity; |
| 254 | unsigned int voltage_max_design; |
| 255 | unsigned int voltage_max; |
| 256 | unsigned int capacity_low; |
| 257 | unsigned int capacity_warning; |
| 258 | unsigned int cycle_count; |
| 259 | unsigned int charge_count; |
| 260 | unsigned int charge_ctrl_start; |
| 261 | unsigned int charge_ctrl_end; |
| 262 | char model_number[BATTMGR_STRING_LEN]; |
| 263 | char serial_number[BATTMGR_STRING_LEN]; |
| 264 | char oem_info[BATTMGR_STRING_LEN]; |
| 265 | unsigned char technology; |
| 266 | unsigned char day; |
| 267 | unsigned char month; |
| 268 | unsigned short year; |
| 269 | }; |
| 270 | |
| 271 | struct qcom_battmgr_status { |
| 272 | unsigned int status; |
| 273 | unsigned int health; |
| 274 | unsigned int capacity; |
| 275 | unsigned int percent; |
| 276 | int current_now; |
| 277 | int power_now; |
| 278 | unsigned int voltage_now; |
| 279 | unsigned int voltage_ocv; |
| 280 | unsigned int temperature; |
| 281 | unsigned int resistance; |
| 282 | unsigned int soh_percent; |
| 283 | |
| 284 | unsigned int discharge_time; |
| 285 | unsigned int charge_time; |
| 286 | }; |
| 287 | |
| 288 | struct qcom_battmgr_ac { |
| 289 | bool online; |
| 290 | }; |
| 291 | |
| 292 | struct qcom_battmgr_usb { |
| 293 | bool online; |
| 294 | unsigned int voltage_now; |
| 295 | unsigned int voltage_max; |
| 296 | unsigned int current_now; |
| 297 | unsigned int current_max; |
| 298 | unsigned int current_limit; |
| 299 | unsigned int usb_type; |
| 300 | }; |
| 301 | |
| 302 | struct qcom_battmgr_wireless { |
| 303 | bool online; |
| 304 | unsigned int voltage_now; |
| 305 | unsigned int voltage_max; |
| 306 | unsigned int current_now; |
| 307 | unsigned int current_max; |
| 308 | }; |
| 309 | |
| 310 | struct qcom_battmgr { |
| 311 | struct device *dev; |
| 312 | struct pmic_glink_client *client; |
| 313 | |
| 314 | enum qcom_battmgr_variant variant; |
| 315 | |
| 316 | struct power_supply *ac_psy; |
| 317 | struct power_supply *bat_psy; |
| 318 | struct power_supply *usb_psy; |
| 319 | struct power_supply *wls_psy; |
| 320 | |
| 321 | enum qcom_battmgr_unit unit; |
| 322 | |
| 323 | int error; |
| 324 | struct completion ack; |
| 325 | |
| 326 | bool service_up; |
| 327 | |
| 328 | struct qcom_battmgr_info info; |
| 329 | struct qcom_battmgr_status status; |
| 330 | struct qcom_battmgr_ac ac; |
| 331 | struct qcom_battmgr_usb usb; |
| 332 | struct qcom_battmgr_wireless wireless; |
| 333 | |
| 334 | struct work_struct enable_work; |
| 335 | |
| 336 | /* |
| 337 | * @lock is used to prevent concurrent power supply requests to the |
| 338 | * firmware, as it then stops responding. |
| 339 | */ |
| 340 | struct mutex lock; |
| 341 | }; |
| 342 | |
| 343 | static int qcom_battmgr_request(struct qcom_battmgr *battmgr, void *data, size_t len) |
| 344 | { |
| 345 | unsigned long left; |
| 346 | int ret; |
| 347 | |
| 348 | reinit_completion(x: &battmgr->ack); |
| 349 | |
| 350 | battmgr->error = 0; |
| 351 | |
| 352 | ret = pmic_glink_send(client: battmgr->client, data, len); |
| 353 | if (ret < 0) |
| 354 | return ret; |
| 355 | |
| 356 | left = wait_for_completion_timeout(x: &battmgr->ack, HZ); |
| 357 | if (!left) |
| 358 | return -ETIMEDOUT; |
| 359 | |
| 360 | return battmgr->error; |
| 361 | } |
| 362 | |
| 363 | static int qcom_battmgr_request_property(struct qcom_battmgr *battmgr, int opcode, |
| 364 | int property, u32 value) |
| 365 | { |
| 366 | struct qcom_battmgr_property_request request = { |
| 367 | .hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR), |
| 368 | .hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP), |
| 369 | .hdr.opcode = cpu_to_le32(opcode), |
| 370 | .battery = cpu_to_le32(0), |
| 371 | .property = cpu_to_le32(property), |
| 372 | .value = cpu_to_le32(value), |
| 373 | }; |
| 374 | |
| 375 | return qcom_battmgr_request(battmgr, data: &request, len: sizeof(request)); |
| 376 | } |
| 377 | |
| 378 | static int qcom_battmgr_update_status(struct qcom_battmgr *battmgr) |
| 379 | { |
| 380 | struct qcom_battmgr_update_request request = { |
| 381 | .hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR), |
| 382 | .hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP), |
| 383 | .hdr.opcode = cpu_to_le32(BATTMGR_BAT_STATUS), |
| 384 | .battery_id = cpu_to_le32(0), |
| 385 | }; |
| 386 | |
| 387 | return qcom_battmgr_request(battmgr, data: &request, len: sizeof(request)); |
| 388 | } |
| 389 | |
| 390 | static int qcom_battmgr_update_info(struct qcom_battmgr *battmgr) |
| 391 | { |
| 392 | struct qcom_battmgr_update_request request = { |
| 393 | .hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR), |
| 394 | .hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP), |
| 395 | .hdr.opcode = cpu_to_le32(BATTMGR_BAT_INFO), |
| 396 | .battery_id = cpu_to_le32(0), |
| 397 | }; |
| 398 | |
| 399 | return qcom_battmgr_request(battmgr, data: &request, len: sizeof(request)); |
| 400 | } |
| 401 | |
| 402 | static int qcom_battmgr_update_charge_time(struct qcom_battmgr *battmgr) |
| 403 | { |
| 404 | struct qcom_battmgr_charge_time_request request = { |
| 405 | .hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR), |
| 406 | .hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP), |
| 407 | .hdr.opcode = cpu_to_le32(BATTMGR_BAT_CHARGE_TIME), |
| 408 | .battery_id = cpu_to_le32(0), |
| 409 | .percent = cpu_to_le32(100), |
| 410 | }; |
| 411 | |
| 412 | return qcom_battmgr_request(battmgr, data: &request, len: sizeof(request)); |
| 413 | } |
| 414 | |
| 415 | static int qcom_battmgr_update_discharge_time(struct qcom_battmgr *battmgr) |
| 416 | { |
| 417 | struct qcom_battmgr_discharge_time_request request = { |
| 418 | .hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR), |
| 419 | .hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP), |
| 420 | .hdr.opcode = cpu_to_le32(BATTMGR_BAT_DISCHARGE_TIME), |
| 421 | .battery_id = cpu_to_le32(0), |
| 422 | .rate = cpu_to_le32(0), |
| 423 | }; |
| 424 | |
| 425 | return qcom_battmgr_request(battmgr, data: &request, len: sizeof(request)); |
| 426 | } |
| 427 | |
| 428 | static const u8 sm8350_bat_prop_map[] = { |
| 429 | [POWER_SUPPLY_PROP_STATUS] = BATT_STATUS, |
| 430 | [POWER_SUPPLY_PROP_HEALTH] = BATT_HEALTH, |
| 431 | [POWER_SUPPLY_PROP_PRESENT] = BATT_PRESENT, |
| 432 | [POWER_SUPPLY_PROP_CHARGE_TYPE] = BATT_CHG_TYPE, |
| 433 | [POWER_SUPPLY_PROP_CAPACITY] = BATT_CAPACITY, |
| 434 | [POWER_SUPPLY_PROP_VOLTAGE_OCV] = BATT_VOLT_OCV, |
| 435 | [POWER_SUPPLY_PROP_VOLTAGE_NOW] = BATT_VOLT_NOW, |
| 436 | [POWER_SUPPLY_PROP_VOLTAGE_MAX] = BATT_VOLT_MAX, |
| 437 | [POWER_SUPPLY_PROP_CURRENT_NOW] = BATT_CURR_NOW, |
| 438 | [POWER_SUPPLY_PROP_TEMP] = BATT_TEMP, |
| 439 | [POWER_SUPPLY_PROP_TECHNOLOGY] = BATT_TECHNOLOGY, |
| 440 | [POWER_SUPPLY_PROP_CHARGE_COUNTER] = BATT_CHG_COUNTER, |
| 441 | [POWER_SUPPLY_PROP_CYCLE_COUNT] = BATT_CYCLE_COUNT, |
| 442 | [POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN] = BATT_CHG_FULL_DESIGN, |
| 443 | [POWER_SUPPLY_PROP_CHARGE_FULL] = BATT_CHG_FULL, |
| 444 | [POWER_SUPPLY_PROP_MODEL_NAME] = BATT_MODEL_NAME, |
| 445 | [POWER_SUPPLY_PROP_TIME_TO_FULL_AVG] = BATT_TTF_AVG, |
| 446 | [POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG] = BATT_TTE_AVG, |
| 447 | [POWER_SUPPLY_PROP_INTERNAL_RESISTANCE] = BATT_RESISTANCE, |
| 448 | [POWER_SUPPLY_PROP_STATE_OF_HEALTH] = BATT_SOH, |
| 449 | [POWER_SUPPLY_PROP_POWER_NOW] = BATT_POWER_NOW, |
| 450 | [POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD] = BATT_CHG_CTRL_START_THR, |
| 451 | [POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD] = BATT_CHG_CTRL_END_THR, |
| 452 | }; |
| 453 | |
| 454 | static int qcom_battmgr_bat_sm8350_update(struct qcom_battmgr *battmgr, |
| 455 | enum power_supply_property psp) |
| 456 | { |
| 457 | unsigned int prop; |
| 458 | int ret; |
| 459 | |
| 460 | if (psp >= ARRAY_SIZE(sm8350_bat_prop_map)) |
| 461 | return -EINVAL; |
| 462 | |
| 463 | prop = sm8350_bat_prop_map[psp]; |
| 464 | |
| 465 | mutex_lock(&battmgr->lock); |
| 466 | ret = qcom_battmgr_request_property(battmgr, BATTMGR_BAT_PROPERTY_GET, property: prop, value: 0); |
| 467 | mutex_unlock(lock: &battmgr->lock); |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | static int qcom_battmgr_bat_sc8280xp_update(struct qcom_battmgr *battmgr, |
| 473 | enum power_supply_property psp) |
| 474 | { |
| 475 | int ret; |
| 476 | |
| 477 | mutex_lock(&battmgr->lock); |
| 478 | |
| 479 | if (!battmgr->info.valid) { |
| 480 | ret = qcom_battmgr_update_info(battmgr); |
| 481 | if (ret < 0) |
| 482 | goto out_unlock; |
| 483 | battmgr->info.valid = true; |
| 484 | } |
| 485 | |
| 486 | ret = qcom_battmgr_update_status(battmgr); |
| 487 | if (ret < 0) |
| 488 | goto out_unlock; |
| 489 | |
| 490 | if (psp == POWER_SUPPLY_PROP_TIME_TO_FULL_AVG) { |
| 491 | ret = qcom_battmgr_update_charge_time(battmgr); |
| 492 | if (ret < 0) { |
| 493 | ret = -ENODATA; |
| 494 | goto out_unlock; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (psp == POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG) { |
| 499 | ret = qcom_battmgr_update_discharge_time(battmgr); |
| 500 | if (ret < 0) { |
| 501 | ret = -ENODATA; |
| 502 | goto out_unlock; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | out_unlock: |
| 507 | mutex_unlock(lock: &battmgr->lock); |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | static int qcom_battmgr_bat_get_property(struct power_supply *psy, |
| 512 | enum power_supply_property psp, |
| 513 | union power_supply_propval *val) |
| 514 | { |
| 515 | struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy); |
| 516 | enum qcom_battmgr_unit unit = battmgr->unit; |
| 517 | int ret; |
| 518 | |
| 519 | if (!battmgr->service_up) |
| 520 | return -EAGAIN; |
| 521 | |
| 522 | if (battmgr->variant == QCOM_BATTMGR_SC8280XP || |
| 523 | battmgr->variant == QCOM_BATTMGR_X1E80100) |
| 524 | ret = qcom_battmgr_bat_sc8280xp_update(battmgr, psp); |
| 525 | else |
| 526 | ret = qcom_battmgr_bat_sm8350_update(battmgr, psp); |
| 527 | if (ret < 0) |
| 528 | return ret; |
| 529 | |
| 530 | switch (psp) { |
| 531 | case POWER_SUPPLY_PROP_STATUS: |
| 532 | val->intval = battmgr->status.status; |
| 533 | break; |
| 534 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 535 | val->intval = battmgr->info.charge_type; |
| 536 | break; |
| 537 | case POWER_SUPPLY_PROP_HEALTH: |
| 538 | val->intval = battmgr->status.health; |
| 539 | break; |
| 540 | case POWER_SUPPLY_PROP_PRESENT: |
| 541 | val->intval = battmgr->info.present; |
| 542 | break; |
| 543 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 544 | val->intval = battmgr->info.technology; |
| 545 | break; |
| 546 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 547 | val->intval = battmgr->info.cycle_count; |
| 548 | break; |
| 549 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 550 | val->intval = battmgr->info.voltage_max_design; |
| 551 | break; |
| 552 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| 553 | val->intval = battmgr->info.voltage_max; |
| 554 | break; |
| 555 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 556 | val->intval = battmgr->status.voltage_now; |
| 557 | break; |
| 558 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
| 559 | val->intval = battmgr->status.voltage_ocv; |
| 560 | break; |
| 561 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 562 | val->intval = battmgr->status.current_now; |
| 563 | break; |
| 564 | case POWER_SUPPLY_PROP_POWER_NOW: |
| 565 | val->intval = battmgr->status.power_now; |
| 566 | break; |
| 567 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 568 | if (unit != QCOM_BATTMGR_UNIT_mAh) |
| 569 | return -ENODATA; |
| 570 | val->intval = battmgr->info.design_capacity; |
| 571 | break; |
| 572 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 573 | if (unit != QCOM_BATTMGR_UNIT_mAh) |
| 574 | return -ENODATA; |
| 575 | val->intval = battmgr->info.last_full_capacity; |
| 576 | break; |
| 577 | case POWER_SUPPLY_PROP_CHARGE_EMPTY: |
| 578 | if (unit != QCOM_BATTMGR_UNIT_mAh) |
| 579 | return -ENODATA; |
| 580 | val->intval = battmgr->info.capacity_low; |
| 581 | break; |
| 582 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 583 | if (unit != QCOM_BATTMGR_UNIT_mAh) |
| 584 | return -ENODATA; |
| 585 | val->intval = battmgr->status.capacity; |
| 586 | break; |
| 587 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
| 588 | val->intval = battmgr->info.charge_count; |
| 589 | break; |
| 590 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
| 591 | if (unit != QCOM_BATTMGR_UNIT_mWh) |
| 592 | return -ENODATA; |
| 593 | val->intval = battmgr->info.design_capacity; |
| 594 | break; |
| 595 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 596 | if (unit != QCOM_BATTMGR_UNIT_mWh) |
| 597 | return -ENODATA; |
| 598 | val->intval = battmgr->info.last_full_capacity; |
| 599 | break; |
| 600 | case POWER_SUPPLY_PROP_ENERGY_EMPTY: |
| 601 | if (unit != QCOM_BATTMGR_UNIT_mWh) |
| 602 | return -ENODATA; |
| 603 | val->intval = battmgr->info.capacity_low; |
| 604 | break; |
| 605 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 606 | if (unit != QCOM_BATTMGR_UNIT_mWh) |
| 607 | return -ENODATA; |
| 608 | val->intval = battmgr->status.capacity; |
| 609 | break; |
| 610 | case POWER_SUPPLY_PROP_CAPACITY: |
| 611 | if (battmgr->status.percent == (unsigned int)-1) |
| 612 | return -ENODATA; |
| 613 | val->intval = battmgr->status.percent; |
| 614 | break; |
| 615 | case POWER_SUPPLY_PROP_TEMP: |
| 616 | val->intval = battmgr->status.temperature; |
| 617 | break; |
| 618 | case POWER_SUPPLY_PROP_INTERNAL_RESISTANCE: |
| 619 | val->intval = battmgr->status.resistance; |
| 620 | break; |
| 621 | case POWER_SUPPLY_PROP_STATE_OF_HEALTH: |
| 622 | val->intval = battmgr->status.soh_percent; |
| 623 | break; |
| 624 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 625 | val->intval = battmgr->status.discharge_time; |
| 626 | break; |
| 627 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
| 628 | val->intval = battmgr->status.charge_time; |
| 629 | break; |
| 630 | case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: |
| 631 | val->intval = battmgr->info.charge_ctrl_start; |
| 632 | break; |
| 633 | case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: |
| 634 | val->intval = battmgr->info.charge_ctrl_end; |
| 635 | break; |
| 636 | case POWER_SUPPLY_PROP_MANUFACTURE_YEAR: |
| 637 | val->intval = battmgr->info.year; |
| 638 | break; |
| 639 | case POWER_SUPPLY_PROP_MANUFACTURE_MONTH: |
| 640 | val->intval = battmgr->info.month; |
| 641 | break; |
| 642 | case POWER_SUPPLY_PROP_MANUFACTURE_DAY: |
| 643 | val->intval = battmgr->info.day; |
| 644 | break; |
| 645 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 646 | val->strval = battmgr->info.model_number; |
| 647 | break; |
| 648 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 649 | val->strval = battmgr->info.oem_info; |
| 650 | break; |
| 651 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
| 652 | val->strval = battmgr->info.serial_number; |
| 653 | break; |
| 654 | default: |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static int qcom_battmgr_set_charge_control(struct qcom_battmgr *battmgr, |
| 662 | u32 target_soc, u32 delta_soc) |
| 663 | { |
| 664 | struct qcom_battmgr_charge_ctrl_request request = { |
| 665 | .hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR), |
| 666 | .hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP), |
| 667 | .hdr.opcode = cpu_to_le32(BATTMGR_CHG_CTRL_LIMIT_EN), |
| 668 | .enable = cpu_to_le32(1), |
| 669 | .target_soc = cpu_to_le32(target_soc), |
| 670 | .delta_soc = cpu_to_le32(delta_soc), |
| 671 | }; |
| 672 | |
| 673 | return qcom_battmgr_request(battmgr, data: &request, len: sizeof(request)); |
| 674 | } |
| 675 | |
| 676 | static int qcom_battmgr_set_charge_start_threshold(struct qcom_battmgr *battmgr, int start_soc) |
| 677 | { |
| 678 | u32 target_soc, delta_soc; |
| 679 | int ret; |
| 680 | |
| 681 | start_soc = clamp(start_soc, CHARGE_CTRL_START_THR_MIN, CHARGE_CTRL_START_THR_MAX); |
| 682 | |
| 683 | /* |
| 684 | * If the new start threshold is larger than the old end threshold, |
| 685 | * move the end threshold one step (DELTA_SOC) after the new start |
| 686 | * threshold. |
| 687 | */ |
| 688 | if (start_soc > battmgr->info.charge_ctrl_end) { |
| 689 | target_soc = start_soc + CHARGE_CTRL_DELTA_SOC; |
| 690 | target_soc = min_t(u32, target_soc, CHARGE_CTRL_END_THR_MAX); |
| 691 | delta_soc = target_soc - start_soc; |
| 692 | delta_soc = min_t(u32, delta_soc, CHARGE_CTRL_DELTA_SOC); |
| 693 | } else { |
| 694 | target_soc = battmgr->info.charge_ctrl_end; |
| 695 | delta_soc = battmgr->info.charge_ctrl_end - start_soc; |
| 696 | } |
| 697 | |
| 698 | mutex_lock(&battmgr->lock); |
| 699 | ret = qcom_battmgr_set_charge_control(battmgr, target_soc, delta_soc); |
| 700 | mutex_unlock(lock: &battmgr->lock); |
| 701 | if (!ret) { |
| 702 | battmgr->info.charge_ctrl_start = start_soc; |
| 703 | battmgr->info.charge_ctrl_end = target_soc; |
| 704 | } |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | static int qcom_battmgr_set_charge_end_threshold(struct qcom_battmgr *battmgr, int end_soc) |
| 710 | { |
| 711 | u32 delta_soc = CHARGE_CTRL_DELTA_SOC; |
| 712 | int ret; |
| 713 | |
| 714 | end_soc = clamp(end_soc, CHARGE_CTRL_END_THR_MIN, CHARGE_CTRL_END_THR_MAX); |
| 715 | |
| 716 | if (battmgr->info.charge_ctrl_start && end_soc > battmgr->info.charge_ctrl_start) |
| 717 | delta_soc = end_soc - battmgr->info.charge_ctrl_start; |
| 718 | |
| 719 | mutex_lock(&battmgr->lock); |
| 720 | ret = qcom_battmgr_set_charge_control(battmgr, target_soc: end_soc, delta_soc); |
| 721 | mutex_unlock(lock: &battmgr->lock); |
| 722 | if (!ret) { |
| 723 | battmgr->info.charge_ctrl_start = end_soc - delta_soc; |
| 724 | battmgr->info.charge_ctrl_end = end_soc; |
| 725 | } |
| 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static int qcom_battmgr_charge_control_thresholds_init(struct qcom_battmgr *battmgr) |
| 731 | { |
| 732 | int ret; |
| 733 | u8 en, end_soc, start_soc, delta_soc; |
| 734 | |
| 735 | ret = nvmem_cell_read_u8(dev: battmgr->dev->parent, cell_id: "charge_limit_en" , val: &en); |
| 736 | if (!ret && en != 0) { |
| 737 | ret = nvmem_cell_read_u8(dev: battmgr->dev->parent, cell_id: "charge_limit_end" , val: &end_soc); |
| 738 | if (ret < 0) |
| 739 | return ret; |
| 740 | |
| 741 | ret = nvmem_cell_read_u8(dev: battmgr->dev->parent, cell_id: "charge_limit_delta" , val: &delta_soc); |
| 742 | if (ret < 0) |
| 743 | return ret; |
| 744 | |
| 745 | if (delta_soc >= end_soc) |
| 746 | return -EINVAL; |
| 747 | |
| 748 | start_soc = end_soc - delta_soc; |
| 749 | end_soc = clamp(end_soc, CHARGE_CTRL_END_THR_MIN, CHARGE_CTRL_END_THR_MAX); |
| 750 | start_soc = clamp(start_soc, CHARGE_CTRL_START_THR_MIN, CHARGE_CTRL_START_THR_MAX); |
| 751 | |
| 752 | battmgr->info.charge_ctrl_start = start_soc; |
| 753 | battmgr->info.charge_ctrl_end = end_soc; |
| 754 | } |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static int qcom_battmgr_bat_is_writeable(struct power_supply *psy, |
| 760 | enum power_supply_property psp) |
| 761 | { |
| 762 | switch (psp) { |
| 763 | case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: |
| 764 | case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: |
| 765 | return 1; |
| 766 | default: |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | static int qcom_battmgr_bat_set_property(struct power_supply *psy, |
| 774 | enum power_supply_property psp, |
| 775 | const union power_supply_propval *pval) |
| 776 | { |
| 777 | struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy); |
| 778 | |
| 779 | if (!battmgr->service_up) |
| 780 | return -EAGAIN; |
| 781 | |
| 782 | switch (psp) { |
| 783 | case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: |
| 784 | return qcom_battmgr_set_charge_start_threshold(battmgr, start_soc: pval->intval); |
| 785 | case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: |
| 786 | return qcom_battmgr_set_charge_end_threshold(battmgr, end_soc: pval->intval); |
| 787 | default: |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static const enum power_supply_property sc8280xp_bat_props[] = { |
| 795 | POWER_SUPPLY_PROP_STATUS, |
| 796 | POWER_SUPPLY_PROP_PRESENT, |
| 797 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 798 | POWER_SUPPLY_PROP_CAPACITY, |
| 799 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 800 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 801 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 802 | POWER_SUPPLY_PROP_POWER_NOW, |
| 803 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 804 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 805 | POWER_SUPPLY_PROP_CHARGE_EMPTY, |
| 806 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 807 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
| 808 | POWER_SUPPLY_PROP_ENERGY_FULL, |
| 809 | POWER_SUPPLY_PROP_ENERGY_EMPTY, |
| 810 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 811 | POWER_SUPPLY_PROP_TEMP, |
| 812 | POWER_SUPPLY_PROP_MANUFACTURE_YEAR, |
| 813 | POWER_SUPPLY_PROP_MANUFACTURE_MONTH, |
| 814 | POWER_SUPPLY_PROP_MANUFACTURE_DAY, |
| 815 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 816 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 817 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
| 818 | }; |
| 819 | |
| 820 | static const struct power_supply_desc sc8280xp_bat_psy_desc = { |
| 821 | .name = "qcom-battmgr-bat" , |
| 822 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 823 | .properties = sc8280xp_bat_props, |
| 824 | .num_properties = ARRAY_SIZE(sc8280xp_bat_props), |
| 825 | .get_property = qcom_battmgr_bat_get_property, |
| 826 | }; |
| 827 | |
| 828 | static const enum power_supply_property x1e80100_bat_props[] = { |
| 829 | POWER_SUPPLY_PROP_STATUS, |
| 830 | POWER_SUPPLY_PROP_PRESENT, |
| 831 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 832 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 833 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 834 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 835 | POWER_SUPPLY_PROP_POWER_NOW, |
| 836 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 837 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 838 | POWER_SUPPLY_PROP_CHARGE_EMPTY, |
| 839 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 840 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
| 841 | POWER_SUPPLY_PROP_ENERGY_FULL, |
| 842 | POWER_SUPPLY_PROP_ENERGY_EMPTY, |
| 843 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 844 | POWER_SUPPLY_PROP_TEMP, |
| 845 | POWER_SUPPLY_PROP_MANUFACTURE_YEAR, |
| 846 | POWER_SUPPLY_PROP_MANUFACTURE_MONTH, |
| 847 | POWER_SUPPLY_PROP_MANUFACTURE_DAY, |
| 848 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 849 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 850 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
| 851 | POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, |
| 852 | POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, |
| 853 | }; |
| 854 | |
| 855 | static const struct power_supply_desc x1e80100_bat_psy_desc = { |
| 856 | .name = "qcom-battmgr-bat" , |
| 857 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 858 | .properties = x1e80100_bat_props, |
| 859 | .num_properties = ARRAY_SIZE(x1e80100_bat_props), |
| 860 | .get_property = qcom_battmgr_bat_get_property, |
| 861 | .set_property = qcom_battmgr_bat_set_property, |
| 862 | .property_is_writeable = qcom_battmgr_bat_is_writeable, |
| 863 | }; |
| 864 | |
| 865 | static const enum power_supply_property sm8350_bat_props[] = { |
| 866 | POWER_SUPPLY_PROP_STATUS, |
| 867 | POWER_SUPPLY_PROP_HEALTH, |
| 868 | POWER_SUPPLY_PROP_PRESENT, |
| 869 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 870 | POWER_SUPPLY_PROP_CAPACITY, |
| 871 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
| 872 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 873 | POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| 874 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 875 | POWER_SUPPLY_PROP_TEMP, |
| 876 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 877 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
| 878 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 879 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 880 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 881 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 882 | POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, |
| 883 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 884 | POWER_SUPPLY_PROP_INTERNAL_RESISTANCE, |
| 885 | POWER_SUPPLY_PROP_STATE_OF_HEALTH, |
| 886 | POWER_SUPPLY_PROP_POWER_NOW, |
| 887 | }; |
| 888 | |
| 889 | static const struct power_supply_desc sm8350_bat_psy_desc = { |
| 890 | .name = "qcom-battmgr-bat" , |
| 891 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 892 | .properties = sm8350_bat_props, |
| 893 | .num_properties = ARRAY_SIZE(sm8350_bat_props), |
| 894 | .get_property = qcom_battmgr_bat_get_property, |
| 895 | }; |
| 896 | |
| 897 | static const enum power_supply_property sm8550_bat_props[] = { |
| 898 | POWER_SUPPLY_PROP_STATUS, |
| 899 | POWER_SUPPLY_PROP_HEALTH, |
| 900 | POWER_SUPPLY_PROP_PRESENT, |
| 901 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 902 | POWER_SUPPLY_PROP_CAPACITY, |
| 903 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
| 904 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 905 | POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| 906 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 907 | POWER_SUPPLY_PROP_TEMP, |
| 908 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 909 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
| 910 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 911 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 912 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 913 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 914 | POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, |
| 915 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 916 | POWER_SUPPLY_PROP_INTERNAL_RESISTANCE, |
| 917 | POWER_SUPPLY_PROP_STATE_OF_HEALTH, |
| 918 | POWER_SUPPLY_PROP_POWER_NOW, |
| 919 | POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, |
| 920 | POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, |
| 921 | }; |
| 922 | |
| 923 | static const struct power_supply_desc sm8550_bat_psy_desc = { |
| 924 | .name = "qcom-battmgr-bat" , |
| 925 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 926 | .properties = sm8550_bat_props, |
| 927 | .num_properties = ARRAY_SIZE(sm8550_bat_props), |
| 928 | .get_property = qcom_battmgr_bat_get_property, |
| 929 | .set_property = qcom_battmgr_bat_set_property, |
| 930 | .property_is_writeable = qcom_battmgr_bat_is_writeable, |
| 931 | }; |
| 932 | |
| 933 | static int qcom_battmgr_ac_get_property(struct power_supply *psy, |
| 934 | enum power_supply_property psp, |
| 935 | union power_supply_propval *val) |
| 936 | { |
| 937 | struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy); |
| 938 | int ret; |
| 939 | |
| 940 | if (!battmgr->service_up) |
| 941 | return -EAGAIN; |
| 942 | |
| 943 | ret = qcom_battmgr_bat_sc8280xp_update(battmgr, psp); |
| 944 | if (ret) |
| 945 | return ret; |
| 946 | |
| 947 | switch (psp) { |
| 948 | case POWER_SUPPLY_PROP_ONLINE: |
| 949 | val->intval = battmgr->ac.online; |
| 950 | break; |
| 951 | default: |
| 952 | return -EINVAL; |
| 953 | } |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | static const enum power_supply_property sc8280xp_ac_props[] = { |
| 959 | POWER_SUPPLY_PROP_ONLINE, |
| 960 | }; |
| 961 | |
| 962 | static const struct power_supply_desc sc8280xp_ac_psy_desc = { |
| 963 | .name = "qcom-battmgr-ac" , |
| 964 | .type = POWER_SUPPLY_TYPE_MAINS, |
| 965 | .properties = sc8280xp_ac_props, |
| 966 | .num_properties = ARRAY_SIZE(sc8280xp_ac_props), |
| 967 | .get_property = qcom_battmgr_ac_get_property, |
| 968 | }; |
| 969 | |
| 970 | static const u8 sm8350_usb_prop_map[] = { |
| 971 | [POWER_SUPPLY_PROP_ONLINE] = USB_ONLINE, |
| 972 | [POWER_SUPPLY_PROP_VOLTAGE_NOW] = USB_VOLT_NOW, |
| 973 | [POWER_SUPPLY_PROP_VOLTAGE_MAX] = USB_VOLT_MAX, |
| 974 | [POWER_SUPPLY_PROP_CURRENT_NOW] = USB_CURR_NOW, |
| 975 | [POWER_SUPPLY_PROP_CURRENT_MAX] = USB_CURR_MAX, |
| 976 | [POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT] = USB_INPUT_CURR_LIMIT, |
| 977 | [POWER_SUPPLY_PROP_USB_TYPE] = USB_TYPE, |
| 978 | }; |
| 979 | |
| 980 | static int qcom_battmgr_usb_sm8350_update(struct qcom_battmgr *battmgr, |
| 981 | enum power_supply_property psp) |
| 982 | { |
| 983 | unsigned int prop; |
| 984 | int ret; |
| 985 | |
| 986 | if (psp >= ARRAY_SIZE(sm8350_usb_prop_map)) |
| 987 | return -EINVAL; |
| 988 | |
| 989 | prop = sm8350_usb_prop_map[psp]; |
| 990 | |
| 991 | mutex_lock(&battmgr->lock); |
| 992 | ret = qcom_battmgr_request_property(battmgr, BATTMGR_USB_PROPERTY_GET, property: prop, value: 0); |
| 993 | mutex_unlock(lock: &battmgr->lock); |
| 994 | |
| 995 | return ret; |
| 996 | } |
| 997 | |
| 998 | static int qcom_battmgr_usb_get_property(struct power_supply *psy, |
| 999 | enum power_supply_property psp, |
| 1000 | union power_supply_propval *val) |
| 1001 | { |
| 1002 | struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy); |
| 1003 | int ret; |
| 1004 | |
| 1005 | if (!battmgr->service_up) |
| 1006 | return -EAGAIN; |
| 1007 | |
| 1008 | if (battmgr->variant == QCOM_BATTMGR_SC8280XP || |
| 1009 | battmgr->variant == QCOM_BATTMGR_X1E80100) |
| 1010 | ret = qcom_battmgr_bat_sc8280xp_update(battmgr, psp); |
| 1011 | else |
| 1012 | ret = qcom_battmgr_usb_sm8350_update(battmgr, psp); |
| 1013 | if (ret) |
| 1014 | return ret; |
| 1015 | |
| 1016 | switch (psp) { |
| 1017 | case POWER_SUPPLY_PROP_ONLINE: |
| 1018 | val->intval = battmgr->usb.online; |
| 1019 | break; |
| 1020 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 1021 | val->intval = battmgr->usb.voltage_now; |
| 1022 | break; |
| 1023 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| 1024 | val->intval = battmgr->usb.voltage_max; |
| 1025 | break; |
| 1026 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 1027 | val->intval = battmgr->usb.current_now; |
| 1028 | break; |
| 1029 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 1030 | val->intval = battmgr->usb.current_max; |
| 1031 | break; |
| 1032 | case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: |
| 1033 | val->intval = battmgr->usb.current_limit; |
| 1034 | break; |
| 1035 | case POWER_SUPPLY_PROP_USB_TYPE: |
| 1036 | val->intval = battmgr->usb.usb_type; |
| 1037 | break; |
| 1038 | default: |
| 1039 | return -EINVAL; |
| 1040 | } |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | static const enum power_supply_property sc8280xp_usb_props[] = { |
| 1046 | POWER_SUPPLY_PROP_ONLINE, |
| 1047 | }; |
| 1048 | |
| 1049 | static const struct power_supply_desc sc8280xp_usb_psy_desc = { |
| 1050 | .name = "qcom-battmgr-usb" , |
| 1051 | .type = POWER_SUPPLY_TYPE_USB, |
| 1052 | .properties = sc8280xp_usb_props, |
| 1053 | .num_properties = ARRAY_SIZE(sc8280xp_usb_props), |
| 1054 | .get_property = qcom_battmgr_usb_get_property, |
| 1055 | .usb_types = BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN) | |
| 1056 | BIT(POWER_SUPPLY_USB_TYPE_SDP) | |
| 1057 | BIT(POWER_SUPPLY_USB_TYPE_DCP) | |
| 1058 | BIT(POWER_SUPPLY_USB_TYPE_CDP) | |
| 1059 | BIT(POWER_SUPPLY_USB_TYPE_ACA) | |
| 1060 | BIT(POWER_SUPPLY_USB_TYPE_C) | |
| 1061 | BIT(POWER_SUPPLY_USB_TYPE_PD) | |
| 1062 | BIT(POWER_SUPPLY_USB_TYPE_PD_DRP) | |
| 1063 | BIT(POWER_SUPPLY_USB_TYPE_PD_PPS) | |
| 1064 | BIT(POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID), |
| 1065 | }; |
| 1066 | |
| 1067 | static const enum power_supply_property sm8350_usb_props[] = { |
| 1068 | POWER_SUPPLY_PROP_ONLINE, |
| 1069 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 1070 | POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| 1071 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 1072 | POWER_SUPPLY_PROP_CURRENT_MAX, |
| 1073 | POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, |
| 1074 | POWER_SUPPLY_PROP_USB_TYPE, |
| 1075 | }; |
| 1076 | |
| 1077 | static const struct power_supply_desc sm8350_usb_psy_desc = { |
| 1078 | .name = "qcom-battmgr-usb" , |
| 1079 | .type = POWER_SUPPLY_TYPE_USB, |
| 1080 | .properties = sm8350_usb_props, |
| 1081 | .num_properties = ARRAY_SIZE(sm8350_usb_props), |
| 1082 | .get_property = qcom_battmgr_usb_get_property, |
| 1083 | .usb_types = BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN) | |
| 1084 | BIT(POWER_SUPPLY_USB_TYPE_SDP) | |
| 1085 | BIT(POWER_SUPPLY_USB_TYPE_DCP) | |
| 1086 | BIT(POWER_SUPPLY_USB_TYPE_CDP) | |
| 1087 | BIT(POWER_SUPPLY_USB_TYPE_ACA) | |
| 1088 | BIT(POWER_SUPPLY_USB_TYPE_C) | |
| 1089 | BIT(POWER_SUPPLY_USB_TYPE_PD) | |
| 1090 | BIT(POWER_SUPPLY_USB_TYPE_PD_DRP) | |
| 1091 | BIT(POWER_SUPPLY_USB_TYPE_PD_PPS) | |
| 1092 | BIT(POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID), |
| 1093 | }; |
| 1094 | |
| 1095 | static const u8 sm8350_wls_prop_map[] = { |
| 1096 | [POWER_SUPPLY_PROP_ONLINE] = WLS_ONLINE, |
| 1097 | [POWER_SUPPLY_PROP_VOLTAGE_NOW] = WLS_VOLT_NOW, |
| 1098 | [POWER_SUPPLY_PROP_VOLTAGE_MAX] = WLS_VOLT_MAX, |
| 1099 | [POWER_SUPPLY_PROP_CURRENT_NOW] = WLS_CURR_NOW, |
| 1100 | [POWER_SUPPLY_PROP_CURRENT_MAX] = WLS_CURR_MAX, |
| 1101 | }; |
| 1102 | |
| 1103 | static int qcom_battmgr_wls_sm8350_update(struct qcom_battmgr *battmgr, |
| 1104 | enum power_supply_property psp) |
| 1105 | { |
| 1106 | unsigned int prop; |
| 1107 | int ret; |
| 1108 | |
| 1109 | if (psp >= ARRAY_SIZE(sm8350_wls_prop_map)) |
| 1110 | return -EINVAL; |
| 1111 | |
| 1112 | prop = sm8350_wls_prop_map[psp]; |
| 1113 | |
| 1114 | mutex_lock(&battmgr->lock); |
| 1115 | ret = qcom_battmgr_request_property(battmgr, BATTMGR_WLS_PROPERTY_GET, property: prop, value: 0); |
| 1116 | mutex_unlock(lock: &battmgr->lock); |
| 1117 | |
| 1118 | return ret; |
| 1119 | } |
| 1120 | |
| 1121 | static int qcom_battmgr_wls_get_property(struct power_supply *psy, |
| 1122 | enum power_supply_property psp, |
| 1123 | union power_supply_propval *val) |
| 1124 | { |
| 1125 | struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy); |
| 1126 | int ret; |
| 1127 | |
| 1128 | if (!battmgr->service_up) |
| 1129 | return -EAGAIN; |
| 1130 | |
| 1131 | if (battmgr->variant == QCOM_BATTMGR_SC8280XP || |
| 1132 | battmgr->variant == QCOM_BATTMGR_X1E80100) |
| 1133 | ret = qcom_battmgr_bat_sc8280xp_update(battmgr, psp); |
| 1134 | else |
| 1135 | ret = qcom_battmgr_wls_sm8350_update(battmgr, psp); |
| 1136 | if (ret < 0) |
| 1137 | return ret; |
| 1138 | |
| 1139 | switch (psp) { |
| 1140 | case POWER_SUPPLY_PROP_ONLINE: |
| 1141 | val->intval = battmgr->wireless.online; |
| 1142 | break; |
| 1143 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 1144 | val->intval = battmgr->wireless.voltage_now; |
| 1145 | break; |
| 1146 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| 1147 | val->intval = battmgr->wireless.voltage_max; |
| 1148 | break; |
| 1149 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 1150 | val->intval = battmgr->wireless.current_now; |
| 1151 | break; |
| 1152 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 1153 | val->intval = battmgr->wireless.current_max; |
| 1154 | break; |
| 1155 | default: |
| 1156 | return -EINVAL; |
| 1157 | } |
| 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | static const enum power_supply_property sc8280xp_wls_props[] = { |
| 1163 | POWER_SUPPLY_PROP_ONLINE, |
| 1164 | }; |
| 1165 | |
| 1166 | static const struct power_supply_desc sc8280xp_wls_psy_desc = { |
| 1167 | .name = "qcom-battmgr-wls" , |
| 1168 | .type = POWER_SUPPLY_TYPE_WIRELESS, |
| 1169 | .properties = sc8280xp_wls_props, |
| 1170 | .num_properties = ARRAY_SIZE(sc8280xp_wls_props), |
| 1171 | .get_property = qcom_battmgr_wls_get_property, |
| 1172 | }; |
| 1173 | |
| 1174 | static const enum power_supply_property sm8350_wls_props[] = { |
| 1175 | POWER_SUPPLY_PROP_ONLINE, |
| 1176 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 1177 | POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| 1178 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 1179 | POWER_SUPPLY_PROP_CURRENT_MAX, |
| 1180 | }; |
| 1181 | |
| 1182 | static const struct power_supply_desc sm8350_wls_psy_desc = { |
| 1183 | .name = "qcom-battmgr-wls" , |
| 1184 | .type = POWER_SUPPLY_TYPE_WIRELESS, |
| 1185 | .properties = sm8350_wls_props, |
| 1186 | .num_properties = ARRAY_SIZE(sm8350_wls_props), |
| 1187 | .get_property = qcom_battmgr_wls_get_property, |
| 1188 | }; |
| 1189 | |
| 1190 | static void qcom_battmgr_notification(struct qcom_battmgr *battmgr, |
| 1191 | const struct qcom_battmgr_message *msg, |
| 1192 | int len) |
| 1193 | { |
| 1194 | size_t payload_len = len - sizeof(struct pmic_glink_hdr); |
| 1195 | unsigned int notification; |
| 1196 | |
| 1197 | if (payload_len != sizeof(msg->notification)) { |
| 1198 | dev_warn(battmgr->dev, "ignoring notification with invalid length\n" ); |
| 1199 | return; |
| 1200 | } |
| 1201 | |
| 1202 | notification = le32_to_cpu(msg->notification); |
| 1203 | notification &= 0xff; |
| 1204 | switch (notification) { |
| 1205 | case NOTIF_BAT_INFO: |
| 1206 | battmgr->info.valid = false; |
| 1207 | fallthrough; |
| 1208 | case NOTIF_BAT_STATUS: |
| 1209 | case NOTIF_BAT_PROPERTY: |
| 1210 | case NOTIF_BAT_CHARGING_STATE: |
| 1211 | power_supply_changed(psy: battmgr->bat_psy); |
| 1212 | break; |
| 1213 | case NOTIF_USB_PROPERTY: |
| 1214 | power_supply_changed(psy: battmgr->usb_psy); |
| 1215 | break; |
| 1216 | case NOTIF_WLS_PROPERTY: |
| 1217 | power_supply_changed(psy: battmgr->wls_psy); |
| 1218 | break; |
| 1219 | default: |
| 1220 | dev_err(battmgr->dev, "unknown notification: %#x\n" , notification); |
| 1221 | break; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | static void qcom_battmgr_sc8280xp_strcpy(char *dest, const char *src) |
| 1226 | { |
| 1227 | size_t len = src[0]; |
| 1228 | |
| 1229 | /* Some firmware versions return Pascal-style strings */ |
| 1230 | if (len < BATTMGR_STRING_LEN && len == strnlen(p: src + 1, BATTMGR_STRING_LEN - 1)) { |
| 1231 | memcpy(dest, src + 1, len); |
| 1232 | dest[len] = '\0'; |
| 1233 | } else { |
| 1234 | memcpy(dest, src, BATTMGR_STRING_LEN); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | static unsigned int qcom_battmgr_sc8280xp_parse_technology(const char *chemistry) |
| 1239 | { |
| 1240 | if ((!strncmp(chemistry, "LIO" , BATTMGR_CHEMISTRY_LEN)) || |
| 1241 | (!strncmp(chemistry, "OOI" , BATTMGR_CHEMISTRY_LEN))) |
| 1242 | return POWER_SUPPLY_TECHNOLOGY_LION; |
| 1243 | if (!strncmp(chemistry, "LIP" , BATTMGR_CHEMISTRY_LEN)) |
| 1244 | return POWER_SUPPLY_TECHNOLOGY_LIPO; |
| 1245 | |
| 1246 | pr_err("Unknown battery technology '%s'\n" , chemistry); |
| 1247 | return POWER_SUPPLY_TECHNOLOGY_UNKNOWN; |
| 1248 | } |
| 1249 | |
| 1250 | static unsigned int qcom_battmgr_sc8280xp_convert_temp(unsigned int temperature) |
| 1251 | { |
| 1252 | return DIV_ROUND_CLOSEST(temperature, 10); |
| 1253 | } |
| 1254 | |
| 1255 | static void qcom_battmgr_sc8280xp_callback(struct qcom_battmgr *battmgr, |
| 1256 | const struct qcom_battmgr_message *resp, |
| 1257 | size_t len) |
| 1258 | { |
| 1259 | unsigned int opcode = le32_to_cpu(resp->hdr.opcode); |
| 1260 | unsigned int source; |
| 1261 | unsigned int state; |
| 1262 | size_t payload_len = len - sizeof(struct pmic_glink_hdr); |
| 1263 | |
| 1264 | if (payload_len < sizeof(__le32)) { |
| 1265 | dev_warn(battmgr->dev, "invalid payload length for %#x: %zd\n" , |
| 1266 | opcode, len); |
| 1267 | return; |
| 1268 | } |
| 1269 | |
| 1270 | switch (opcode) { |
| 1271 | case BATTMGR_REQUEST_NOTIFICATION: |
| 1272 | battmgr->error = 0; |
| 1273 | break; |
| 1274 | case BATTMGR_BAT_INFO: |
| 1275 | /* some firmware versions report an extra __le32 at the end of the payload */ |
| 1276 | if (payload_len != sizeof(resp->info) && |
| 1277 | payload_len != (sizeof(resp->info) + sizeof(__le32))) { |
| 1278 | dev_warn(battmgr->dev, |
| 1279 | "invalid payload length for battery information request: %zd\n" , |
| 1280 | payload_len); |
| 1281 | battmgr->error = -ENODATA; |
| 1282 | return; |
| 1283 | } |
| 1284 | |
| 1285 | battmgr->unit = le32_to_cpu(resp->info.power_unit); |
| 1286 | |
| 1287 | battmgr->info.present = true; |
| 1288 | battmgr->info.design_capacity = le32_to_cpu(resp->info.design_capacity) * 1000; |
| 1289 | battmgr->info.last_full_capacity = le32_to_cpu(resp->info.last_full_capacity) * 1000; |
| 1290 | battmgr->info.voltage_max_design = le32_to_cpu(resp->info.design_voltage) * 1000; |
| 1291 | battmgr->info.capacity_low = le32_to_cpu(resp->info.capacity_low) * 1000; |
| 1292 | battmgr->info.cycle_count = le32_to_cpu(resp->info.cycle_count); |
| 1293 | qcom_battmgr_sc8280xp_strcpy(dest: battmgr->info.model_number, src: resp->info.model_number); |
| 1294 | qcom_battmgr_sc8280xp_strcpy(dest: battmgr->info.serial_number, src: resp->info.serial_number); |
| 1295 | battmgr->info.technology = qcom_battmgr_sc8280xp_parse_technology(chemistry: resp->info.battery_chemistry); |
| 1296 | qcom_battmgr_sc8280xp_strcpy(dest: battmgr->info.oem_info, src: resp->info.oem_info); |
| 1297 | battmgr->info.day = resp->info.day; |
| 1298 | battmgr->info.month = resp->info.month; |
| 1299 | battmgr->info.year = le16_to_cpu(resp->info.year); |
| 1300 | break; |
| 1301 | case BATTMGR_BAT_STATUS: |
| 1302 | if (payload_len != sizeof(resp->status)) { |
| 1303 | dev_warn(battmgr->dev, |
| 1304 | "invalid payload length for battery status request: %zd\n" , |
| 1305 | payload_len); |
| 1306 | battmgr->error = -ENODATA; |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | state = le32_to_cpu(resp->status.battery_state); |
| 1311 | if (state & BIT(0)) |
| 1312 | battmgr->status.status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 1313 | else if (state & BIT(1)) |
| 1314 | battmgr->status.status = POWER_SUPPLY_STATUS_CHARGING; |
| 1315 | else |
| 1316 | battmgr->status.status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 1317 | |
| 1318 | battmgr->status.capacity = le32_to_cpu(resp->status.capacity) * 1000; |
| 1319 | battmgr->status.power_now = le32_to_cpu(resp->status.rate) * 1000; |
| 1320 | battmgr->status.voltage_now = le32_to_cpu(resp->status.battery_voltage) * 1000; |
| 1321 | battmgr->status.temperature = qcom_battmgr_sc8280xp_convert_temp(le32_to_cpu(resp->status.temperature)); |
| 1322 | |
| 1323 | source = le32_to_cpu(resp->status.charging_source); |
| 1324 | battmgr->ac.online = source == BATTMGR_CHARGING_SOURCE_AC; |
| 1325 | battmgr->usb.online = source == BATTMGR_CHARGING_SOURCE_USB; |
| 1326 | battmgr->wireless.online = source == BATTMGR_CHARGING_SOURCE_WIRELESS; |
| 1327 | if (battmgr->info.last_full_capacity != 0) { |
| 1328 | /* |
| 1329 | * 100 * battmgr->status.capacity can overflow a 32bit |
| 1330 | * unsigned integer. FW readings are in m{W/A}h, which |
| 1331 | * are multiplied by 1000 converting them to u{W/A}h, |
| 1332 | * the format the power_supply API expects. |
| 1333 | * To avoid overflow use the original value for dividend |
| 1334 | * and convert the divider back to m{W/A}h, which can be |
| 1335 | * done without any loss of precision. |
| 1336 | */ |
| 1337 | battmgr->status.percent = |
| 1338 | (100 * le32_to_cpu(resp->status.capacity)) / |
| 1339 | (battmgr->info.last_full_capacity / 1000); |
| 1340 | } else { |
| 1341 | /* |
| 1342 | * Let the sysfs handler know no data is available at |
| 1343 | * this time. |
| 1344 | */ |
| 1345 | battmgr->status.percent = (unsigned int)-1; |
| 1346 | } |
| 1347 | break; |
| 1348 | case BATTMGR_BAT_DISCHARGE_TIME: |
| 1349 | battmgr->status.discharge_time = le32_to_cpu(resp->time); |
| 1350 | break; |
| 1351 | case BATTMGR_BAT_CHARGE_TIME: |
| 1352 | battmgr->status.charge_time = le32_to_cpu(resp->time); |
| 1353 | break; |
| 1354 | case BATTMGR_CHG_CTRL_LIMIT_EN: |
| 1355 | battmgr->error = 0; |
| 1356 | break; |
| 1357 | default: |
| 1358 | dev_warn(battmgr->dev, "unknown message %#x\n" , opcode); |
| 1359 | break; |
| 1360 | } |
| 1361 | |
| 1362 | complete(&battmgr->ack); |
| 1363 | } |
| 1364 | |
| 1365 | static void qcom_battmgr_sm8350_callback(struct qcom_battmgr *battmgr, |
| 1366 | const struct qcom_battmgr_message *resp, |
| 1367 | size_t len) |
| 1368 | { |
| 1369 | unsigned int property; |
| 1370 | unsigned int opcode = le32_to_cpu(resp->hdr.opcode); |
| 1371 | size_t payload_len = len - sizeof(struct pmic_glink_hdr); |
| 1372 | unsigned int val; |
| 1373 | |
| 1374 | if (payload_len < sizeof(__le32)) { |
| 1375 | dev_warn(battmgr->dev, "invalid payload length for %#x: %zd\n" , |
| 1376 | opcode, len); |
| 1377 | return; |
| 1378 | } |
| 1379 | |
| 1380 | switch (opcode) { |
| 1381 | case BATTMGR_BAT_PROPERTY_GET: |
| 1382 | property = le32_to_cpu(resp->intval.property); |
| 1383 | if (property == BATT_MODEL_NAME) { |
| 1384 | if (payload_len != sizeof(resp->strval)) { |
| 1385 | dev_warn(battmgr->dev, |
| 1386 | "invalid payload length for BATT_MODEL_NAME request: %zd\n" , |
| 1387 | payload_len); |
| 1388 | battmgr->error = -ENODATA; |
| 1389 | return; |
| 1390 | } |
| 1391 | } else { |
| 1392 | if (payload_len != sizeof(resp->intval)) { |
| 1393 | dev_warn(battmgr->dev, |
| 1394 | "invalid payload length for %#x request: %zd\n" , |
| 1395 | property, payload_len); |
| 1396 | battmgr->error = -ENODATA; |
| 1397 | return; |
| 1398 | } |
| 1399 | |
| 1400 | battmgr->error = le32_to_cpu(resp->intval.result); |
| 1401 | if (battmgr->error) |
| 1402 | goto out_complete; |
| 1403 | } |
| 1404 | |
| 1405 | switch (property) { |
| 1406 | case BATT_STATUS: |
| 1407 | battmgr->status.status = le32_to_cpu(resp->intval.value); |
| 1408 | break; |
| 1409 | case BATT_HEALTH: |
| 1410 | battmgr->status.health = le32_to_cpu(resp->intval.value); |
| 1411 | break; |
| 1412 | case BATT_PRESENT: |
| 1413 | battmgr->info.present = le32_to_cpu(resp->intval.value); |
| 1414 | break; |
| 1415 | case BATT_CHG_TYPE: |
| 1416 | battmgr->info.charge_type = le32_to_cpu(resp->intval.value); |
| 1417 | break; |
| 1418 | case BATT_CAPACITY: |
| 1419 | battmgr->status.percent = le32_to_cpu(resp->intval.value) / 100; |
| 1420 | break; |
| 1421 | case BATT_SOH: |
| 1422 | battmgr->status.soh_percent = le32_to_cpu(resp->intval.value); |
| 1423 | break; |
| 1424 | case BATT_VOLT_OCV: |
| 1425 | battmgr->status.voltage_ocv = le32_to_cpu(resp->intval.value); |
| 1426 | break; |
| 1427 | case BATT_VOLT_NOW: |
| 1428 | battmgr->status.voltage_now = le32_to_cpu(resp->intval.value); |
| 1429 | break; |
| 1430 | case BATT_VOLT_MAX: |
| 1431 | battmgr->info.voltage_max = le32_to_cpu(resp->intval.value); |
| 1432 | break; |
| 1433 | case BATT_CURR_NOW: |
| 1434 | battmgr->status.current_now = le32_to_cpu(resp->intval.value); |
| 1435 | break; |
| 1436 | case BATT_TEMP: |
| 1437 | val = le32_to_cpu(resp->intval.value); |
| 1438 | battmgr->status.temperature = DIV_ROUND_CLOSEST(val, 10); |
| 1439 | break; |
| 1440 | case BATT_TECHNOLOGY: |
| 1441 | battmgr->info.technology = le32_to_cpu(resp->intval.value); |
| 1442 | break; |
| 1443 | case BATT_CHG_COUNTER: |
| 1444 | battmgr->info.charge_count = le32_to_cpu(resp->intval.value); |
| 1445 | break; |
| 1446 | case BATT_CYCLE_COUNT: |
| 1447 | battmgr->info.cycle_count = le32_to_cpu(resp->intval.value); |
| 1448 | break; |
| 1449 | case BATT_CHG_FULL_DESIGN: |
| 1450 | battmgr->info.design_capacity = le32_to_cpu(resp->intval.value); |
| 1451 | break; |
| 1452 | case BATT_CHG_FULL: |
| 1453 | battmgr->info.last_full_capacity = le32_to_cpu(resp->intval.value); |
| 1454 | break; |
| 1455 | case BATT_MODEL_NAME: |
| 1456 | strscpy(battmgr->info.model_number, resp->strval.model, BATTMGR_STRING_LEN); |
| 1457 | break; |
| 1458 | case BATT_TTF_AVG: |
| 1459 | battmgr->status.charge_time = le32_to_cpu(resp->intval.value); |
| 1460 | break; |
| 1461 | case BATT_TTE_AVG: |
| 1462 | battmgr->status.discharge_time = le32_to_cpu(resp->intval.value); |
| 1463 | break; |
| 1464 | case BATT_RESISTANCE: |
| 1465 | battmgr->status.resistance = le32_to_cpu(resp->intval.value); |
| 1466 | break; |
| 1467 | case BATT_POWER_NOW: |
| 1468 | battmgr->status.power_now = le32_to_cpu(resp->intval.value); |
| 1469 | break; |
| 1470 | case BATT_CHG_CTRL_START_THR: |
| 1471 | battmgr->info.charge_ctrl_start = le32_to_cpu(resp->intval.value); |
| 1472 | break; |
| 1473 | case BATT_CHG_CTRL_END_THR: |
| 1474 | battmgr->info.charge_ctrl_end = le32_to_cpu(resp->intval.value); |
| 1475 | break; |
| 1476 | default: |
| 1477 | dev_warn(battmgr->dev, "unknown property %#x\n" , property); |
| 1478 | break; |
| 1479 | } |
| 1480 | break; |
| 1481 | case BATTMGR_USB_PROPERTY_GET: |
| 1482 | property = le32_to_cpu(resp->intval.property); |
| 1483 | if (payload_len != sizeof(resp->intval)) { |
| 1484 | dev_warn(battmgr->dev, |
| 1485 | "invalid payload length for %#x request: %zd\n" , |
| 1486 | property, payload_len); |
| 1487 | battmgr->error = -ENODATA; |
| 1488 | return; |
| 1489 | } |
| 1490 | |
| 1491 | battmgr->error = le32_to_cpu(resp->intval.result); |
| 1492 | if (battmgr->error) |
| 1493 | goto out_complete; |
| 1494 | |
| 1495 | switch (property) { |
| 1496 | case USB_ONLINE: |
| 1497 | battmgr->usb.online = le32_to_cpu(resp->intval.value); |
| 1498 | break; |
| 1499 | case USB_VOLT_NOW: |
| 1500 | battmgr->usb.voltage_now = le32_to_cpu(resp->intval.value); |
| 1501 | break; |
| 1502 | case USB_VOLT_MAX: |
| 1503 | battmgr->usb.voltage_max = le32_to_cpu(resp->intval.value); |
| 1504 | break; |
| 1505 | case USB_CURR_NOW: |
| 1506 | battmgr->usb.current_now = le32_to_cpu(resp->intval.value); |
| 1507 | break; |
| 1508 | case USB_CURR_MAX: |
| 1509 | battmgr->usb.current_max = le32_to_cpu(resp->intval.value); |
| 1510 | break; |
| 1511 | case USB_INPUT_CURR_LIMIT: |
| 1512 | battmgr->usb.current_limit = le32_to_cpu(resp->intval.value); |
| 1513 | break; |
| 1514 | case USB_TYPE: |
| 1515 | battmgr->usb.usb_type = le32_to_cpu(resp->intval.value); |
| 1516 | break; |
| 1517 | default: |
| 1518 | dev_warn(battmgr->dev, "unknown property %#x\n" , property); |
| 1519 | break; |
| 1520 | } |
| 1521 | break; |
| 1522 | case BATTMGR_WLS_PROPERTY_GET: |
| 1523 | property = le32_to_cpu(resp->intval.property); |
| 1524 | if (payload_len != sizeof(resp->intval)) { |
| 1525 | dev_warn(battmgr->dev, |
| 1526 | "invalid payload length for %#x request: %zd\n" , |
| 1527 | property, payload_len); |
| 1528 | battmgr->error = -ENODATA; |
| 1529 | return; |
| 1530 | } |
| 1531 | |
| 1532 | battmgr->error = le32_to_cpu(resp->intval.result); |
| 1533 | if (battmgr->error) |
| 1534 | goto out_complete; |
| 1535 | |
| 1536 | switch (property) { |
| 1537 | case WLS_ONLINE: |
| 1538 | battmgr->wireless.online = le32_to_cpu(resp->intval.value); |
| 1539 | break; |
| 1540 | case WLS_VOLT_NOW: |
| 1541 | battmgr->wireless.voltage_now = le32_to_cpu(resp->intval.value); |
| 1542 | break; |
| 1543 | case WLS_VOLT_MAX: |
| 1544 | battmgr->wireless.voltage_max = le32_to_cpu(resp->intval.value); |
| 1545 | break; |
| 1546 | case WLS_CURR_NOW: |
| 1547 | battmgr->wireless.current_now = le32_to_cpu(resp->intval.value); |
| 1548 | break; |
| 1549 | case WLS_CURR_MAX: |
| 1550 | battmgr->wireless.current_max = le32_to_cpu(resp->intval.value); |
| 1551 | break; |
| 1552 | default: |
| 1553 | dev_warn(battmgr->dev, "unknown property %#x\n" , property); |
| 1554 | break; |
| 1555 | } |
| 1556 | break; |
| 1557 | case BATTMGR_REQUEST_NOTIFICATION: |
| 1558 | case BATTMGR_CHG_CTRL_LIMIT_EN: |
| 1559 | battmgr->error = 0; |
| 1560 | break; |
| 1561 | default: |
| 1562 | dev_warn(battmgr->dev, "unknown message %#x\n" , opcode); |
| 1563 | break; |
| 1564 | } |
| 1565 | |
| 1566 | out_complete: |
| 1567 | complete(&battmgr->ack); |
| 1568 | } |
| 1569 | |
| 1570 | static void qcom_battmgr_callback(const void *data, size_t len, void *priv) |
| 1571 | { |
| 1572 | const struct pmic_glink_hdr *hdr = data; |
| 1573 | struct qcom_battmgr *battmgr = priv; |
| 1574 | unsigned int opcode = le32_to_cpu(hdr->opcode); |
| 1575 | |
| 1576 | if (opcode == BATTMGR_NOTIFICATION) |
| 1577 | qcom_battmgr_notification(battmgr, msg: data, len); |
| 1578 | else if (battmgr->variant == QCOM_BATTMGR_SC8280XP || |
| 1579 | battmgr->variant == QCOM_BATTMGR_X1E80100) |
| 1580 | qcom_battmgr_sc8280xp_callback(battmgr, resp: data, len); |
| 1581 | else |
| 1582 | qcom_battmgr_sm8350_callback(battmgr, resp: data, len); |
| 1583 | } |
| 1584 | |
| 1585 | static void qcom_battmgr_enable_worker(struct work_struct *work) |
| 1586 | { |
| 1587 | struct qcom_battmgr *battmgr = container_of(work, struct qcom_battmgr, enable_work); |
| 1588 | struct qcom_battmgr_enable_request req = { |
| 1589 | .hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR), |
| 1590 | .hdr.type = cpu_to_le32(PMIC_GLINK_NOTIFY), |
| 1591 | .hdr.opcode = cpu_to_le32(BATTMGR_REQUEST_NOTIFICATION), |
| 1592 | }; |
| 1593 | int ret; |
| 1594 | |
| 1595 | ret = qcom_battmgr_request(battmgr, data: &req, len: sizeof(req)); |
| 1596 | if (ret) |
| 1597 | dev_err(battmgr->dev, "failed to request power notifications\n" ); |
| 1598 | } |
| 1599 | |
| 1600 | static void qcom_battmgr_pdr_notify(void *priv, int state) |
| 1601 | { |
| 1602 | struct qcom_battmgr *battmgr = priv; |
| 1603 | |
| 1604 | if (state == SERVREG_SERVICE_STATE_UP) { |
| 1605 | battmgr->service_up = true; |
| 1606 | schedule_work(work: &battmgr->enable_work); |
| 1607 | } else { |
| 1608 | battmgr->service_up = false; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | static const struct of_device_id qcom_battmgr_of_variants[] = { |
| 1613 | { .compatible = "qcom,sc8180x-pmic-glink" , .data = (void *)QCOM_BATTMGR_SC8280XP }, |
| 1614 | { .compatible = "qcom,sc8280xp-pmic-glink" , .data = (void *)QCOM_BATTMGR_SC8280XP }, |
| 1615 | { .compatible = "qcom,sm8550-pmic-glink" , .data = (void *)QCOM_BATTMGR_SM8550 }, |
| 1616 | { .compatible = "qcom,x1e80100-pmic-glink" , .data = (void *)QCOM_BATTMGR_X1E80100 }, |
| 1617 | /* Unmatched devices falls back to QCOM_BATTMGR_SM8350 */ |
| 1618 | {} |
| 1619 | }; |
| 1620 | |
| 1621 | static char *qcom_battmgr_battery[] = { "battery" }; |
| 1622 | |
| 1623 | static int qcom_battmgr_probe(struct auxiliary_device *adev, |
| 1624 | const struct auxiliary_device_id *id) |
| 1625 | { |
| 1626 | const struct power_supply_desc *psy_desc; |
| 1627 | struct power_supply_config psy_cfg_supply = {}; |
| 1628 | struct power_supply_config psy_cfg = {}; |
| 1629 | const struct of_device_id *match; |
| 1630 | struct qcom_battmgr *battmgr; |
| 1631 | struct device *dev = &adev->dev; |
| 1632 | int ret; |
| 1633 | |
| 1634 | battmgr = devm_kzalloc(dev, size: sizeof(*battmgr), GFP_KERNEL); |
| 1635 | if (!battmgr) |
| 1636 | return -ENOMEM; |
| 1637 | |
| 1638 | battmgr->dev = dev; |
| 1639 | |
| 1640 | psy_cfg.drv_data = battmgr; |
| 1641 | psy_cfg.fwnode = dev_fwnode(&adev->dev); |
| 1642 | |
| 1643 | psy_cfg_supply.drv_data = battmgr; |
| 1644 | psy_cfg_supply.fwnode = dev_fwnode(&adev->dev); |
| 1645 | psy_cfg_supply.supplied_to = qcom_battmgr_battery; |
| 1646 | psy_cfg_supply.num_supplicants = 1; |
| 1647 | |
| 1648 | INIT_WORK(&battmgr->enable_work, qcom_battmgr_enable_worker); |
| 1649 | mutex_init(&battmgr->lock); |
| 1650 | init_completion(x: &battmgr->ack); |
| 1651 | |
| 1652 | match = of_match_device(matches: qcom_battmgr_of_variants, dev: dev->parent); |
| 1653 | if (match) |
| 1654 | battmgr->variant = (unsigned long)match->data; |
| 1655 | else |
| 1656 | battmgr->variant = QCOM_BATTMGR_SM8350; |
| 1657 | |
| 1658 | ret = qcom_battmgr_charge_control_thresholds_init(battmgr); |
| 1659 | if (ret < 0) |
| 1660 | return dev_err_probe(dev, err: ret, |
| 1661 | fmt: "failed to init battery charge control thresholds\n" ); |
| 1662 | |
| 1663 | if (battmgr->variant == QCOM_BATTMGR_SC8280XP || |
| 1664 | battmgr->variant == QCOM_BATTMGR_X1E80100) { |
| 1665 | if (battmgr->variant == QCOM_BATTMGR_X1E80100) |
| 1666 | psy_desc = &x1e80100_bat_psy_desc; |
| 1667 | else |
| 1668 | psy_desc = &sc8280xp_bat_psy_desc; |
| 1669 | |
| 1670 | battmgr->bat_psy = devm_power_supply_register(parent: dev, desc: psy_desc, cfg: &psy_cfg); |
| 1671 | if (IS_ERR(ptr: battmgr->bat_psy)) |
| 1672 | return dev_err_probe(dev, err: PTR_ERR(ptr: battmgr->bat_psy), |
| 1673 | fmt: "failed to register battery power supply\n" ); |
| 1674 | |
| 1675 | battmgr->ac_psy = devm_power_supply_register(parent: dev, desc: &sc8280xp_ac_psy_desc, cfg: &psy_cfg_supply); |
| 1676 | if (IS_ERR(ptr: battmgr->ac_psy)) |
| 1677 | return dev_err_probe(dev, err: PTR_ERR(ptr: battmgr->ac_psy), |
| 1678 | fmt: "failed to register AC power supply\n" ); |
| 1679 | |
| 1680 | battmgr->usb_psy = devm_power_supply_register(parent: dev, desc: &sc8280xp_usb_psy_desc, cfg: &psy_cfg_supply); |
| 1681 | if (IS_ERR(ptr: battmgr->usb_psy)) |
| 1682 | return dev_err_probe(dev, err: PTR_ERR(ptr: battmgr->usb_psy), |
| 1683 | fmt: "failed to register USB power supply\n" ); |
| 1684 | |
| 1685 | battmgr->wls_psy = devm_power_supply_register(parent: dev, desc: &sc8280xp_wls_psy_desc, cfg: &psy_cfg_supply); |
| 1686 | if (IS_ERR(ptr: battmgr->wls_psy)) |
| 1687 | return dev_err_probe(dev, err: PTR_ERR(ptr: battmgr->wls_psy), |
| 1688 | fmt: "failed to register wireless charing power supply\n" ); |
| 1689 | } else { |
| 1690 | if (battmgr->variant == QCOM_BATTMGR_SM8550) |
| 1691 | psy_desc = &sm8550_bat_psy_desc; |
| 1692 | else |
| 1693 | psy_desc = &sm8350_bat_psy_desc; |
| 1694 | |
| 1695 | battmgr->bat_psy = devm_power_supply_register(parent: dev, desc: psy_desc, cfg: &psy_cfg); |
| 1696 | if (IS_ERR(ptr: battmgr->bat_psy)) |
| 1697 | return dev_err_probe(dev, err: PTR_ERR(ptr: battmgr->bat_psy), |
| 1698 | fmt: "failed to register battery power supply\n" ); |
| 1699 | |
| 1700 | battmgr->usb_psy = devm_power_supply_register(parent: dev, desc: &sm8350_usb_psy_desc, cfg: &psy_cfg_supply); |
| 1701 | if (IS_ERR(ptr: battmgr->usb_psy)) |
| 1702 | return dev_err_probe(dev, err: PTR_ERR(ptr: battmgr->usb_psy), |
| 1703 | fmt: "failed to register USB power supply\n" ); |
| 1704 | |
| 1705 | battmgr->wls_psy = devm_power_supply_register(parent: dev, desc: &sm8350_wls_psy_desc, cfg: &psy_cfg_supply); |
| 1706 | if (IS_ERR(ptr: battmgr->wls_psy)) |
| 1707 | return dev_err_probe(dev, err: PTR_ERR(ptr: battmgr->wls_psy), |
| 1708 | fmt: "failed to register wireless charing power supply\n" ); |
| 1709 | } |
| 1710 | |
| 1711 | battmgr->client = devm_pmic_glink_client_alloc(dev, PMIC_GLINK_OWNER_BATTMGR, |
| 1712 | cb: qcom_battmgr_callback, |
| 1713 | pdr: qcom_battmgr_pdr_notify, |
| 1714 | priv: battmgr); |
| 1715 | if (IS_ERR(ptr: battmgr->client)) |
| 1716 | return PTR_ERR(ptr: battmgr->client); |
| 1717 | |
| 1718 | pmic_glink_client_register(client: battmgr->client); |
| 1719 | |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | static const struct auxiliary_device_id qcom_battmgr_id_table[] = { |
| 1724 | { .name = "pmic_glink.power-supply" , }, |
| 1725 | {}, |
| 1726 | }; |
| 1727 | MODULE_DEVICE_TABLE(auxiliary, qcom_battmgr_id_table); |
| 1728 | |
| 1729 | static struct auxiliary_driver qcom_battmgr_driver = { |
| 1730 | .name = "pmic_glink_power_supply" , |
| 1731 | .probe = qcom_battmgr_probe, |
| 1732 | .id_table = qcom_battmgr_id_table, |
| 1733 | }; |
| 1734 | |
| 1735 | module_auxiliary_driver(qcom_battmgr_driver); |
| 1736 | |
| 1737 | MODULE_DESCRIPTION("Qualcomm PMIC GLINK battery manager driver" ); |
| 1738 | MODULE_LICENSE("GPL" ); |
| 1739 | |