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