arm: configs: linux: enable wifi load driver when boot kernel
[firefly-linux-kernel-4.4.55.git] / drivers / cir / bu92747guw_cir.c
1 /*drivers/cir/bu92747guw_cir.c - driver for bu92747guw
2  *
3  * Copyright (C) 2010 ROCKCHIP, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include <linux/interrupt.h>
15 #include <linux/slab.h>
16 #include <linux/irq.h>
17 #include <linux/gpio.h>
18 #include <asm/uaccess.h>
19 #include <linux/workqueue.h>
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/platform_device.h>
23 #include <linux/types.h>
24 #include <linux/bcd.h>
25 #include <linux/spinlock.h>
26 #include <linux/fs.h>
27 #include <linux/delay.h>
28 #include <linux/wakelock.h>
29 #include <linux/miscdevice.h>
30 #include <linux/freezer.h>
31 #include <mach/gpio.h>
32 #include <mach/iomux.h>
33 #include <mach/rk29_smc.h>
34
35 #include "bu92747guw_cir.h"
36
37 #if 1
38 #define BU92747_DBG(x...) printk(x)
39 #else
40 #define BU92747_DBG(x...)
41 #endif
42
43 #define CIR_IIC_SPEED   100 * 1000
44
45 #define XIN_INPUT_FREQ 48*1000 //KHz
46
47 struct bu92747_data_info {
48         struct bu92747guw_platform_data *platdata;
49         struct i2c_client *client;
50         char state;
51         int irq;
52         int base_clock;
53         int sys_clock;
54         struct delayed_work      dwork;
55 };
56
57 static struct miscdevice bu92747guw_device;
58
59 int repeat_flag=-1;
60 int start_flag = 0;
61 //mutex lock between remote and irda
62
63
64
65
66 #ifdef CONFIG_RK_IRDA_UART
67 extern int bu92747_try_lock(void);
68 extern void bu92747_unlock(void);
69 #else
70 int bu92747_try_lock(void) {return 1;}
71 void bu92747_unlock(void) {return;}
72 #endif
73
74
75
76 static int bu92747_cir_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf, int len)
77 {
78         int ret; 
79         ret = i2c_master_reg8_recv(client, reg, buf, len, CIR_IIC_SPEED);
80         return ret; 
81 }
82
83 static int bu92747_cir_i2c_set_regs(struct i2c_client *client, u8 reg, u8 *buf, int len)
84 {
85         int ret; 
86         ret = i2c_master_reg8_send(client, reg, buf, len, CIR_IIC_SPEED);
87         return ret;
88 }
89
90
91 static int bu92747_stop(struct i2c_client *client) 
92 {
93         u8 reg_value[2];
94         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
95         //BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
96         printk("line %d: enter %s\n", __LINE__, __FUNCTION__);
97 //      disable_irq(bu92747->irq);
98         //diable clk, repeat=1
99         bu92747_cir_i2c_read_regs(client, REG_SETTING0, reg_value, 2);
100         reg_value[0] = reg_value[0]&0xfe;
101         reg_value[1] = reg_value[1]&0xf1;
102         bu92747_cir_i2c_set_regs(client, REG_SETTING0, reg_value, 2);
103         start_flag = 0;
104         repeat_flag = -1;
105         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
106
107         return 0;
108 }
109 static void bu92747_dwork_handler(struct work_struct *work)
110 {
111         struct bu92747_data_info *bu92747 = 
112                 (struct bu92747_data_info *)container_of(work, struct bu92747_data_info, dwork.work);
113         u8 reg_value[2];
114         
115         BU92747_DBG("------- sss  enter %s\n", __func__);
116
117         if  ((repeat_flag != -10) && (repeat_flag <= -1)){
118                 bu92747_stop(bu92747->client);
119                 BU92747_DBG("----------exit %s\n", __func__);
120                 return ;
121         }
122
123         //set repeat=0
124         bu92747_cir_i2c_read_regs(bu92747->client, REG_SETTING1, reg_value, 1);
125         reg_value[0] &= 0xf0;
126         bu92747_cir_i2c_set_regs(bu92747->client, REG_SETTING1, reg_value, 1);
127         //printk("----------exit %s  reg_value = %d\n", __func__, reg_value[1]);
128
129
130         reg_value[0] = 1;
131         
132         bu92747_cir_i2c_set_regs(bu92747->client, REG_SEND, reg_value, 1);
133 //      bu92747_cir_i2c_read_regs(bu92747->client, REG_FRMLEN1, reg_value, 2);
134
135 //      printk("frame_interval = 0x%x\n", reg_value[1], reg_value[2]);
136         //power down
137
138         BU92747_DBG("----------exit %s\n", __func__);
139         return;
140 }
141
142
143 static irqreturn_t bu92747_cir_irq(int irq, void *dev_id)
144 {
145 //      u8 reg_value[2];
146         struct i2c_client *client = container_of(bu92747guw_device.parent, struct i2c_client, dev);
147         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
148
149         BU92747_DBG("----------enter %s  repeat_flag = %d\n", __func__, repeat_flag);
150
151
152         if (start_flag == 1){
153                 if (repeat_flag == -10){
154                         if ((bu92747->state++)%10 == 0){
155                                 schedule_delayed_work(&bu92747->dwork, msecs_to_jiffies(0));
156                                 bu92747->state = 0;
157                         } 
158                 }else if (((--repeat_flag%16) == 0) || (repeat_flag < 0)){
159                         schedule_delayed_work(&bu92747->dwork, msecs_to_jiffies(0));
160                 }
161         }
162         return IRQ_HANDLED;
163 }
164
165
166
167 #if 0
168 static int bu92747_send_data(struct i2c_client *client, struct rk29_cir_struct_info *cir) 
169 {
170         u8 inv0,inv1;
171         unsigned int hlo, hhi;
172         u8 reg_value[16];
173         int i, nByte;
174         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
175         struct bu92747guw_platform_data *pdata = bu92747->platdata;
176         int sys_clock = bu92747->sys_clock;
177         unsigned long flags;
178
179         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
180         //if (bu92747->state == BU92747_BUSY) {
181         //      printk("line %d, func: %s, dev is busy now\n", __LINE__, __func__);
182         //      return -EBUSY;
183         //}
184
185         //bu92747->state = BU92747_BUSY;
186
187         repeat_flag = cir->repeat%16;
188
189         bu92747_cir_i2c_read_regs(client, REG_SETTING0, reg_value, 2);
190         
191         inv0 = cir->inv & 0x01;
192         inv1 = (cir->inv>>1) & 0x01;
193         reg_value[0] = reg_value[0] | (inv0<<1) | (inv1<<2);
194         reg_value[1] = (reg_value[1]&0xf0) | (repeat_flag&0x0f);
195         bu92747_cir_i2c_set_regs(client, REG_SETTING0, reg_value, 2);
196         BU92747_DBG("inv0 = %d, inv1 = %d, repeat=%d\n", inv0, inv1, repeat_flag);
197         
198         //head maybe different while repeat
199         if ((bu92747->head_burst_time!=cir->head_burst_time) 
200                 || (bu92747->head_space_time!=cir->head_space_time)) {
201                 hlo = (cir->head_space_time*sys_clock)/1000;
202                 hhi = (cir->head_burst_time*sys_clock)/1000;
203                 reg_value[0] = hlo>>8;
204                 reg_value[1] = hlo&0xff;
205                 reg_value[2] = hhi>>8;
206                 reg_value[3] = hhi&0xff;        
207                 bu92747_cir_i2c_set_regs(client, REG_HLO1, reg_value, 4);
208                 BU92747_DBG("hlo = 0x%x, hhi = 0x%x\n", hlo, hhi);
209         }
210
211         //switch to remote control
212         //bu92747_lock();
213         
214         //data bit length
215         reg_value[0] = cir->frame_bit_len;
216         bu92747_cir_i2c_set_regs(client, REG_BITLEN, reg_value, 1);
217         BU92747_DBG("frame_bit_len = 0x%x\n", cir->frame_bit_len);
218         
219         //data
220         nByte = (cir->frame_bit_len+7)/8;
221         for (i=0; i<nByte; i++) {
222                 reg_value[i] = ((cir->frame)>>(8*i))&0xff;
223                 BU92747_DBG("reg_value[%d] = %d\n", i, reg_value[i]);
224         }
225         bu92747_cir_i2c_set_regs(client, REG_OUT0, reg_value, nByte);
226         BU92747_DBG("nByte = %d\n", nByte);
227         
228         //clear irq, start send
229         //reg_value[0] = 1;
230         //reg_value[1] = 1;
231         //bu92747_cir_i2c_set_regs(client, REG_IRQC, reg_value, 2);
232         
233         //send ok?
234 //      while (gpio_get_value(pdata->intr_pin)) {
235 //              BU92747_DBG("line %d: data register is not null\n", __LINE__);
236 //      }
237
238         //switch to irda control        
239         //smc0_write(3, smc0_read(3) & 0xfbff);
240         //bu92747_unlock();
241
242         //enable_irq(bu92747->irq);
243         
244         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
245         
246         return 0;
247 }
248 #endif
249
250
251
252 static int bu92747_set_format(struct i2c_client *client, struct rk29_cir_struct_info *cir) 
253 {
254         u8 inv0, inv1;
255         unsigned int clo, chi, clo_org, chi_org;
256         unsigned int d0lo, d0hi, d1lo, d1hi;
257         unsigned int end;
258         unsigned int hlo = cir->carry_low , hhi = cir->carry_high;      
259         int repeat;
260         u32 frame_interval;
261         int i, nByte;
262         u8 reg_value[32];
263         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
264         int sys_clock = bu92747->sys_clock;
265
266         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
267
268         if (!cir)
269                 return -1;
270
271         //set inv0, inv1
272         inv0 = cir->inv & 0x01;
273         inv1 = (cir->inv>>1) & 0x01;
274         bu92747_cir_i2c_read_regs(client, REG_SETTING0, reg_value, 3);
275         reg_value[0] = reg_value[0] | (inv0<<1) | (inv1<<2);
276         //bu92747_cir_i2c_set_regs(client, REG_SETTING0, reg_value, 1);
277         BU92747_DBG("inv0 = %d, inv1 = %d\n", inv0, inv1);
278
279
280         repeat_flag = cir->repeat;
281         printk("repeat 11111 =%d\n", repeat_flag);
282         
283         if (repeat_flag == -1){
284                 repeat_flag = -10;
285                 repeat = 0;             
286         }else if (repeat_flag > 16){
287                 repeat = 16; 
288         }else{
289                 repeat = repeat_flag;
290         }
291
292         repeat = repeat % 16;
293         reg_value[1] = ((reg_value[1]&0xf0) | (repeat&0x0f));
294
295
296         //carry
297         clo = XIN_INPUT_FREQ / 3000 * hlo;
298         chi = XIN_INPUT_FREQ / 3000 * hhi;
299         reg_value[3] = clo>>8;
300         reg_value[4] = clo&0xff;
301         reg_value[5] = chi>>8;
302         reg_value[6] = chi&0xff;
303         BU92747_DBG("clo = 0x%x, chi = 0x%x\n", clo, chi);
304         //bu92747_cir_i2c_set_regs(client, REG_CLO1, reg_value, 4);
305
306         
307
308         //head
309         hlo = (cir->head_space_time*sys_clock)/1000;
310         hhi = (cir->head_burst_time*sys_clock)/1000;
311         reg_value[7] = hlo>>8;
312         reg_value[8] = hlo&0xff;
313         reg_value[9] = hhi>>8;
314         reg_value[10] = hhi&0xff;
315         BU92747_DBG("hlo = 0x%x, hhi = 0x%x\n", hlo, hhi);
316
317
318         //data0
319         d0lo = (cir->logic_low_space_time*sys_clock)/1000;
320         d0hi = (cir->logic_low_burst_time*sys_clock)/1000;
321         reg_value[11] = d0lo>>8;
322         reg_value[12] = d0lo&0xff;
323         reg_value[13] = d0hi>>8;
324         reg_value[14] = d0hi&0xff;
325         BU92747_DBG("d0lo = 0x%x, d0hi = 0x%x\n", d0lo, d0hi);
326
327
328         //data1
329         d1lo = (cir->logic_high_space_time*sys_clock)/1000;
330         d1hi = (cir->logic_high_burst_time*sys_clock)/1000;
331         reg_value[15] = d1lo>>8;
332         reg_value[16] = d1lo&0xff;
333         reg_value[17] = d1hi>>8;
334         reg_value[18] = d1hi&0xff;
335         BU92747_DBG("d1lo = 0x%x, d1hi = 0x%x\n", d1lo, d1hi);
336
337         //end
338         end = (cir->stop_bit_interval*sys_clock)/1000;
339         reg_value[19] = end>>8;
340         reg_value[20] = end&0xff;
341         //bu92747_cir_i2c_set_regs(client, REG_CLO1, reg_value, 18);
342
343         BU92747_DBG("end = 0x%x\n", end);
344
345         
346         //data bit length
347         reg_value[21] = cir->frame_bit_len;
348         BU92747_DBG("frame_bit_len = 0x%x\n", cir->frame_bit_len);
349
350         //frame interval
351         frame_interval = (cir->frame_interval*sys_clock)/1000;
352         reg_value[22] = frame_interval>>8;
353         reg_value[23] = frame_interval&0xff;
354         //bu92747_cir_i2c_set_regs(client, REG_FRMLEN1, reg_value, 2);  
355         BU92747_DBG("cir->frame_interval =%d frame_interval = %d\n\n", cir->frame_interval,frame_interval);
356
357         
358         //data
359         nByte = (cir->frame_bit_len+7)/8;
360         for (i=0; i<nByte; i++) {
361                 reg_value[24+i] = ((cir->frame)>>(8*i))&0xff;
362                 BU92747_DBG("reg_value[%d] = %d\n", 24+i, reg_value[24+i]);
363         }
364
365         bu92747_cir_i2c_set_regs(client, REG_SETTING0, reg_value, 24+i);
366         
367
368         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
369
370         return 0;
371         
372 }
373
374
375 static int bu92747_start(struct i2c_client *client) 
376 {
377         u8 reg_value[2];
378         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
379         printk("line %d: enter %s\n", __LINE__, __FUNCTION__);
380
381
382         start_flag = 1;
383         bu92747->state = 0;
384         bu92747_cir_i2c_read_regs(client, REG_SETTING0, reg_value, 1);
385         reg_value[0] = reg_value[0]|0x01;
386         bu92747_cir_i2c_set_regs(client, REG_SETTING0, reg_value, 1);
387
388         //clear irq, start send
389         reg_value[0] = 1;
390         reg_value[1] = 1;
391         bu92747_cir_i2c_set_regs(client, REG_IRQC, reg_value, 2);
392
393         
394         printk("line %d: exit %s\n", __LINE__, __FUNCTION__);
395         return 0;
396 }
397
398
399 static void bu92747_printk_cir(struct rk29_cir_struct_info *cir)
400 {
401         BU92747_DBG("\ncir struct:\n");
402         BU92747_DBG("carry_high                  = %d\n", cir->carry_high);
403         BU92747_DBG("carry_low       = %d\n", cir->carry_low);
404         BU92747_DBG("repeat            = %d\n", cir->repeat);
405         BU92747_DBG("inv                    = %d\n", cir->inv);
406         BU92747_DBG("frame_bit_len          = %d\n", cir->frame_bit_len);
407         BU92747_DBG("stop_bit_interval      = %d\n", cir->stop_bit_interval);
408         BU92747_DBG("frame                  = %lld\n", cir->frame);
409         BU92747_DBG("frame_interval         = %d\n", cir->frame_interval);
410         BU92747_DBG("head_burst_time        = %d\n", cir->head_burst_time);
411         BU92747_DBG("head_space_time        = %d\n", cir->head_space_time);
412         BU92747_DBG("logic_high_burst_time  = %d\n", cir->logic_high_burst_time);
413         BU92747_DBG("logic_high_space_time  = %d\n", cir->logic_high_space_time);
414         BU92747_DBG("logic_low_burst_time   = %d\n", cir->logic_low_burst_time);
415         BU92747_DBG("logic_low_space_time   = %d\n", cir->logic_low_space_time);
416         BU92747_DBG("\n");
417 }
418
419
420 static int bu92747_set_duration(struct i2c_client *client, struct rk29_cir_struct_info *cir) 
421 {
422         u32 frame_interval;
423
424         u8 reg_value[20];
425         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
426         int sys_clock = bu92747->sys_clock;
427
428         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
429
430         if (!cir)
431                 return -1;
432
433         BU92747_DBG("sys_clock = %d,  frame_interval = 0x%x\n", sys_clock,cir->frame_interval);
434         //frame interval
435         frame_interval = (cir->frame_interval*sys_clock)/1000;
436         
437         reg_value[0] = frame_interval>>8;
438         reg_value[1] = frame_interval&0xff;
439         bu92747_cir_i2c_set_regs(client, REG_FRMLEN1, reg_value, 2);
440
441         BU92747_DBG("frame_interval = 0x%x\n", frame_interval);
442
443         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
444
445         return 0;
446         
447 }
448
449 static int bu92747_set_data(struct i2c_client *client, struct rk29_cir_struct_info *cir) 
450 {
451
452         u8 reg_value[16];
453         int i, nByte;
454         
455
456
457         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
458         
459         //data bit length
460         reg_value[0] = cir->frame_bit_len;
461         bu92747_cir_i2c_set_regs(client, REG_BITLEN, reg_value, 1);
462         BU92747_DBG("frame_bit_len = 0x%x\n", cir->frame_bit_len);
463         
464         //data
465         nByte = (cir->frame_bit_len+7)/8;
466         for (i=0; i<nByte; i++) {
467                 reg_value[i] = ((cir->frame)>>(8*i))&0xff;
468                 BU92747_DBG("reg_value[%d] = %d\n", i, reg_value[i]);
469         }
470         bu92747_cir_i2c_set_regs(client, REG_OUT0, reg_value, nByte);
471         BU92747_DBG("nByte = %d\n", nByte);
472         
473         
474         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
475         
476         return 0;
477 }
478
479 static int bu92747_set_pulse(struct i2c_client *client, struct rk29_cir_struct_info *cir) 
480 {
481         u8 inv0, inv1;
482
483
484         unsigned int d0lo, d0hi, d1lo, d1hi;
485
486
487         u8 reg_value[8] = {0};
488         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
489         int sys_clock = bu92747->sys_clock;
490
491         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
492
493         if (!cir)
494                 return -1;
495
496         //set inv0, inv1
497         inv0 = cir->inv & 0x01;
498         inv1 = (cir->inv>>1) & 0x01;
499         bu92747_cir_i2c_read_regs(client, REG_SETTING0, reg_value, 1);
500         reg_value[0] = reg_value[0] | (inv0<<1) | (inv1<<2);
501         bu92747_cir_i2c_set_regs(client, REG_SETTING0, reg_value, 1);
502         BU92747_DBG("inv0 = %d, inv1 = %d\n", inv0, inv1);
503
504
505
506         //data0
507         d0lo = (cir->logic_low_space_time*sys_clock)/1000;
508         d0hi = (cir->logic_low_burst_time*sys_clock)/1000;
509         reg_value[0] = d0lo>>8;
510         reg_value[1] = d0lo&0xff;
511         reg_value[2] = d0hi>>8;
512         reg_value[3] = d0hi&0xff;
513         BU92747_DBG("d0lo = 0x%x, d0hi = 0x%x\n", d0lo, d0hi);
514
515         //data1
516         d1lo = (cir->logic_high_space_time*sys_clock)/1000;
517         d1hi = (cir->logic_high_burst_time*sys_clock)/1000;
518         reg_value[4] = d1lo>>8;
519         reg_value[5] = d1lo&0xff;
520         reg_value[6] = d1hi>>8;
521         reg_value[7] = d1hi&0xff;
522         BU92747_DBG("d1lo = 0x%x, d1hi = 0x%x\n", d1lo, d1hi);
523         bu92747_cir_i2c_set_regs(client, REG_D0LO1, reg_value, 8);
524
525         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
526
527         return 0;
528         
529 }
530
531
532
533 static int bu92747_set_parameter(struct i2c_client *client, struct rk29_cir_struct_info *cir) 
534 {
535         unsigned int hlo, hhi;
536         unsigned int end;
537
538         u8 reg_value[4];
539         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
540         int sys_clock = bu92747->sys_clock;
541
542         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
543
544         if (!cir)
545                 return -1;
546
547         //head
548         hlo = (cir->head_space_time*sys_clock)/1000;
549         hhi = (cir->head_burst_time*sys_clock)/1000;
550         reg_value[0] = hlo>>8;
551         reg_value[1] = hlo&0xff;
552         reg_value[2] = hhi>>8;
553         reg_value[3] = hhi&0xff;
554         BU92747_DBG("hlo = 0x%x, hhi = 0x%x\n", hlo, hhi);
555         bu92747_cir_i2c_set_regs(client, REG_HLO1, reg_value, 4);
556
557         //end
558         end = (cir->stop_bit_interval*sys_clock)/1000;
559         reg_value[0] = end>>8;
560         reg_value[1] = end&0xff;
561         bu92747_cir_i2c_set_regs(client, REG_ENDLEN1, reg_value, 2);
562
563         BU92747_DBG("end = 0x%x\n", end);
564
565         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
566
567         return 0;
568         
569 }
570
571
572 static int bu92747_set_repeat(struct i2c_client *client, struct rk29_cir_struct_info *cir) 
573 {
574
575         u8 reg_value[2];
576         int repeat;
577
578         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
579
580
581     repeat_flag = cir->repeat;
582         printk("repeat 11111 =%d\n", repeat_flag);
583         
584         if (repeat_flag == -1){
585                 repeat_flag = -10;
586                 repeat = 0;             
587         }else if (repeat_flag > 16){
588                 repeat = 16; 
589         }else{
590                 repeat = repeat_flag;
591         }
592
593
594         repeat = repeat % 16;
595         
596         bu92747_cir_i2c_read_regs(client, REG_SETTING1, reg_value, 1);
597         
598         reg_value[0] = (reg_value[0]&0xf0) | (repeat&0x0f);
599         bu92747_cir_i2c_set_regs(client, REG_SETTING1, reg_value, 1);
600         printk("repeat  2222  =%d  reg_value = %d\n", repeat, reg_value[0]);
601          
602         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
603         
604         return 0;
605 }
606
607 static int bu92747_set_carrier(struct i2c_client *client, struct rk29_cir_struct_info *cir) 
608 {
609         u8 reg_value[4];
610         u16 clo = 0, chi = 0;  
611         unsigned int hlo = cir->carry_low , hhi = cir->carry_high;      
612
613         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
614
615         if (!cir)
616                 return -1;
617
618         //carry
619
620         clo = XIN_INPUT_FREQ / 3000 * hlo;
621         chi = XIN_INPUT_FREQ / 3000 * hhi;
622         reg_value[0] = clo>>8;
623         reg_value[1] = clo&0xff;
624         reg_value[2] = chi>>8;
625         reg_value[3] = chi&0xff;
626         BU92747_DBG("clo = 0x%x, chi = 0x%x\n", clo, chi);
627         bu92747_cir_i2c_set_regs(client, REG_CLO1, reg_value, 4);
628
629         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
630
631         return 0;
632         
633 }
634
635
636 static int bu92747_cir_init_device(struct i2c_client *client, struct bu92747_data_info *bu92747)        
637 {
638         u8 reg_value[3];
639         
640         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
641         
642         //transmission buff null intertupt, base clock div, irq enable, clock power up
643         //reg_value[0] = /*REG0_OPM | */REG0_DIVS | REG0_PWR | REG0_IRQE;
644         reg_value[0] = /*REG0_OPM | */REG0_DIVS | REG0_IRQE;
645         reg_value[1] = REG1_FRME | REG1_RPT;  //enable frame interval, repeat = 1
646         reg_value[2] = 80;   //base clock = 100KHz
647         BU92747_DBG("line %d: reg0=0x%x, reg1=0x%x, reg2=0x%x\n", __LINE__, reg_value[0], reg_value[1], reg_value[2]);
648         bu92747_cir_i2c_set_regs(client, REG_SETTING0, reg_value, 3);
649         bu92747_cir_i2c_read_regs(client, REG_SETTING0, reg_value, 3);
650         BU92747_DBG("line %d: reg0=0x%x, reg1=0x%x, reg2=0x%x\n", __LINE__, reg_value[0], reg_value[1], reg_value[2]);
651
652         bu92747->base_clock = 100; //KHz
653         bu92747->sys_clock = bu92747->base_clock;
654
655         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
656
657         return 0;
658 }
659
660 static int bu92747_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
661 {
662         void __user *argp = (void __user *)arg;
663         char msg[CIR_FRAME_SIZE];
664         int ret;
665         struct i2c_client *client = container_of(bu92747guw_device.parent, struct i2c_client, dev);
666
667         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
668
669
670         switch (cmd) {
671         
672         case BU92747_IOCTL_START:
673                 ret = bu92747_start(client);
674                 if (ret < 0)
675                         return ret;
676                 break;
677                 
678         case BU92747_IOCTL_STOP:
679                 ret = bu92747_stop(client);
680                 if (ret < 0)
681                         return ret;
682                 break;
683                 
684         case BU92747_IOCTL_PULSE:
685                 if (copy_from_user(&msg, argp, sizeof(msg)))
686                         return -EFAULT;
687                         
688                 //bu92747_printk_cir((struct rk29_cir_struct_info *)msg);
689                 ret = bu92747_set_pulse(client, (struct rk29_cir_struct_info *)msg);
690                 if (ret < 0)
691                         return ret;
692                 break;
693     
694         case BU92747_IOCTL_DATA:
695                 if (copy_from_user(&msg, argp, sizeof(msg)))
696                         return -EFAULT;
697                 ret = bu92747_set_data(client, (struct rk29_cir_struct_info *)msg);
698                 if (ret < 0)
699                         return ret;
700                 break;
701
702         case BU92747_IOCTL_CARRIER:
703                 if (copy_from_user(&msg, argp, sizeof(msg)))
704                         return -EFAULT;
705                 ret = bu92747_set_carrier(client, (struct rk29_cir_struct_info *)msg);
706                 if (ret < 0)
707                         return ret;
708                 break;  
709         case BU92747_IOCTL_REPEAT:
710                 if (copy_from_user(&msg, argp, sizeof(msg)))
711                         return -EFAULT;
712                 ret = bu92747_set_repeat(client, (struct rk29_cir_struct_info *)msg);
713                 if (ret < 0)
714                         return ret;     
715                 break;
716                 
717         case BU92747_IOCTL_DURATION:
718                 if (copy_from_user(&msg, argp, sizeof(msg)))
719                         return -EFAULT;
720                 ret = bu92747_set_duration(client, (struct rk29_cir_struct_info *)msg);
721                 if (ret < 0)
722                         return ret;     
723                 break;  
724         case BU92747_IOCTL_PARAMETER:
725                 if (copy_from_user(&msg, argp, sizeof(msg)))
726                         return -EFAULT;
727                         
728                 ret = bu92747_set_parameter(client, (struct rk29_cir_struct_info *)msg);
729                 if (ret < 0)
730                         return ret;             
731                 break;  
732         case BU92747_IOCTL_FORMATE:
733                         if (copy_from_user(&msg, argp, sizeof(msg)))
734                                 return -EFAULT;
735                                 
736                         ret = bu92747_set_format(client, (struct rk29_cir_struct_info *)msg);
737                         if (ret < 0)
738                                 return ret;     
739                         break;  
740
741                 
742         default:
743                 return -ENOTTY;
744         }
745         
746         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
747
748         return 0;
749 }
750
751 static int bu92747_open(struct inode *inode, struct file *file)
752 {
753         struct i2c_client *client = container_of(bu92747guw_device.parent, struct i2c_client, dev);
754         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
755         struct bu92747guw_platform_data *pdata = bu92747->platdata;
756         int ret = 0;
757         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
758
759         printk("bu92747_open\n");
760
761
762         ret = bu92747_try_lock();
763         if (ret == 0){
764                 printk("cannot get lock. Please close irda!\n");
765                 return -2;
766         }
767         
768         bu92747->state = 0;
769         start_flag = 0;
770         repeat_flag = -1;
771 //      if (BU92747_OPEN == bu92747->state) 
772 //              return -EBUSY;
773 //      bu92747->state = BU92747_OPEN;
774         
775         //power on
776         if (pdata && pdata->cir_pwr_ctl) {
777                 pdata->cir_pwr_ctl(1);
778         }
779                 
780         //switch to remote control, mcr, ec_en=1,rc_mode=1
781         smc0_write(REG_MCR_ADDR, smc0_read(REG_MCR_ADDR)|(3<<10));
782         //set irda pwdownpin = 0
783         smc0_write(REG_TRCR_ADDR, smc0_read(REG_TRCR_ADDR)&0xffbf);
784         BU92747_DBG("irda power down pin = %d\n", gpio_get_value(RK29_PIN5_PA7));
785
786         //register init
787         bu92747_cir_init_device(client, bu92747);
788         start_flag = 0;
789         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
790
791         return 0;
792 }
793
794 static int bu92747_release(struct inode *inode, struct file *file)
795 {
796         struct i2c_client *client = container_of(bu92747guw_device.parent, struct i2c_client, dev);
797         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
798         struct bu92747guw_platform_data *pdata = bu92747->platdata;
799
800         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
801         smc0_write(REG_TRCR_ADDR, smc0_read(REG_TRCR_ADDR)|0x0040);
802         start_flag = -1;
803
804         //power down
805         if (pdata && pdata->cir_pwr_ctl) {
806                 pdata->cir_pwr_ctl(0);
807         }
808         
809 //      bu92747->state = BU92747_CLOSE;
810         bu92747_unlock();
811
812         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
813         
814         return 0;
815 }
816
817 #if  CONFIG_PM
818 static int bu92747_suspend(struct i2c_client *i, pm_message_t mesg)
819 {
820         struct i2c_client *client = container_of(bu92747guw_device.parent, struct i2c_client, dev);
821         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
822         struct bu92747guw_platform_data *pdata = bu92747->platdata;
823
824         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
825
826         if (start_flag == 0){
827                 BU92747_DBG("realy suspend!\n");
828                 disable_irq(bu92747->irq);
829                 smc0_write(REG_TRCR_ADDR, smc0_read(REG_TRCR_ADDR)|0x0040);
830                 start_flag = 0;
831                 repeat_flag = -1;
832                 
833                 if (pdata && pdata->cir_pwr_ctl) {
834                         pdata->cir_pwr_ctl(0);
835                 }
836         }
837         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
838
839         return 0;
840 }
841
842 static int bu92747_resume(struct i2c_client *i)
843 {
844         struct i2c_client *client = container_of(bu92747guw_device.parent, struct i2c_client, dev);
845         struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
846         struct bu92747guw_platform_data *pdata = bu92747->platdata;
847         
848         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
849
850         if (start_flag == 0){
851                 BU92747_DBG("realy resume!\n");
852                 enable_irq(bu92747->irq);
853                 bu92747->state = 0;
854
855                 //power on
856                 if (pdata && pdata->cir_pwr_ctl) {
857                         pdata->cir_pwr_ctl(1);
858                 }
859                         
860                 //switch to remote control, mcr, ec_en=1,rc_mode=1
861                 smc0_write(REG_MCR_ADDR, smc0_read(REG_MCR_ADDR)|(3<<10));
862                 //set irda pwdownpin = 0
863                 smc0_write(REG_TRCR_ADDR, smc0_read(REG_TRCR_ADDR)&0xffbf);
864                 BU92747_DBG("irda power down pin = %d\n", gpio_get_value(RK29_PIN5_PA7));
865
866                 //register init
867                 bu92747_cir_init_device(client, bu92747);
868         }
869         
870         printk("line %d: exit %s\n", __LINE__, __FUNCTION__);
871
872
873         return 0;
874         
875 }
876 #else
877 #define bu92747_suspend NULL
878 #define bu92747_resume NULL
879 #endif
880
881 static struct file_operations bu92747_fops = {
882         .owner = THIS_MODULE,
883         .open = bu92747_open,
884         .release = bu92747_release,
885         .ioctl = bu92747_ioctl,
886 };
887
888 static struct miscdevice bu92747guw_device = {
889         .minor = MISC_DYNAMIC_MINOR,
890         .name = "rk29_cir",
891         .fops = &bu92747_fops,
892 };
893
894 static int __devinit bu92747_cir_probe(struct i2c_client *client, const struct i2c_device_id *id)
895 {
896         struct bu92747_data_info *bu92747;
897         struct bu92747guw_platform_data *pdata;
898         int err;
899         
900         BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
901
902     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
903                 return -ENODEV;
904         
905         bu92747 = kzalloc(sizeof(struct bu92747_data_info), GFP_KERNEL);
906         if (!bu92747) {
907                 printk("line %d: bu92747 alloc data failed.\n", __LINE__);
908                 err = -ENOMEM;
909                 goto MEM_ERR;
910         }
911         //ioinit
912         pdata = client->dev.platform_data;
913         if (pdata && pdata->iomux_init) {
914                 pdata->iomux_init();
915         }
916         
917         bu92747->platdata = pdata;
918         bu92747->client = client;
919         i2c_set_clientdata(client, bu92747);
920         bu92747->state = 0;
921         start_flag = -1;
922
923         //register device
924         bu92747guw_device.parent = &client->dev;
925         err = misc_register(&bu92747guw_device);
926         if (err) {
927                 printk("line %d: bu92747 misc_register failed.\n", __LINE__);
928                 goto REGISTER_ERR;
929         }
930
931         //request irq
932         if (pdata && gpio_is_valid(pdata->intr_pin)) {
933                 printk("-------request irq\n");
934                 err = gpio_request(pdata->intr_pin, "rk29 cir irq");
935                 if (err) {
936                         printk("line %d: bu92747 request gpio failed.\n", __LINE__);
937                         goto GPIO_ERR;
938                 }
939                 gpio_direction_input(pdata->intr_pin);
940                 gpio_request(RK29_PIN5_PA7, NULL);
941                 if (err) {
942                         printk("line %d: bu92747 request gpio failed.\n", __LINE__);
943                 }
944                 gpio_direction_input(RK29_PIN5_PA7);
945                 bu92747->irq = gpio_to_irq(pdata->intr_pin);
946                 err = request_irq(bu92747->irq, bu92747_cir_irq, IRQF_TRIGGER_FALLING, client->dev.driver->name, bu92747);
947                 if (err) {
948                         BU92747_DBG("line %d: bu92747 request gpio failed.\n", __LINE__);
949                         goto IRQ_ERR;
950                 }
951                 //disable_irq(bu92747->irq);
952         }
953         
954         //INIT_DELAYED_WORK(&bu92747->dwork, bu92747_dwork_handler);
955         INIT_DELAYED_WORK(&bu92747->dwork, bu92747_dwork_handler);
956         
957
958 #if 0
959         //bu92747_start(client);
960         //while (1) {
961                 //bu92747_send_data_test(client);
962                 //BU92747_DBG("line %d: test %s\n", __LINE__, __FUNCTION__);
963                 //mdelay(500);
964         //}
965 #endif
966
967         BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
968         return 0;
969
970 IRQ_ERR:
971         gpio_free(pdata->intr_pin);
972 GPIO_ERR:
973     misc_deregister(&bu92747guw_device);
974 REGISTER_ERR:
975         if (pdata && pdata->iomux_deinit)
976                 pdata->iomux_deinit();
977         kfree(bu92747);
978 MEM_ERR:
979         return err;
980 }
981
982 static int __devexit bu92747_cir_remove(struct i2c_client *client)
983 {
984         
985         struct bu92747_data_info *bu92747 = i2c_get_clientdata(client);
986         struct bu92747guw_platform_data *pdata = bu92747->platdata;
987
988         printk(" cir_remove \n");
989         start_flag = -1;
990         free_irq(bu92747->irq, bu92747);
991         gpio_free(pdata->intr_pin);
992     misc_deregister(&bu92747guw_device);
993         if (pdata && pdata->iomux_deinit)
994                 pdata->iomux_deinit();
995         kfree(bu92747);
996         return 0;
997 }
998
999 static const struct i2c_device_id bu92747_cir_id[] = {
1000         { "bu92747_cir", 0 },
1001         { }
1002 };
1003 MODULE_DEVICE_TABLE(i2c, bu92747_cir_id);
1004
1005 static struct i2c_driver bu92747_cir_driver = {
1006         .driver         = {
1007                 .name   = "bu92747_cir",
1008                 .owner  = THIS_MODULE,
1009         },
1010         .probe          = bu92747_cir_probe,
1011         .remove         = __devexit_p(bu92747_cir_remove),
1012         .id_table       = bu92747_cir_id,
1013 #ifdef CONFIG_PM
1014         .suspend = bu92747_suspend,
1015         .resume = bu92747_resume,
1016 #endif
1017 };
1018
1019 static int __init bu92747_cir_init(void)
1020 {
1021         return i2c_add_driver(&bu92747_cir_driver);
1022 }
1023
1024 static void __exit bu92747_cir_exit(void)
1025 {
1026         i2c_del_driver(&bu92747_cir_driver);
1027 }
1028
1029 MODULE_AUTHOR("zyw zyw@rock-chips.com");
1030 MODULE_DESCRIPTION("bu92747 cir driver");
1031 MODULE_LICENSE("GPL");
1032
1033 module_init(bu92747_cir_init);
1034 module_exit(bu92747_cir_exit);
1035