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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
/*
* drivers/power/nvec_battery.c
*
* Battery driver for batteries connected to NVIDIA NvEc-compliant embedded
* controller
*
* Copyright (c) 2009, NVIDIA Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/debugfs.h>
#include <linux/power_supply.h>
#include <linux/wakelock.h>
#include <linux/tegra_devices.h>
#include "nvcommon.h"
#include "nvos.h"
#include "nvodm_battery.h"
#include "nvec_device.h"
/* This defines the manufacturer name and model name length */
#define BATTERY_INFO_NAME_LEN 30
#define GET_CHARGER_STATUS 1
#define NVBATTERY_POLLING_INTERVAL 30000 /* 30 Seconds */
typedef enum
{
NvCharger_Type_Battery = 0,
NvCharger_Type_USB,
NvCharger_Type_AC,
NvCharger_Type_Num,
NvCharger_Type_Force32 = 0x7FFFFFFF
} NvCharger_Type;
typedef enum
{
NvCharge_Control_Charging_Disable = 0,
NvCharge_Control_Charging_Enable,
NvCharge_Control_Num,
NvCharge_Control_Force32 = 0x7FFFFFFF
} NvCharge_Control;
static enum power_supply_property tegra_battery_properties[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_TECHNOLOGY,
POWER_SUPPLY_PROP_CAPACITY,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_CURRENT_AVG,
POWER_SUPPLY_PROP_CHARGE_NOW,
POWER_SUPPLY_PROP_CHARGE_FULL,
POWER_SUPPLY_PROP_CHARGE_EMPTY,
POWER_SUPPLY_PROP_TEMP,
// POWER_SUPPLY_PROP_MODEL_NAME,
// POWER_SUPPLY_PROP_MANUFACTURER,
};
static enum power_supply_property tegra_power_properties[] = {
POWER_SUPPLY_PROP_ONLINE,
};
static char *supply_list[] = {
"battery",
};
static int tegra_power_get_property(struct power_supply *psy,
enum power_supply_property psp, union power_supply_propval *val);
static int tegra_battery_get_property(struct power_supply *psy,
enum power_supply_property psp, union power_supply_propval *val);
static struct power_supply tegra_power_supplies[] = {
{
.name = "battery",
.type = POWER_SUPPLY_TYPE_BATTERY,
.properties = tegra_battery_properties,
.num_properties = ARRAY_SIZE(tegra_battery_properties),
.get_property = tegra_battery_get_property,
},
{
.name = "usb",
.type = POWER_SUPPLY_TYPE_USB,
.supplied_to = supply_list,
.num_supplicants = ARRAY_SIZE(supply_list),
.properties = tegra_power_properties,
.num_properties = ARRAY_SIZE(tegra_power_properties),
.get_property = tegra_power_get_property,
},
{
.name = "ac",
.type = POWER_SUPPLY_TYPE_MAINS,
.supplied_to = supply_list,
.num_supplicants = ARRAY_SIZE(supply_list),
.properties = tegra_power_properties,
.num_properties = ARRAY_SIZE(tegra_power_properties),
.get_property = tegra_power_get_property,
},
};
/* This is the Battery context structure */
struct tegra_battery_dev {
struct timer_list battery_poll_timer;
NvOdmBatteryDeviceHandle hOdmBattDev;
NvU32 batt_id;
NvU32 voltage; /* voltage */
NvU32 temp; /* Temperature */
NvU32 current_ma; /* Battery current */
NvU32 current_avg; /* average current */
NvU32 charging_source; /* 0: no cable, 1:usb, 2:AC */
NvU32 charging_enabled; /* 0: Disable, 1: Enable */
NvU32 capacity; /* full capacity of battery (mAh) */
NvU32 capacity_crit; /* critical capacity level */
NvU32 capacity_remain; /* remaining battery capacity */
NvU32 percent_remain; /* percentage of battery remaining */
NvU32 lifetime;
NvU32 consumed;
NvU32 batt_status_poll_period;
NvBool ac_status;
NvBool present;
};
static struct tegra_battery_dev *batt_dev;
static ssize_t tegra_battery_show_property(
struct device *dev,
struct device_attribute *attr,
char *buf)
{
return sprintf(buf, "%d\n", batt_dev->batt_status_poll_period);
}
static ssize_t tegra_battery_store_property(
struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
{
unsigned int value = 0;
value = simple_strtoul(buf, NULL, 0);
batt_dev->batt_status_poll_period = value;
mod_timer(&(batt_dev->battery_poll_timer),
jiffies + msecs_to_jiffies(batt_dev->batt_status_poll_period));
return count;
}
static struct device_attribute tegra_battery_attr = {
.attr = { .name = "status_poll_period", .mode = S_IRUGO | S_IWUSR,
.owner = THIS_MODULE },
.show = tegra_battery_show_property,
.store = tegra_battery_store_property,
};
static void tegra_get_battery_tech(int *val, NvOdmBatteryInstance inst)
{
NvOdmBatteryChemistry chemistry = NvOdmBatteryChemistry_Num;
NvOdmBatteryGetBatteryChemistry(batt_dev->hOdmBattDev,
inst, &chemistry);
switch(chemistry) {
case NvOdmBatteryChemistry_NICD:
*val = POWER_SUPPLY_TECHNOLOGY_NiCd;
break;
case NvOdmBatteryChemistry_NIMH:
*val = POWER_SUPPLY_TECHNOLOGY_NiMH;
break;
case NvOdmBatteryChemistry_LION:
*val = POWER_SUPPLY_TECHNOLOGY_LION;
break;
case NvOdmBatteryChemistry_LIPOLY:
*val = POWER_SUPPLY_TECHNOLOGY_LIPO;
break;
case NvOdmBatteryChemistry_XINCAIR:
case NvOdmBatteryChemistry_Alkaline:
default:
*val = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
break;
}
}
static void tegra_battery_convert(NvOdmBatteryData *data)
{
if (data->BatteryLifePercent == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryLifePercent = 0;
if (data->BatteryLifeTime == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryLifeTime = 0;
if (data->BatteryVoltage == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryVoltage = 0;
if (data->BatteryCurrent == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryCurrent = 0;
if (data->BatteryMahConsumed == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryMahConsumed = 0;
if (data->BatteryTemperature == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryTemperature = 0;
if (data->BatteryAverageCurrent == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryAverageCurrent = 0;
if (data->BatteryLastChargeFullCapacity == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryLastChargeFullCapacity = 0;
if (data->BatteryCriticalCapacity == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryCriticalCapacity = 0;
if (data->BatteryRemainingCapacity == NVODM_BATTERY_DATA_UNKNOWN)
data->BatteryRemainingCapacity = 0;
}
static NvBool tegra_battery_data(NvOdmBatteryInstance NvBatteryInst)
{
NvOdmBatteryData data = {0};
if (!NvOdmBatteryGetBatteryData(batt_dev->hOdmBattDev,
NvBatteryInst, &data))
return NV_FALSE;
tegra_battery_convert(&data);
if (NvBatteryInst == NvOdmBatteryInst_Main) {
batt_dev->voltage = data.BatteryVoltage;
batt_dev->current_ma = data.BatteryCurrent;
batt_dev->current_avg = data.BatteryAverageCurrent;
batt_dev->temp = data.BatteryTemperature;
batt_dev->percent_remain = data.BatteryLifePercent;
batt_dev->lifetime = data.BatteryLifeTime;
batt_dev->consumed = data.BatteryMahConsumed;
batt_dev->capacity = data.BatteryLastChargeFullCapacity;
batt_dev->capacity_crit = data.BatteryCriticalCapacity;
batt_dev->capacity_remain = data.BatteryRemainingCapacity;
}
return NV_TRUE;
}
static int tegra_power_get_property(struct power_supply *psy,
enum power_supply_property psp, union power_supply_propval *val)
{
NvCharger_Type charger = 0;
NvOdmBatteryAcLineStatus status = NvOdmBatteryAcLine_Offline;
/* Need to find out the way which tell the charger source */
switch (psp) {
case POWER_SUPPLY_PROP_ONLINE:
if (!NvOdmBatteryGetAcLineStatus(batt_dev->hOdmBattDev,
&status))
return -ENODEV;
if (status == NvOdmBatteryAcLine_Offline) {
batt_dev->ac_status = NV_FALSE;
}
else if (status == NvOdmBatteryAcLine_Online) {
batt_dev->ac_status = NV_TRUE;
charger = NvCharger_Type_AC;
}
else
batt_dev->ac_status = NV_FALSE;
if (psy->type == POWER_SUPPLY_TYPE_MAINS)
val->intval = (charger == NvCharger_Type_AC);
else if (psy->type == POWER_SUPPLY_TYPE_USB)
val->intval = (charger == NvCharger_Type_USB);
else
val->intval = 0;
break;
default:
return -EINVAL;
}
return 0;
}
static int tegra_battery_get_property(struct power_supply *psy,
enum power_supply_property psp, union power_supply_propval *val)
{
NvU8 name[BATTERY_INFO_NAME_LEN] = {0};
int technology = 0;
NvU8 state = 0;
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
/* TODO:Get Charger status here */
#if GET_CHARGER_STATUS
if (!NvOdmBatteryGetBatteryStatus(batt_dev->hOdmBattDev,
NvOdmBatteryInst_Main, &state))
return -ENODEV;
if (state == NVODM_BATTERY_STATUS_UNKNOWN) {
batt_dev->present = NV_FALSE;
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
} else if (state == NVODM_BATTERY_STATUS_NO_BATTERY) {
batt_dev->present = NV_FALSE;
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
} else if (state & NVODM_BATTERY_STATUS_CHARGING) {
batt_dev->present = NV_TRUE;
val->intval = POWER_SUPPLY_STATUS_CHARGING;
/* TODO:Get Charger status here */
} else if (state & NVODM_BATTERY_STATUS_HIGH) {
batt_dev->present = NV_TRUE;
val->intval = POWER_SUPPLY_STATUS_FULL;
/* TODO:Get Charger status here */
} else
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
#endif
/* Getting the battery info once here so for the other property
* requests there will not be lot of ec req */
if (!tegra_battery_data(NvOdmBatteryInst_Main)) {
; /* FIXME: return error? */
}
break;
case POWER_SUPPLY_PROP_HEALTH:
if (batt_dev->present)
val->intval = POWER_SUPPLY_HEALTH_GOOD;
else
val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
break;
case POWER_SUPPLY_PROP_PRESENT:
if (!NvOdmBatteryGetBatteryStatus(batt_dev->hOdmBattDev,
NvOdmBatteryInst_Main, &state))
return -EINVAL;
if (state == NVODM_BATTERY_STATUS_UNKNOWN) {
batt_dev->present = NV_FALSE;
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
} else {
if (state == NVODM_BATTERY_STATUS_NO_BATTERY) {
batt_dev->present = NV_FALSE;
val->intval = NV_FALSE;
}
if (state & (NVODM_BATTERY_STATUS_HIGH |
NVODM_BATTERY_STATUS_LOW |
NVODM_BATTERY_STATUS_CRITICAL |
NVODM_BATTERY_STATUS_CHARGING)) {
batt_dev->present = NV_TRUE;
val->intval = NV_TRUE;
}
}
break;
case POWER_SUPPLY_PROP_TECHNOLOGY:
tegra_get_battery_tech(&technology, NvOdmBatteryInst_Main);
val->intval = technology;
break;
case POWER_SUPPLY_PROP_CAPACITY:
val->intval = batt_dev->percent_remain;
break;
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
val->intval = batt_dev->voltage*1000;
break;
case POWER_SUPPLY_PROP_CURRENT_NOW:
val->intval = batt_dev->current_ma;
break;
case POWER_SUPPLY_PROP_CURRENT_AVG:
val->intval = batt_dev->current_avg;
break;
case POWER_SUPPLY_PROP_CHARGE_NOW:
val->intval = batt_dev->capacity_remain;
break;
case POWER_SUPPLY_PROP_CHARGE_FULL:
val->intval = batt_dev->capacity;
break;
case POWER_SUPPLY_PROP_CHARGE_EMPTY:
val->intval = batt_dev->capacity_crit;
break;
case POWER_SUPPLY_PROP_TEMP:
/* returned value is degrees C * 10 */
val->intval = batt_dev->temp/10;
break;
case POWER_SUPPLY_PROP_MODEL_NAME:
if (!NvOdmBatteryGetModel(batt_dev->hOdmBattDev,
NvOdmBatteryInst_Main, name))
return -EINVAL;
strncpy((char *)val->strval, name, strlen(name));
break;
case POWER_SUPPLY_PROP_MANUFACTURER:
if (!NvOdmBatteryGetManufacturer(batt_dev->hOdmBattDev,
NvOdmBatteryInst_Main, name))
return -EINVAL;
strncpy((char *)val->strval, name, strlen(name));
break;
default:
return -EINVAL;
}
return 0;
}
static void tegra_battery_poll_timer_func(unsigned long unused)
{
power_supply_changed(&tegra_power_supplies[NvCharger_Type_Battery]);
power_supply_changed(&tegra_power_supplies[NvCharger_Type_USB]);
power_supply_changed(&tegra_power_supplies[NvCharger_Type_AC]);
mod_timer(&(batt_dev->battery_poll_timer),
jiffies + msecs_to_jiffies(batt_dev->batt_status_poll_period));
}
static int nvec_battery_probe(struct nvec_device *pdev)
{
int i, rc;
NvBool result = NV_FALSE;
batt_dev = kzalloc(sizeof(struct tegra_battery_dev), GFP_KERNEL);
if (!batt_dev) {
pr_err("nvec_battery_probe:NOMEM\n");
return -ENOMEM;
}
result = NvOdmBatteryDeviceOpen(&(batt_dev->hOdmBattDev), NULL);
if (!result || !batt_dev->hOdmBattDev) {
kfree(batt_dev);
batt_dev = NULL;
pr_err("NvOdmBatteryDeviceOpen FAILED\n");
return -1;
}
for (i = 0; i < ARRAY_SIZE(tegra_power_supplies); i++) {
if (power_supply_register(&pdev->dev, &tegra_power_supplies[i]))
pr_err("Failed to register power supply\n");
}
batt_dev->batt_status_poll_period = NVBATTERY_POLLING_INTERVAL;
setup_timer(&(batt_dev->battery_poll_timer), tegra_battery_poll_timer_func, 0);
mod_timer(&(batt_dev->battery_poll_timer),
jiffies + msecs_to_jiffies(batt_dev->batt_status_poll_period));
rc = device_create_file(&pdev->dev, &tegra_battery_attr);
if (rc) {
NvOdmBatteryDeviceClose(batt_dev->hOdmBattDev);
batt_dev->hOdmBattDev = NULL;
for (i = 0; i < ARRAY_SIZE(tegra_power_supplies); i++) {
power_supply_unregister(&tegra_power_supplies[i]);
}
del_timer_sync(&(batt_dev->battery_poll_timer));
kfree(batt_dev);
batt_dev = NULL;
pr_err("nvec_battery_probe:device_create_file FAILED");
return rc;
}
return 0;
}
static int nvec_battery_remove(struct nvec_device *pdev)
{
unsigned int i = 0;
if (batt_dev) {
if (batt_dev->hOdmBattDev) {
device_remove_file(&pdev->dev, &tegra_battery_attr);
del_timer_sync(&(batt_dev->battery_poll_timer));
NvOdmBatteryDeviceClose(batt_dev->hOdmBattDev);
batt_dev->hOdmBattDev = NULL;
for (i = 0; i < ARRAY_SIZE(tegra_power_supplies); i++) {
power_supply_unregister(&tegra_power_supplies[i]);
}
}
kfree(batt_dev);
batt_dev = NULL;
}
return 0;
}
static int nvec_battery_suspend(struct nvec_device *dev,
pm_message_t state)
{
/* Kill the Battery Polling timer */
del_timer_sync(&batt_dev->battery_poll_timer);
return 0;
}
static int nvec_battery_resume(struct nvec_device *dev)
{
/*Create Battery Polling timer */
setup_timer(&batt_dev->battery_poll_timer, tegra_battery_poll_timer_func, 0);
mod_timer(&batt_dev->battery_poll_timer,
jiffies + msecs_to_jiffies(batt_dev->batt_status_poll_period));
return 0;
}
static struct nvec_driver nvec_battery_driver = {
.name = "nvec_battery",
.probe = nvec_battery_probe,
.remove = nvec_battery_remove,
.suspend = nvec_battery_suspend,
.resume = nvec_battery_resume,
};
static struct nvec_device nvec_battery_device = {
.name = "nvec_battery",
.driver = &nvec_battery_driver,
};
static int __init nvec_battery_init(void)
{
int err;
err = nvec_register_driver(&nvec_battery_driver);
if (err)
{
pr_err("**nvec_battery_init: nvec_register_driver: fail\n");
return err;
}
err = nvec_register_device(&nvec_battery_device);
if (err)
{
pr_err("**nvec_battery_init: nvec_device_add: fail\n");
nvec_unregister_driver(&nvec_battery_driver);
return err;
}
return 0;
}
static void __exit nvec_battery_exit(void)
{
nvec_unregister_device(&nvec_battery_device);
nvec_unregister_driver(&nvec_battery_driver);
}
module_init(nvec_battery_init);
module_exit(nvec_battery_exit);
MODULE_DESCRIPTION("TEGRA EC based Battery Driver");
|