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