1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2022-2024, Linaro Ltd
* Authors:
* Bjorn Andersson
* Dmitry Baryshkov
*/
#include <linux/auxiliary_bus.h>
#include <linux/bits.h>
#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/notifier.h>
#include <linux/power_supply.h>
#include <linux/platform_data/lenovo-yoga-c630.h>
struct yoga_c630_psy {
struct yoga_c630_ec *ec;
struct device *dev;
struct fwnode_handle *fwnode;
struct notifier_block nb;
/* guards all battery properties and registration of power supplies */
struct mutex lock;
struct power_supply *adp_psy;
struct power_supply *bat_psy;
unsigned long last_status_update;
bool adapter_online;
bool unit_mA;
bool bat_present;
unsigned int bat_status;
unsigned int design_capacity;
unsigned int design_voltage;
unsigned int full_charge_capacity;
unsigned int capacity_now;
unsigned int voltage_now;
int current_now;
int rate_now;
};
#define LENOVO_EC_CACHE_TIME (10 * HZ)
#define LENOVO_EC_ADPT_STATUS 0xa3
#define LENOVO_EC_ADPT_STATUS_PRESENT BIT(7)
#define LENOVO_EC_BAT_ATTRIBUTES 0xc0
#define LENOVO_EC_BAT_ATTRIBUTES_UNIT_IS_MA BIT(1)
#define LENOVO_EC_BAT_STATUS 0xc1
#define LENOVO_EC_BAT_STATUS_DISCHARGING BIT(0)
#define LENOVO_EC_BAT_STATUS_CHARGING BIT(1)
#define LENOVO_EC_BAT_REMAIN_CAPACITY 0xc2
#define LENOVO_EC_BAT_VOLTAGE 0xc6
#define LENOVO_EC_BAT_DESIGN_VOLTAGE 0xc8
#define LENOVO_EC_BAT_DESIGN_CAPACITY 0xca
#define LENOVO_EC_BAT_FULL_CAPACITY 0xcc
#define LENOVO_EC_BAT_CURRENT 0xd2
#define LENOVO_EC_BAT_FULL_FACTORY 0xd6
#define LENOVO_EC_BAT_PRESENT 0xda
#define LENOVO_EC_BAT_PRESENT_IS_PRESENT BIT(0)
#define LENOVO_EC_BAT_FULL_REGISTER 0xdb
#define LENOVO_EC_BAT_FULL_REGISTER_IS_FACTORY BIT(0)
static int yoga_c630_psy_update_bat_info(struct yoga_c630_psy *ecbat)
{
struct yoga_c630_ec *ec = ecbat->ec;
int val;
lockdep_assert_held(&ecbat->lock);
val = yoga_c630_ec_read8(ec, LENOVO_EC_BAT_PRESENT);
if (val < 0)
return val;
ecbat->bat_present = !!(val & LENOVO_EC_BAT_PRESENT_IS_PRESENT);
if (!ecbat->bat_present)
return val;
val = yoga_c630_ec_read8(ec, LENOVO_EC_BAT_ATTRIBUTES);
if (val < 0)
return val;
ecbat->unit_mA = val & LENOVO_EC_BAT_ATTRIBUTES_UNIT_IS_MA;
val = yoga_c630_ec_read16(ec, LENOVO_EC_BAT_DESIGN_CAPACITY);
if (val < 0)
return val;
ecbat->design_capacity = val * 1000;
/*
* DSDT has delays after most of EC reads in these methods.
* Having no documentation for the EC we have to follow and sleep here.
*/
msleep(50);
val = yoga_c630_ec_read16(ec, LENOVO_EC_BAT_DESIGN_VOLTAGE);
if (val < 0)
return val;
ecbat->design_voltage = val;
msleep(50);
val = yoga_c630_ec_read8(ec, LENOVO_EC_BAT_FULL_REGISTER);
if (val < 0)
return val;
val = yoga_c630_ec_read16(ec,
val & LENOVO_EC_BAT_FULL_REGISTER_IS_FACTORY ?
LENOVO_EC_BAT_FULL_FACTORY :
LENOVO_EC_BAT_FULL_CAPACITY);
if (val < 0)
return val;
ecbat->full_charge_capacity = val * 1000;
if (!ecbat->unit_mA) {
ecbat->design_capacity *= 10;
ecbat->full_charge_capacity *= 10;
}
return 0;
}
static int yoga_c630_psy_maybe_update_bat_status(struct yoga_c630_psy *ecbat)
{
struct yoga_c630_ec *ec = ecbat->ec;
int current_mA;
int val;
guard(mutex)(&ecbat->lock);
if (time_before(jiffies, ecbat->last_status_update + LENOVO_EC_CACHE_TIME))
return 0;
val = yoga_c630_ec_read8(ec, LENOVO_EC_BAT_STATUS);
if (val < 0)
return val;
ecbat->bat_status = val;
msleep(50);
val = yoga_c630_ec_read16(ec, LENOVO_EC_BAT_REMAIN_CAPACITY);
if (val < 0)
return val;
ecbat->capacity_now = val * 1000;
msleep(50);
val = yoga_c630_ec_read16(ec, LENOVO_EC_BAT_VOLTAGE);
if (val < 0)
return val;
ecbat->voltage_now = val * 1000;
msleep(50);
val = yoga_c630_ec_read16(ec, LENOVO_EC_BAT_CURRENT);
if (val < 0)
return val;
current_mA = sign_extend32(val, 15);
ecbat->current_now = current_mA * 1000;
ecbat->rate_now = current_mA * (ecbat->voltage_now / 1000);
msleep(50);
if (!ecbat->unit_mA)
ecbat->capacity_now *= 10;
ecbat->last_status_update = jiffies;
return 0;
}
static int yoga_c630_psy_update_adapter_status(struct yoga_c630_psy *ecbat)
{
struct yoga_c630_ec *ec = ecbat->ec;
int val;
guard(mutex)(&ecbat->lock);
val = yoga_c630_ec_read8(ec, LENOVO_EC_ADPT_STATUS);
if (val < 0)
return val;
ecbat->adapter_online = !!(val & LENOVO_EC_ADPT_STATUS_PRESENT);
return 0;
}
static bool yoga_c630_psy_is_charged(struct yoga_c630_psy *ecbat)
{
if (ecbat->bat_status != 0)
return false;
if (ecbat->full_charge_capacity <= ecbat->capacity_now)
return true;
if (ecbat->design_capacity <= ecbat->capacity_now)
return true;
return false;
}
static int yoga_c630_psy_bat_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct yoga_c630_psy *ecbat = power_supply_get_drvdata(psy);
int rc = 0;
if (!ecbat->bat_present && psp != POWER_SUPPLY_PROP_PRESENT)
return -ENODEV;
rc = yoga_c630_psy_maybe_update_bat_status(ecbat);
if (rc)
return rc;
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
if (ecbat->bat_status & LENOVO_EC_BAT_STATUS_DISCHARGING)
val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
else if (ecbat->bat_status & LENOVO_EC_BAT_STATUS_CHARGING)
val->intval = POWER_SUPPLY_STATUS_CHARGING;
else if (yoga_c630_psy_is_charged(ecbat))
val->intval = POWER_SUPPLY_STATUS_FULL;
else
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
break;
case POWER_SUPPLY_PROP_PRESENT:
val->intval = ecbat->bat_present;
break;
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
val->intval = ecbat->design_voltage;
break;
case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
val->intval = ecbat->design_capacity;
break;
case POWER_SUPPLY_PROP_CHARGE_FULL:
case POWER_SUPPLY_PROP_ENERGY_FULL:
val->intval = ecbat->full_charge_capacity;
break;
case POWER_SUPPLY_PROP_CHARGE_NOW:
case POWER_SUPPLY_PROP_ENERGY_NOW:
val->intval = ecbat->capacity_now;
break;
case POWER_SUPPLY_PROP_CURRENT_NOW:
val->intval = ecbat->current_now;
break;
case POWER_SUPPLY_PROP_POWER_NOW:
val->intval = ecbat->rate_now;
break;
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
val->intval = ecbat->voltage_now;
break;
case POWER_SUPPLY_PROP_TECHNOLOGY:
val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
break;
case POWER_SUPPLY_PROP_MODEL_NAME:
val->strval = "PABAS0241231";
break;
case POWER_SUPPLY_PROP_MANUFACTURER:
val->strval = "Compal";
break;
case POWER_SUPPLY_PROP_SCOPE:
val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
break;
default:
rc = -EINVAL;
break;
}
return rc;
}
static enum power_supply_property yoga_c630_psy_bat_mA_properties[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
POWER_SUPPLY_PROP_CHARGE_FULL,
POWER_SUPPLY_PROP_CHARGE_NOW,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_POWER_NOW,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_TECHNOLOGY,
POWER_SUPPLY_PROP_MODEL_NAME,
POWER_SUPPLY_PROP_MANUFACTURER,
POWER_SUPPLY_PROP_SCOPE,
};
static enum power_supply_property yoga_c630_psy_bat_mWh_properties[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
POWER_SUPPLY_PROP_ENERGY_FULL,
POWER_SUPPLY_PROP_ENERGY_NOW,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_POWER_NOW,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_TECHNOLOGY,
POWER_SUPPLY_PROP_MODEL_NAME,
POWER_SUPPLY_PROP_MANUFACTURER,
POWER_SUPPLY_PROP_SCOPE,
};
static const struct power_supply_desc yoga_c630_psy_bat_psy_desc_mA = {
.name = "yoga-c630-battery",
.type = POWER_SUPPLY_TYPE_BATTERY,
.properties = yoga_c630_psy_bat_mA_properties,
.num_properties = ARRAY_SIZE(yoga_c630_psy_bat_mA_properties),
.get_property = yoga_c630_psy_bat_get_property,
};
static const struct power_supply_desc yoga_c630_psy_bat_psy_desc_mWh = {
.name = "yoga-c630-battery",
.type = POWER_SUPPLY_TYPE_BATTERY,
.properties = yoga_c630_psy_bat_mWh_properties,
.num_properties = ARRAY_SIZE(yoga_c630_psy_bat_mWh_properties),
.get_property = yoga_c630_psy_bat_get_property,
};
static int yoga_c630_psy_adpt_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct yoga_c630_psy *ecbat = power_supply_get_drvdata(psy);
int ret = 0;
ret = yoga_c630_psy_update_adapter_status(ecbat);
if (ret < 0)
return ret;
switch (psp) {
case POWER_SUPPLY_PROP_ONLINE:
val->intval = ecbat->adapter_online;
break;
case POWER_SUPPLY_PROP_USB_TYPE:
val->intval = POWER_SUPPLY_USB_TYPE_C;
break;
default:
return -EINVAL;
}
return 0;
}
static enum power_supply_property yoga_c630_psy_adpt_properties[] = {
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_USB_TYPE,
};
static const enum power_supply_usb_type yoga_c630_psy_adpt_usb_type[] = {
POWER_SUPPLY_USB_TYPE_C,
};
static const struct power_supply_desc yoga_c630_psy_adpt_psy_desc = {
.name = "yoga-c630-adapter",
.type = POWER_SUPPLY_TYPE_USB,
.usb_types = yoga_c630_psy_adpt_usb_type,
.num_usb_types = ARRAY_SIZE(yoga_c630_psy_adpt_usb_type),
.properties = yoga_c630_psy_adpt_properties,
.num_properties = ARRAY_SIZE(yoga_c630_psy_adpt_properties),
.get_property = yoga_c630_psy_adpt_get_property,
};
static int yoga_c630_psy_register_bat_psy(struct yoga_c630_psy *ecbat)
{
struct power_supply_config bat_cfg = {};
bat_cfg.drv_data = ecbat;
bat_cfg.fwnode = ecbat->fwnode;
ecbat->bat_psy = power_supply_register_no_ws(ecbat->dev,
ecbat->unit_mA ?
&yoga_c630_psy_bat_psy_desc_mA :
&yoga_c630_psy_bat_psy_desc_mWh,
&bat_cfg);
if (IS_ERR(ecbat->bat_psy)) {
dev_err(ecbat->dev, "failed to register battery supply\n");
return PTR_ERR(ecbat->bat_psy);
}
return 0;
}
static void yoga_c630_ec_refresh_bat_info(struct yoga_c630_psy *ecbat)
{
bool current_unit;
guard(mutex)(&ecbat->lock);
current_unit = ecbat->unit_mA;
yoga_c630_psy_update_bat_info(ecbat);
if (current_unit != ecbat->unit_mA) {
power_supply_unregister(ecbat->bat_psy);
yoga_c630_psy_register_bat_psy(ecbat);
}
}
static int yoga_c630_psy_notify(struct notifier_block *nb,
unsigned long action, void *data)
{
struct yoga_c630_psy *ecbat = container_of(nb, struct yoga_c630_psy, nb);
switch (action) {
case LENOVO_EC_EVENT_BAT_INFO:
yoga_c630_ec_refresh_bat_info(ecbat);
break;
case LENOVO_EC_EVENT_BAT_ADPT_STATUS:
power_supply_changed(ecbat->adp_psy);
fallthrough;
case LENOVO_EC_EVENT_BAT_STATUS:
power_supply_changed(ecbat->bat_psy);
break;
}
return NOTIFY_OK;
}
static int yoga_c630_psy_probe(struct auxiliary_device *adev,
const struct auxiliary_device_id *id)
{
struct yoga_c630_ec *ec = adev->dev.platform_data;
struct power_supply_config adp_cfg = {};
struct device *dev = &adev->dev;
struct yoga_c630_psy *ecbat;
int ret;
ecbat = devm_kzalloc(&adev->dev, sizeof(*ecbat), GFP_KERNEL);
if (!ecbat)
return -ENOMEM;
ecbat->ec = ec;
ecbat->dev = dev;
mutex_init(&ecbat->lock);
ecbat->fwnode = adev->dev.parent->fwnode;
ecbat->nb.notifier_call = yoga_c630_psy_notify;
auxiliary_set_drvdata(adev, ecbat);
adp_cfg.drv_data = ecbat;
adp_cfg.fwnode = ecbat->fwnode;
adp_cfg.supplied_to = (char **)&yoga_c630_psy_bat_psy_desc_mA.name;
adp_cfg.num_supplicants = 1;
ecbat->adp_psy = devm_power_supply_register_no_ws(dev, &yoga_c630_psy_adpt_psy_desc, &adp_cfg);
if (IS_ERR(ecbat->adp_psy)) {
dev_err(dev, "failed to register AC adapter supply\n");
return PTR_ERR(ecbat->adp_psy);
}
scoped_guard(mutex, &ecbat->lock) {
ret = yoga_c630_psy_update_bat_info(ecbat);
if (ret)
goto err_unreg_bat;
ret = yoga_c630_psy_register_bat_psy(ecbat);
if (ret)
goto err_unreg_bat;
}
ret = yoga_c630_ec_register_notify(ecbat->ec, &ecbat->nb);
if (ret)
goto err_unreg_bat;
return 0;
err_unreg_bat:
power_supply_unregister(ecbat->bat_psy);
return ret;
}
static void yoga_c630_psy_remove(struct auxiliary_device *adev)
{
struct yoga_c630_psy *ecbat = auxiliary_get_drvdata(adev);
yoga_c630_ec_unregister_notify(ecbat->ec, &ecbat->nb);
power_supply_unregister(ecbat->bat_psy);
}
static const struct auxiliary_device_id yoga_c630_psy_id_table[] = {
{ .name = YOGA_C630_MOD_NAME "." YOGA_C630_DEV_PSY, },
{}
};
MODULE_DEVICE_TABLE(auxiliary, yoga_c630_psy_id_table);
static struct auxiliary_driver yoga_c630_psy_driver = {
.name = YOGA_C630_DEV_PSY,
.id_table = yoga_c630_psy_id_table,
.probe = yoga_c630_psy_probe,
.remove = yoga_c630_psy_remove,
};
module_auxiliary_driver(yoga_c630_psy_driver);
MODULE_DESCRIPTION("Lenovo Yoga C630 psy");
MODULE_LICENSE("GPL");
|