Merge branch 'develop-3.0' of ssh://10.10.10.29/rk/kernel into develop-3.0
[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  10  // '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         RTINFO("onoff = %d\n", onoff);
31         if (onoff)
32                 ret = rt5025_set_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCCEN_MASK);
33         else
34                 ret = rt5025_clr_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCCEN_MASK);
35         return ret;
36 }
37
38 static int rt5025_set_charging_cc(struct i2c_client *i2c, int cur_value)
39 {
40         int ret;
41         u8 data;
42
43         RTINFO("current value = %d\n", cur_value);
44         if (cur_value < 500)
45                 data = 0;
46         else if (cur_value > 2000)
47                 data = 0xf<<RT5025_CHGICC_SHIFT;
48         else
49                 data = ((cur_value-500)/100)<<RT5025_CHGICC_SHIFT;
50
51         ret = rt5025_assign_bits(i2c, RT5025_REG_CHGCTL4, RT5025_CHGICC_MASK, data);
52
53         if (cur_value == 0)
54                 rt5025_set_charging_cc_switch(i2c, 0);
55         else
56                 rt5025_set_charging_cc_switch(i2c, 1);
57
58         return ret;
59 }
60
61 static int rt5025_set_charging_cv(struct i2c_client *i2c, int voltage)
62 {
63         int ret;
64         u8 data;
65
66         RTINFO("voltage = %d\n", voltage);
67         if (voltage < 3500)
68                 data = 0;
69         else if (voltage > 4440)
70                 data = 0x2f<<RT5025_CHGCV_SHIFT;
71         else
72                 data = ((voltage-3500)/20)<<RT5025_CHGCV_SHIFT;
73
74         ret = rt5025_assign_bits(i2c, RT5025_REG_CHGCTL3, RT5025_CHGCV_MASK, data);
75         return ret;
76 }
77
78 static int rt5025_sel_external_temp_index(struct rt5025_swjeita_info *swji)
79 {
80         int temp = swji->cur_temp;
81         int sect_index;
82
83         RTINFO("\n");
84         if (temp < swji->temp[0])
85                 sect_index = 0;
86         else if (temp >= swji->temp[0] && temp < swji->temp[1])
87                 sect_index = 1;
88         else if (temp >= swji->temp[1] && temp < swji->temp[2])
89                 sect_index = 2;
90         else if (temp >= swji->temp[2] && temp < swji->temp[3])
91                 sect_index = 3;
92         else if (temp >= swji->temp[3])
93                 sect_index = 4;
94
95         RTINFO("sect_index = %d\n", sect_index);
96         return sect_index;
97 }
98
99 static int rt5025_get_external_temp_index(struct rt5025_swjeita_info *swji)
100 {
101         u8 data[2];
102         long int temp;
103         int sect_index;
104         
105         RTINFO("\n");
106         if (rt5025_reg_block_read(swji->i2c, RT5025_REG_AINH, 2, data) < 0)
107                 pr_err("%s: failed to read ext_temp register\n", __func__);
108
109         temp = (data[0]*256+data[1])*61/100;
110         temp = (temp * (-91738) +81521000)/100000;
111
112         swji->cur_temp = temp;
113
114         RTINFO("cur_section = %d, cur_temp = %d\n", swji->cur_section, swji->cur_temp);
115
116         switch (swji->cur_section)
117         {
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 int rt5025_set_exttemp_alert(struct rt5025_swjeita_info *swji, int index)
170 {
171         int ret = 0;
172
173         RTINFO("index = %d\n", index);
174
175         switch (index)
176         {
177                 case 0:
178                         rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[0]);
179                         break;
180                 case 1:
181                         rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[0]);
182                         rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[1]);
183                         break;
184                 case 2:
185                         rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[1]);
186                         rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[2]);
187                         break;
188                 case 3:
189                         rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[2]);
190                         rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[3]);
191                         break;
192                 case 4:
193                         rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[3]);
194                         break;
195         }
196
197         return ret;
198 }
199
200 static int rt5025_exttemp_alert_switch(struct rt5025_swjeita_info *swji, int onoff)
201 {
202         if (!onoff)
203         {
204                 rt5025_clr_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
205                 rt5025_clr_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
206         }
207         else
208         {
209                 switch (swji->cur_section)
210                 {
211                         case 0:
212                                 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
213                                 break;
214                         case 1:
215                                 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
216                                 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
217                                 break;
218                         case 2:
219                                 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
220                                 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
221                                 break;
222                         case 3:
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 4:
227                                 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
228                                 break;
229                 }
230         }
231
232         RTINFO("index=%d, onoff=%d\n", swji->cur_section, onoff);
233         return 0;               
234 }
235
236 int rt5025_notify_charging_cable(struct rt5025_swjeita_info *swji, int cable_type)
237 {
238         int sect_index;
239         int ret = 0;
240
241         RTINFO("cable_type = %d\n", cable_type);
242
243         rt5025_exttemp_alert_switch(swji, 0);
244
245         sect_index = rt5025_get_external_temp_index(swji);
246         if (swji->cur_section != sect_index || swji->init_once == 0)
247         {
248                 rt5025_set_exttemp_alert(swji, sect_index);
249                 swji->cur_section = sect_index;
250                 swji->init_once = 1;
251         }
252
253         switch (cable_type)
254         {
255                 case JEITA_NORMAL_USB:
256                         rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]);
257                         rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
258                         break;
259                 case JEITA_USB_TA:
260                         rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]);
261                         rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
262                         break;
263                 case JEITA_AC_ADAPTER:
264                         rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]);
265                         rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
266                         break;
267                 case JEITA_NO_CHARGE:
268                         rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]);
269                         rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
270                         break;
271         }
272         swji->cur_cable = cable_type;
273
274         rt5025_exttemp_alert_switch(swji, 1);
275
276         return ret;
277 }
278 EXPORT_SYMBOL(rt5025_notify_charging_cable);
279
280 int rt5025_swjeita_irq_handler(struct rt5025_swjeita_info *swji, unsigned char event)
281 {
282         int ret = 0;
283         RTINFO("event = 0x%02x\n", event);
284
285         if (event&(RT5025_TMXEN_MASK|RT5025_TMNEN_MASK))
286                 rt5025_notify_charging_cable(swji, swji->cur_cable);
287
288         return ret;
289 }
290 EXPORT_SYMBOL(rt5025_swjeita_irq_handler);
291
292 static int __devinit rt5025_swjeita_probe(struct platform_device *pdev)
293 {
294         struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
295         struct rt5025_platform_data *pdata = chip->dev->platform_data; 
296         struct rt5025_swjeita_info *swji;
297         int ret = 0;
298
299         swji = kzalloc(sizeof(*swji), GFP_KERNEL);
300         if (!swji)
301                 return -ENOMEM;
302
303         #if 0 // for debug pdata->jeita_data
304         for (ret=0; ret<4; ret++)
305                 RTINFO("jeita temp value %d\n", pdata->jeita_data->temp[ret]);
306         for (ret=0; ret<4; ret++)
307         {
308                 RTINFO("jeita temp_cc value %d, %d, %d, %d, %d\n", pdata->jeita_data->temp_cc[ret][0], \
309                 pdata->jeita_data->temp_cc[ret][1], pdata->jeita_data->temp_cc[ret][2], \
310                 pdata->jeita_data->temp_cc[ret][3], pdata->jeita_data->temp_cc[ret][4]);
311         }
312         for (ret=0; ret<4; ret++)
313         {
314                 RTINFO("jeita temp_cv value %d, %d, %d, %d, %d\n", pdata->jeita_data->temp_cv[ret][0], \
315                 pdata->jeita_data->temp_cv[ret][1], pdata->jeita_data->temp_cv[ret][2], \
316                 pdata->jeita_data->temp_cv[ret][3], pdata->jeita_data->temp_cv[ret][4]);
317         }
318         for (ret=0; ret<4; ret++)
319         {
320                 RTINFO("temp_scalar[%d] = 0x%02x\n", ret, pdata->jeita_data->temp_scalar[ret]);
321         }
322         ret = 0;
323         #endif /* #if 0 */
324
325         swji->i2c = chip->i2c;
326         swji->chip = chip;
327         swji->cur_section = 2; //initial as the normal temperature
328         swji->cur_cable = JEITA_NO_CHARGE;
329         swji->temp = pdata->jeita_data->temp;
330         swji->temp_scalar = pdata->jeita_data->temp_scalar;
331         swji->temp_cc = pdata->jeita_data->temp_cc;
332         swji->temp_cv = pdata->jeita_data->temp_cv;
333         platform_set_drvdata(pdev, swji);
334
335         rt5025_set_ainadc_onoff(swji, 1);
336         mdelay(100);
337         rt5025_notify_charging_cable(swji, swji->cur_cable);
338
339         chip->jeita_info = swji;
340         RTINFO("rt5025-swjeita driver is successfully loaded\n");
341         return ret;
342 }
343
344 static int __devexit rt5025_swjeita_remove(struct platform_device *pdev)
345 {
346         struct rt5025_swjeita_info *swji = platform_get_drvdata(pdev);
347
348         swji->chip->jeita_info = NULL;
349         kfree(swji);
350         return 0;
351 }
352
353 static int rt5025_swjeita_suspend(struct platform_device *pdev, pm_message_t state)
354 {
355         struct rt5025_swjeita_info *swji = platform_get_drvdata(pdev);
356         
357         swji->suspend = 1;
358         return 0;
359 }
360
361 static int rt5025_swjeita_resume(struct platform_device *pdev)
362 {
363         struct rt5025_swjeita_info *swji = platform_get_drvdata(pdev);
364
365         swji->suspend = 0;
366         return 0;
367 }
368
369 static struct platform_driver rt5025_swjeita_driver = 
370 {
371         .driver = {
372                 .name = RT5025_DEVICE_NAME "-swjeita",
373                 .owner = THIS_MODULE,
374         },
375         .probe = rt5025_swjeita_probe,
376         .remove = __devexit_p(rt5025_swjeita_remove),
377         .suspend = rt5025_swjeita_suspend,
378         .resume = rt5025_swjeita_resume,
379 };
380
381 static int __init rt5025_swjeita_init(void)
382 {
383         return platform_driver_register(&rt5025_swjeita_driver);
384 }
385 module_init(rt5025_swjeita_init);
386
387 static void __exit rt5025_swjeita_exit(void)
388 {
389         platform_driver_unregister(&rt5025_swjeita_driver);
390 }
391 module_exit(rt5025_swjeita_exit);
392
393
394 MODULE_LICENSE("GPL v2");
395 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
396 MODULE_DESCRIPTION("Swjeita driver for RT5025");
397 MODULE_ALIAS("platform:" RT5025_DEVICE_NAME "-swjeita");