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
|
/*
* Samsung Mobile VE Group.
*
* drivers/battery/sec_batt_selfdchg_ic_use.c
*
* Drivers for samsung batter self discharging with IC.
*
* Copyright (C) 2015, Samsung Electronics.
*
* This program is free software. You can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation
*/
/******************************************/
#include <linux/sec_batt_selfdchg_common.h>
/******************************************/
// Samsung Custom Header
#ifdef CONFIG_BATTERY_SAMSUNG
#include <linux/battery/sec_battery.h>
#endif
/******************************************/
static struct sdchg_info_t *sdchg_info;
static char sdchg_type[] = "sdchg_ic";
static void sdchg_ic_use_parse_dt(struct device *dev)
{
struct device_node *np = dev->of_node;
int ret;
u32 temp;
sdchg_info->chip->factory_discharging =
of_get_named_gpio(np, "battery,factory_discharging", 0);
if (sdchg_info->chip->factory_discharging < 0) {
pr_info("%s : Force Discharging pin is Empty(%d)\n",
__func__, sdchg_info->chip->factory_discharging);
sdchg_info->chip->factory_discharging = 0;
}
/**************************************/
sdchg_info->chip->sdchg_en = of_property_read_bool(np,
"battery,self_discharging_en");
/**************************************/
ret = of_property_read_u32(np, "battery,force_discharging_limit",
&temp);
sdchg_info->temp_start = (int)temp;
if (ret)
pr_info("%s : Force Discharging limit is Empty", __func__);
/**************************************/
ret = of_property_read_u32(np, "battery,force_discharging_recov",
&temp);
sdchg_info->temp_end = (int)temp;
if (ret)
pr_info("%s : Force Discharging recov is Empty", __func__);
/**************************************/
pr_info("%s : FORCE_DISCHARGING_LIMT(%d), FORCE_DISCHARGING_RECOV(%d)\n",
__func__, sdchg_info->temp_start, sdchg_info->temp_end);
/**************************************/
ret = of_property_read_u32(np, "battery,discharging_adc_min",
(unsigned int *)&sdchg_info->chip->adc_min);
if (ret)
pr_info("%s : Discharging ADC Min is Empty", __func__);
/**************************************/
ret = of_property_read_u32(np, "battery,discharging_adc_max",
(unsigned int *)&sdchg_info->chip->adc_max);
if (ret)
pr_info("%s : Discharging ADC Max is Empty", __func__);
/**************************************/
ret = of_property_read_u32(np, "battery,self_discharging_voltage_limit",
(unsigned int *)&sdchg_info->voltage_start);
if (ret)
pr_info("%s : Force Discharging recov is Empty", __func__);
/**************************************/
// voltage_end : using swelling_drop_float_voltage value
if (of_property_read_u32(np, "battery,swelling_drop_float_voltage",
&sdchg_info->voltage_end))
pr_err("%s : Force Discharging voltage recov is Empty", __func__);
/**************************************/
ret = of_property_read_u32(np, "battery,discharging_ntc_limit",
(unsigned int *)&sdchg_info->chip->ntc_limit);
if (ret)
pr_info("%s : Discharging NTC LIMIT is Empty", __func__);
/**************************************/
return;
}
// prev func : sec_bat_self_discharging_check
extern int sec_bat_get_adc_value_using_sdchg
(struct sec_battery_info *battery, int channel);
static void sdchg_ic_use_adc_check(void *arg)
{
struct sec_battery_info *battery = (struct sec_battery_info *)arg;
unsigned int dis_adc;
dis_adc = sec_bat_get_adc_value_using_sdchg
(battery, SEC_BAT_ADC_CHANNEL_DISCHARGING_CHECK);
if (dis_adc)
battery->self_discharging_adc = dis_adc;
else
battery->self_discharging_adc = 0;
if ((dis_adc >= (int)sdchg_info->chip->adc_min) &&
(dis_adc <= (int)sdchg_info->chip->adc_max))
battery->self_discharging = true;
else
battery->self_discharging = false;
pr_info("%s : SELF_DISCHARGING(%d) SELF_DISCHARGING_ADC(%d)\n",
__func__, battery->self_discharging, battery->self_discharging_adc);
return;
}
// prev func : sec_bat_self_discharging_ntc_check
static void sdchg_ic_use_ntc_check(void *arg)
{
struct sec_battery_info *battery = (struct sec_battery_info *)arg;
int ntc_adc;
ntc_adc = sec_bat_get_adc_value_using_sdchg
(battery, SEC_BAT_ADC_CHANNEL_DISCHARGING_NTC);
if (ntc_adc)
battery->discharging_ntc_adc = ntc_adc;
else
battery->discharging_ntc_adc = 0;
if (ntc_adc > (int)sdchg_info->chip->ntc_limit)
battery->discharging_ntc = true;
else
battery->discharging_ntc = false;
pr_info("%s : DISCHARGING_NTC(%d) DISCHARGING_NTC_ADC(%d)\n",
__func__,battery->discharging_ntc, battery->discharging_ntc_adc);
return;
}
// prev func : sec_bat_self_discharging_control
static void sdchg_ic_use_force_control(void *arg, bool dis_en)
{
struct sec_battery_info *battery = (struct sec_battery_info *)arg;
if (!sdchg_info->chip->factory_discharging) {
pr_info("Can't control Self Discharging IC (No Factory Discharging Pin).\n");
return;
}
if (dis_en) {
dev_info(battery->dev,
"%s : Self Discharging IC doesn't act until (%d) degree & (%d) voltage. "
"Auto Discharging IC ENABLE\n", __func__,
battery->temperature, battery->voltage_now);
gpio_direction_output(sdchg_info->chip->factory_discharging, 1);
battery->force_discharging = true;
} else {
dev_info(battery->dev, "%s : Self Discharging IC disable.\n", __func__);
gpio_direction_output(sdchg_info->chip->factory_discharging, 0);
battery->force_discharging = false;
}
return;
}
// prev func : sec_bat_discharging_check
static void sdchg_ic_use_discharging_check(void *arg)
{
struct sec_battery_info *battery = (struct sec_battery_info *)arg;
if (!sdchg_info->chip->sdchg_en) {
pr_info("[SDCHG][%s] --(sdchg_en false)\n", __func__);
return;
}
sdchg_ic_use_adc_check(battery);
if(battery->factory_self_discharging_mode_on) {
dev_info(battery->dev,
"%s: It is Factory mode by self discharging mode, Auto_DIS(%d), Force_DIS(%d)\n",
__func__, battery->self_discharging, battery->force_discharging);
return;
}
if (!battery->self_discharging &&
(battery->temperature >= (int)sdchg_info->temp_start) &&
(battery->voltage_now >= (int)sdchg_info->voltage_start)) {
sdchg_ic_use_force_control((void *)battery, true);
} else if(battery->force_discharging &&
((battery->temperature <= (int)sdchg_info->temp_end) ||
(battery->voltage_now <= (int)sdchg_info->voltage_end))) {
sdchg_ic_use_force_control((void *)battery, false);
}
dev_info(battery->dev,
"%s: Auto_DIS(%d), Force_DIS(%d)\n",
__func__, battery->self_discharging, battery->force_discharging);
return;
}
static int sdchg_ic_use_force_check(void *arg)
{
int ret = 0;
if (!sdchg_info->chip->factory_discharging) {
pr_info("Can't control Self Discharging IC (No Factory Discharging Pin).\n");
goto out;
}
ret = gpio_get_value(sdchg_info->chip->factory_discharging);
out:
return ret;
}
static int sdchg_ic_use_probe(void *battery)
{
int ret = 0;
sdchg_info->battery = battery;
if (sdchg_info->chip->factory_discharging) {
ret = gpio_request((unsigned)sdchg_info->chip->factory_discharging,
"FACTORY_DISCHARGING");
if (ret) {
pr_err("[SDCHG][%s] failed to request GPIO %d\n",
__func__, sdchg_info->chip->factory_discharging);
goto err_gpio;
}
}
err_gpio:
return ret;
}
static int sdchg_ic_use_remove(void)
{
if (sdchg_info->chip->factory_discharging)
gpio_free(sdchg_info->chip->factory_discharging);
return 0;
}
static int __init sdchg_ic_use_init(void)
{
int ret;
pr_info("[SDCHG][%s] ++\n", __func__);
sdchg_info = kzalloc(sizeof(struct sdchg_info_t), GFP_KERNEL);
if (!sdchg_info) {
pr_err("Failed to allocate memory for self discharging\n");
ret = -ENOMEM;
goto fail_out;
}
sdchg_info->type = sdchg_type;
/*****************************************/
sdchg_info->chip = kzalloc(sizeof(struct sdchg_info_chip_t), GFP_KERNEL);
if (!sdchg_info) {
pr_err("Failed to allocate nochip memory for self discharging\n");
ret = -ENOMEM;
goto after_alloc_info;
}
sdchg_info->chip->pinfo = sdchg_info;
/*****************************************/
sdchg_info->sdchg_probe = sdchg_ic_use_probe;
sdchg_info->sdchg_remove = sdchg_ic_use_remove;
sdchg_info->sdchg_parse_dt = sdchg_ic_use_parse_dt;
sdchg_info->sdchg_adc_check = sdchg_ic_use_adc_check;
sdchg_info->sdchg_ntc_check = sdchg_ic_use_ntc_check;
sdchg_info->sdchg_force_control = sdchg_ic_use_force_control;
sdchg_info->sdchg_discharging_check = sdchg_ic_use_discharging_check;
sdchg_info->sdchg_force_check = sdchg_ic_use_force_check;
/*****************************************/
list_add(&sdchg_info->info_list, &sdchg_info_head);
pr_info("[SDCHG][%s] --\n", __func__);
return 0;
after_alloc_info:
if (sdchg_info) {
kfree(sdchg_info);
sdchg_info = NULL;
}
fail_out:
pr_info("[SDCHG][%s] --(%d)\n", __func__, ret);
return ret;
}
static void __exit sdchg_ic_use_exit(void)
{
pr_info("[SDCHG][%s] ++\n", __func__);
if (sdchg_info) {
if (sdchg_info->chip) {
kfree(sdchg_info->chip);
sdchg_info->chip = NULL;
}
kfree(sdchg_info);
sdchg_info = NULL;
}
pr_info("[SDCHG][%s] --\n", __func__);
return;
}
arch_initcall(sdchg_ic_use_init);
module_exit(sdchg_ic_use_exit);
MODULE_AUTHOR("jeeon.park@samsung.com");
MODULE_DESCRIPTION("Samsung Electronics Co. Battery Self Discharging \
for Preventing Swelling with Self Discharging IC");
MODULE_LICENSE("GPL");
|