drm/prime: add return check for dma_buf_fd
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / battery / sec_dt_init.c
1 /*
2  *  sec_dt_init.c
3  *  Samsung battery data parsing from device tree
4  *
5  *  Copyright (C) 2014 Samsung Electronics
6  *
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13
14 #include <linux/of_device.h>
15 #include <linux/of_gpio.h>
16 #include <linux/of_irq.h>
17 #include <soc/sprd/adc.h>
18 #include <linux/battery/sec_battery.h>
19
20 #include <soc/sprd/sci_glb_regs.h>
21 #include <soc/sprd/sci.h>
22 #include <soc/sprd/adi.h>
23 #include <soc/sprd/arch_misc.h>
24 #include <soc/sprd/hardware.h>
25 #include <linux/delay.h>
26
27 #ifdef CONFIG_MFD_RT8973
28 #include <linux/mfd/rt8973.h>
29 #endif
30 #ifdef CONFIG_SM5502_MUIC
31 #include <linux/mfd/sm5502-muic.h>
32 #endif
33 #ifdef CONFIG_MFD_SM5504
34 #include <linux/mfd/sm5504.h>
35 #endif
36 #ifdef CONFIG_FUELGAUGE_SPRD4SAMSUNG27X3
37 #include <linux/battery/fuelgauge/sprd27x3_fuelgauge4samsung.h>
38 #endif
39
40 #define BATT_DET_CURRENT 313
41
42 static int current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
43 static u8 attached_cable;
44 static bool is_jig_on;
45 /* static unsigned int lpcharge; */
46 /* EXPORT_SYMBOL(lpcharge); */
47 unsigned int lpcharge;
48 EXPORT_SYMBOL(lpcharge);
49 static unsigned chg_irq;
50 static int gpio_vbat_detect;
51
52 #if defined(CONFIG_INPUT_TOUCHSCREEN)
53 void (*tsp_charger_status_cb)(int);
54 EXPORT_SYMBOL(tsp_charger_status_cb);
55 #endif
56
57 static bool sec_fg_gpio_init(void)
58 {
59         return true;
60 }
61
62 static bool sec_fg_fuelalert_process(bool is_fuel_alerted) { return true; }
63
64 static bool sec_bat_adc_none_init(struct platform_device *pdev) { return true; }
65 static bool sec_bat_adc_none_exit(void) { return true; }
66 static int sec_bat_adc_none_read(unsigned int channel) { return 0; }
67
68 static bool sec_bat_adc_ap_init(struct platform_device *pdev) { return true; }
69 static bool sec_bat_adc_ap_exit(void) { return true; }
70
71 static bool sec_bat_adc_ic_init(struct platform_device *pdev) { return true; }
72 static bool sec_bat_adc_ic_exit(void) { return true; }
73 static int sec_bat_adc_ic_read(unsigned int channel) { return 0; }
74
75 static sec_bat_adc_api_t adc_api[SEC_BATTERY_ADC_TYPE_NUM] = {
76         {
77                 .init = sec_bat_adc_none_init,
78                 .exit = sec_bat_adc_none_exit,
79                 .read = sec_bat_adc_none_read
80         },
81         {
82                 .init = sec_bat_adc_ap_init,
83                 .exit = sec_bat_adc_ap_exit,
84         },
85         {
86                 .init = sec_bat_adc_ic_init,
87                 .exit = sec_bat_adc_ic_exit,
88                 .read = sec_bat_adc_ic_read
89         }
90 };
91
92 static bool sec_chg_gpio_init(void)
93 {
94         return true;
95 }
96
97 bool sec_bat_check_cable_result_callback(int cable_type)
98 {
99         bool ret = true;
100         current_cable_type = cable_type;
101
102         switch (cable_type) {
103         case POWER_SUPPLY_TYPE_USB:
104                 pr_info("%s set vbus applied\n",
105                                 __func__);
106                 break;
107         case POWER_SUPPLY_TYPE_BATTERY:
108                 pr_info("%s set vbus cut\n",
109                                 __func__);
110                 break;
111         case POWER_SUPPLY_TYPE_MAINS:
112                 break;
113         default:
114                 pr_err("%s cable type (%d)\n",
115                                 __func__, cable_type);
116                 ret = false;
117                 break;
118         }
119
120         return ret;
121 }
122
123 static bool sec_bat_ovp_uvlo_result_callback(int health)
124 {
125         return true;
126 }
127
128 /* Get LP charging mode state */
129 static int sec_bat_is_lpm_check(char *str)
130 {
131         if (strncmp(str, "charger", 7) == 0)
132                 lpcharge = 1;
133
134         pr_info("%s: Low power charging mode: %d\n", __func__, lpcharge);
135
136         return lpcharge;
137 }
138 __setup("androidboot.mode=", sec_bat_is_lpm_check);
139
140 bool sec_bat_is_lpm(void)
141 {
142         return lpcharge == 1 ? true : false;
143 }
144 /*
145  * val.intval : temperature
146  */
147 static bool sec_bat_get_temperature_callback(
148                 enum power_supply_property psp,
149                 union power_supply_propval *val)
150 {
151         return true;
152 }
153
154 void check_jig_status(int status)
155 {
156         if (status) {
157                 pr_info("%s: JIG On so reset fuel gauge capacity\n", __func__);
158                 is_jig_on = true;
159         }
160
161 }
162
163 bool sec_bat_check_jig_status(void)
164 {
165         return is_jig_on;
166 }
167
168 //int sec_bat_check_cable_callback(void)
169 int sec_bat_check_cable_callback(struct sec_battery_info *battery)
170 {
171         int ta_nconnected;
172         union power_supply_propval value;
173         struct power_supply *psy = power_supply_get_by_name("battery");
174
175         ta_nconnected = gpio_get_value(chg_irq);
176
177         pr_info("%s : ta_nconnected : %d\n", __func__, ta_nconnected);
178
179         if (ta_nconnected)
180                 current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
181         else
182                 current_cable_type = POWER_SUPPLY_TYPE_MAINS;
183
184 #ifdef CONFIG_MFD_RT8973
185         if (attached_cable == MUIC_RT8973_CABLE_TYPE_0x1A) {
186                 if (ta_nconnected)
187                         current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
188                 else
189                         current_cable_type = POWER_SUPPLY_TYPE_MISC;
190         } else
191                 return current_cable_type;
192 #endif
193
194 #ifdef CONFIG_MFD_SM5504
195         if (attached_cable == MUIC_SM5504_CABLE_TYPE_0x1A) {
196                 if (ta_nconnected)
197                         current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
198                 else
199                         current_cable_type = POWER_SUPPLY_TYPE_MISC;
200         } else
201                 return current_cable_type;
202 #endif
203         if (!psy || !psy->set_property)
204                 pr_err("%s: fail to get battery psy\n", __func__);
205         else {
206                 value.intval = current_cable_type;
207                 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &value);
208         }
209
210         return current_cable_type;
211 }
212
213 void sec_bat_initial_check(void)
214 {
215         union power_supply_propval value;
216
217         if (POWER_SUPPLY_TYPE_BATTERY < current_cable_type) {
218                 value.intval = current_cable_type;
219                 psy_do_property("battery", set,
220                         POWER_SUPPLY_PROP_ONLINE, value);
221         } else {
222                 psy_do_property("sec-charger", get,
223                                 POWER_SUPPLY_PROP_ONLINE, value);
224                 if (value.intval == POWER_SUPPLY_TYPE_WPC) {
225                         value.intval = POWER_SUPPLY_TYPE_WPC;
226                         psy_do_property("battery", set,
227                                         POWER_SUPPLY_PROP_ONLINE, value);
228                 }
229 #if 0
230                 else {
231                         value.intval = POWER_SUPPLY_TYPE_BATTERY;
232                                 psy_do_property("sec-charger", set,
233                                         POWER_SUPPLY_PROP_ONLINE, value);
234                 }
235 #endif
236         }
237 }
238
239 static bool sec_bat_gpio_init(void)
240 {
241         return true;
242 }
243
244 //static bool sec_bat_check_callback(void) { return true; }
245 extern bool sec_bat_check_callback(struct sec_battery_info *battery){return true; }
246 static bool sec_bat_check_result_callback(void) { return true; }
247
248 /* callback for OVP/UVLO check
249  * return : int
250  * battery health
251  */
252 static int sec_bat_ovp_uvlo_callback(void)
253 {
254         int health;
255         health = POWER_SUPPLY_HEALTH_GOOD;
256
257         return health;
258 }
259
260 #if defined(CONFIG_SENSORS_GRIP_SX9500) && defined(CONFIG_MACH_DEGAS)
261 extern void charger_status_cb(int status);
262 #endif
263
264 #ifdef CONFIG_MFD_RT8973
265 void sec_charger_cb(u8 cable_type)
266 {
267         union power_supply_propval value;
268         struct power_supply *psy = power_supply_get_by_name("battery");
269
270         pr_info("%s: cable type (0x%02x)\n", __func__, cable_type);
271
272         attached_cable = cable_type;
273
274         switch (cable_type) {
275         case MUIC_RT8973_CABLE_TYPE_NONE:
276         case MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF:
277                 current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
278                 break;
279         case MUIC_RT8973_CABLE_TYPE_USB:
280         case MUIC_RT8973_CABLE_TYPE_L_USB:
281         case MUIC_RT8973_CABLE_TYPE_CDP:
282         case MUIC_RT8973_CABLE_TYPE_JIG_USB_ON:
283         case MUIC_RT8973_CABLE_TYPE_JIG_USB_OFF:
284                 current_cable_type = POWER_SUPPLY_TYPE_USB;
285                 break;
286         case MUIC_RT8973_CABLE_TYPE_REGULAR_TA:
287         case MUIC_RT8973_CABLE_TYPE_ATT_TA:
288         case MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF_WITH_VBUS:
289         case MUIC_RT8973_CABLE_TYPE_JIG_UART_ON_WITH_VBUS:
290         case MUIC_RT8973_CABLE_TYPE_TYPE1_CHARGER:
291                 current_cable_type = POWER_SUPPLY_TYPE_MAINS;
292                 break;
293         case MUIC_RT8973_CABLE_TYPE_OTG:
294                 goto skip;
295         case MUIC_RT8973_CABLE_TYPE_0x15:
296         case MUIC_RT8973_CABLE_TYPE_0x1A:
297         //case MUIC_RT8973_CABLE_TYPE_0x1A_VBUS:
298                 current_cable_type = POWER_SUPPLY_TYPE_MISC;
299                 break;
300         default:
301                 pr_err("%s: invalid type for charger:%d\n",
302                         __func__, cable_type);
303                 current_cable_type = POWER_SUPPLY_TYPE_UNKNOWN;
304                 goto skip;
305         }
306
307         if (!psy || !psy->set_property)
308                 pr_err("%s: fail to get battery psy\n", __func__);
309         else {
310                 value.intval = current_cable_type;
311                 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &value);
312
313 #if defined(CONFIG_INPUT_TOUCHSCREEN)
314         /* for changing of tsp operation mode */
315                 if (tsp_charger_status_cb)
316                         tsp_charger_status_cb(current_cable_type);
317 #endif
318         }
319
320 skip:
321         return 0;
322 }
323 #endif
324
325 #ifdef CONFIG_SM5502_MUIC
326 void sec_charger_cb(u8 cable_type)
327 {
328
329         union power_supply_propval value;
330         struct power_supply *psy = power_supply_get_by_name("battery");
331
332         pr_info("%s: cable type (0x%02x)\n", __func__, cable_type);
333
334         attached_cable = cable_type;
335         is_jig_on = false;
336
337         switch (cable_type) {
338         case CABLE_TYPE_NONE_MUIC:
339         case CABLE_TYPE2_DESKDOCK_MUIC:
340                 current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
341                 break;
342         case CABLE_TYPE1_USB_MUIC:
343                 current_cable_type = POWER_SUPPLY_TYPE_USB;
344                 break;
345         case CABLE_TYPE1_TA_MUIC:
346         case CABLE_TYPE3_U200CHG_MUIC:
347         case CABLE_TYPE3_NONSTD_SDP_MUIC:
348                 pr_info("%s: VBUS Online\n", __func__);
349                 current_cable_type = POWER_SUPPLY_TYPE_MAINS;
350                 break;
351         case CABLE_TYPE2_JIG_UART_OFF_VB_MUIC:
352         case CABLE_TYPE2_JIG_UART_ON_VB_MUIC:
353                 is_jig_on = true;
354                 current_cable_type = POWER_SUPPLY_TYPE_MAINS;
355                 break;
356         case CABLE_TYPE1_OTG_MUIC:
357                 goto skip;
358         case CABLE_TYPE2_JIG_UART_OFF_MUIC:
359                 is_jig_on = true;
360                 current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
361                 break;
362         case CABLE_TYPE2_JIG_USB_ON_MUIC:
363         case CABLE_TYPE2_JIG_USB_OFF_MUIC:
364                 is_jig_on = true;
365                 current_cable_type = POWER_SUPPLY_TYPE_USB;
366                 break;
367         case CABLE_TYPE1_CARKIT_T1OR2_MUIC:
368                 current_cable_type = POWER_SUPPLY_TYPE_USB;
369                 break;
370         case CABLE_TYPE3_DESKDOCK_VB_MUIC:
371                 current_cable_type = POWER_SUPPLY_TYPE_MISC;
372                 break;
373         default:
374                 pr_err("%s: invalid type for charger:%d\n",
375                         __func__, cable_type);
376                 current_cable_type = POWER_SUPPLY_TYPE_UNKNOWN;
377                 goto skip;
378         }
379
380         if (!psy || !psy->set_property)
381                 pr_err("%s: fail to get battery psy\n", __func__);
382         else {
383                 value.intval = current_cable_type;
384                 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &value);
385
386 #if defined(CONFIG_INPUT_TOUCHSCREEN)
387         /* for changing of tsp operation mode */
388                 if (tsp_charger_status_cb)
389                         tsp_charger_status_cb(current_cable_type);
390 #endif
391         }
392 #if defined(CONFIG_SENSORS_GRIP_SX9500) && defined(CONFIG_MACH_DEGAS)
393         /* for grip sensor threshold change at TA/USB status - sx9500.c */
394         charger_status_cb(current_cable_type);
395 #endif
396
397 skip:
398         return;
399 }
400 #endif /* CONFIG_SM5502_MUIC */
401
402
403 #ifdef CONFIG_MFD_SM5504
404 void sec_charger_cb(u8 cable_type)
405 {
406         union power_supply_propval value;
407         struct power_supply *psy = power_supply_get_by_name("battery");
408
409         pr_info("%s: cable type (0x%02x)\n", __func__, cable_type);
410
411         attached_cable = cable_type;
412
413         switch (cable_type) {
414         case MUIC_SM5504_CABLE_TYPE_NONE:
415         case MUIC_SM5504_CABLE_TYPE_JIG_UART_ON:
416         case MUIC_SM5504_CABLE_TYPE_JIG_UART_OFF:
417         case MUIC_SM5504_CABLE_TYPE_JIG_UART_ON_WITH_VBUS:
418                 current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
419                 break;
420         case MUIC_SM5504_CABLE_TYPE_USB:
421         case MUIC_SM5504_CABLE_TYPE_CDP:
422         case MUIC_SM5504_CABLE_TYPE_L_USB:
423         case MUIC_SM5504_CABLE_TYPE_JIG_USB_ON:
424         case MUIC_SM5504_CABLE_TYPE_JIG_USB_OFF:
425                 current_cable_type = POWER_SUPPLY_TYPE_USB;
426                 break;
427         case MUIC_SM5504_CABLE_TYPE_REGULAR_TA:
428         case MUIC_SM5504_CABLE_TYPE_ATT_TA:
429         case MUIC_SM5504_CABLE_TYPE_JIG_UART_OFF_WITH_VBUS:
430         case MUIC_SM5504_CABLE_TYPE_TYPE1_CHARGER:
431                 current_cable_type = POWER_SUPPLY_TYPE_MAINS;
432                 break;
433         case MUIC_SM5504_CABLE_TYPE_UART:
434                 current_cable_type = POWER_SUPPLY_TYPE_UNKNOWN;
435                 break;
436         case MUIC_SM5504_CABLE_TYPE_OTG:
437         case MUIC_SM5504_CABLE_TYPE_OTG_WITH_VBUS:
438                 current_cable_type = POWER_SUPPLY_TYPE_OTG;
439                 break;
440         case MUIC_SM5504_CABLE_TYPE_0x15:
441         case MUIC_SM5504_CABLE_TYPE_0x1A:
442 //      case MUIC_SM5504_CABLE_TYPE_0x1A_VBUS:
443                 current_cable_type = POWER_SUPPLY_TYPE_MISC;
444                 break;
445         default:
446                 pr_err("%s: invalid type for charger:%d\n",
447                         __func__, cable_type);
448                 current_cable_type = POWER_SUPPLY_TYPE_UNKNOWN;
449                 goto skip;
450         }
451
452         if (!psy || !psy->set_property)
453                 pr_err("%s: fail to get battery psy\n", __func__);
454         else {
455                 value.intval = current_cable_type;
456                 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &value);
457
458 #if defined(CONFIG_INPUT_TOUCHSCREEN)
459         /* for changing of tsp operation mode */
460                 if (tsp_charger_status_cb)
461                         tsp_charger_status_cb(current_cable_type);
462 #endif
463         }
464
465 skip:
466         return 0;
467
468 }
469 #endif /* CONFIG_MFD_SM5504 */
470
471 int sec_vf_adc_current_check(void)
472 {
473         int battery_on = 0;
474
475         sci_adi_set(ANA_REG_GLB_AUXAD_CTL,
476                 (BIT_AUXAD_CURRENTSEN_EN | BITS_AUXAD_CURRENT_IBS(0x6)));
477         sci_adi_set(ANA_REG_GLB_BATDET_CUR_CTRL,
478                 (BIT_BATDET_CUR_EN | BITS_BATDET_CUR_I(0x4)));
479         mdelay(5);
480
481         if (gpio_get_value(gpio_vbat_detect))
482         {
483                 printk("%s : vbat good!\n", __func__);
484                 battery_on = 1;
485         }
486         else
487         {
488                 printk("%s : vbat is not connected!\n", __func__);
489                 battery_on = 0;
490         }
491
492         sci_adi_clr(ANA_REG_GLB_AUXAD_CTL, BIT_AUXAD_CURRENTSEN_EN);
493         sci_adi_clr(ANA_REG_GLB_BATDET_CUR_CTRL, BIT_BATDET_CUR_EN);
494
495         return battery_on;
496 }
497
498 /* vf adc check, only for MUIC & young2dtv */
499 int sec_vf_adc_check(void)
500 {
501         int adc = 0;
502
503         adc = sci_adc_get_value(0, false);
504         pr_info("%s, vf adc : %d", __func__, adc);
505
506         if (adc > 500)
507                 return 0;
508         else
509                 return 1;
510 }
511
512 int sec_bat_dt_init(struct device_node *np,
513                          struct device *dev,
514                          sec_battery_platform_data_t *pdata)
515 {
516         int ret, i, len;
517         unsigned int bat_irq_attr;
518         const u32 *p;
519
520         ret = of_property_read_string(np,
521                 "battery,vendor", (char const **)&pdata->vendor);
522         if (ret)
523                 pr_info("%s: Vendor is Empty\n", __func__);
524
525         ret = of_property_read_string(np,
526                 "battery,charger_name", (char const **)&pdata->charger_name);
527         if (ret)
528                 pr_info("%s: charger_name is Empty\n", __func__);
529
530         ret = of_property_read_string(np,
531                 "battery,fuelgauge_name", (char const **)&pdata->fuelgauge_name);
532         if (ret)
533                 pr_info("%s: fuelguage_name is Empty\n", __func__);
534
535         ret = of_property_read_u32(np, "battery,technology",
536                 &pdata->technology);
537         if (ret)
538                 pr_info("%s : technology is Empty\n", __func__);
539
540         p = of_get_property(np, "battery,polling_time", &len);
541         if (!p)
542                 return 1;
543
544         len = len / sizeof(u32);
545         pdata->polling_time = kzalloc(sizeof(*pdata->polling_time) * len, GFP_KERNEL);
546         ret = of_property_read_u32_array(np, "battery,polling_time",
547                         pdata->polling_time, len);
548
549         p = of_get_property(np, "battery,temp_table_adc", &len);
550         if (!p)
551                 return 1;
552
553         len = len / sizeof(u32);
554
555         pdata->temp_adc_table_size = len;
556         pdata->temp_amb_adc_table_size = len;
557
558         pdata->temp_adc_table =
559                 kzalloc(sizeof(sec_bat_adc_table_data_t) *
560                                 pdata->temp_adc_table_size, GFP_KERNEL);
561         pdata->temp_amb_adc_table =
562                 kzalloc(sizeof(sec_bat_adc_table_data_t) *
563                                 pdata->temp_adc_table_size, GFP_KERNEL);
564
565         for(i = 0; i < pdata->temp_adc_table_size; i++) {
566                 ret = of_property_read_u32_index(np,
567                                 "battery,temp_table_adc", i,
568                                 &pdata->temp_adc_table[i].adc);
569                 if (ret)
570                         pr_info("%s : Temp_adc_table(adc) is Empty\n",
571                                         __func__);
572
573                 ret = of_property_read_u32_index(np,
574                                 "battery,temp_table_data", i,
575                                 &pdata->temp_adc_table[i].temperature);
576                 if (ret)
577                         pr_info("%s : Temp_adc_table(data) is Empty\n",
578                                         __func__);
579
580                 ret = of_property_read_u32_index(np,
581                                 "battery,temp_table_adc", i,
582                                 &pdata->temp_amb_adc_table[i].adc);
583                 if (ret)
584                         pr_info("%s : Temp_amb_adc_table(adc) is Empty\n",
585                                         __func__);
586
587                 ret = of_property_read_u32_index(np,
588                                 "battery,temp_table_data", i,
589                                 &pdata->temp_amb_adc_table[i].temperature);
590                 if (ret)
591                         pr_info("%s : Temp_amb_adc_table(data) is Empty\n",
592                                         __func__);
593         }
594
595         p = of_get_property(np, "battery,input_current_limit", &len);
596         if (!p)
597                 return 1;
598
599         len = len / sizeof(u32);
600
601         pdata->charging_current =
602                 kzalloc(sizeof(sec_charging_current_t) * len,
603                                 GFP_KERNEL);
604
605         for(i = 0; i < len; i++) {
606                 ret = of_property_read_u32_index(np,
607                                 "battery,input_current_limit", i,
608                                 &pdata->charging_current[i].input_current_limit);
609                 if (ret)
610                         pr_info("%s : Input_current_limit is Empty\n",
611                                         __func__);
612
613                 ret = of_property_read_u32_index(np,
614                                 "battery,fast_charging_current", i,
615                                 &pdata->charging_current[i].fast_charging_current);
616                 if (ret)
617                         pr_info("%s : Fast charging current is Empty\n",
618                                         __func__);
619
620                 ret = of_property_read_u32_index(np,
621                                 "battery,full_check_current_1st", i,
622                                 &pdata->charging_current[i].full_check_current_1st);
623                 if (ret)
624                         pr_info("%s : Full check current 1st is Empty\n",
625                                         __func__);
626
627                 ret = of_property_read_u32_index(np,
628                                 "battery,full_check_current_2nd", i,
629                                 &pdata->charging_current[i].full_check_current_2nd);
630                 if (ret)
631                         pr_info("%s : Full check current 2nd is Empty\n",
632                                         __func__);
633         }
634
635         ret = of_property_read_u32(np, "battery,adc_check_count",
636                         &pdata->adc_check_count);
637         if (ret)
638                 pr_info("%s : Adc check count is Empty\n", __func__);
639
640         ret = of_property_read_u32(np, "battery,cable_check_type",
641                         &pdata->cable_check_type);
642         if (ret)
643                 pr_info("%s : Cable check type is Empty\n", __func__);
644
645         ret = of_property_read_u32(np, "battery,cable_source_type",
646                         &pdata->cable_source_type);
647         if (ret)
648                 pr_info("%s : Cable source type is Empty\n", __func__);
649
650         ret = of_property_read_u32(np, "battery,event_waiting_time",
651                         &pdata->event_waiting_time);
652         if (ret)
653                 pr_info("%s : Event waiting time is Empty\n", __func__);
654
655         ret = of_property_read_u32(np, "battery,polling_type",
656                         &pdata->polling_type);
657         if (ret)
658                 pr_info("%s : Polling type is Empty\n", __func__);
659
660         ret = of_property_read_u32(np, "battery,monitor_initial_count",
661                         &pdata->monitor_initial_count);
662         if (ret)
663                 pr_info("%s : Monitor initial count is Empty\n", __func__);
664
665         ret = of_property_read_u32(np, "battery,battery_check_type",
666                         &pdata->battery_check_type);
667         if (ret)
668                 pr_info("%s : Battery check type is Empty\n", __func__);
669
670         ret = of_property_read_u32(np, "battery,check_count",
671                         &pdata->check_count);
672
673         if (ret)
674                 pr_info("%s : Check count is Empty\n", __func__);
675
676         ret = of_property_read_u32(np, "battery,check_adc_max",
677                         &pdata->check_adc_max);
678         if (ret)
679                 pr_info("%s : Check adc max is Empty\n", __func__);
680
681         ret = of_property_read_u32(np, "battery,check_adc_min",
682                         &pdata->check_adc_min);
683         if (ret)
684                 pr_info("%s : Check adc min is Empty\n", __func__);
685
686         ret = of_property_read_u32(np, "battery,ovp_uvlo_check_type",
687                         &pdata->ovp_uvlo_check_type);
688         if (ret)
689                 pr_info("%s : Ovp Uvlo check type is Empty\n", __func__);
690
691         ret = of_property_read_u32(np, "battery,thermal_source",
692                         &pdata->thermal_source);
693         if (ret)
694                 pr_info("%s : Thermal source is Empty\n", __func__);
695
696         ret = of_property_read_u32(np, "battery,temp_check_type",
697                         &pdata->temp_check_type);
698         if (ret)
699                 pr_info("%s : Temp check type is Empty\n", __func__);
700
701         ret = of_property_read_u32(np, "battery,temp_check_count",
702                         &pdata->temp_check_count);
703         if (ret)
704                 pr_info("%s : Temp check count is Empty\n", __func__);
705
706         ret = of_property_read_u32(np, "battery,temp_high_recovery_event",
707                         &pdata->temp_high_recovery_event);
708         if (ret)
709                 pr_info("%s : Temp high recovery event is Empty\n", __func__);
710
711 #if defined(CONFIG_MACH_DEGAS_USA)
712         pdata->temp_high_threshold_event = 550;
713         pdata->temp_high_recovery_event = 480;
714         pdata->temp_low_threshold_event = 20;
715         pdata->temp_low_recovery_event = 50;
716         pdata->temp_high_threshold_normal = 550;
717         pdata->temp_high_recovery_normal = 480;
718         pdata->temp_low_threshold_normal = 20;
719         pdata->temp_low_recovery_normal = 50;
720         pdata->temp_high_threshold_lpm = 550;
721         pdata->temp_high_recovery_lpm = 480;
722         pdata->temp_low_threshold_lpm = 20;
723         pdata->temp_low_recovery_lpm = 50;
724 #elif defined(CONFIG_MACH_DEGAS_BMW)
725         pdata->temp_high_threshold_event = 450;
726         pdata->temp_high_recovery_event = 400;
727         pdata->temp_low_threshold_event = 0;
728         pdata->temp_low_recovery_event = 50;
729         pdata->temp_high_threshold_normal = 450;
730         pdata->temp_high_recovery_normal = 400;
731         pdata->temp_low_threshold_normal = 0;
732         pdata->temp_low_recovery_normal = 50;
733         pdata->temp_high_threshold_lpm = 450;
734         pdata->temp_high_recovery_lpm = 400;
735         pdata->temp_low_threshold_lpm = 0;
736         pdata->temp_low_recovery_lpm = 50;
737 #else
738         ret = of_property_read_u32(np, "battery,temp_highlimit_threshold_event",
739                 &pdata->temp_highlimit_threshold_event);
740         if (ret)
741                 pr_info("%s : Temp highlimit threshold event is Empty\n", __func__);
742
743         ret = of_property_read_u32(np, "battery,battery,temp_highlimit_recovery_event",
744                 &pdata->temp_highlimit_recovery_event);
745         if (ret)
746                 pr_info("%s : Temp highlimit threshold event recovery is Empty\n", __func__);
747
748         ret = of_property_read_u32(np, "battery,temp_highlimit_threshold_normal",
749                 &pdata->temp_highlimit_threshold_normal);
750         if (ret)
751                 pr_info("%s : Temp highlimit threshold normal is Empty\n", __func__);
752
753         ret = of_property_read_u32(np, "battery,temp_highlimit_recovery_normal",
754                 &pdata->temp_highlimit_recovery_normal);
755         if (ret)
756                 pr_info("%s : Temp highlimit threshold normal recovery is Empty\n", __func__);
757
758         ret = of_property_read_u32(np, "battery,temp_highlimit_threshold_lpm",
759                 &pdata->temp_highlimit_threshold_lpm);
760         if (ret)
761                 pr_info("%s : Temp highlimit threshold lpm is Empty\n", __func__);
762
763         ret = of_property_read_u32(np, "battery,temp_highlimit_recovery_lpm",
764                 &pdata->temp_highlimit_recovery_lpm);
765         if (ret)
766                 pr_info("%s : Temp highlimit threshold lpm recovery is Empty\n", __func__);
767
768         ret = of_property_read_u32(np, "battery,temp_high_threshold_event",
769                 &pdata->temp_high_threshold_event);
770         if (ret)
771                 pr_info("%s : Temp high threshold event is Empty\n", __func__);
772
773         ret = of_property_read_u32(np, "battery,temp_low_threshold_event",
774                         &pdata->temp_low_threshold_event);
775         if (ret)
776                 pr_info("%s : Temp low threshold event is Empty\n", __func__);
777
778         ret = of_property_read_u32(np, "battery,temp_low_recovery_event",
779                         &pdata->temp_low_recovery_event);
780         if (ret)
781                 pr_info("%s : Temp low recovery event is Empty\n", __func__);
782
783         ret = of_property_read_u32(np, "battery,temp_high_threshold_normal",
784                         &pdata->temp_high_threshold_normal);
785         if (ret)
786                 pr_info("%s : Temp high threshold normal is Empty\n", __func__);
787
788         ret = of_property_read_u32(np, "battery,temp_high_recovery_normal",
789                         &pdata->temp_high_recovery_normal);
790         if (ret)
791                 pr_info("%s : Temp high recovery normal is Empty\n", __func__);
792
793         ret = of_property_read_u32(np, "battery,temp_low_threshold_normal",
794                         &pdata->temp_low_threshold_normal);
795         if (ret)
796                 pr_info("%s : Temp low threshold normal is Empty\n", __func__);
797
798         ret = of_property_read_u32(np, "battery,temp_low_recovery_normal",
799                         &pdata->temp_low_recovery_normal);
800
801         if (ret)
802                 pr_info("%s : Temp low recovery normal is Empty\n", __func__);
803
804         ret = of_property_read_u32(np, "battery,temp_high_threshold_lpm",
805                         &pdata->temp_high_threshold_lpm);
806         if (ret)
807                 pr_info("%s : Temp high threshold lpm is Empty\n", __func__);
808
809         ret = of_property_read_u32(np, "battery,temp_high_recovery_lpm",
810                         &pdata->temp_high_recovery_lpm);
811         if (ret)
812                 pr_info("%s : Temp high recovery lpm is Empty\n", __func__);
813
814         ret = of_property_read_u32(np, "battery,temp_low_threshold_lpm",
815                         &pdata->temp_low_threshold_lpm);
816         if (ret)
817                 pr_info("%s : Temp low threshold lpm is Empty\n", __func__);
818
819         ret = of_property_read_u32(np, "battery,temp_low_recovery_lpm",
820                         &pdata->temp_low_recovery_lpm);
821         if (ret)
822                 pr_info("%s : Temp low recovery lpm is Empty\n", __func__);
823 #endif
824
825         ret = of_property_read_u32(np, "battery,full_check_type",
826                         &pdata->full_check_type);
827         if (ret)
828                 pr_info("%s : Full check type is Empty\n", __func__);
829
830         ret = of_property_read_u32(np, "battery,full_check_type_2nd",
831                         &pdata->full_check_type_2nd);
832         if (ret)
833                 pr_info("%s : Full check type 2nd is Empty\n", __func__);
834
835         ret = of_property_read_u32(np, "battery,full_check_count",
836                         &pdata->full_check_count);
837         if (ret)
838                 pr_info("%s : Full check count is Empty\n", __func__);
839
840         ret = of_property_read_u32(np, "battery,chg_gpio_full_check",
841                         &pdata->chg_gpio_full_check);
842         if (ret)
843                 pr_info("%s : Chg gpio full check is Empty\n", __func__);
844
845         ret = of_property_read_u32(np, "battery,chg_polarity_full_check",
846                         &pdata->chg_polarity_full_check);
847         if (ret)
848                 pr_info("%s : Chg polarity full check is Empty\n", __func__);
849
850         ret = of_property_read_u32(np, "battery,full_condition_type",
851                         &pdata->full_condition_type);
852         if (ret)
853                 pr_info("%s : Full condition type is Empty\n", __func__);
854
855         ret = of_property_read_u32(np, "battery,full_condition_soc",
856                         &pdata->full_condition_soc);
857         if (ret)
858                 pr_info("%s : Full condition soc is Empty\n", __func__);
859
860         ret = of_property_read_u32(np, "battery,full_condition_vcell",
861                         &pdata->full_condition_vcell);
862         if (ret)
863                 pr_info("%s : Full condition vcell is Empty\n", __func__);
864
865         ret = of_property_read_u32(np, "battery,recharge_check_count",
866                         &pdata->recharge_check_count);
867         if (ret)
868                 pr_info("%s : Recharge check count is Empty\n", __func__);
869
870         ret = of_property_read_u32(np, "battery,recharge_condition_type",
871                         &pdata->recharge_condition_type);
872         if (ret)
873                 pr_info("%s : Recharge condition type is Empty\n", __func__);
874
875         ret = of_property_read_u32(np, "battery,recharge_condition_soc",
876                         &pdata->recharge_condition_soc);
877         if (ret)
878                 pr_info("%s : Recharge condition soc is Empty\n", __func__);
879
880         ret = of_property_read_u32(np, "battery,recharge_condition_vcell",
881                         &pdata->recharge_condition_vcell);
882         if (ret)
883                 pr_info("%s : Recharge condition vcell is Empty\n", __func__);
884
885         ret = of_property_read_u32(np, "battery,recharge_condition_avgvcell",
886                         &pdata->recharge_condition_avgvcell);
887         if (ret)
888                 pr_info("%s : Recharge condition avgvcell is Empty\n", __func__);
889
890         ret = of_property_read_u32(np, "battery,charging_total_time",
891                         (unsigned int *)&pdata->charging_total_time);
892         if (ret)
893                 pr_info("%s : Charging total time is Empty\n", __func__);
894
895         ret = of_property_read_u32(np, "battery,recharging_total_time",
896                         (unsigned int *)&pdata->recharging_total_time);
897         if (ret)
898                 pr_info("%s : Recharging total time is Empty\n", __func__);
899
900         ret = of_property_read_u32(np, "battery,charging_reset_time",
901                         (unsigned int *)&pdata->charging_reset_time);
902         if (ret)
903                 pr_info("%s : Charging reset time is Empty\n", __func__);
904
905         ret = of_property_read_u32(np, "battery,charging_reset_time",
906                         (unsigned int *)&pdata->charging_reset_time);
907         if (ret)
908                 pr_info("%s : Charging reset time is Empty\n", __func__);
909
910         ret = of_property_read_u32(np, "battery,bat-irq-attr", &bat_irq_attr);
911         if (ret)
912                 pr_info("%s : Battery irq is Empty\n", __func__);
913
914         pdata->bat_gpio_init = sec_bat_gpio_init;
915         pdata->check_battery_callback = sec_bat_check_callback;
916         pdata->check_battery_result_callback = sec_bat_check_result_callback;
917         pdata->check_cable_callback = sec_bat_check_cable_callback;
918         pdata->check_cable_result_callback =
919                 sec_bat_check_cable_result_callback;
920         pdata->get_temperature_callback = sec_bat_get_temperature_callback;
921         pdata->initial_check = sec_bat_initial_check;
922         pdata->is_lpm = sec_bat_is_lpm;
923         pdata->ovp_uvlo_callback = sec_bat_ovp_uvlo_callback;
924         pdata->ovp_uvlo_result_callback = sec_bat_ovp_uvlo_result_callback;
925         pdata->check_jig_status = sec_bat_check_jig_status;
926
927         for (i = 0; i < SEC_BATTERY_ADC_TYPE_NUM; i++)
928                 pdata->adc_api[i] = adc_api[i];
929
930         return 0;
931 }
932
933 int sec_chg_dt_init(struct device_node *np,
934                          struct device *dev,
935                          sec_battery_platform_data_t *pdata)
936 {
937         int ret = 0, len = 0;
938         unsigned int chg_irq_attr = 0;
939         int chg_gpio_en = 0;
940         int chg_irq_gpio = 0;
941
942         if (!np)
943                 return -EINVAL;
944
945         chg_gpio_en = of_get_named_gpio(np, "chgen-gpio", 0);
946         if (chg_gpio_en < 0) {
947                 pr_err("%s: of_get_named_gpio failed: %d\n", __func__,
948                                                         chg_gpio_en);
949                 return chg_gpio_en;
950         }
951
952         ret = of_property_read_u32(np, "chg-irq-attr",
953                                         &chg_irq_attr);
954         if (ret) {
955                 pr_info("%s chg_irq_attr request failed: %d\n", __func__, chg_irq_attr);
956         }
957
958         ret = gpio_request(chg_gpio_en, "chgen-gpio");
959         if (ret) {
960                 pr_err("%s gpio_request failed: %d\n", __func__, chg_gpio_en);
961                 return ret;
962         }
963
964         chg_irq_gpio = of_get_named_gpio(np, "chgirq-gpio", 0);
965         if (chg_irq_gpio < 0)
966                 pr_err("%s: chgirq gpio get failed: %d\n", __func__, chg_irq_gpio);
967
968         ret = gpio_request(chg_irq_gpio, "chgirq-gpio");
969         if (ret)
970                 pr_err("%s gpio_request failed: %d\n", __func__, chg_irq_gpio);
971
972         ret = of_property_read_u32(np, "chg-float-voltage",
973                                         &pdata->chg_float_voltage);
974         if (ret)
975                 return ret;
976
977         np = of_find_node_by_name(NULL, "sec-battery");
978         if (!np) {
979                 pr_err("%s np NULL\n", __func__);
980         }
981         else {
982                 int i = 0;
983                 const u32 *p;
984                 p = of_get_property(np, "battery,input_current_limit", &len);
985                 if (!p){
986
987                         pr_err("%s charger,input_current_limit is Empty\n", __func__);
988                         //      return 1;
989                 }
990                 else{
991
992                         len = len / sizeof(u32);
993
994                         pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len,
995                                         GFP_KERNEL);
996
997                         for(i = 0; i < len; i++) {
998                                 ret = of_property_read_u32_index(np,
999                                                 "battery,input_current_limit", i,
1000                                                 &pdata->charging_current[i].input_current_limit);
1001                                 if (ret)
1002                                         pr_info("%s : Input_current_limit is Empty\n",
1003                                                         __func__);
1004
1005                                 ret = of_property_read_u32_index(np,
1006                                                 "battery,fast_charging_current", i,
1007                                                 &pdata->charging_current[i].fast_charging_current);
1008                                 if (ret)
1009                                         pr_info("%s : Fast charging current is Empty\n",
1010                                                         __func__);
1011
1012                                 ret = of_property_read_u32_index(np,
1013                                                 "battery,full_check_current_1st", i,
1014                                                 &pdata->charging_current[i].full_check_current_1st);
1015                                 if (ret)
1016                                         pr_info("%s : Full check current 1st is Empty\n",
1017                                                         __func__);
1018
1019                                 ret = of_property_read_u32_index(np,
1020                                                 "battery,full_check_current_2nd", i,
1021                                                 &pdata->charging_current[i].full_check_current_2nd);
1022                                 if (ret)
1023                                         pr_info("%s : Full check current 2nd is Empty\n",
1024                                                         __func__);
1025                         }
1026                 }
1027         }
1028
1029         ret = of_property_read_u32(np, "battery,ovp_uvlo_check_type",
1030                         &pdata->ovp_uvlo_check_type);
1031         if (ret)
1032                 pr_info("%s : Ovp Uvlo check type is Empty\n", __func__);
1033
1034         ret = of_property_read_u32(np, "battery,full_check_type",
1035                         &pdata->full_check_type);
1036         if (ret)
1037                 pr_info("%s : Full check type is Empty\n", __func__);
1038
1039         ret = of_property_read_u32(np, "battery,full_check_type_2nd",
1040                         &pdata->full_check_type_2nd);
1041         if (ret)
1042                 pr_info("%s : Full check type 2nd is Empty\n", __func__);
1043
1044 #if defined(CONFIG_MACH_PIKEAYOUNG2DTV)
1045         gpio_vbat_detect = BATT_DET_CURRENT;
1046
1047         if (!gpio_is_valid(gpio_vbat_detect)) {
1048                 printk("%s: battery detect gpio %d failed\n", __func__, gpio_vbat_detect);
1049                 return 1;
1050         }
1051         gpio_request(gpio_vbat_detect, "battery-detect");
1052         gpio_direction_input(gpio_vbat_detect);
1053         printk("%s: battery detect gpio %d\n", __func__, gpio_vbat_detect);
1054 #endif
1055
1056         if (chg_irq_gpio > 0) {
1057                 pdata->chg_irq_attr = chg_irq_attr;
1058                 pdata->chg_irq = gpio_to_irq(chg_irq_gpio);
1059         }
1060         pdata->chg_gpio_en = chg_gpio_en;
1061         pdata->chg_gpio_init = sec_chg_gpio_init;
1062         pdata->check_cable_result_callback =
1063                 sec_bat_check_cable_result_callback;
1064
1065         return 0;
1066 }
1067
1068 #ifdef CONFIG_FUELGAUGE_SPRD4SAMSUNG27X3
1069 struct battery_data_t bat_data;
1070 #endif
1071
1072 int sec_fg_dt_init(struct device_node *np,
1073                          struct device *dev,
1074                          sec_battery_platform_data_t *pdata)
1075 {
1076         int ret;
1077         unsigned int fg_irq_attr;
1078
1079         ret = of_property_read_u32(np, "capacity-max", &pdata->capacity_max);
1080         if (ret)
1081                 return ret;
1082         ret = of_property_read_u32(np, "capacity-max-margin",
1083                         &pdata->capacity_max_margin);
1084         if (ret)
1085                 return ret;
1086         ret = of_property_read_u32(np, "capacity-min", &pdata->capacity_min);
1087         if (ret)
1088                 return ret;
1089         ret = of_property_read_u32(np, "fg-irq-attr", &fg_irq_attr);
1090         if (ret)
1091                 return ret;
1092         ret = of_property_read_u32(np, "fuel-alert-soc",
1093                         &pdata->fuel_alert_soc);
1094         if (ret)
1095                 return ret;
1096         if (of_find_property(np, "repeated-fuelalert", NULL))
1097                 pdata->repeated_fuelalert = true;
1098
1099         ret = of_property_read_u32(np, "temp_adc_channel",
1100                         &pdata->temp_adc_channel);
1101         if (ret)
1102                 return ret;
1103
1104 #ifdef CONFIG_FUELGAUGE_SPRD4SAMSUNG27X3
1105         {
1106                 struct sprd_battery_platform_data *battery_data;
1107                 int len, i;
1108                 uint32_t cell_value;
1109                 struct device_node *temp_np = NULL;
1110
1111                 battery_data = devm_kzalloc(dev, sizeof(*battery_data), GFP_KERNEL);
1112                 temp_np = of_get_child_by_name(np, "sprd_fgu");
1113                 if (!temp_np) {
1114                         return ERR_PTR(-EINVAL);
1115                 }
1116                 battery_data->gpio_vchg_detect = (uint32_t) of_get_named_gpio(np, "gpios", 0);
1117                 battery_data->irq_fgu = irq_of_parse_and_map(temp_np, 0);
1118                 of_property_read_u32(np, "chg_bat_safety_vol", &battery_data->chg_bat_safety_vol);
1119                 of_property_read_u32(np, "soft_vbat_uvlo", &battery_data->soft_vbat_uvlo);
1120                 of_property_read_u32(np, "vmod", &battery_data->fgu_mode);
1121                 of_property_read_u32(np, "alm_soc", &battery_data->alm_soc);
1122                 of_property_read_u32(np, "alm_vbat", &battery_data->alm_vol);
1123                 of_property_read_u32(np, "rint", &battery_data->rint);
1124                 of_property_read_u32(np, "cnom", &battery_data->cnom);
1125                 of_property_read_u32(np, "rsense_real", &battery_data->rsense_real);
1126                 of_property_read_u32(np, "rsense_spec", &battery_data->rsense_spec);
1127                 of_property_read_u32(np, "relax_current", &battery_data->relax_current);
1128                 of_property_read_u32(np, "cal_ajust", &battery_data->fgu_cal_ajust);
1129                 of_get_property(np, "cnom_temp_tab", &len);
1130                 len /= sizeof(u32);
1131                 battery_data->cnom_temp_tab_size = len >> 1;
1132                 battery_data->cnom_temp_tab = kzalloc(sizeof(struct sprdbat_table_data) *
1133                                  battery_data->cnom_temp_tab_size, GFP_KERNEL);
1134                 for (i = 0; i < len; i++) {
1135                         of_property_read_u32_index(np, "cnom_temp_tab", i, &cell_value);
1136                    if(i&0x1) {
1137                             battery_data->cnom_temp_tab[i >> 1].y = cell_value;
1138                     } else {
1139                         battery_data->cnom_temp_tab[i >> 1].x = cell_value - 1000;
1140                     }
1141                 }
1142
1143                 of_get_property(np, "rint_temp_tab", &len);
1144                 len /= sizeof(u32);
1145                 battery_data->rint_temp_tab_size = len >> 1;
1146                 battery_data->rint_temp_tab = kzalloc(sizeof(struct sprdbat_table_data) *
1147                                  battery_data->rint_temp_tab_size, GFP_KERNEL);
1148                 for (i = 0; i < len; i++) {
1149                         of_property_read_u32_index(np, "rint_temp_tab", i, &cell_value);
1150                    if(i&0x1) {
1151                             battery_data->rint_temp_tab[i >> 1].y = cell_value;
1152                     } else {
1153                         battery_data->rint_temp_tab[i >> 1].x = cell_value - 1000;
1154                     }
1155                 }
1156                 of_get_property(np, "ocv_table", &len);
1157                 len /= sizeof(u32);
1158                 battery_data->ocv_tab_size = len >> 1;
1159                 battery_data->ocv_tab = kzalloc(sizeof(struct sprdbat_table_data) *
1160                                  battery_data->ocv_tab_size, GFP_KERNEL);
1161                 for (i = 0; i < len; i++) {
1162                         of_property_read_u32_index(np, "ocv_table", i, &cell_value);
1163                    if(i&0x1) {
1164                             battery_data->ocv_tab[i >> 1].y = cell_value;
1165                     } else {
1166                         battery_data->ocv_tab[i >> 1].x = cell_value;
1167                     }
1168                 }
1169                 bat_data.pdata = battery_data;
1170                 pdata->battery_data = &bat_data;
1171         }
1172 #endif
1173
1174         pdata->fg_irq_attr = fg_irq_attr;
1175         pdata->fuelalert_process = sec_fg_fuelalert_process;
1176         pdata->fg_gpio_init = sec_fg_gpio_init;
1177         pdata->capacity_calculation_type =
1178                         SEC_FUELGAUGE_CAPACITY_TYPE_RAW;
1179         pdata->check_jig_status = sec_bat_check_jig_status;
1180
1181         return 0;
1182 }