xref: /linux/drivers/power/supply/qcom_battmgr.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
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  */
6 #include <linux/auxiliary_bus.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/of_device.h>
10 #include <linux/power_supply.h>
11 #include <linux/soc/qcom/pdr.h>
12 #include <linux/soc/qcom/pmic_glink.h>
13 #include <linux/math.h>
14 #include <linux/units.h>
15 
16 #define BATTMGR_CHEMISTRY_LEN	4
17 #define BATTMGR_STRING_LEN	128
18 
19 enum qcom_battmgr_variant {
20 	QCOM_BATTMGR_SM8350,
21 	QCOM_BATTMGR_SC8280XP,
22 };
23 
24 #define BATTMGR_BAT_STATUS		0x1
25 
26 #define BATTMGR_REQUEST_NOTIFICATION	0x4
27 
28 #define BATTMGR_NOTIFICATION		0x7
29 #define NOTIF_BAT_PROPERTY		0x30
30 #define NOTIF_USB_PROPERTY		0x32
31 #define NOTIF_WLS_PROPERTY		0x34
32 #define NOTIF_BAT_INFO			0x81
33 #define NOTIF_BAT_STATUS		0x80
34 
35 #define BATTMGR_BAT_INFO		0x9
36 
37 #define BATTMGR_BAT_DISCHARGE_TIME	0xc
38 
39 #define BATTMGR_BAT_CHARGE_TIME		0xd
40 
41 #define BATTMGR_BAT_PROPERTY_GET	0x30
42 #define BATTMGR_BAT_PROPERTY_SET	0x31
43 #define BATT_STATUS			0
44 #define BATT_HEALTH			1
45 #define BATT_PRESENT			2
46 #define BATT_CHG_TYPE			3
47 #define BATT_CAPACITY			4
48 #define BATT_SOH			5
49 #define BATT_VOLT_OCV			6
50 #define BATT_VOLT_NOW			7
51 #define BATT_VOLT_MAX			8
52 #define BATT_CURR_NOW			9
53 #define BATT_CHG_CTRL_LIM		10
54 #define BATT_CHG_CTRL_LIM_MAX		11
55 #define BATT_TEMP			12
56 #define BATT_TECHNOLOGY			13
57 #define BATT_CHG_COUNTER		14
58 #define BATT_CYCLE_COUNT		15
59 #define BATT_CHG_FULL_DESIGN		16
60 #define BATT_CHG_FULL			17
61 #define BATT_MODEL_NAME			18
62 #define BATT_TTF_AVG			19
63 #define BATT_TTE_AVG			20
64 #define BATT_RESISTANCE			21
65 #define BATT_POWER_NOW			22
66 #define BATT_POWER_AVG			23
67 
68 #define BATTMGR_USB_PROPERTY_GET	0x32
69 #define BATTMGR_USB_PROPERTY_SET	0x33
70 #define USB_ONLINE			0
71 #define USB_VOLT_NOW			1
72 #define USB_VOLT_MAX			2
73 #define USB_CURR_NOW			3
74 #define USB_CURR_MAX			4
75 #define USB_INPUT_CURR_LIMIT		5
76 #define USB_TYPE			6
77 #define USB_ADAP_TYPE			7
78 #define USB_MOISTURE_DET_EN		8
79 #define USB_MOISTURE_DET_STS		9
80 
81 #define BATTMGR_WLS_PROPERTY_GET	0x34
82 #define BATTMGR_WLS_PROPERTY_SET	0x35
83 #define WLS_ONLINE			0
84 #define WLS_VOLT_NOW			1
85 #define WLS_VOLT_MAX			2
86 #define WLS_CURR_NOW			3
87 #define WLS_CURR_MAX			4
88 #define WLS_TYPE			5
89 #define WLS_BOOST_EN			6
90 
91 struct qcom_battmgr_enable_request {
92 	struct pmic_glink_hdr hdr;
93 	__le32 battery_id;
94 	__le32 power_state;
95 	__le32 low_capacity;
96 	__le32 high_capacity;
97 };
98 
99 struct qcom_battmgr_property_request {
100 	struct pmic_glink_hdr hdr;
101 	__le32 battery;
102 	__le32 property;
103 	__le32 value;
104 };
105 
106 struct qcom_battmgr_update_request {
107 	struct pmic_glink_hdr hdr;
108 	__le32 battery_id;
109 };
110 
111 struct qcom_battmgr_charge_time_request {
112 	struct pmic_glink_hdr hdr;
113 	__le32 battery_id;
114 	__le32 percent;
115 	__le32 reserved;
116 };
117 
118 struct qcom_battmgr_discharge_time_request {
119 	struct pmic_glink_hdr hdr;
120 	__le32 battery_id;
121 	__le32 rate; /* 0 for current rate */
122 	__le32 reserved;
123 };
124 
125 struct qcom_battmgr_message {
126 	struct pmic_glink_hdr hdr;
127 	union {
128 		struct {
129 			__le32 property;
130 			__le32 value;
131 			__le32 result;
132 		} intval;
133 		struct {
134 			__le32 property;
135 			char model[BATTMGR_STRING_LEN];
136 		} strval;
137 		struct {
138 			/*
139 			 * 0: mWh
140 			 * 1: mAh
141 			 */
142 			__le32 power_unit;
143 			__le32 design_capacity;
144 			__le32 last_full_capacity;
145 			/*
146 			 * 0 nonrechargable
147 			 * 1 rechargable
148 			 */
149 			__le32 battery_tech;
150 			__le32 design_voltage; /* mV */
151 			__le32 capacity_low;
152 			__le32 capacity_warning;
153 			__le32 cycle_count;
154 			/* thousandth of persent */
155 			__le32 accuracy;
156 			__le32 max_sample_time_ms;
157 			__le32 min_sample_time_ms;
158 			__le32 max_average_interval_ms;
159 			__le32 min_average_interval_ms;
160 			/* granularity between low and warning */
161 			__le32 capacity_granularity1;
162 			/* granularity between warning and full */
163 			__le32 capacity_granularity2;
164 			/*
165 			 * 0: no
166 			 * 1: cold
167 			 * 2: hot
168 			 */
169 			__le32 swappable;
170 			__le32 capabilities;
171 			char model_number[BATTMGR_STRING_LEN];
172 			char serial_number[BATTMGR_STRING_LEN];
173 			char battery_type[BATTMGR_STRING_LEN];
174 			char oem_info[BATTMGR_STRING_LEN];
175 			char battery_chemistry[BATTMGR_CHEMISTRY_LEN];
176 			char uid[BATTMGR_STRING_LEN];
177 			__le32 critical_bias;
178 			u8 day;
179 			u8 month;
180 			__le16 year;
181 			__le32 battery_id;
182 		} info;
183 		struct {
184 			/*
185 			 * BIT(0) discharging
186 			 * BIT(1) charging
187 			 * BIT(2) critical low
188 			 */
189 			__le32 battery_state;
190 			/* mWh or mAh, based on info->power_unit */
191 			__le32 capacity;
192 			__le32 rate;
193 			/* mv */
194 			__le32 battery_voltage;
195 			/*
196 			 * BIT(0) power online
197 			 * BIT(1) discharging
198 			 * BIT(2) charging
199 			 * BIT(3) battery critical
200 			 */
201 			__le32 power_state;
202 			/*
203 			 * 1: AC
204 			 * 2: USB
205 			 * 3: Wireless
206 			 */
207 			__le32 charging_source;
208 			__le32 temperature;
209 		} status;
210 		__le32 time;
211 		__le32 notification;
212 	};
213 };
214 
215 #define BATTMGR_CHARGING_SOURCE_AC	1
216 #define BATTMGR_CHARGING_SOURCE_USB	2
217 #define BATTMGR_CHARGING_SOURCE_WIRELESS 3
218 
219 enum qcom_battmgr_unit {
220 	QCOM_BATTMGR_UNIT_mWh = 0,
221 	QCOM_BATTMGR_UNIT_mAh = 1
222 };
223 
224 struct qcom_battmgr_info {
225 	bool valid;
226 
227 	bool present;
228 	unsigned int charge_type;
229 	unsigned int design_capacity;
230 	unsigned int last_full_capacity;
231 	unsigned int voltage_max_design;
232 	unsigned int voltage_max;
233 	unsigned int capacity_low;
234 	unsigned int capacity_warning;
235 	unsigned int cycle_count;
236 	unsigned int charge_count;
237 	char model_number[BATTMGR_STRING_LEN];
238 	char serial_number[BATTMGR_STRING_LEN];
239 	char oem_info[BATTMGR_STRING_LEN];
240 	unsigned char technology;
241 	unsigned char day;
242 	unsigned char month;
243 	unsigned short year;
244 };
245 
246 struct qcom_battmgr_status {
247 	unsigned int status;
248 	unsigned int health;
249 	unsigned int capacity;
250 	unsigned int percent;
251 	int current_now;
252 	int power_now;
253 	unsigned int voltage_now;
254 	unsigned int voltage_ocv;
255 	unsigned int temperature;
256 
257 	unsigned int discharge_time;
258 	unsigned int charge_time;
259 };
260 
261 struct qcom_battmgr_ac {
262 	bool online;
263 };
264 
265 struct qcom_battmgr_usb {
266 	bool online;
267 	unsigned int voltage_now;
268 	unsigned int voltage_max;
269 	unsigned int current_now;
270 	unsigned int current_max;
271 	unsigned int current_limit;
272 	unsigned int usb_type;
273 };
274 
275 struct qcom_battmgr_wireless {
276 	bool online;
277 	unsigned int voltage_now;
278 	unsigned int voltage_max;
279 	unsigned int current_now;
280 	unsigned int current_max;
281 };
282 
283 struct qcom_battmgr {
284 	struct device *dev;
285 	struct auxiliary_device *adev;
286 	struct pmic_glink_client *client;
287 
288 	enum qcom_battmgr_variant variant;
289 
290 	struct power_supply *ac_psy;
291 	struct power_supply *bat_psy;
292 	struct power_supply *usb_psy;
293 	struct power_supply *wls_psy;
294 
295 	enum qcom_battmgr_unit unit;
296 
297 	int error;
298 	struct completion ack;
299 
300 	bool service_up;
301 
302 	struct qcom_battmgr_info info;
303 	struct qcom_battmgr_status status;
304 	struct qcom_battmgr_ac ac;
305 	struct qcom_battmgr_usb usb;
306 	struct qcom_battmgr_wireless wireless;
307 
308 	struct work_struct enable_work;
309 
310 	/*
311 	 * @lock is used to prevent concurrent power supply requests to the
312 	 * firmware, as it then stops responding.
313 	 */
314 	struct mutex lock;
315 };
316 
317 static int qcom_battmgr_request(struct qcom_battmgr *battmgr, void *data, size_t len)
318 {
319 	unsigned long left;
320 	int ret;
321 
322 	reinit_completion(&battmgr->ack);
323 
324 	battmgr->error = 0;
325 
326 	ret = pmic_glink_send(battmgr->client, data, len);
327 	if (ret < 0)
328 		return ret;
329 
330 	left = wait_for_completion_timeout(&battmgr->ack, HZ);
331 	if (!left)
332 		return -ETIMEDOUT;
333 
334 	return battmgr->error;
335 }
336 
337 static int qcom_battmgr_request_property(struct qcom_battmgr *battmgr, int opcode,
338 					 int property, u32 value)
339 {
340 	struct qcom_battmgr_property_request request = {
341 		.hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR),
342 		.hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP),
343 		.hdr.opcode = cpu_to_le32(opcode),
344 		.battery = cpu_to_le32(0),
345 		.property = cpu_to_le32(property),
346 		.value = cpu_to_le32(value),
347 	};
348 
349 	return qcom_battmgr_request(battmgr, &request, sizeof(request));
350 }
351 
352 static int qcom_battmgr_update_status(struct qcom_battmgr *battmgr)
353 {
354 	struct qcom_battmgr_update_request request = {
355 		.hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR),
356 		.hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP),
357 		.hdr.opcode = cpu_to_le32(BATTMGR_BAT_STATUS),
358 		.battery_id = cpu_to_le32(0),
359 	};
360 
361 	return qcom_battmgr_request(battmgr, &request, sizeof(request));
362 }
363 
364 static int qcom_battmgr_update_info(struct qcom_battmgr *battmgr)
365 {
366 	struct qcom_battmgr_update_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(BATTMGR_BAT_INFO),
370 		.battery_id = cpu_to_le32(0),
371 	};
372 
373 	return qcom_battmgr_request(battmgr, &request, sizeof(request));
374 }
375 
376 static int qcom_battmgr_update_charge_time(struct qcom_battmgr *battmgr)
377 {
378 	struct qcom_battmgr_charge_time_request request = {
379 		.hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR),
380 		.hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP),
381 		.hdr.opcode = cpu_to_le32(BATTMGR_BAT_CHARGE_TIME),
382 		.battery_id = cpu_to_le32(0),
383 		.percent = cpu_to_le32(100),
384 	};
385 
386 	return qcom_battmgr_request(battmgr, &request, sizeof(request));
387 }
388 
389 static int qcom_battmgr_update_discharge_time(struct qcom_battmgr *battmgr)
390 {
391 	struct qcom_battmgr_discharge_time_request request = {
392 		.hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR),
393 		.hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP),
394 		.hdr.opcode = cpu_to_le32(BATTMGR_BAT_DISCHARGE_TIME),
395 		.battery_id = cpu_to_le32(0),
396 		.rate = cpu_to_le32(0),
397 	};
398 
399 	return qcom_battmgr_request(battmgr, &request, sizeof(request));
400 }
401 
402 static const u8 sm8350_bat_prop_map[] = {
403 	[POWER_SUPPLY_PROP_STATUS] = BATT_STATUS,
404 	[POWER_SUPPLY_PROP_HEALTH] = BATT_HEALTH,
405 	[POWER_SUPPLY_PROP_PRESENT] = BATT_PRESENT,
406 	[POWER_SUPPLY_PROP_CHARGE_TYPE] = BATT_CHG_TYPE,
407 	[POWER_SUPPLY_PROP_CAPACITY] = BATT_CAPACITY,
408 	[POWER_SUPPLY_PROP_VOLTAGE_OCV] = BATT_VOLT_OCV,
409 	[POWER_SUPPLY_PROP_VOLTAGE_NOW] = BATT_VOLT_NOW,
410 	[POWER_SUPPLY_PROP_VOLTAGE_MAX] = BATT_VOLT_MAX,
411 	[POWER_SUPPLY_PROP_CURRENT_NOW] = BATT_CURR_NOW,
412 	[POWER_SUPPLY_PROP_TEMP] = BATT_TEMP,
413 	[POWER_SUPPLY_PROP_TECHNOLOGY] = BATT_TECHNOLOGY,
414 	[POWER_SUPPLY_PROP_CHARGE_COUNTER] =  BATT_CHG_COUNTER,
415 	[POWER_SUPPLY_PROP_CYCLE_COUNT] = BATT_CYCLE_COUNT,
416 	[POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN] =  BATT_CHG_FULL_DESIGN,
417 	[POWER_SUPPLY_PROP_CHARGE_FULL] = BATT_CHG_FULL,
418 	[POWER_SUPPLY_PROP_MODEL_NAME] = BATT_MODEL_NAME,
419 	[POWER_SUPPLY_PROP_TIME_TO_FULL_AVG] = BATT_TTF_AVG,
420 	[POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG] = BATT_TTE_AVG,
421 	[POWER_SUPPLY_PROP_POWER_NOW] = BATT_POWER_NOW,
422 };
423 
424 static int qcom_battmgr_bat_sm8350_update(struct qcom_battmgr *battmgr,
425 					  enum power_supply_property psp)
426 {
427 	unsigned int prop;
428 	int ret;
429 
430 	if (psp >= ARRAY_SIZE(sm8350_bat_prop_map))
431 		return -EINVAL;
432 
433 	prop = sm8350_bat_prop_map[psp];
434 
435 	mutex_lock(&battmgr->lock);
436 	ret = qcom_battmgr_request_property(battmgr, BATTMGR_BAT_PROPERTY_GET, prop, 0);
437 	mutex_unlock(&battmgr->lock);
438 
439 	return ret;
440 }
441 
442 static int qcom_battmgr_bat_sc8280xp_update(struct qcom_battmgr *battmgr,
443 					    enum power_supply_property psp)
444 {
445 	int ret;
446 
447 	mutex_lock(&battmgr->lock);
448 
449 	if (!battmgr->info.valid) {
450 		ret = qcom_battmgr_update_info(battmgr);
451 		if (ret < 0)
452 			goto out_unlock;
453 		battmgr->info.valid = true;
454 	}
455 
456 	ret = qcom_battmgr_update_status(battmgr);
457 	if (ret < 0)
458 		goto out_unlock;
459 
460 	if (psp == POWER_SUPPLY_PROP_TIME_TO_FULL_AVG) {
461 		ret = qcom_battmgr_update_charge_time(battmgr);
462 		if (ret < 0) {
463 			ret = -ENODATA;
464 			goto out_unlock;
465 		}
466 	}
467 
468 	if (psp == POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG) {
469 		ret = qcom_battmgr_update_discharge_time(battmgr);
470 		if (ret < 0) {
471 			ret = -ENODATA;
472 			goto out_unlock;
473 		}
474 	}
475 
476 out_unlock:
477 	mutex_unlock(&battmgr->lock);
478 	return ret;
479 }
480 
481 static int qcom_battmgr_bat_get_property(struct power_supply *psy,
482 					 enum power_supply_property psp,
483 					 union power_supply_propval *val)
484 {
485 	struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy);
486 	enum qcom_battmgr_unit unit = battmgr->unit;
487 	int ret;
488 
489 	if (!battmgr->service_up)
490 		return -ENODEV;
491 
492 	if (battmgr->variant == QCOM_BATTMGR_SC8280XP)
493 		ret = qcom_battmgr_bat_sc8280xp_update(battmgr, psp);
494 	else
495 		ret = qcom_battmgr_bat_sm8350_update(battmgr, psp);
496 	if (ret < 0)
497 		return ret;
498 
499 	switch (psp) {
500 	case POWER_SUPPLY_PROP_STATUS:
501 		val->intval = battmgr->status.status;
502 		break;
503 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
504 		val->intval = battmgr->info.charge_type;
505 		break;
506 	case POWER_SUPPLY_PROP_HEALTH:
507 		val->intval = battmgr->status.health;
508 		break;
509 	case POWER_SUPPLY_PROP_PRESENT:
510 		val->intval = battmgr->info.present;
511 		break;
512 	case POWER_SUPPLY_PROP_TECHNOLOGY:
513 		val->intval = battmgr->info.technology;
514 		break;
515 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
516 		val->intval = battmgr->info.cycle_count;
517 		break;
518 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
519 		val->intval = battmgr->info.voltage_max_design;
520 		break;
521 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
522 		val->intval = battmgr->info.voltage_max;
523 		break;
524 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
525 		val->intval = battmgr->status.voltage_now;
526 		break;
527 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
528 		val->intval = battmgr->status.voltage_ocv;
529 		break;
530 	case POWER_SUPPLY_PROP_CURRENT_NOW:
531 		val->intval = battmgr->status.current_now;
532 		break;
533 	case POWER_SUPPLY_PROP_POWER_NOW:
534 		val->intval = battmgr->status.power_now;
535 		break;
536 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
537 		if (unit != QCOM_BATTMGR_UNIT_mAh)
538 			return -ENODATA;
539 		val->intval = battmgr->info.design_capacity;
540 		break;
541 	case POWER_SUPPLY_PROP_CHARGE_FULL:
542 		if (unit != QCOM_BATTMGR_UNIT_mAh)
543 			return -ENODATA;
544 		val->intval = battmgr->info.last_full_capacity;
545 		break;
546 	case POWER_SUPPLY_PROP_CHARGE_EMPTY:
547 		if (unit != QCOM_BATTMGR_UNIT_mAh)
548 			return -ENODATA;
549 		val->intval = battmgr->info.capacity_low;
550 		break;
551 	case POWER_SUPPLY_PROP_CHARGE_NOW:
552 		if (unit != QCOM_BATTMGR_UNIT_mAh)
553 			return -ENODATA;
554 		val->intval = battmgr->status.capacity;
555 		break;
556 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
557 		val->intval = battmgr->info.charge_count;
558 		break;
559 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
560 		if (unit != QCOM_BATTMGR_UNIT_mWh)
561 			return -ENODATA;
562 		val->intval = battmgr->info.design_capacity;
563 		break;
564 	case POWER_SUPPLY_PROP_ENERGY_FULL:
565 		if (unit != QCOM_BATTMGR_UNIT_mWh)
566 			return -ENODATA;
567 		val->intval = battmgr->info.last_full_capacity;
568 		break;
569 	case POWER_SUPPLY_PROP_ENERGY_EMPTY:
570 		if (unit != QCOM_BATTMGR_UNIT_mWh)
571 			return -ENODATA;
572 		val->intval = battmgr->info.capacity_low;
573 		break;
574 	case POWER_SUPPLY_PROP_ENERGY_NOW:
575 		if (unit != QCOM_BATTMGR_UNIT_mWh)
576 			return -ENODATA;
577 		val->intval = battmgr->status.capacity;
578 		break;
579 	case POWER_SUPPLY_PROP_CAPACITY:
580 		val->intval = battmgr->status.percent;
581 		break;
582 	case POWER_SUPPLY_PROP_TEMP:
583 		val->intval = battmgr->status.temperature;
584 		break;
585 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
586 		val->intval = battmgr->status.discharge_time;
587 		break;
588 	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
589 		val->intval = battmgr->status.charge_time;
590 		break;
591 	case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
592 		val->intval = battmgr->info.year;
593 		break;
594 	case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
595 		val->intval = battmgr->info.month;
596 		break;
597 	case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
598 		val->intval = battmgr->info.day;
599 		break;
600 	case POWER_SUPPLY_PROP_MODEL_NAME:
601 		val->strval = battmgr->info.model_number;
602 		break;
603 	case POWER_SUPPLY_PROP_MANUFACTURER:
604 		val->strval = battmgr->info.oem_info;
605 		break;
606 	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
607 		val->strval = battmgr->info.serial_number;
608 		break;
609 	default:
610 		return -EINVAL;
611 	}
612 
613 	return 0;
614 }
615 
616 static const enum power_supply_property sc8280xp_bat_props[] = {
617 	POWER_SUPPLY_PROP_STATUS,
618 	POWER_SUPPLY_PROP_PRESENT,
619 	POWER_SUPPLY_PROP_TECHNOLOGY,
620 	POWER_SUPPLY_PROP_CYCLE_COUNT,
621 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
622 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
623 	POWER_SUPPLY_PROP_POWER_NOW,
624 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
625 	POWER_SUPPLY_PROP_CHARGE_FULL,
626 	POWER_SUPPLY_PROP_CHARGE_EMPTY,
627 	POWER_SUPPLY_PROP_CHARGE_NOW,
628 	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
629 	POWER_SUPPLY_PROP_ENERGY_FULL,
630 	POWER_SUPPLY_PROP_ENERGY_EMPTY,
631 	POWER_SUPPLY_PROP_ENERGY_NOW,
632 	POWER_SUPPLY_PROP_TEMP,
633 	POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
634 	POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
635 	POWER_SUPPLY_PROP_MANUFACTURE_DAY,
636 	POWER_SUPPLY_PROP_MODEL_NAME,
637 	POWER_SUPPLY_PROP_MANUFACTURER,
638 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
639 };
640 
641 static const struct power_supply_desc sc8280xp_bat_psy_desc = {
642 	.name = "qcom-battmgr-bat",
643 	.type = POWER_SUPPLY_TYPE_BATTERY,
644 	.properties = sc8280xp_bat_props,
645 	.num_properties = ARRAY_SIZE(sc8280xp_bat_props),
646 	.get_property = qcom_battmgr_bat_get_property,
647 };
648 
649 static const enum power_supply_property sm8350_bat_props[] = {
650 	POWER_SUPPLY_PROP_STATUS,
651 	POWER_SUPPLY_PROP_HEALTH,
652 	POWER_SUPPLY_PROP_PRESENT,
653 	POWER_SUPPLY_PROP_CHARGE_TYPE,
654 	POWER_SUPPLY_PROP_CAPACITY,
655 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
656 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
657 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
658 	POWER_SUPPLY_PROP_CURRENT_NOW,
659 	POWER_SUPPLY_PROP_TEMP,
660 	POWER_SUPPLY_PROP_TECHNOLOGY,
661 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
662 	POWER_SUPPLY_PROP_CYCLE_COUNT,
663 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
664 	POWER_SUPPLY_PROP_CHARGE_FULL,
665 	POWER_SUPPLY_PROP_MODEL_NAME,
666 	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
667 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
668 	POWER_SUPPLY_PROP_POWER_NOW,
669 };
670 
671 static const struct power_supply_desc sm8350_bat_psy_desc = {
672 	.name = "qcom-battmgr-bat",
673 	.type = POWER_SUPPLY_TYPE_BATTERY,
674 	.properties = sm8350_bat_props,
675 	.num_properties = ARRAY_SIZE(sm8350_bat_props),
676 	.get_property = qcom_battmgr_bat_get_property,
677 };
678 
679 static int qcom_battmgr_ac_get_property(struct power_supply *psy,
680 					enum power_supply_property psp,
681 					union power_supply_propval *val)
682 {
683 	struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy);
684 	int ret;
685 
686 	if (!battmgr->service_up)
687 		return -ENODEV;
688 
689 	ret = qcom_battmgr_bat_sc8280xp_update(battmgr, psp);
690 	if (ret)
691 		return ret;
692 
693 	switch (psp) {
694 	case POWER_SUPPLY_PROP_ONLINE:
695 		val->intval = battmgr->ac.online;
696 		break;
697 	default:
698 		return -EINVAL;
699 	}
700 
701 	return 0;
702 }
703 
704 static const enum power_supply_property sc8280xp_ac_props[] = {
705 	POWER_SUPPLY_PROP_ONLINE,
706 };
707 
708 static const struct power_supply_desc sc8280xp_ac_psy_desc = {
709 	.name = "qcom-battmgr-ac",
710 	.type = POWER_SUPPLY_TYPE_MAINS,
711 	.properties = sc8280xp_ac_props,
712 	.num_properties = ARRAY_SIZE(sc8280xp_ac_props),
713 	.get_property = qcom_battmgr_ac_get_property,
714 };
715 
716 static const u8 sm8350_usb_prop_map[] = {
717 	[POWER_SUPPLY_PROP_ONLINE] = USB_ONLINE,
718 	[POWER_SUPPLY_PROP_VOLTAGE_NOW] = USB_VOLT_NOW,
719 	[POWER_SUPPLY_PROP_VOLTAGE_MAX] = USB_VOLT_MAX,
720 	[POWER_SUPPLY_PROP_CURRENT_NOW] = USB_CURR_NOW,
721 	[POWER_SUPPLY_PROP_CURRENT_MAX] = USB_CURR_MAX,
722 	[POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT] = USB_INPUT_CURR_LIMIT,
723 	[POWER_SUPPLY_PROP_USB_TYPE] = USB_TYPE,
724 };
725 
726 static int qcom_battmgr_usb_sm8350_update(struct qcom_battmgr *battmgr,
727 					  enum power_supply_property psp)
728 {
729 	unsigned int prop;
730 	int ret;
731 
732 	if (psp >= ARRAY_SIZE(sm8350_usb_prop_map))
733 		return -EINVAL;
734 
735 	prop = sm8350_usb_prop_map[psp];
736 
737 	mutex_lock(&battmgr->lock);
738 	ret = qcom_battmgr_request_property(battmgr, BATTMGR_USB_PROPERTY_GET, prop, 0);
739 	mutex_unlock(&battmgr->lock);
740 
741 	return ret;
742 }
743 
744 static int qcom_battmgr_usb_get_property(struct power_supply *psy,
745 					 enum power_supply_property psp,
746 					 union power_supply_propval *val)
747 {
748 	struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy);
749 	int ret;
750 
751 	if (!battmgr->service_up)
752 		return -ENODEV;
753 
754 	if (battmgr->variant == QCOM_BATTMGR_SC8280XP)
755 		ret = qcom_battmgr_bat_sc8280xp_update(battmgr, psp);
756 	else
757 		ret = qcom_battmgr_usb_sm8350_update(battmgr, psp);
758 	if (ret)
759 		return ret;
760 
761 	switch (psp) {
762 	case POWER_SUPPLY_PROP_ONLINE:
763 		val->intval = battmgr->usb.online;
764 		break;
765 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
766 		val->intval = battmgr->usb.voltage_now;
767 		break;
768 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
769 		val->intval = battmgr->usb.voltage_max;
770 		break;
771 	case POWER_SUPPLY_PROP_CURRENT_NOW:
772 		val->intval = battmgr->usb.current_now;
773 		break;
774 	case POWER_SUPPLY_PROP_CURRENT_MAX:
775 		val->intval = battmgr->usb.current_max;
776 		break;
777 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
778 		val->intval = battmgr->usb.current_limit;
779 		break;
780 	case POWER_SUPPLY_PROP_USB_TYPE:
781 		val->intval = battmgr->usb.usb_type;
782 		break;
783 	default:
784 		return -EINVAL;
785 	}
786 
787 	return 0;
788 }
789 
790 static const enum power_supply_usb_type usb_psy_supported_types[] = {
791 	POWER_SUPPLY_USB_TYPE_UNKNOWN,
792 	POWER_SUPPLY_USB_TYPE_SDP,
793 	POWER_SUPPLY_USB_TYPE_DCP,
794 	POWER_SUPPLY_USB_TYPE_CDP,
795 	POWER_SUPPLY_USB_TYPE_ACA,
796 	POWER_SUPPLY_USB_TYPE_C,
797 	POWER_SUPPLY_USB_TYPE_PD,
798 	POWER_SUPPLY_USB_TYPE_PD_DRP,
799 	POWER_SUPPLY_USB_TYPE_PD_PPS,
800 	POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID,
801 };
802 
803 static const enum power_supply_property sc8280xp_usb_props[] = {
804 	POWER_SUPPLY_PROP_ONLINE,
805 };
806 
807 static const struct power_supply_desc sc8280xp_usb_psy_desc = {
808 	.name = "qcom-battmgr-usb",
809 	.type = POWER_SUPPLY_TYPE_USB,
810 	.properties = sc8280xp_usb_props,
811 	.num_properties = ARRAY_SIZE(sc8280xp_usb_props),
812 	.get_property = qcom_battmgr_usb_get_property,
813 	.usb_types = usb_psy_supported_types,
814 	.num_usb_types = ARRAY_SIZE(usb_psy_supported_types),
815 };
816 
817 static const enum power_supply_property sm8350_usb_props[] = {
818 	POWER_SUPPLY_PROP_ONLINE,
819 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
820 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
821 	POWER_SUPPLY_PROP_CURRENT_NOW,
822 	POWER_SUPPLY_PROP_CURRENT_MAX,
823 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
824 	POWER_SUPPLY_PROP_USB_TYPE,
825 };
826 
827 static const struct power_supply_desc sm8350_usb_psy_desc = {
828 	.name = "qcom-battmgr-usb",
829 	.type = POWER_SUPPLY_TYPE_USB,
830 	.properties = sm8350_usb_props,
831 	.num_properties = ARRAY_SIZE(sm8350_usb_props),
832 	.get_property = qcom_battmgr_usb_get_property,
833 	.usb_types = usb_psy_supported_types,
834 	.num_usb_types = ARRAY_SIZE(usb_psy_supported_types),
835 };
836 
837 static const u8 sm8350_wls_prop_map[] = {
838 	[POWER_SUPPLY_PROP_ONLINE] = WLS_ONLINE,
839 	[POWER_SUPPLY_PROP_VOLTAGE_NOW] = WLS_VOLT_NOW,
840 	[POWER_SUPPLY_PROP_VOLTAGE_MAX] = WLS_VOLT_MAX,
841 	[POWER_SUPPLY_PROP_CURRENT_NOW] = WLS_CURR_NOW,
842 	[POWER_SUPPLY_PROP_CURRENT_MAX] = WLS_CURR_MAX,
843 };
844 
845 static int qcom_battmgr_wls_sm8350_update(struct qcom_battmgr *battmgr,
846 					  enum power_supply_property psp)
847 {
848 	unsigned int prop;
849 	int ret;
850 
851 	if (psp >= ARRAY_SIZE(sm8350_wls_prop_map))
852 		return -EINVAL;
853 
854 	prop = sm8350_wls_prop_map[psp];
855 
856 	mutex_lock(&battmgr->lock);
857 	ret = qcom_battmgr_request_property(battmgr, BATTMGR_WLS_PROPERTY_GET, prop, 0);
858 	mutex_unlock(&battmgr->lock);
859 
860 	return ret;
861 }
862 
863 static int qcom_battmgr_wls_get_property(struct power_supply *psy,
864 					 enum power_supply_property psp,
865 					 union power_supply_propval *val)
866 {
867 	struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy);
868 	int ret;
869 
870 	if (!battmgr->service_up)
871 		return -ENODEV;
872 
873 	if (battmgr->variant == QCOM_BATTMGR_SC8280XP)
874 		ret = qcom_battmgr_bat_sc8280xp_update(battmgr, psp);
875 	else
876 		ret = qcom_battmgr_wls_sm8350_update(battmgr, psp);
877 	if (ret < 0)
878 		return ret;
879 
880 	switch (psp) {
881 	case POWER_SUPPLY_PROP_ONLINE:
882 		val->intval = battmgr->wireless.online;
883 		break;
884 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
885 		val->intval = battmgr->wireless.voltage_now;
886 		break;
887 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
888 		val->intval = battmgr->wireless.voltage_max;
889 		break;
890 	case POWER_SUPPLY_PROP_CURRENT_NOW:
891 		val->intval = battmgr->wireless.current_now;
892 		break;
893 	case POWER_SUPPLY_PROP_CURRENT_MAX:
894 		val->intval = battmgr->wireless.current_max;
895 		break;
896 	default:
897 		return -EINVAL;
898 	}
899 
900 	return 0;
901 }
902 
903 static const enum power_supply_property sc8280xp_wls_props[] = {
904 	POWER_SUPPLY_PROP_ONLINE,
905 };
906 
907 static const struct power_supply_desc sc8280xp_wls_psy_desc = {
908 	.name = "qcom-battmgr-wls",
909 	.type = POWER_SUPPLY_TYPE_WIRELESS,
910 	.properties = sc8280xp_wls_props,
911 	.num_properties = ARRAY_SIZE(sc8280xp_wls_props),
912 	.get_property = qcom_battmgr_wls_get_property,
913 };
914 
915 static const enum power_supply_property sm8350_wls_props[] = {
916 	POWER_SUPPLY_PROP_ONLINE,
917 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
918 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
919 	POWER_SUPPLY_PROP_CURRENT_NOW,
920 	POWER_SUPPLY_PROP_CURRENT_MAX,
921 };
922 
923 static const struct power_supply_desc sm8350_wls_psy_desc = {
924 	.name = "qcom-battmgr-wls",
925 	.type = POWER_SUPPLY_TYPE_WIRELESS,
926 	.properties = sm8350_wls_props,
927 	.num_properties = ARRAY_SIZE(sm8350_wls_props),
928 	.get_property = qcom_battmgr_wls_get_property,
929 };
930 
931 static void qcom_battmgr_notification(struct qcom_battmgr *battmgr,
932 				      const struct qcom_battmgr_message *msg,
933 				      int len)
934 {
935 	size_t payload_len = len - sizeof(struct pmic_glink_hdr);
936 	unsigned int notification;
937 
938 	if (payload_len != sizeof(msg->notification)) {
939 		dev_warn(battmgr->dev, "ignoring notification with invalid length\n");
940 		return;
941 	}
942 
943 	notification = le32_to_cpu(msg->notification);
944 	switch (notification) {
945 	case NOTIF_BAT_INFO:
946 		battmgr->info.valid = false;
947 		fallthrough;
948 	case NOTIF_BAT_STATUS:
949 	case NOTIF_BAT_PROPERTY:
950 		power_supply_changed(battmgr->bat_psy);
951 		break;
952 	case NOTIF_USB_PROPERTY:
953 		power_supply_changed(battmgr->usb_psy);
954 		break;
955 	case NOTIF_WLS_PROPERTY:
956 		power_supply_changed(battmgr->wls_psy);
957 		break;
958 	default:
959 		dev_err(battmgr->dev, "unknown notification: %#x\n", notification);
960 		break;
961 	}
962 }
963 
964 static void qcom_battmgr_sc8280xp_strcpy(char *dest, const char *src)
965 {
966 	size_t len = src[0];
967 
968 	/* Some firmware versions return Pascal-style strings */
969 	if (len < BATTMGR_STRING_LEN && len == strnlen(src + 1, BATTMGR_STRING_LEN - 1)) {
970 		memcpy(dest, src + 1, len);
971 		dest[len] = '\0';
972 	} else {
973 		memcpy(dest, src, BATTMGR_STRING_LEN);
974 	}
975 }
976 
977 static unsigned int qcom_battmgr_sc8280xp_parse_technology(const char *chemistry)
978 {
979 	if (!strncmp(chemistry, "LIO", BATTMGR_CHEMISTRY_LEN))
980 		return POWER_SUPPLY_TECHNOLOGY_LION;
981 
982 	pr_err("Unknown battery technology '%s'\n", chemistry);
983 	return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
984 }
985 
986 static unsigned int qcom_battmgr_sc8280xp_convert_temp(unsigned int temperature)
987 {
988 	return DIV_ROUND_CLOSEST(temperature, 10);
989 }
990 
991 static void qcom_battmgr_sc8280xp_callback(struct qcom_battmgr *battmgr,
992 					   const struct qcom_battmgr_message *resp,
993 					   size_t len)
994 {
995 	unsigned int opcode = le32_to_cpu(resp->hdr.opcode);
996 	unsigned int source;
997 	unsigned int state;
998 	size_t payload_len = len - sizeof(struct pmic_glink_hdr);
999 
1000 	if (payload_len < sizeof(__le32)) {
1001 		dev_warn(battmgr->dev, "invalid payload length for %#x: %zd\n",
1002 			 opcode, len);
1003 		return;
1004 	}
1005 
1006 	switch (opcode) {
1007 	case BATTMGR_REQUEST_NOTIFICATION:
1008 		battmgr->error = 0;
1009 		break;
1010 	case BATTMGR_BAT_INFO:
1011 		if (payload_len != sizeof(resp->info)) {
1012 			dev_warn(battmgr->dev,
1013 				 "invalid payload length for battery information request: %zd\n",
1014 				 payload_len);
1015 			battmgr->error = -ENODATA;
1016 			return;
1017 		}
1018 
1019 		battmgr->unit = le32_to_cpu(resp->info.power_unit);
1020 
1021 		battmgr->info.present = true;
1022 		battmgr->info.design_capacity = le32_to_cpu(resp->info.design_capacity) * 1000;
1023 		battmgr->info.last_full_capacity = le32_to_cpu(resp->info.last_full_capacity) * 1000;
1024 		battmgr->info.voltage_max_design = le32_to_cpu(resp->info.design_voltage) * 1000;
1025 		battmgr->info.capacity_low = le32_to_cpu(resp->info.capacity_low) * 1000;
1026 		battmgr->info.cycle_count = le32_to_cpu(resp->info.cycle_count);
1027 		qcom_battmgr_sc8280xp_strcpy(battmgr->info.model_number, resp->info.model_number);
1028 		qcom_battmgr_sc8280xp_strcpy(battmgr->info.serial_number, resp->info.serial_number);
1029 		battmgr->info.technology = qcom_battmgr_sc8280xp_parse_technology(resp->info.battery_chemistry);
1030 		qcom_battmgr_sc8280xp_strcpy(battmgr->info.oem_info, resp->info.oem_info);
1031 		battmgr->info.day = resp->info.day;
1032 		battmgr->info.month = resp->info.month;
1033 		battmgr->info.year = le16_to_cpu(resp->info.year);
1034 		break;
1035 	case BATTMGR_BAT_STATUS:
1036 		if (payload_len != sizeof(resp->status)) {
1037 			dev_warn(battmgr->dev,
1038 				 "invalid payload length for battery status request: %zd\n",
1039 				 payload_len);
1040 			battmgr->error = -ENODATA;
1041 			return;
1042 		}
1043 
1044 		state = le32_to_cpu(resp->status.battery_state);
1045 		if (state & BIT(0))
1046 			battmgr->status.status = POWER_SUPPLY_STATUS_DISCHARGING;
1047 		else if (state & BIT(1))
1048 			battmgr->status.status = POWER_SUPPLY_STATUS_CHARGING;
1049 		else
1050 			battmgr->status.status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1051 
1052 		battmgr->status.capacity = le32_to_cpu(resp->status.capacity) * 1000;
1053 		battmgr->status.power_now = le32_to_cpu(resp->status.rate) * 1000;
1054 		battmgr->status.voltage_now = le32_to_cpu(resp->status.battery_voltage) * 1000;
1055 		battmgr->status.temperature = qcom_battmgr_sc8280xp_convert_temp(le32_to_cpu(resp->status.temperature));
1056 
1057 		source = le32_to_cpu(resp->status.charging_source);
1058 		battmgr->ac.online = source == BATTMGR_CHARGING_SOURCE_AC;
1059 		battmgr->usb.online = source == BATTMGR_CHARGING_SOURCE_USB;
1060 		battmgr->wireless.online = source == BATTMGR_CHARGING_SOURCE_WIRELESS;
1061 		break;
1062 	case BATTMGR_BAT_DISCHARGE_TIME:
1063 		battmgr->status.discharge_time = le32_to_cpu(resp->time);
1064 		break;
1065 	case BATTMGR_BAT_CHARGE_TIME:
1066 		battmgr->status.charge_time = le32_to_cpu(resp->time);
1067 		break;
1068 	default:
1069 		dev_warn(battmgr->dev, "unknown message %#x\n", opcode);
1070 		break;
1071 	}
1072 
1073 	complete(&battmgr->ack);
1074 }
1075 
1076 static void qcom_battmgr_sm8350_callback(struct qcom_battmgr *battmgr,
1077 					 const struct qcom_battmgr_message *resp,
1078 					 size_t len)
1079 {
1080 	unsigned int property;
1081 	unsigned int opcode = le32_to_cpu(resp->hdr.opcode);
1082 	size_t payload_len = len - sizeof(struct pmic_glink_hdr);
1083 	unsigned int val;
1084 
1085 	if (payload_len < sizeof(__le32)) {
1086 		dev_warn(battmgr->dev, "invalid payload length for %#x: %zd\n",
1087 			 opcode, len);
1088 		return;
1089 	}
1090 
1091 	switch (opcode) {
1092 	case BATTMGR_BAT_PROPERTY_GET:
1093 		property = le32_to_cpu(resp->intval.property);
1094 		if (property == BATT_MODEL_NAME) {
1095 			if (payload_len != sizeof(resp->strval)) {
1096 				dev_warn(battmgr->dev,
1097 					 "invalid payload length for BATT_MODEL_NAME request: %zd\n",
1098 					 payload_len);
1099 				battmgr->error = -ENODATA;
1100 				return;
1101 			}
1102 		} else {
1103 			if (payload_len != sizeof(resp->intval)) {
1104 				dev_warn(battmgr->dev,
1105 					 "invalid payload length for %#x request: %zd\n",
1106 					 property, payload_len);
1107 				battmgr->error = -ENODATA;
1108 				return;
1109 			}
1110 
1111 			battmgr->error = le32_to_cpu(resp->intval.result);
1112 			if (battmgr->error)
1113 				goto out_complete;
1114 		}
1115 
1116 		switch (property) {
1117 		case BATT_STATUS:
1118 			battmgr->status.status = le32_to_cpu(resp->intval.value);
1119 			break;
1120 		case BATT_HEALTH:
1121 			battmgr->status.health = le32_to_cpu(resp->intval.value);
1122 			break;
1123 		case BATT_PRESENT:
1124 			battmgr->info.present = le32_to_cpu(resp->intval.value);
1125 			break;
1126 		case BATT_CHG_TYPE:
1127 			battmgr->info.charge_type = le32_to_cpu(resp->intval.value);
1128 			break;
1129 		case BATT_CAPACITY:
1130 			battmgr->status.percent = le32_to_cpu(resp->intval.value) / 100;
1131 			break;
1132 		case BATT_VOLT_OCV:
1133 			battmgr->status.voltage_ocv = le32_to_cpu(resp->intval.value);
1134 			break;
1135 		case BATT_VOLT_NOW:
1136 			battmgr->status.voltage_now = le32_to_cpu(resp->intval.value);
1137 			break;
1138 		case BATT_VOLT_MAX:
1139 			battmgr->info.voltage_max = le32_to_cpu(resp->intval.value);
1140 			break;
1141 		case BATT_CURR_NOW:
1142 			battmgr->status.current_now = le32_to_cpu(resp->intval.value);
1143 			break;
1144 		case BATT_TEMP:
1145 			val = le32_to_cpu(resp->intval.value);
1146 			battmgr->status.temperature = DIV_ROUND_CLOSEST(val, 10);
1147 			break;
1148 		case BATT_TECHNOLOGY:
1149 			battmgr->info.technology = le32_to_cpu(resp->intval.value);
1150 			break;
1151 		case BATT_CHG_COUNTER:
1152 			battmgr->info.charge_count = le32_to_cpu(resp->intval.value);
1153 			break;
1154 		case BATT_CYCLE_COUNT:
1155 			battmgr->info.cycle_count = le32_to_cpu(resp->intval.value);
1156 			break;
1157 		case BATT_CHG_FULL_DESIGN:
1158 			battmgr->info.design_capacity = le32_to_cpu(resp->intval.value);
1159 			break;
1160 		case BATT_CHG_FULL:
1161 			battmgr->info.last_full_capacity = le32_to_cpu(resp->intval.value);
1162 			break;
1163 		case BATT_MODEL_NAME:
1164 			strscpy(battmgr->info.model_number, resp->strval.model, BATTMGR_STRING_LEN);
1165 			break;
1166 		case BATT_TTF_AVG:
1167 			battmgr->status.charge_time = le32_to_cpu(resp->intval.value);
1168 			break;
1169 		case BATT_TTE_AVG:
1170 			battmgr->status.discharge_time = le32_to_cpu(resp->intval.value);
1171 			break;
1172 		case BATT_POWER_NOW:
1173 			battmgr->status.power_now = le32_to_cpu(resp->intval.value);
1174 			break;
1175 		default:
1176 			dev_warn(battmgr->dev, "unknown property %#x\n", property);
1177 			break;
1178 		}
1179 		break;
1180 	case BATTMGR_USB_PROPERTY_GET:
1181 		property = le32_to_cpu(resp->intval.property);
1182 		if (payload_len != sizeof(resp->intval)) {
1183 			dev_warn(battmgr->dev,
1184 				 "invalid payload length for %#x request: %zd\n",
1185 				 property, payload_len);
1186 			battmgr->error = -ENODATA;
1187 			return;
1188 		}
1189 
1190 		battmgr->error = le32_to_cpu(resp->intval.result);
1191 		if (battmgr->error)
1192 			goto out_complete;
1193 
1194 		switch (property) {
1195 		case USB_ONLINE:
1196 			battmgr->usb.online = le32_to_cpu(resp->intval.value);
1197 			break;
1198 		case USB_VOLT_NOW:
1199 			battmgr->usb.voltage_now = le32_to_cpu(resp->intval.value);
1200 			break;
1201 		case USB_VOLT_MAX:
1202 			battmgr->usb.voltage_max = le32_to_cpu(resp->intval.value);
1203 			break;
1204 		case USB_CURR_NOW:
1205 			battmgr->usb.current_now = le32_to_cpu(resp->intval.value);
1206 			break;
1207 		case USB_CURR_MAX:
1208 			battmgr->usb.current_max = le32_to_cpu(resp->intval.value);
1209 			break;
1210 		case USB_INPUT_CURR_LIMIT:
1211 			battmgr->usb.current_limit = le32_to_cpu(resp->intval.value);
1212 			break;
1213 		case USB_TYPE:
1214 			battmgr->usb.usb_type = le32_to_cpu(resp->intval.value);
1215 			break;
1216 		default:
1217 			dev_warn(battmgr->dev, "unknown property %#x\n", property);
1218 			break;
1219 		}
1220 		break;
1221 	case BATTMGR_WLS_PROPERTY_GET:
1222 		property = le32_to_cpu(resp->intval.property);
1223 		if (payload_len != sizeof(resp->intval)) {
1224 			dev_warn(battmgr->dev,
1225 				 "invalid payload length for %#x request: %zd\n",
1226 				 property, payload_len);
1227 			battmgr->error = -ENODATA;
1228 			return;
1229 		}
1230 
1231 		battmgr->error = le32_to_cpu(resp->intval.result);
1232 		if (battmgr->error)
1233 			goto out_complete;
1234 
1235 		switch (property) {
1236 		case WLS_ONLINE:
1237 			battmgr->wireless.online = le32_to_cpu(resp->intval.value);
1238 			break;
1239 		case WLS_VOLT_NOW:
1240 			battmgr->wireless.voltage_now = le32_to_cpu(resp->intval.value);
1241 			break;
1242 		case WLS_VOLT_MAX:
1243 			battmgr->wireless.voltage_max = le32_to_cpu(resp->intval.value);
1244 			break;
1245 		case WLS_CURR_NOW:
1246 			battmgr->wireless.current_now = le32_to_cpu(resp->intval.value);
1247 			break;
1248 		case WLS_CURR_MAX:
1249 			battmgr->wireless.current_max = le32_to_cpu(resp->intval.value);
1250 			break;
1251 		default:
1252 			dev_warn(battmgr->dev, "unknown property %#x\n", property);
1253 			break;
1254 		}
1255 		break;
1256 	case BATTMGR_REQUEST_NOTIFICATION:
1257 		battmgr->error = 0;
1258 		break;
1259 	default:
1260 		dev_warn(battmgr->dev, "unknown message %#x\n", opcode);
1261 		break;
1262 	}
1263 
1264 out_complete:
1265 	complete(&battmgr->ack);
1266 }
1267 
1268 static void qcom_battmgr_callback(const void *data, size_t len, void *priv)
1269 {
1270 	const struct pmic_glink_hdr *hdr = data;
1271 	struct qcom_battmgr *battmgr = priv;
1272 	unsigned int opcode = le32_to_cpu(hdr->opcode);
1273 
1274 	if (opcode == BATTMGR_NOTIFICATION)
1275 		qcom_battmgr_notification(battmgr, data, len);
1276 	else if (battmgr->variant == QCOM_BATTMGR_SC8280XP)
1277 		qcom_battmgr_sc8280xp_callback(battmgr, data, len);
1278 	else
1279 		qcom_battmgr_sm8350_callback(battmgr, data, len);
1280 }
1281 
1282 static void qcom_battmgr_enable_worker(struct work_struct *work)
1283 {
1284 	struct qcom_battmgr *battmgr = container_of(work, struct qcom_battmgr, enable_work);
1285 	struct qcom_battmgr_enable_request req = {
1286 		.hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_BATTMGR),
1287 		.hdr.type = cpu_to_le32(PMIC_GLINK_NOTIFY),
1288 		.hdr.opcode = cpu_to_le32(BATTMGR_REQUEST_NOTIFICATION),
1289 	};
1290 	int ret;
1291 
1292 	ret = qcom_battmgr_request(battmgr, &req, sizeof(req));
1293 	if (ret)
1294 		dev_err(battmgr->dev, "failed to request power notifications\n");
1295 }
1296 
1297 static char *qcom_battmgr_battery[] = { "battery" };
1298 
1299 static void qcom_battmgr_register_psy(struct qcom_battmgr *battmgr)
1300 {
1301 	struct power_supply_config psy_cfg_supply = {};
1302 	struct auxiliary_device *adev = battmgr->adev;
1303 	struct power_supply_config psy_cfg = {};
1304 	struct device *dev = &adev->dev;
1305 
1306 	psy_cfg.drv_data = battmgr;
1307 	psy_cfg.of_node = adev->dev.of_node;
1308 
1309 	psy_cfg_supply.drv_data = battmgr;
1310 	psy_cfg_supply.of_node = adev->dev.of_node;
1311 	psy_cfg_supply.supplied_to = qcom_battmgr_battery;
1312 	psy_cfg_supply.num_supplicants = 1;
1313 
1314 	if (battmgr->variant == QCOM_BATTMGR_SC8280XP) {
1315 		battmgr->bat_psy = devm_power_supply_register(dev, &sc8280xp_bat_psy_desc, &psy_cfg);
1316 		if (IS_ERR(battmgr->bat_psy))
1317 			dev_err(dev, "failed to register battery power supply (%ld)\n",
1318 				PTR_ERR(battmgr->bat_psy));
1319 
1320 		battmgr->ac_psy = devm_power_supply_register(dev, &sc8280xp_ac_psy_desc, &psy_cfg_supply);
1321 		if (IS_ERR(battmgr->ac_psy))
1322 			dev_err(dev, "failed to register AC power supply (%ld)\n",
1323 				PTR_ERR(battmgr->ac_psy));
1324 
1325 		battmgr->usb_psy = devm_power_supply_register(dev, &sc8280xp_usb_psy_desc, &psy_cfg_supply);
1326 		if (IS_ERR(battmgr->usb_psy))
1327 			dev_err(dev, "failed to register USB power supply (%ld)\n",
1328 				PTR_ERR(battmgr->usb_psy));
1329 
1330 		battmgr->wls_psy = devm_power_supply_register(dev, &sc8280xp_wls_psy_desc, &psy_cfg_supply);
1331 		if (IS_ERR(battmgr->wls_psy))
1332 			dev_err(dev, "failed to register wireless charing power supply (%ld)\n",
1333 				PTR_ERR(battmgr->wls_psy));
1334 	} else {
1335 		battmgr->bat_psy = devm_power_supply_register(dev, &sm8350_bat_psy_desc, &psy_cfg);
1336 		if (IS_ERR(battmgr->bat_psy))
1337 			dev_err(dev, "failed to register battery power supply (%ld)\n",
1338 				PTR_ERR(battmgr->bat_psy));
1339 
1340 		battmgr->usb_psy = devm_power_supply_register(dev, &sm8350_usb_psy_desc, &psy_cfg_supply);
1341 		if (IS_ERR(battmgr->usb_psy))
1342 			dev_err(dev, "failed to register USB power supply (%ld)\n",
1343 				PTR_ERR(battmgr->usb_psy));
1344 
1345 		battmgr->wls_psy = devm_power_supply_register(dev, &sm8350_wls_psy_desc, &psy_cfg_supply);
1346 		if (IS_ERR(battmgr->wls_psy))
1347 			dev_err(dev, "failed to register wireless charing power supply (%ld)\n",
1348 				PTR_ERR(battmgr->wls_psy));
1349 	}
1350 }
1351 
1352 static void qcom_battmgr_pdr_notify(void *priv, int state)
1353 {
1354 	struct qcom_battmgr *battmgr = priv;
1355 
1356 	if (state == SERVREG_SERVICE_STATE_UP) {
1357 		if (!battmgr->bat_psy)
1358 			qcom_battmgr_register_psy(battmgr);
1359 
1360 		battmgr->service_up = true;
1361 		schedule_work(&battmgr->enable_work);
1362 	} else {
1363 		battmgr->service_up = false;
1364 	}
1365 }
1366 
1367 static const struct of_device_id qcom_battmgr_of_variants[] = {
1368 	{ .compatible = "qcom,sc8180x-pmic-glink", .data = (void *)QCOM_BATTMGR_SC8280XP },
1369 	{ .compatible = "qcom,sc8280xp-pmic-glink", .data = (void *)QCOM_BATTMGR_SC8280XP },
1370 	/* Unmatched devices falls back to QCOM_BATTMGR_SM8350 */
1371 	{}
1372 };
1373 
1374 static int qcom_battmgr_probe(struct auxiliary_device *adev,
1375 			      const struct auxiliary_device_id *id)
1376 {
1377 	const struct of_device_id *match;
1378 	struct qcom_battmgr *battmgr;
1379 	struct device *dev = &adev->dev;
1380 
1381 	battmgr = devm_kzalloc(dev, sizeof(*battmgr), GFP_KERNEL);
1382 	if (!battmgr)
1383 		return -ENOMEM;
1384 
1385 	battmgr->dev = dev;
1386 	battmgr->adev = adev;
1387 
1388 	INIT_WORK(&battmgr->enable_work, qcom_battmgr_enable_worker);
1389 	mutex_init(&battmgr->lock);
1390 	init_completion(&battmgr->ack);
1391 
1392 	match = of_match_device(qcom_battmgr_of_variants, dev->parent);
1393 	if (match)
1394 		battmgr->variant = (unsigned long)match->data;
1395 	else
1396 		battmgr->variant = QCOM_BATTMGR_SM8350;
1397 
1398 	battmgr->client = devm_pmic_glink_register_client(dev,
1399 							  PMIC_GLINK_OWNER_BATTMGR,
1400 							  qcom_battmgr_callback,
1401 							  qcom_battmgr_pdr_notify,
1402 							  battmgr);
1403 	return PTR_ERR_OR_ZERO(battmgr->client);
1404 }
1405 
1406 static const struct auxiliary_device_id qcom_battmgr_id_table[] = {
1407 	{ .name = "pmic_glink.power-supply", },
1408 	{},
1409 };
1410 MODULE_DEVICE_TABLE(auxiliary, qcom_battmgr_id_table);
1411 
1412 static struct auxiliary_driver qcom_battmgr_driver = {
1413 	.name = "pmic_glink_power_supply",
1414 	.probe = qcom_battmgr_probe,
1415 	.id_table = qcom_battmgr_id_table,
1416 };
1417 
1418 module_auxiliary_driver(qcom_battmgr_driver);
1419 
1420 MODULE_DESCRIPTION("Qualcomm PMIC GLINK battery manager driver");
1421 MODULE_LICENSE("GPL");
1422