1 /*drivers/cir/bu92747guw_cir.c - driver for bu92747guw
3 * Copyright (C) 2010 ROCKCHIP, Inc.
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.
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.
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>
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>
35 #include "bu92747guw_cir.h"
38 #define BU92747_DBG(x...) printk(x)
40 #define BU92747_DBG(x...)
43 #define CIR_IIC_SPEED 100 * 1000
45 #define XIN_INPUT_FREQ 48*1000 //KHz
47 struct bu92747_data_info {
48 struct bu92747guw_platform_data *platdata;
49 struct i2c_client *client;
54 struct delayed_work dwork;
57 static struct miscdevice bu92747guw_device;
61 //mutex lock between remote and irda
66 #ifdef CONFIG_RK_IRDA_UART
67 extern int bu92747_try_lock(void);
68 extern void bu92747_unlock(void);
70 int bu92747_try_lock(void) {return 1;}
71 void bu92747_unlock(void) {return;}
76 static int bu92747_cir_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf, int len)
79 ret = i2c_master_reg8_recv(client, reg, buf, len, CIR_IIC_SPEED);
83 static int bu92747_cir_i2c_set_regs(struct i2c_client *client, u8 reg, u8 *buf, int len)
86 ret = i2c_master_reg8_send(client, reg, buf, len, CIR_IIC_SPEED);
91 static int bu92747_stop(struct i2c_client *client)
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);
105 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
109 static void bu92747_dwork_handler(struct work_struct *work)
111 struct bu92747_data_info *bu92747 =
112 (struct bu92747_data_info *)container_of(work, struct bu92747_data_info, dwork.work);
115 BU92747_DBG("------- sss enter %s\n", __func__);
117 if ((repeat_flag != -10) && (repeat_flag <= -1)){
118 bu92747_stop(bu92747->client);
119 BU92747_DBG("----------exit %s\n", __func__);
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]);
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);
135 // printk("frame_interval = 0x%x\n", reg_value[1], reg_value[2]);
138 BU92747_DBG("----------exit %s\n", __func__);
143 static irqreturn_t bu92747_cir_irq(int irq, void *dev_id)
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);
149 BU92747_DBG("----------enter %s repeat_flag = %d\n", __func__, repeat_flag);
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));
158 }else if (((--repeat_flag%16) == 0) || (repeat_flag < 0)){
159 schedule_delayed_work(&bu92747->dwork, msecs_to_jiffies(0));
168 static int bu92747_send_data(struct i2c_client *client, struct rk29_cir_struct_info *cir)
171 unsigned int hlo, hhi;
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;
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__);
185 //bu92747->state = BU92747_BUSY;
187 repeat_flag = cir->repeat%16;
189 bu92747_cir_i2c_read_regs(client, REG_SETTING0, reg_value, 2);
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);
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);
211 //switch to remote control
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);
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]);
225 bu92747_cir_i2c_set_regs(client, REG_OUT0, reg_value, nByte);
226 BU92747_DBG("nByte = %d\n", nByte);
228 //clear irq, start send
231 //bu92747_cir_i2c_set_regs(client, REG_IRQC, reg_value, 2);
234 // while (gpio_get_value(pdata->intr_pin)) {
235 // BU92747_DBG("line %d: data register is not null\n", __LINE__);
238 //switch to irda control
239 //smc0_write(3, smc0_read(3) & 0xfbff);
242 //enable_irq(bu92747->irq);
244 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
252 static int bu92747_set_format(struct i2c_client *client, struct rk29_cir_struct_info *cir)
255 unsigned int clo, chi, clo_org, chi_org;
256 unsigned int d0lo, d0hi, d1lo, d1hi;
258 unsigned int hlo = cir->carry_low , hhi = cir->carry_high;
263 struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
264 int sys_clock = bu92747->sys_clock;
266 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
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);
280 repeat_flag = cir->repeat;
281 printk("repeat 11111 =%d\n", repeat_flag);
283 if (repeat_flag == -1){
286 }else if (repeat_flag > 16){
289 repeat = repeat_flag;
292 repeat = repeat % 16;
293 reg_value[1] = ((reg_value[1]&0xf0) | (repeat&0x0f));
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);
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);
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);
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);
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);
343 BU92747_DBG("end = 0x%x\n", end);
347 reg_value[21] = cir->frame_bit_len;
348 BU92747_DBG("frame_bit_len = 0x%x\n", cir->frame_bit_len);
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);
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]);
365 bu92747_cir_i2c_set_regs(client, REG_SETTING0, reg_value, 24+i);
368 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
375 static int bu92747_start(struct i2c_client *client)
378 struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
379 printk("line %d: enter %s\n", __LINE__, __FUNCTION__);
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);
388 //clear irq, start send
391 bu92747_cir_i2c_set_regs(client, REG_IRQC, reg_value, 2);
394 printk("line %d: exit %s\n", __LINE__, __FUNCTION__);
399 static void bu92747_printk_cir(struct rk29_cir_struct_info *cir)
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);
420 static int bu92747_set_duration(struct i2c_client *client, struct rk29_cir_struct_info *cir)
425 struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
426 int sys_clock = bu92747->sys_clock;
428 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
433 BU92747_DBG("sys_clock = %d, frame_interval = 0x%x\n", sys_clock,cir->frame_interval);
435 frame_interval = (cir->frame_interval*sys_clock)/1000;
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);
441 BU92747_DBG("frame_interval = 0x%x\n", frame_interval);
443 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
449 static int bu92747_set_data(struct i2c_client *client, struct rk29_cir_struct_info *cir)
457 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
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);
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]);
470 bu92747_cir_i2c_set_regs(client, REG_OUT0, reg_value, nByte);
471 BU92747_DBG("nByte = %d\n", nByte);
474 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
479 static int bu92747_set_pulse(struct i2c_client *client, struct rk29_cir_struct_info *cir)
484 unsigned int d0lo, d0hi, d1lo, d1hi;
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;
491 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
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);
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);
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);
525 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
533 static int bu92747_set_parameter(struct i2c_client *client, struct rk29_cir_struct_info *cir)
535 unsigned int hlo, hhi;
539 struct bu92747_data_info *bu92747 = (struct bu92747_data_info *)i2c_get_clientdata(client);
540 int sys_clock = bu92747->sys_clock;
542 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
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);
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);
563 BU92747_DBG("end = 0x%x\n", end);
565 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
572 static int bu92747_set_repeat(struct i2c_client *client, struct rk29_cir_struct_info *cir)
578 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
581 repeat_flag = cir->repeat;
582 printk("repeat 11111 =%d\n", repeat_flag);
584 if (repeat_flag == -1){
587 }else if (repeat_flag > 16){
590 repeat = repeat_flag;
594 repeat = repeat % 16;
596 bu92747_cir_i2c_read_regs(client, REG_SETTING1, reg_value, 1);
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]);
602 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
607 static int bu92747_set_carrier(struct i2c_client *client, struct rk29_cir_struct_info *cir)
610 u16 clo = 0, chi = 0;
611 unsigned int hlo = cir->carry_low , hhi = cir->carry_high;
613 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
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);
629 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
636 static int bu92747_cir_init_device(struct i2c_client *client, struct bu92747_data_info *bu92747)
640 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
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]);
652 bu92747->base_clock = 100; //KHz
653 bu92747->sys_clock = bu92747->base_clock;
655 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
660 static int bu92747_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
662 void __user *argp = (void __user *)arg;
663 char msg[CIR_FRAME_SIZE];
665 struct i2c_client *client = container_of(bu92747guw_device.parent, struct i2c_client, dev);
667 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
672 case BU92747_IOCTL_START:
673 ret = bu92747_start(client);
678 case BU92747_IOCTL_STOP:
679 ret = bu92747_stop(client);
684 case BU92747_IOCTL_PULSE:
685 if (copy_from_user(&msg, argp, sizeof(msg)))
688 //bu92747_printk_cir((struct rk29_cir_struct_info *)msg);
689 ret = bu92747_set_pulse(client, (struct rk29_cir_struct_info *)msg);
694 case BU92747_IOCTL_DATA:
695 if (copy_from_user(&msg, argp, sizeof(msg)))
697 ret = bu92747_set_data(client, (struct rk29_cir_struct_info *)msg);
702 case BU92747_IOCTL_CARRIER:
703 if (copy_from_user(&msg, argp, sizeof(msg)))
705 ret = bu92747_set_carrier(client, (struct rk29_cir_struct_info *)msg);
709 case BU92747_IOCTL_REPEAT:
710 if (copy_from_user(&msg, argp, sizeof(msg)))
712 ret = bu92747_set_repeat(client, (struct rk29_cir_struct_info *)msg);
717 case BU92747_IOCTL_DURATION:
718 if (copy_from_user(&msg, argp, sizeof(msg)))
720 ret = bu92747_set_duration(client, (struct rk29_cir_struct_info *)msg);
724 case BU92747_IOCTL_PARAMETER:
725 if (copy_from_user(&msg, argp, sizeof(msg)))
728 ret = bu92747_set_parameter(client, (struct rk29_cir_struct_info *)msg);
732 case BU92747_IOCTL_FORMATE:
733 if (copy_from_user(&msg, argp, sizeof(msg)))
736 ret = bu92747_set_format(client, (struct rk29_cir_struct_info *)msg);
746 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
751 static int bu92747_open(struct inode *inode, struct file *file)
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;
757 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
759 printk("bu92747_open\n");
762 ret = bu92747_try_lock();
764 printk("cannot get lock. Please close irda!\n");
771 // if (BU92747_OPEN == bu92747->state)
773 // bu92747->state = BU92747_OPEN;
776 if (pdata && pdata->cir_pwr_ctl) {
777 pdata->cir_pwr_ctl(1);
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));
787 bu92747_cir_init_device(client, bu92747);
789 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
794 static int bu92747_release(struct inode *inode, struct file *file)
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;
800 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
801 smc0_write(REG_TRCR_ADDR, smc0_read(REG_TRCR_ADDR)|0x0040);
805 if (pdata && pdata->cir_pwr_ctl) {
806 pdata->cir_pwr_ctl(0);
809 // bu92747->state = BU92747_CLOSE;
812 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
818 static int bu92747_suspend(struct i2c_client *i, pm_message_t mesg)
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;
824 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
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);
833 if (pdata && pdata->cir_pwr_ctl) {
834 pdata->cir_pwr_ctl(0);
837 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
842 static int bu92747_resume(struct i2c_client *i)
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;
848 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
850 if (start_flag == 0){
851 BU92747_DBG("realy resume!\n");
852 enable_irq(bu92747->irq);
856 if (pdata && pdata->cir_pwr_ctl) {
857 pdata->cir_pwr_ctl(1);
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));
867 bu92747_cir_init_device(client, bu92747);
870 printk("line %d: exit %s\n", __LINE__, __FUNCTION__);
877 #define bu92747_suspend NULL
878 #define bu92747_resume NULL
881 static struct file_operations bu92747_fops = {
882 .owner = THIS_MODULE,
883 .open = bu92747_open,
884 .release = bu92747_release,
885 .ioctl = bu92747_ioctl,
888 static struct miscdevice bu92747guw_device = {
889 .minor = MISC_DYNAMIC_MINOR,
891 .fops = &bu92747_fops,
894 static int __devinit bu92747_cir_probe(struct i2c_client *client, const struct i2c_device_id *id)
896 struct bu92747_data_info *bu92747;
897 struct bu92747guw_platform_data *pdata;
900 BU92747_DBG("line %d: enter %s\n", __LINE__, __FUNCTION__);
902 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
905 bu92747 = kzalloc(sizeof(struct bu92747_data_info), GFP_KERNEL);
907 printk("line %d: bu92747 alloc data failed.\n", __LINE__);
912 pdata = client->dev.platform_data;
913 if (pdata && pdata->iomux_init) {
917 bu92747->platdata = pdata;
918 bu92747->client = client;
919 i2c_set_clientdata(client, bu92747);
924 bu92747guw_device.parent = &client->dev;
925 err = misc_register(&bu92747guw_device);
927 printk("line %d: bu92747 misc_register failed.\n", __LINE__);
932 if (pdata && gpio_is_valid(pdata->intr_pin)) {
933 printk("-------request irq\n");
934 err = gpio_request(pdata->intr_pin, "rk29 cir irq");
936 printk("line %d: bu92747 request gpio failed.\n", __LINE__);
939 gpio_direction_input(pdata->intr_pin);
940 gpio_request(RK29_PIN5_PA7, NULL);
942 printk("line %d: bu92747 request gpio failed.\n", __LINE__);
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);
948 BU92747_DBG("line %d: bu92747 request gpio failed.\n", __LINE__);
951 //disable_irq(bu92747->irq);
954 //INIT_DELAYED_WORK(&bu92747->dwork, bu92747_dwork_handler);
955 INIT_DELAYED_WORK(&bu92747->dwork, bu92747_dwork_handler);
959 //bu92747_start(client);
961 //bu92747_send_data_test(client);
962 //BU92747_DBG("line %d: test %s\n", __LINE__, __FUNCTION__);
967 BU92747_DBG("line %d: exit %s\n", __LINE__, __FUNCTION__);
971 gpio_free(pdata->intr_pin);
973 misc_deregister(&bu92747guw_device);
975 if (pdata && pdata->iomux_deinit)
976 pdata->iomux_deinit();
982 static int __devexit bu92747_cir_remove(struct i2c_client *client)
985 struct bu92747_data_info *bu92747 = i2c_get_clientdata(client);
986 struct bu92747guw_platform_data *pdata = bu92747->platdata;
988 printk(" cir_remove \n");
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();
999 static const struct i2c_device_id bu92747_cir_id[] = {
1000 { "bu92747_cir", 0 },
1003 MODULE_DEVICE_TABLE(i2c, bu92747_cir_id);
1005 static struct i2c_driver bu92747_cir_driver = {
1007 .name = "bu92747_cir",
1008 .owner = THIS_MODULE,
1010 .probe = bu92747_cir_probe,
1011 .remove = __devexit_p(bu92747_cir_remove),
1012 .id_table = bu92747_cir_id,
1014 .suspend = bu92747_suspend,
1015 .resume = bu92747_resume,
1019 static int __init bu92747_cir_init(void)
1021 return i2c_add_driver(&bu92747_cir_driver);
1024 static void __exit bu92747_cir_exit(void)
1026 i2c_del_driver(&bu92747_cir_driver);
1029 MODULE_AUTHOR("zyw zyw@rock-chips.com");
1030 MODULE_DESCRIPTION("bu92747 cir driver");
1031 MODULE_LICENSE("GPL");
1033 module_init(bu92747_cir_init);
1034 module_exit(bu92747_cir_exit);