UPSTREAM: PCI: rockchip: remove the pointer to L1 substate cap
[firefly-linux-kernel-4.4.55.git] / drivers / power / rt5025-swjeita.c
1 /* drivers/power/rt5025-swjeita.c
2  * swjeita Driver for Richtek RT5025 PMIC
3  * Multi function device - multi functional baseband PMIC swjeita part
4  *
5  * Copyright (C) 2013
6  * Author: CY Huang <cy_huang@richtek.com>
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 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/platform_device.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
21
22 #include <linux/mfd/rt5025.h>
23 #include <linux/power/rt5025-swjeita.h>
24
25 #define TEMP_TOLERANCE  30  /*'c*10 gap for tolerance*/
26
27 static int rt5025_set_charging_cc_switch (struct i2c_client *i2c, int onoff)
28 {
29         int ret;
30
31         RTINFO("onoff = %d\n", onoff);
32         if (onoff)
33                 ret = rt5025_set_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCCEN_MASK);
34         else
35                 ret = rt5025_clr_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCCEN_MASK);
36         return ret;
37 }
38
39 static int rt5025_set_charging_cc(struct i2c_client *i2c, int cur_value)
40 {
41         int ret;
42         u8 data;
43
44         RTINFO("current value = %d\n", cur_value);
45         if (cur_value < 500)
46                 data = 0;
47         else if (cur_value > 2000)
48                 data = 0xf << RT5025_CHGICC_SHIFT;
49         else
50                 data = ((cur_value - 500) / 100) << RT5025_CHGICC_SHIFT;
51
52         ret = rt5025_assign_bits(i2c, RT5025_REG_CHGCTL4, RT5025_CHGICC_MASK, data);
53
54         if (cur_value < 500)
55                 rt5025_set_charging_cc_switch(i2c, 0);
56         else
57                 rt5025_set_charging_cc_switch(i2c, 1);
58
59         return ret;
60 }
61
62 static int rt5025_set_charging_cv(struct i2c_client *i2c, int voltage)
63 {
64         int ret;
65         u8 data;
66
67         RTINFO("voltage = %d\n", voltage);
68         if (voltage < 3500)
69                 data = 0;
70         else if (voltage > 4440)
71                 data = 0x2f << RT5025_CHGCV_SHIFT;
72         else
73                 data = ((voltage - 3500) / 20) << RT5025_CHGCV_SHIFT;
74
75         ret = rt5025_assign_bits(i2c, RT5025_REG_CHGCTL3, RT5025_CHGCV_MASK, data);
76         return ret;
77 }
78
79 static int rt5025_sel_external_temp_index(struct rt5025_swjeita_info *swji)
80 {
81         int temp = swji->cur_temp;
82         int sect_index;
83
84         RTINFO("\n");
85         if (temp < swji->temp[0])
86                 sect_index = 0;
87         else if (temp >= swji->temp[0] && temp < swji->temp[1])
88                 sect_index = 1;
89         else if (temp >= swji->temp[1] && temp < swji->temp[2])
90                 sect_index = 2;
91         else if (temp >= swji->temp[2] && temp < swji->temp[3])
92                 sect_index = 3;
93         else if (temp >= swji->temp[3])
94                 sect_index = 4;
95
96         RTINFO("sect_index = %d\n", sect_index);
97         return sect_index;
98 }
99
100 static int rt5025_get_external_temp_index(struct rt5025_swjeita_info *swji)
101 {
102         u8 data[2];
103         long int temp;
104         int sect_index;
105
106         RTINFO("\n");
107         if (rt5025_reg_block_read(swji->i2c, RT5025_REG_AINH, 2, data) < 0)
108                 pr_err("%s: failed to read ext_temp register\n", __func__);
109
110         temp = (data[0] * 256 + data[1]) * 61 / 100;
111         temp = (temp  *  (-91738) + 81521000) / 100000;
112
113         swji->cur_temp = temp;
114
115         RTINFO("cur_section = %d, cur_temp = %d\n", swji->cur_section, swji->cur_temp);
116
117         switch (swji->cur_section) {
118         case 0:
119                 if (temp < swji->temp[0] + TEMP_TOLERANCE)
120                         sect_index = rt5025_sel_external_temp_index(swji);
121                 else
122                         sect_index = swji->cur_section;
123                 break;
124         case 1:
125                 if (temp <= swji->temp[0] - TEMP_TOLERANCE || temp >= swji->temp[1] + TEMP_TOLERANCE)
126                         sect_index = rt5025_sel_external_temp_index(swji);
127                 else
128                         sect_index = swji->cur_section;
129                 break;
130         case 2:
131                 if (temp <= swji->temp[1] - TEMP_TOLERANCE || temp >= swji->temp[2] + TEMP_TOLERANCE)
132                         sect_index = rt5025_sel_external_temp_index(swji);
133                 else
134                         sect_index = swji->cur_section;
135                 break;
136         case 3:
137                 if (temp <= swji->temp[2] - TEMP_TOLERANCE || temp >= swji->temp[3] + TEMP_TOLERANCE)
138                         sect_index = rt5025_sel_external_temp_index(swji);
139                 else
140                         sect_index = swji->cur_section;
141                 break;
142         case 4:
143                 if (temp <= swji->temp[3] - TEMP_TOLERANCE)
144                         sect_index = rt5025_sel_external_temp_index(swji);
145                 else
146                         sect_index = swji->cur_section;
147                 break;
148         default:
149                 sect_index = swji->cur_section;
150                 break;
151         }
152         RTINFO("sect_index = %d\n", sect_index);
153         return sect_index;
154 }
155
156 static inline int rt5025_set_ainadc_onoff(struct rt5025_swjeita_info *swji, int enable)
157 {
158         int ret;
159
160         RTINFO("enable = %d\n", enable);
161         if (enable)
162                 ret = rt5025_set_bits(swji->i2c, RT5025_REG_CHANNELL, RT5025_AINEN_MASK);
163         else
164                 ret = rt5025_clr_bits(swji->i2c, RT5025_REG_CHANNELL, RT5025_AINEN_MASK);
165
166         return ret;
167 }
168
169 static inline int rt5025_set_intadc_onoff(struct rt5025_swjeita_info *swji, int enable)
170 {
171         int ret;
172
173         RTINFO("enable = %d\n", enable);
174         if (enable)
175                 ret = rt5025_set_bits(swji->i2c, RT5025_REG_CHANNELL, RT5025_INTEN_MASK);
176         else
177                 ret = rt5025_clr_bits(swji->i2c, RT5025_REG_CHANNELL, RT5025_INTEN_MASK);
178
179         return ret;
180 }
181
182 static int rt5025_set_exttemp_alert(struct rt5025_swjeita_info *swji, int index)
183 {
184         int ret = 0;
185
186         RTINFO("index = %d\n", index);
187
188         switch (index) {
189         case 0:
190                 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[1]);
191                 break;
192         case 1:
193                 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[0]);
194                 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[3]);
195                 break;
196         case 2:
197                 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[2]);
198                 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[5]);
199                 break;
200         case 3:
201                 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[4]);
202                 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[7]);
203                 break;
204         case 4:
205                 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[6]);
206                 break;
207         }
208
209         return ret;
210 }
211
212 static int rt5025_exttemp_alert_switch(struct rt5025_swjeita_info *swji, int onoff)
213 {
214         if (!onoff) {
215                 rt5025_clr_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
216                 rt5025_clr_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
217         } else {
218                 switch (swji->cur_section) {
219                 case 0:
220                         rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
221                         break;
222                 case 1:
223                         rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
224                         rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
225                         break;
226                 case 2:
227                         rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
228                         rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
229                         break;
230                 case 3:
231                         rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
232                         rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
233                         break;
234                 case 4:
235                         rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
236                         break;
237                 }
238         }
239
240         RTINFO("index=%d, onoff=%d\n", swji->cur_section, onoff);
241         return 0;
242 }
243
244 int rt5025_notify_charging_cable(struct rt5025_swjeita_info *swji, int cable_type)
245 {
246         int sect_index;
247         int ret = 0;
248
249         RTINFO("cable_type = %d\n", cable_type);
250
251         rt5025_exttemp_alert_switch(swji, 0);
252
253         sect_index = rt5025_get_external_temp_index(swji);
254         if (swji->cur_section != sect_index || swji->init_once == 0) {
255                 rt5025_set_exttemp_alert(swji, sect_index);
256                 swji->cur_section = sect_index;
257                 swji->init_once = 1;
258         }
259
260         switch (cable_type) {
261         case JEITA_NORMAL_USB:
262                 rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]\
263                         - swji->dec_current);
264                 rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
265                 break;
266         case JEITA_USB_TA:
267                 rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]\
268                         - swji->dec_current);
269                 rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
270                 break;
271         case JEITA_AC_ADAPTER:
272                 rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]\
273                         - swji->dec_current);
274                 rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
275                 break;
276         case JEITA_NO_CHARGE:
277                 rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]);
278                 rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
279                 break;
280         }
281         swji->cur_cable = cable_type;
282
283         rt5025_exttemp_alert_switch(swji, 1);
284
285         return ret;
286 }
287 EXPORT_SYMBOL(rt5025_notify_charging_cable);
288
289 int rt5025_swjeita_irq_handler(struct rt5025_swjeita_info *swji, unsigned char event)
290 {
291         int ret = 0;
292         RTINFO("event = 0x%02x\n", event);
293
294         if (event&(RT5025_TMXEN_MASK | RT5025_TMNEN_MASK))
295                 rt5025_notify_charging_cable(swji, swji->cur_cable);
296
297         return ret;
298 }
299 EXPORT_SYMBOL(rt5025_swjeita_irq_handler);
300
301 static void rt5025_get_internal_temp(struct rt5025_swjeita_info *swji)
302 {
303         u8 data[2];
304         s32 temp;
305         if (rt5025_reg_block_read(swji->i2c, RT5025_REG_INTTEMP_MSB, 2, data) < 0)
306                 pr_err("%s: Failed to read internal TEMPERATURE\n", __func__);
307
308         temp = ((data[0] & 0x1F) << 8) + data[1];
309         temp *= 15625;
310         temp /= 100000;
311
312         temp = (data[0] & 0x20) ? -temp : temp;
313         swji->cur_inttemp = temp;
314
315         RTINFO("internal temperature: %d\n", temp);
316 }
317
318 static void thermal_reg_work_func(struct work_struct *work)
319 {
320         struct delayed_work *delayed_work = (struct delayed_work *)container_of(work, struct delayed_work, work);
321         struct rt5025_swjeita_info *swji = (struct rt5025_swjeita_info *)container_of(delayed_work, struct rt5025_swjeita_info, thermal_reg_work);
322         int therm_region = 0;
323
324         RTINFO("%s ++", __func__);
325         rt5025_get_internal_temp(swji);
326
327         #if 1
328         switch (swji->cur_therm_region) {
329         case 0:
330                 if (swji->cur_inttemp >= 820)
331                         therm_region = 1;
332                 else
333                         therm_region = 0;
334                 break;
335         case 1:
336                 if (swji->cur_inttemp <= 780)
337                         therm_region = 0;
338                 else if (swji->cur_inttemp >= 1020)
339                         therm_region = 2;
340                 else
341                         therm_region = 1;
342                 break;
343         case 2:
344                 if (swji->cur_inttemp <= 980)
345                         therm_region = 1;
346                 else
347                         therm_region = 2;
348                 break;
349                 }
350         #else
351         if (swji->cur_inttemp < 800)
352                 therm_region = 0;
353         else if (swji->cur_inttemp >= 800 && swji->cur_inttemp < 1000)
354                 therm_region = 1;
355         else
356                 therm_region = 2;
357         #endif /* #if 1*/
358
359         if (therm_region != swji->cur_therm_region) {
360                 switch (therm_region) {
361                 case 0:
362                         swji->dec_current = 0;
363                         break;
364                 case 1:
365                         swji->dec_current = 300;
366                         break;
367                 case 2:
368                         swji->dec_current = 800;
369                         break;
370                 }
371                 swji->cur_therm_region = therm_region;
372                 rt5025_notify_charging_cable(swji, swji->cur_cable);
373         }
374
375         if (!swji->suspend)
376                 schedule_delayed_work(&swji->thermal_reg_work, 5*HZ);
377
378         RTINFO("%s --", __func__);
379 }
380
381 static int rt5025_swjeita_probe(struct platform_device *pdev)
382 {
383         struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
384         struct rt5025_platform_data *pdata = chip->dev->platform_data;
385         struct rt5025_swjeita_info *swji;
386         int ret = 0;
387
388         swji = kzalloc(sizeof(*swji), GFP_KERNEL);
389         if (!swji)
390                 return -ENOMEM;
391
392         #if 0 /* for debug pdata->jeita_data*/
393         for (ret = 0; ret < 4; ret++)
394                 RTINFO("jeita temp value %d\n", pdata->jeita_data->temp[ret]);
395         for (ret = 0; ret < 4; ret++) {
396                 RTINFO("jeita temp_cc value %d, %d, %d, %d, %d\n", pdata->jeita_data->temp_cc[ret][0], \
397                 pdata->jeita_data->temp_cc[ret][1], pdata->jeita_data->temp_cc[ret][2], \
398                 pdata->jeita_data->temp_cc[ret][3], pdata->jeita_data->temp_cc[ret][4]);
399         }
400         for (ret = 0; ret < 4; ret++) {
401                 RTINFO("jeita temp_cv value %d, %d, %d, %d, %d\n", pdata->jeita_data->temp_cv[ret][0], \
402                 pdata->jeita_data->temp_cv[ret][1], pdata->jeita_data->temp_cv[ret][2], \
403                 pdata->jeita_data->temp_cv[ret][3], pdata->jeita_data->temp_cv[ret][4]);
404         }
405         for (ret = 0; ret < 8; ret++) {
406                 RTINFO("temp_scalar[%d] = 0x%02x\n", ret, pdata->jeita_data->temp_scalar[ret]);
407         }
408         ret = 0;
409         #endif /* #if 0 */
410
411         swji->i2c = chip->i2c;
412         swji->chip = chip;
413         swji->cur_section = 2;
414         /*initial as the normal temperature*/
415         swji->cur_cable = JEITA_NO_CHARGE;
416         swji->temp = pdata->jeita_data->temp;
417         swji->temp_scalar = pdata->jeita_data->temp_scalar;
418         swji->temp_cc = pdata->jeita_data->temp_cc;
419         swji->temp_cv = pdata->jeita_data->temp_cv;
420         INIT_DELAYED_WORK(&swji->thermal_reg_work, thermal_reg_work_func);
421         platform_set_drvdata(pdev, swji);
422
423         rt5025_set_ainadc_onoff(swji, 1);
424         rt5025_set_intadc_onoff(swji, 1);
425         mdelay(100);
426         rt5025_notify_charging_cable(swji, swji->cur_cable);
427         schedule_delayed_work(&swji->thermal_reg_work, 1*HZ);
428
429         chip->jeita_info = swji;
430         RTINFO("rt5025-swjeita driver is successfully loaded\n");
431         return ret;
432 }
433
434 static int rt5025_swjeita_remove(struct platform_device *pdev)
435 {
436         struct rt5025_swjeita_info *swji = platform_get_drvdata(pdev);
437
438         swji->chip->jeita_info = NULL;
439         kfree(swji);
440         RTINFO("\n");
441         return 0;
442 }
443
444 static int rt5025_swjeita_suspend(struct platform_device *pdev, pm_message_t state)
445 {
446         struct rt5025_swjeita_info *swji = platform_get_drvdata(pdev);
447
448         swji->suspend = 1;
449         cancel_delayed_work_sync(&swji->thermal_reg_work);
450         swji->cur_therm_region = swji->dec_current = 0;
451         rt5025_notify_charging_cable(swji, swji->cur_cable);
452         RTINFO("\n");
453         return 0;
454 }
455
456 static int rt5025_swjeita_resume(struct platform_device *pdev)
457 {
458         struct rt5025_swjeita_info *swji = platform_get_drvdata(pdev);
459
460         swji->suspend = 0;
461         schedule_delayed_work(&swji->thermal_reg_work, 0);
462         RTINFO("\n");
463         return 0;
464 }
465
466 static struct platform_driver rt5025_swjeita_driver = {
467         .driver = {
468                 .name = RT5025_DEVICE_NAME "-swjeita",
469                 .owner = THIS_MODULE,
470         },
471         .probe = rt5025_swjeita_probe,
472         .remove = __devexit_p(rt5025_swjeita_remove),
473         .suspend = rt5025_swjeita_suspend,
474         .resume = rt5025_swjeita_resume,
475 };
476
477 static int rt5025_swjeita_init(void)
478 {
479         return platform_driver_register(&rt5025_swjeita_driver);
480 }
481 module_init(rt5025_swjeita_init);
482
483 static void rt5025_swjeita_exit(void)
484 {
485         platform_driver_unregister(&rt5025_swjeita_driver);
486 }
487 module_exit(rt5025_swjeita_exit);
488
489
490 MODULE_LICENSE("GPL v2");
491 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
492 MODULE_DESCRIPTION("Swjeita driver for RT5025");
493 MODULE_ALIAS("platform:" RT5025_DEVICE_NAME "-swjeita");
494 MODULE_VERSION(RT5025_DRV_VER);