1 /* drivers/power/rt5025-swjeita.c
2 * swjeita Driver for Richtek RT5025 PMIC
3 * Multi function device - multi functional baseband PMIC swjeita part
6 * Author: CY Huang <cy_huang@richtek.com>
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.
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>
22 #include <linux/mfd/rt5025.h>
23 #include <linux/power/rt5025-swjeita.h>
25 #define TEMP_TOLERANCE 30 // 'c*10 gap for tolerance
27 static int rt5025_set_charging_cc_switch (struct i2c_client *i2c, int onoff)
30 RTINFO("onoff = %d\n", onoff);
32 ret = rt5025_set_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCCEN_MASK);
34 ret = rt5025_clr_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCCEN_MASK);
38 static int rt5025_set_charging_cc(struct i2c_client *i2c, int cur_value)
43 RTINFO("current value = %d\n", cur_value);
46 else if (cur_value > 2000)
47 data = 0xf<<RT5025_CHGICC_SHIFT;
49 data = ((cur_value-500)/100)<<RT5025_CHGICC_SHIFT;
51 ret = rt5025_assign_bits(i2c, RT5025_REG_CHGCTL4, RT5025_CHGICC_MASK, data);
54 rt5025_set_charging_cc_switch(i2c, 0);
56 rt5025_set_charging_cc_switch(i2c, 1);
61 static int rt5025_set_charging_cv(struct i2c_client *i2c, int voltage)
66 RTINFO("voltage = %d\n", voltage);
69 else if (voltage > 4440)
70 data = 0x2f<<RT5025_CHGCV_SHIFT;
72 data = ((voltage-3500)/20)<<RT5025_CHGCV_SHIFT;
74 ret = rt5025_assign_bits(i2c, RT5025_REG_CHGCTL3, RT5025_CHGCV_MASK, data);
78 static int rt5025_sel_external_temp_index(struct rt5025_swjeita_info *swji)
80 int temp = swji->cur_temp;
84 if (temp < swji->temp[0])
86 else if (temp >= swji->temp[0] && temp < swji->temp[1])
88 else if (temp >= swji->temp[1] && temp < swji->temp[2])
90 else if (temp >= swji->temp[2] && temp < swji->temp[3])
92 else if (temp >= swji->temp[3])
95 RTINFO("sect_index = %d\n", sect_index);
99 static int rt5025_get_external_temp_index(struct rt5025_swjeita_info *swji)
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__);
109 temp = (data[0]*256+data[1])*61/100;
110 temp = (temp * (-91738) +81521000)/100000;
112 swji->cur_temp = temp;
114 RTINFO("cur_section = %d, cur_temp = %d\n", swji->cur_section, swji->cur_temp);
116 switch (swji->cur_section)
119 if (temp < swji->temp[0]+TEMP_TOLERANCE)
120 sect_index = rt5025_sel_external_temp_index(swji);
122 sect_index = swji->cur_section;
125 if (temp <= swji->temp[0]-TEMP_TOLERANCE || temp >= swji->temp[1]+TEMP_TOLERANCE)
126 sect_index = rt5025_sel_external_temp_index(swji);
128 sect_index = swji->cur_section;
131 if (temp <= swji->temp[1]-TEMP_TOLERANCE || temp >= swji->temp[2]+TEMP_TOLERANCE)
132 sect_index = rt5025_sel_external_temp_index(swji);
134 sect_index = swji->cur_section;
137 if (temp <= swji->temp[2]-TEMP_TOLERANCE || temp >= swji->temp[3]+TEMP_TOLERANCE)
138 sect_index = rt5025_sel_external_temp_index(swji);
140 sect_index = swji->cur_section;
143 if (temp <= swji->temp[3]-TEMP_TOLERANCE)
144 sect_index = rt5025_sel_external_temp_index(swji);
146 sect_index = swji->cur_section;
149 sect_index = swji->cur_section;
152 RTINFO("sect_index = %d\n", sect_index);
156 static inline int rt5025_set_ainadc_onoff(struct rt5025_swjeita_info *swji, int enable)
160 RTINFO("enable = %d\n", enable);
162 ret = rt5025_set_bits(swji->i2c, RT5025_REG_CHANNELL, RT5025_AINEN_MASK);
164 ret = rt5025_clr_bits(swji->i2c, RT5025_REG_CHANNELL, RT5025_AINEN_MASK);
169 static inline int rt5025_set_intadc_onoff(struct rt5025_swjeita_info *swji, int enable)
173 RTINFO("enable = %d\n", enable);
175 ret = rt5025_set_bits(swji->i2c, RT5025_REG_CHANNELL, RT5025_INTEN_MASK);
177 ret = rt5025_clr_bits(swji->i2c, RT5025_REG_CHANNELL, RT5025_INTEN_MASK);
182 static int rt5025_set_exttemp_alert(struct rt5025_swjeita_info *swji, int index)
186 RTINFO("index = %d\n", index);
191 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[1]);
194 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[0]);
195 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[3]);
198 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[2]);
199 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[5]);
202 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[4]);
203 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMIN, swji->temp_scalar[7]);
206 rt5025_reg_write(swji->i2c, RT5025_REG_TALRTMAX, swji->temp_scalar[6]);
213 static int rt5025_exttemp_alert_switch(struct rt5025_swjeita_info *swji, int onoff)
217 rt5025_clr_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
218 rt5025_clr_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
222 switch (swji->cur_section)
225 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
228 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
229 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
232 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
233 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
236 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
237 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMNEN_MASK);
240 rt5025_set_bits(swji->i2c, RT5025_REG_IRQCTL, RT5025_TMXEN_MASK);
245 RTINFO("index=%d, onoff=%d\n", swji->cur_section, onoff);
249 int rt5025_notify_charging_cable(struct rt5025_swjeita_info *swji, int cable_type)
254 RTINFO("cable_type = %d\n", cable_type);
256 rt5025_exttemp_alert_switch(swji, 0);
258 sect_index = rt5025_get_external_temp_index(swji);
259 if (swji->cur_section != sect_index || swji->init_once == 0)
261 rt5025_set_exttemp_alert(swji, sect_index);
262 swji->cur_section = sect_index;
268 case JEITA_NORMAL_USB:
269 rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]\
270 - swji->dec_current);
271 rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
274 rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]\
275 - swji->dec_current);
276 rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
278 case JEITA_AC_ADAPTER:
279 rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]\
280 - swji->dec_current);
281 rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
283 case JEITA_NO_CHARGE:
284 rt5025_set_charging_cc(swji->i2c, swji->temp_cc[cable_type][swji->cur_section]);
285 rt5025_set_charging_cv(swji->i2c, swji->temp_cv[cable_type][swji->cur_section]);
288 swji->cur_cable = cable_type;
290 rt5025_exttemp_alert_switch(swji, 1);
294 EXPORT_SYMBOL(rt5025_notify_charging_cable);
296 int rt5025_swjeita_irq_handler(struct rt5025_swjeita_info *swji, unsigned char event)
299 RTINFO("event = 0x%02x\n", event);
301 if (event&(RT5025_TMXEN_MASK|RT5025_TMNEN_MASK))
302 rt5025_notify_charging_cable(swji, swji->cur_cable);
306 EXPORT_SYMBOL(rt5025_swjeita_irq_handler);
308 static void rt5025_get_internal_temp(struct rt5025_swjeita_info *swji)
312 if (rt5025_reg_block_read(swji->i2c, RT5025_REG_INTTEMP_MSB, 2, data) < 0){
313 pr_err("%s: Failed to read internal TEMPERATURE\n", __func__);
316 temp = ((data[0]&0x1F)<<8) + data[1];
320 temp = (data[0]&0x20)?-temp:temp;
321 swji->cur_inttemp = temp;
323 RTINFO("internal temperature: %d\n", temp);
326 static void thermal_reg_work_func(struct work_struct *work)
328 struct delayed_work *delayed_work = (struct delayed_work *)container_of(work, struct delayed_work, work);
329 struct rt5025_swjeita_info *swji = (struct rt5025_swjeita_info *)container_of(delayed_work, struct rt5025_swjeita_info, thermal_reg_work);
330 int therm_region = 0;
332 RTINFO("%s ++", __func__);
333 rt5025_get_internal_temp(swji);
336 switch (swji->cur_therm_region)
339 if (swji->cur_inttemp >=820)
345 if (swji->cur_inttemp <= 780)
347 else if (swji->cur_inttemp >= 1020)
353 if (swji->cur_inttemp <= 980)
361 if (swji->cur_inttemp < 800)
363 else if (swji->cur_inttemp >= 800 && swji->cur_inttemp < 1000)
369 if (therm_region != swji->cur_therm_region)
371 switch (therm_region)
374 swji->dec_current = 0;
377 swji->dec_current = 300;
380 swji->dec_current = 800;
383 swji->cur_therm_region = therm_region;
384 rt5025_notify_charging_cable(swji, swji->cur_cable);
388 schedule_delayed_work(&swji->thermal_reg_work, 5*HZ);
390 RTINFO("%s --", __func__);
393 static int __devinit rt5025_swjeita_probe(struct platform_device *pdev)
395 struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
396 struct rt5025_platform_data *pdata = chip->dev->platform_data;
397 struct rt5025_swjeita_info *swji;
400 swji = kzalloc(sizeof(*swji), GFP_KERNEL);
404 #if 0 // for debug pdata->jeita_data
405 for (ret=0; ret<4; ret++)
406 RTINFO("jeita temp value %d\n", pdata->jeita_data->temp[ret]);
407 for (ret=0; ret<4; ret++)
409 RTINFO("jeita temp_cc value %d, %d, %d, %d, %d\n", pdata->jeita_data->temp_cc[ret][0], \
410 pdata->jeita_data->temp_cc[ret][1], pdata->jeita_data->temp_cc[ret][2], \
411 pdata->jeita_data->temp_cc[ret][3], pdata->jeita_data->temp_cc[ret][4]);
413 for (ret=0; ret<4; ret++)
415 RTINFO("jeita temp_cv value %d, %d, %d, %d, %d\n", pdata->jeita_data->temp_cv[ret][0], \
416 pdata->jeita_data->temp_cv[ret][1], pdata->jeita_data->temp_cv[ret][2], \
417 pdata->jeita_data->temp_cv[ret][3], pdata->jeita_data->temp_cv[ret][4]);
419 for (ret=0; ret<8; ret++)
421 RTINFO("temp_scalar[%d] = 0x%02x\n", ret, pdata->jeita_data->temp_scalar[ret]);
426 swji->i2c = chip->i2c;
428 swji->cur_section = 2; //initial as the normal temperature
429 swji->cur_cable = JEITA_NO_CHARGE;
430 swji->temp = pdata->jeita_data->temp;
431 swji->temp_scalar = pdata->jeita_data->temp_scalar;
432 swji->temp_cc = pdata->jeita_data->temp_cc;
433 swji->temp_cv = pdata->jeita_data->temp_cv;
434 INIT_DELAYED_WORK(&swji->thermal_reg_work, thermal_reg_work_func);
435 platform_set_drvdata(pdev, swji);
437 rt5025_set_ainadc_onoff(swji, 1);
438 rt5025_set_intadc_onoff(swji, 1);
440 rt5025_notify_charging_cable(swji, swji->cur_cable);
441 schedule_delayed_work(&swji->thermal_reg_work, 1*HZ);
443 chip->jeita_info = swji;
444 RTINFO("rt5025-swjeita driver is successfully loaded\n");
448 static int __devexit rt5025_swjeita_remove(struct platform_device *pdev)
450 struct rt5025_swjeita_info *swji = platform_get_drvdata(pdev);
452 swji->chip->jeita_info = NULL;
458 static int rt5025_swjeita_suspend(struct platform_device *pdev, pm_message_t state)
460 struct rt5025_swjeita_info *swji = platform_get_drvdata(pdev);
462 cancel_delayed_work_sync(&swji->thermal_reg_work);
463 swji->cur_therm_region = swji->dec_current = 0;
464 rt5025_notify_charging_cable(swji, swji->cur_cable);
469 static int rt5025_swjeita_resume(struct platform_device *pdev)
471 struct rt5025_swjeita_info *swji = platform_get_drvdata(pdev);
474 schedule_delayed_work(&swji->thermal_reg_work, 0);
479 static struct platform_driver rt5025_swjeita_driver =
482 .name = RT5025_DEVICE_NAME "-swjeita",
483 .owner = THIS_MODULE,
485 .probe = rt5025_swjeita_probe,
486 .remove = __devexit_p(rt5025_swjeita_remove),
487 .suspend = rt5025_swjeita_suspend,
488 .resume = rt5025_swjeita_resume,
491 static int __init rt5025_swjeita_init(void)
493 return platform_driver_register(&rt5025_swjeita_driver);
495 module_init(rt5025_swjeita_init);
497 static void __exit rt5025_swjeita_exit(void)
499 platform_driver_unregister(&rt5025_swjeita_driver);
501 module_exit(rt5025_swjeita_exit);
504 MODULE_LICENSE("GPL v2");
505 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
506 MODULE_DESCRIPTION("Swjeita driver for RT5025");
507 MODULE_ALIAS("platform:" RT5025_DEVICE_NAME "-swjeita");
508 MODULE_VERSION(RT5025_DRV_VER);