2 handle cx231xx IR remotes via linux kernel input layer.
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
7 < This is a place holder for IR now.>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/input.h>
29 #include <linux/usb.h>
30 #include <linux/slab.h>
34 static unsigned int ir_debug;
35 module_param(ir_debug, int, 0644);
36 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
38 #define MODULE_NAME "cx231xx"
40 #define i2cdprintk(fmt, arg...) \
42 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
45 #define dprintk(fmt, arg...) \
47 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
50 /**********************************************************
51 Polling structure used by cx231xx IR's
52 **********************************************************/
54 struct cx231xx_ir_poll_result {
55 unsigned int toggle_bit:1;
56 unsigned int read_count:7;
63 struct input_dev *input;
67 /* poll external decoder */
69 struct work_struct work;
70 struct timer_list timer;
71 unsigned int last_readcount;
73 int (*get_key) (struct cx231xx_IR *, struct cx231xx_ir_poll_result *);
76 /**********************************************************
77 Polling code for cx231xx
78 **********************************************************/
80 static void cx231xx_ir_handle_key(struct cx231xx_IR *ir)
83 struct cx231xx_ir_poll_result poll_result;
85 /* read the registers containing the IR status */
86 result = ir->get_key(ir, &poll_result);
88 dprintk("ir->get_key() failed %d\n", result);
92 dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x\n",
93 poll_result.toggle_bit, poll_result.read_count,
94 ir->last_readcount, poll_result.rc_data[0]);
96 if (poll_result.read_count > 0 &&
97 poll_result.read_count != ir->last_readcount)
99 poll_result.rc_data[0],
100 poll_result.toggle_bit);
102 if (ir->dev->chip_id == CHIP_ID_EM2874)
103 /* The em2874 clears the readcount field every time the
104 register is read. The em2860/2880 datasheet says that it
105 is supposed to clear the readcount, but it doesn't. So with
106 the em2874, we are looking for a non-zero read count as
107 opposed to a readcount that is incrementing */
108 ir->last_readcount = 0;
110 ir->last_readcount = poll_result.read_count;
115 static void ir_timer(unsigned long data)
117 struct cx231xx_IR *ir = (struct cx231xx_IR *)data;
119 schedule_work(&ir->work);
122 static void cx231xx_ir_work(struct work_struct *work)
124 struct cx231xx_IR *ir = container_of(work, struct cx231xx_IR, work);
126 cx231xx_ir_handle_key(ir);
127 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
130 void cx231xx_ir_start(struct cx231xx_IR *ir)
132 setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
133 INIT_WORK(&ir->work, cx231xx_ir_work);
134 schedule_work(&ir->work);
137 static void cx231xx_ir_stop(struct cx231xx_IR *ir)
139 del_timer_sync(&ir->timer);
140 flush_scheduled_work();
143 int cx231xx_ir_init(struct cx231xx *dev)
145 struct cx231xx_IR *ir;
146 struct input_dev *input_dev;
150 if (dev->board.ir_codes == NULL) {
151 /* No remote control support */
155 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
156 input_dev = input_allocate_device();
157 if (!ir || !input_dev)
160 ir->input = input_dev;
162 /* Setup the proper handler based on the chip */
163 switch (dev->chip_id) {
165 printk("Unrecognized cx231xx chip id: IR not supported\n");
169 /* This is how often we ask the chip for IR information */
170 ir->polling = 100; /* ms */
172 /* init input device */
173 snprintf(ir->name, sizeof(ir->name), "cx231xx IR (%s)", dev->name);
175 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
176 strlcat(ir->phys, "/input0", sizeof(ir->phys));
178 input_dev->name = ir->name;
179 input_dev->phys = ir->phys;
180 input_dev->id.bustype = BUS_USB;
181 input_dev->id.version = 1;
182 input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
183 input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
185 input_dev->dev.parent = &dev->udev->dev;
186 /* record handles to ourself */
190 cx231xx_ir_start(ir);
193 err = __ir_input_register(ir->input, dev->board.ir_codes,
207 int cx231xx_ir_fini(struct cx231xx *dev)
209 struct cx231xx_IR *ir = dev->ir;
211 /* skip detach on non attached boards */
216 ir_input_unregister(ir->input);