import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / misc / mediatek / chargepump / rt8542 / rt8542_driver.c
CommitLineData
6fa3eb70
S
1/*
2 * This software program is licensed subject to the GNU General Public License
3 * (GPL).Version 2,June 1991, available at http://www.fsf.org/copyleft/gpl.html
4
5 * (C) Copyright 2011 Bosch Sensortec GmbH
6 * All Rights Reserved
7 */
8
9
10/* file rt8542.c
11 brief This file contains all function implementations for the rt8542 in linux
12 this source file refer to MT6572 platform
13*/
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/i2c.h>
18#include <linux/input.h>
19#include <linux/workqueue.h>
20#include <linux/mutex.h>
21#include <linux/slab.h>
22#include <linux/mutex.h>
23#ifdef CONFIG_HAS_EARLYSUSPEND
24#include <linux/earlysuspend.h>
25#endif
26#include <linux/kernel.h>
27#include <linux/delay.h>
28
29#include <mach/mt_typedefs.h>
30#include <mach/mt_gpio.h>
31#include <cust_gpio_usage.h>
32#include <mach/mt_pm_ldo.h>
33
34#include <linux/platform_device.h>
35#include <cust_acc.h>
36
37#include <linux/hwmsensor.h>
38#include <linux/hwmsen_dev.h>
39#include <linux/sensors_io.h>
40#include <linux/hwmsen_helper.h>
41
42#include <linux/leds.h>
43
44#define LCD_LED_MAX 0x7F
45#define LCD_LED_MIN 0
46
47#define DEFAULT_BRIGHTNESS 0x73 //for 20mA
48#define RT8542_MIN_VALUE_SETTINGS 20 /* value leds_brightness_set*/
49#define RT8542_MAX_VALUE_SETTINGS 255 /* value leds_brightness_set*/
50#define MIN_MAX_SCALE(x) (((x)<RT8542_MIN_VALUE_SETTINGS) ? RT8542_MIN_VALUE_SETTINGS : (((x)>RT8542_MAX_VALUE_SETTINGS) ? RT8542_MAX_VALUE_SETTINGS:(x)))
51
52#define BACKLIHGT_NAME "charge-pump"
53
54#define RT8542_GET_BITSLICE(regvar, bitname)\
55 ((regvar & bitname##__MSK) >> bitname##__POS)
56
57#define RT8542_SET_BITSLICE(regvar, bitname, val)\
58 ((regvar & ~bitname##__MSK) | ((val<<bitname##__POS)&bitname##__MSK))
59
60#define RT8542_DEV_NAME "charge-pump"
61
62#define CPD_TAG "[ChargePump] "
63#define CPD_FUN(f) printk(CPD_TAG"%s\n", __FUNCTION__)
64#define CPD_ERR(fmt, args...) printk(CPD_TAG"%s %d : "fmt, __FUNCTION__, __LINE__, ##args)
65#define CPD_LOG(fmt, args...) printk(CPD_TAG fmt, ##args)
66
67// I2C variable
68static struct i2c_client *new_client = NULL;
69static const struct i2c_device_id rt8542_i2c_id[] = {{RT8542_DEV_NAME,0},{}};
70static struct i2c_board_info __initdata i2c_rt8542={ I2C_BOARD_INFO("charge-pump", 0x39)};
71
72// Flash control
73unsigned char strobe_ctrl;
74unsigned char flash_ctrl;
75unsigned char flash_status;
76#define GPIO_LCD_BL_EN GPIO_LCM_BL_EN
77#define GPIO_LCD_BL_EN_M_GPIO GPIO_LCM_BL_EN_M_GPIO
78// Gamma 2.2 Table
79unsigned char bright_arr[] = {
80 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12,
81 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 23, 24,
82 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 38, 39, 40, 41, 43, 44, 45,
83 47, 48, 49, 51, 52, 54, 55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 73, 74, 76, 78,
84 80, 82, 84, 86, 89, 91, 93, 95, 97, 100, 102, 104, 106, 109, 111, 114, 116, 119, 121, 124, 127};
85
86static unsigned char current_brightness = 0;
87static unsigned char is_suspend = 0;
88
89struct semaphore rt8542_lock;
90
91/* generic */
92#define RT8542_MAX_RETRY_I2C_XFER (100)
93#define RT8542_I2C_WRITE_DELAY_TIME 1
94
95typedef struct
96{
97 kal_bool bat_exist;
98 kal_bool bat_full;
99 kal_bool bat_low;
100 UINT32 bat_charging_state;
101 UINT32 bat_vol;
102 kal_bool charger_exist;
103 UINT32 pre_charging_current;
104 UINT32 charging_current;
105 INT32 charger_vol;
106 UINT32 charger_protect_status;
107 UINT32 ISENSE;
108 UINT32 ICharging;
109 INT32 temperature;
110 UINT32 total_charging_time;
111 UINT32 PRE_charging_time;
112 UINT32 CC_charging_time;
113 UINT32 TOPOFF_charging_time;
114 UINT32 POSTFULL_charging_time;
115 UINT32 charger_type;
116 UINT32 PWR_SRC;
117 UINT32 SOC;
118 UINT32 ADC_BAT_SENSE;
119 UINT32 ADC_I_SENSE;
120} PMU_ChargerStruct;
121
122extern PMU_ChargerStruct BMT_status;
123
124/* i2c read routine for API*/
125static char rt8542_i2c_read(struct i2c_client *client, u8 reg_addr,
126 u8 *data, u8 len)
127{
128#if !defined BMA_USE_BASIC_I2C_FUNC
129 s32 dummy;
130 if (NULL == client)
131 return -1;
132
133 while (0 != len--) {
134#ifdef BMA_SMBUS
135 dummy = i2c_smbus_read_byte_data(client, reg_addr);
136 if (dummy < 0) {
137 CPD_ERR("i2c bus read error");
138 return -1;
139 }
140 *data = (u8)(dummy & 0xff);
141#else
142 dummy = i2c_master_send(client, (char *)&reg_addr, 1);
143 if (dummy < 0)
144 {
145 printk("send dummy is %d", dummy);
146 return -1;
147 }
148
149 dummy = i2c_master_recv(client, (char *)data, 1);
150 if (dummy < 0)
151 {
152 printk("recv dummy is %d", dummy);
153 return -1;
154 }
155#endif
156 reg_addr++;
157 data++;
158 }
159 return 0;
160#else
161 int retry;
162
163 struct i2c_msg msg[] = {
164 {
165 .addr = client->addr,
166 .flags = 0,
167 .len = 1,
168 .buf = &reg_addr,
169 },
170
171 {
172 .addr = client->addr,
173 .flags = I2C_M_RD,
174 .len = len,
175 .buf = data,
176 },
177 };
178
179 for (retry = 0; retry < RT8542_MAX_RETRY_I2C_XFER; retry++) {
180 if (i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)) > 0)
181 break;
182 else
183 mdelay(RT8542_I2C_WRITE_DELAY_TIME);
184 }
185
186 if (RT8542_MAX_RETRY_I2C_XFER <= retry) {
187 CPD_ERR("I2C xfer error");
188 return -EIO;
189 }
190
191 return 0;
192#endif
193}
194
195/* i2c write routine for */
196static char rt8542_i2c_write(struct i2c_client *client, u8 reg_addr,
197 u8 *data, u8 len)
198{
199#if !defined BMA_USE_BASIC_I2C_FUNC
200 s32 dummy;
201#ifndef BMA_SMBUS
202 u8 buffer[2];
203#endif
204
205 if (NULL == client)
206 return -1;
207
208 while (0 != len--) {
209 #if 1
210 dummy = i2c_smbus_write_byte_data(client, reg_addr, *data);
211#else
212 buffer[0] = reg_addr;
213 buffer[1] = *data;
214 dummy = i2c_master_send(client, (char *)buffer, 2);
215 #endif
216
217 reg_addr++;
218 data++;
219 if (dummy < 0) {
220 return -1;
221 }
222 }
223
224#else
225 u8 buffer[2];
226 int retry;
227 struct i2c_msg msg[] = {
228 {
229 .addr = client->addr,
230 .flags = 0,
231 .len = 2,
232 .buf = buffer,
233 },
234 };
235
236 while (0 != len--) {
237 buffer[0] = reg_addr;
238 buffer[1] = *data;
239 for (retry = 0; retry < RT8542_MAX_RETRY_I2C_XFER; retry++) {
240 if (i2c_transfer(client->adapter, msg,
241 ARRAY_SIZE(msg)) > 0) {
242 break;
243 } else {
244 mdelay(RT8542_I2C_WRITE_DELAY_TIME);
245 }
246 }
247 if (RT8542_MAX_RETRY_I2C_XFER <= retry) {
248 return -EIO;
249 }
250 reg_addr++;
251 data++;
252 }
253#endif
254 printk("\n [RT8542] rt8542_i2c_write \n");
255 return 0;
256}
257
258static int rt8542_smbus_read_byte(struct i2c_client *client,
259 unsigned char reg_addr, unsigned char *data)
260{
261 return rt8542_i2c_read(client,reg_addr,data,1);
262}
263
264static int rt8542_smbus_write_byte(struct i2c_client *client,
265 unsigned char reg_addr, unsigned char *data)
266{
267 int ret_val = 0;
268 int i = 0;
269
270 ret_val = rt8542_i2c_write(client,reg_addr,data,1);
271
272 for ( i = 0; i < 5; i++)
273 {
274 if (ret_val != 0)
275 rt8542_i2c_write(client,reg_addr,data,1);
276 else
277 return ret_val;
278 }
279 return ret_val;
280}
281
282static int rt8542_smbus_read_byte_block(struct i2c_client *client,
283 unsigned char reg_addr, unsigned char *data, unsigned char len)
284{
285 return rt8542_i2c_read(client,reg_addr,data,len);
286}
287
288bool check_charger_pump_vendor()
289{
290 int err = 0;
291 unsigned char data = 0;
292
293 err = rt8542_smbus_read_byte(new_client,0x01,&data);
294
295 if(err < 0)
296 printk(KERN_ERR "%s read charge-pump vendor id fail\n", __func__);
297
298 printk(KERN_INFO "%s vendor is 0x%x\n", __func__, data&0x03);
299
300 if((data&0x03) == 0x03) //Richtek
301 return FALSE;
302 else
303 return TRUE;
304}
305
306int chargepump_set_backlight_level(unsigned int level)
307{
308 unsigned char data = 0;
309 unsigned char data1 = 0;
310 unsigned int bright_per = 0;
311
312 printk("\n[RT8542] chargepump_set_backlight_level [%d]\n",data1);
313
314 if (level == 0)
315 {
316 printk( "------ backlight_level suspend-----\n");
317 down_interruptible(&rt8542_lock);
318 data1 = 0x00; //backlight2 brightness 0
319 rt8542_smbus_write_byte(new_client, 0x05, &data1);
320
321 rt8542_smbus_read_byte(new_client, 0x0A, &data1);
322 data1 &= 0x66;
323
324 rt8542_smbus_write_byte(new_client, 0x0A, &data1);
325 up(&rt8542_lock);
326 printk( "[RT8542] flash_status = %d\n", flash_status);
327 if( flash_status == 0 ){
328 mt_set_gpio_out(GPIO_LCD_BL_EN,GPIO_OUT_ZERO);
329 }
330 is_suspend = 1;
331 }
332 else
333 {
334 level = MIN_MAX_SCALE(level);
335 printk(KERN_ERR "[RT8542] level = %d", level);
336
337 //Gamma 2.2 Table adapted
338 bright_per = (level - (unsigned int)20) *(unsigned int)100 / (unsigned int)235;
339 data = bright_arr[bright_per];
340
341 //printk("[Backlight] %s bright_per = %d, data = %d\n", __func__, bright_per, data);
342
343 if (is_suspend == 1)
344 {
345 //printk( "------ backlight_level resume-----\n");
346 is_suspend = 0;
347 mt_set_gpio_out(GPIO_LCD_BL_EN,GPIO_OUT_ONE);
348 mdelay(10);
349 down_interruptible(&rt8542_lock);
350 if(check_charger_pump_vendor() == FALSE)
351 {
352 data1 = 0x54;//0x37;
353 rt8542_smbus_write_byte(new_client, 0x02, &data1);
354 printk("[ChargePump]-Richtek\n");
355 }
356 else
357 {
358 data1 = 0x54;//0x57;
359 rt8542_smbus_write_byte(new_client, 0x02, &data1);
360 printk("[RT8542]-TI\n");
361 }
362
363 rt8542_smbus_write_byte(new_client, 0x05, &data);
364 printk("[RT8542]-backlight brightness Setting[reg0x05][value:0x%x]\n",data);
365
366 rt8542_smbus_read_byte(new_client, 0x0A, &data1);
367 data1 |= 0x19;
368
369 rt8542_smbus_write_byte(new_client, 0x0A, &data1);
370 up(&rt8542_lock);
371 }
372
373 down_interruptible(&rt8542_lock);
374 {
375 unsigned char read_data = 0;
376
377 rt8542_smbus_read_byte(new_client, 0x02, &read_data);
378 printk("[RT8542]-OVP[0x%x]\n",read_data);
379 }
380
381 printk("[RT8542]-backlight Seting[reg0x05][value:0x%x]\n",data);
382 rt8542_smbus_write_byte(new_client, 0x05, &data);
383 up(&rt8542_lock);
384 }
385 return 0;
386}
387
388static int rt8542_probe(struct i2c_client *client,
389 const struct i2c_device_id *id)
390{
391 new_client = client;
392
393 CPD_FUN();
394 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
395 CPD_LOG("i2c_check_functionality error\n");
396 return -1;
397 }
398
399 sema_init(&rt8542_lock, 1);
400
401 if (client == NULL)
402 printk("%s client is NULL\n", __func__);
403 else
404 {
405 printk("%s %p %x %x\n", __func__, client->adapter, client->addr, client->flags);
406 }
407 return 0;
408}
409
410
411static int rt8542_remove(struct i2c_client *client)
412{
413 new_client = NULL;
414 return 0;
415}
416
417
418static int __attribute__ ((unused)) rt8542_detect(struct i2c_client *client, int kind, struct i2c_board_info *info)
419{
420 return 0;
421}
422
423static struct i2c_driver rt8542_i2c_driver = {
424 .driver = {
425// .owner = THIS_MODULE,
426 .name = RT8542_DEV_NAME,
427 },
428 .probe = rt8542_probe,
429 .remove = rt8542_remove,
430// .detect = rt8542_detect,
431 .id_table = rt8542_i2c_id,
432// .address_data = &rt8542250_i2c_addr_data,
433};
434
435static int rt8542_pd_probe(struct platform_device *pdev)
436{
437 mt_set_gpio_mode(GPIO_LCD_BL_EN, GPIO_LCD_BL_EN_M_GPIO);
438 mt_set_gpio_pull_enable(GPIO_LCD_BL_EN, GPIO_PULL_ENABLE);
439 mt_set_gpio_dir(GPIO_LCD_BL_EN, GPIO_DIR_OUT);
440
441 i2c_add_driver(&rt8542_i2c_driver);
442 return 0;
443}
444
445static int __attribute__ ((unused)) rt8542_pd_remove(struct platform_device *pdev)
446{
447 CPD_FUN();
448 i2c_del_driver(&rt8542_i2c_driver);
449 return 0;
450}
451
452#ifdef CONFIG_HAS_EARLYSUSPEND
453static void rt8542_early_suspend(struct early_suspend *h)
454{
455 int err = 0;
456 unsigned char data;
457 down_interruptible(&rt8542_lock);
458 data = 0x00; //backlight2 brightness 0
459 err = rt8542_smbus_write_byte(new_client, 0x05, &data);
460
461 err = rt8542_smbus_read_byte(new_client, 0x0A, &data);
462 data &= 0x66;
463
464 err = rt8542_smbus_write_byte(new_client, 0x0A, &data);
465 up(&rt8542_lock);
466 printk("\n[RT8542] rt8542_early_suspend [%d]",data);
467 mt_set_gpio_out(GPIO_LCD_BL_EN,GPIO_OUT_ZERO);
468}
469
470void rt8542_flash_strobe_prepare(char OnOff,char ActiveHigh)
471{
472 int err = 0;
473
474 down_interruptible(&rt8542_lock);
475
476 err = rt8542_smbus_read_byte(new_client, 0x09, &strobe_ctrl);
477
478 err = rt8542_smbus_read_byte(new_client, 0x0A, &flash_ctrl);
479
480 strobe_ctrl &= 0xF3;
481
482 if(ActiveHigh)
483 {
484 strobe_ctrl |= 0x20;
485 }
486 else
487 {
488 strobe_ctrl &= 0xDF;
489 }
490
491 if(OnOff == 1)
492 {
493 CPD_LOG("Strobe mode On\n");
494 strobe_ctrl |= 0x10;
495 flash_ctrl |= 0x66;
496 flash_status = 1;
497 printk( "[RT8542][Strobe] flash_status = %d\n", flash_status);
498 }
499 else if(OnOff == 2)
500 {
501 CPD_LOG("Torch mode On\n");
502 strobe_ctrl |= 0x10;
503 flash_ctrl |= 0x62;
504 flash_status = 1;
505 printk( "[RT8542][Torch] flash_status = %d\n", flash_status);
506 }
507 else
508 {
509 CPD_LOG("Flash Off\n");
510 strobe_ctrl &= 0xEF;
511 flash_ctrl &= 0x99;
512 flash_status = 0;
513 printk( "[RT8542][off] flash_status = %d\n", flash_status);
514 }
515
516 err = rt8542_smbus_write_byte(new_client, 0x09, &strobe_ctrl);
517
518 up(&rt8542_lock);
519}
520
521//strobe enable
522void rt8542_flash_strobe_en()
523{
524 int err = 0;
525 down_interruptible(&rt8542_lock);
526 err = rt8542_smbus_write_byte(new_client, 0x0A, &flash_ctrl);
527 up(&rt8542_lock);
528}
529
530//strobe level
531void rt8542_flash_strobe_level(char level)
532{
533 int err = 0;
534 unsigned char data1=0;
535 unsigned char data2=0;
536 unsigned char torch_level;
537 unsigned char strobe_timeout = 0x1F;
538 down_interruptible(&rt8542_lock);
539#if 0
540 if( level == 1)
541 {
542 torch_level = 0x20;
543 }
544 else
545 {
546 torch_level = 0x50;
547 }
548
549 err = rt8542_smbus_read_byte(new_client, 0x06, &data1);
550
551 if(31 < level)
552 {
553 data1= torch_level | 0x0A;
554 strobe_timeout = 0x0F;
555 }
556 else if(level < 0)
557 {
558 data1= torch_level ;
559 }
560 else
561 {
562 data1= torch_level | level;
563 }
564
565#else
566 torch_level = 0x50;
567
568 err = rt8542_smbus_read_byte(new_client, 0x06, &data1);
569
570 strobe_timeout = 0x0F;
571 if(level < 0)
572 data1= torch_level;
573 else if(level == 1)
574 data1= torch_level | 0x03;
575 else if(level == 2)
576 data1= torch_level | 0x05;
577 else if(level == 3)
578 data1= torch_level | 0x08;
579 else if(level == 4)
580 data1= torch_level | 0x0A;
581 else
582 data1= torch_level | level;
583
584#endif
585
586/*
587 if(0)
588 {
589 CPD_LOG("Batt temp=%d\n", BMT_status.temperature );
590
591 torch_level = 0xF0 & data1;
592 level = 0x0F & data1;
593 torch_level = 0xF0 & (torch_level >> 2);
594 level = 0x0F & (level >> 2);
595
596 data1 = torch_level | level;
597 }
598*/
599 CPD_LOG("Flash Level =0x%x\n", data1);
600 err = rt8542_smbus_write_byte(new_client, 0x06, &data1);
601
602 data2 = 0x40 | strobe_timeout;
603 CPD_LOG("Storbe Timeout =0x%x\n", data2);
604 err |= rt8542_smbus_write_byte(new_client, 0x07, &data2);
605 up(&rt8542_lock);
606}
607
608static void rt8542_late_resume(struct early_suspend *h)
609{
610 int err = 0;
611 unsigned char data1;
612
613 mt_set_gpio_out(GPIO_LCD_BL_EN, GPIO_OUT_ONE);
614 mdelay(50);
615 down_interruptible(&rt8542_lock);
616 err = rt8542_smbus_write_byte(new_client, 0x05, &current_brightness);
617
618 err = rt8542_smbus_read_byte(new_client, 0x0A, &data1);
619 data1 |= 0x19;//backlight enable
620
621 err = rt8542_smbus_write_byte(new_client, 0x0A, &data1);
622 up(&rt8542_lock);
623 printk("\n [RT8542] rt8542_late_resume [%d]",data1);
624}
625
626static struct early_suspend __attribute__ ((unused)) rt8542_early_suspend_desc = {
627 .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN,
628 .suspend = rt8542_early_suspend,
629 .resume = rt8542_late_resume,
630};
631#endif
632
633static struct platform_driver rt8542_backlight_driver = {
634 .probe = rt8542_pd_probe,
635 .remove = rt8542_pd_remove,
636 .driver = {
637 .name = "charge-pump",
638 .owner = THIS_MODULE,
639 }
640};
641
642static struct platform_device mtk_backlight_dev = {
643 .name = "charge-pump",
644 .id = -1,};
645
646static int __init rt8542_init(void)
647{
648 CPD_FUN();
649 if (platform_device_register(&mtk_backlight_dev))
650 {
651 CPD_ERR("failed to register device");
652 return -1;
653
654 }
655 //i2c number 1(0~2) control
656 i2c_register_board_info(3, &i2c_rt8542, 1);
657
658 #ifndef CONFIG_MTK_LEDS
659 register_early_suspend(&rt8542_early_suspend_desc);
660 #endif
661
662 if(platform_driver_register(&rt8542_backlight_driver))
663 {
664 CPD_ERR("failed to register driver");
665 return -1;
666 }
667
668 return 0;
669}
670
671static void __exit rt8542_exit(void)
672{
673 platform_driver_unregister(&rt8542_backlight_driver);
674}
675
676EXPORT_SYMBOL(rt8542_flash_strobe_en);
677EXPORT_SYMBOL(rt8542_flash_strobe_prepare);
678EXPORT_SYMBOL(rt8542_flash_strobe_level);
679MODULE_AUTHOR("Albert Zhang <xu.zhang@bosch-sensortec.com>");
680MODULE_DESCRIPTION("rt8542 driver");
681MODULE_LICENSE("GPL");
682
683module_init(rt8542_init);
684module_exit(rt8542_exit);
685