2 * LIRC driver for ITE8709 CIR port
4 * Copyright (C) 2008 Grégory Lardière <spmf2004-lirc@yahoo.fr>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/sched.h>
25 #include <linux/delay.h>
26 #include <linux/pnp.h>
29 #include <media/lirc.h>
30 #include <media/lirc_dev.h>
32 #define LIRC_DRIVER_NAME "lirc_ite8709"
34 #define BUF_CHUNK_SIZE sizeof(int)
35 #define BUF_SIZE (128*BUF_CHUNK_SIZE)
38 * The ITE8709 device seems to be the combination of IT8512 superIO chip and
39 * a specific firmware running on the IT8512's embedded micro-controller.
40 * In addition of the embedded micro-controller, the IT8512 chip contains a
41 * CIR module and several other modules. A few modules are directly accessible
42 * by the host CPU, but most of them are only accessible by the
43 * micro-controller. The CIR module is only accessible by the micro-controller.
44 * The battery-backed SRAM module is accessible by the host CPU and the
45 * micro-controller. So one of the MC's firmware role is to act as a bridge
46 * between the host CPU and the CIR module. The firmware implements a kind of
47 * communication protocol using the SRAM module as a shared memory. The IT8512
48 * specification is publicly available on ITE's web site, but the communication
49 * protocol is not, so it was reverse-engineered.
52 /* ITE8709 Registers addresses and values (reverse-engineered) */
53 #define ITE8709_MODE 0x1a
54 #define ITE8709_REG_ADR 0x1b
55 #define ITE8709_REG_VAL 0x1c
56 #define ITE8709_IIR 0x1e /* Interrupt identification register */
57 #define ITE8709_RFSR 0x1f /* Receiver FIFO status register */
58 #define ITE8709_FIFO_START 0x20
60 #define ITE8709_MODE_READY 0X00
61 #define ITE8709_MODE_WRITE 0X01
62 #define ITE8709_MODE_READ 0X02
63 #define ITE8709_IIR_RDAI 0x02 /* Receiver data available interrupt */
64 #define ITE8709_IIR_RFOI 0x04 /* Receiver FIFO overrun interrupt */
65 #define ITE8709_RFSR_MASK 0x3f /* FIFO byte count mask */
68 * IT8512 CIR-module registers addresses and values
69 * (from IT8512 E/F specification v0.4.1)
71 #define IT8512_REG_MSTCR 0x01 /* Master control register */
72 #define IT8512_REG_IER 0x02 /* Interrupt enable register */
73 #define IT8512_REG_CFR 0x04 /* Carrier frequency register */
74 #define IT8512_REG_RCR 0x05 /* Receive control register */
75 #define IT8512_REG_BDLR 0x08 /* Baud rate divisor low byte register */
76 #define IT8512_REG_BDHR 0x09 /* Baud rate divisor high byte register */
78 #define IT8512_MSTCR_RESET 0x01 /* Reset registers to default value */
79 #define IT8512_MSTCR_FIFOCLR 0x02 /* Clear FIFO */
80 #define IT8512_MSTCR_FIFOTL_7 0x04 /* FIFO threshold level : 7 */
81 #define IT8512_MSTCR_FIFOTL_25 0x0c /* FIFO threshold level : 25 */
82 #define IT8512_IER_RDAIE 0x02 /* Enable data interrupt request */
83 #define IT8512_IER_RFOIE 0x04 /* Enable FIFO overrun interrupt req */
84 #define IT8512_IER_IEC 0x80 /* Enable interrupt request */
85 #define IT8512_CFR_CF_36KHZ 0x09 /* Carrier freq : low speed, 36kHz */
86 #define IT8512_RCR_RXDCR_1 0x01 /* Demodulation carrier range : 1 */
87 #define IT8512_RCR_RXACT 0x08 /* Receiver active */
88 #define IT8512_RCR_RXEN 0x80 /* Receiver enable */
89 #define IT8512_BDR_6 6 /* Baud rate divisor : 6 */
91 /* Actual values used by this driver */
92 #define CFG_FIFOTL IT8512_MSTCR_FIFOTL_25
93 #define CFG_CR_FREQ IT8512_CFR_CF_36KHZ
94 #define CFG_DCR IT8512_RCR_RXDCR_1
95 #define CFG_BDR IT8512_BDR_6
96 #define CFG_TIMEOUT 100000 /* Rearm interrupt when a space is > 100 ms */
100 struct ite8709_device
{
104 spinlock_t hardware_lock
;
108 struct timeval last_tv
;
109 struct lirc_driver driver
;
110 struct tasklet_struct tasklet
;
116 #define dprintk(fmt, args...) \
119 printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
124 static unsigned char ite8709_read(struct ite8709_device
*dev
,
128 return inb(dev
->io
+1);
131 static void ite8709_write(struct ite8709_device
*dev
, unsigned char port
,
135 outb(data
, dev
->io
+1);
138 static void ite8709_wait_device(struct ite8709_device
*dev
)
142 * loop until device tells it's ready to continue
143 * iterations count is usually ~750 but can sometimes achieve 13000
145 for (i
= 0; i
< 15000; i
++) {
147 if (ite8709_read(dev
, ITE8709_MODE
) == ITE8709_MODE_READY
)
152 static void ite8709_write_register(struct ite8709_device
*dev
,
153 unsigned char reg_adr
, unsigned char reg_value
)
155 ite8709_wait_device(dev
);
157 ite8709_write(dev
, ITE8709_REG_VAL
, reg_value
);
158 ite8709_write(dev
, ITE8709_REG_ADR
, reg_adr
);
159 ite8709_write(dev
, ITE8709_MODE
, ITE8709_MODE_WRITE
);
162 static void ite8709_init_hardware(struct ite8709_device
*dev
)
164 spin_lock_irq(&dev
->hardware_lock
);
165 dev
->device_busy
= 1;
166 spin_unlock_irq(&dev
->hardware_lock
);
168 ite8709_write_register(dev
, IT8512_REG_BDHR
, (CFG_BDR
>> 8) & 0xff);
169 ite8709_write_register(dev
, IT8512_REG_BDLR
, CFG_BDR
& 0xff);
170 ite8709_write_register(dev
, IT8512_REG_CFR
, CFG_CR_FREQ
);
171 ite8709_write_register(dev
, IT8512_REG_IER
,
172 IT8512_IER_IEC
| IT8512_IER_RFOIE
| IT8512_IER_RDAIE
);
173 ite8709_write_register(dev
, IT8512_REG_RCR
, CFG_DCR
);
174 ite8709_write_register(dev
, IT8512_REG_MSTCR
,
175 CFG_FIFOTL
| IT8512_MSTCR_FIFOCLR
);
176 ite8709_write_register(dev
, IT8512_REG_RCR
,
177 IT8512_RCR_RXEN
| IT8512_RCR_RXACT
| CFG_DCR
);
179 spin_lock_irq(&dev
->hardware_lock
);
180 dev
->device_busy
= 0;
181 spin_unlock_irq(&dev
->hardware_lock
);
183 tasklet_enable(&dev
->tasklet
);
186 static void ite8709_drop_hardware(struct ite8709_device
*dev
)
188 tasklet_disable(&dev
->tasklet
);
190 spin_lock_irq(&dev
->hardware_lock
);
191 dev
->device_busy
= 1;
192 spin_unlock_irq(&dev
->hardware_lock
);
194 ite8709_write_register(dev
, IT8512_REG_RCR
, 0);
195 ite8709_write_register(dev
, IT8512_REG_MSTCR
,
196 IT8512_MSTCR_RESET
| IT8512_MSTCR_FIFOCLR
);
198 spin_lock_irq(&dev
->hardware_lock
);
199 dev
->device_busy
= 0;
200 spin_unlock_irq(&dev
->hardware_lock
);
203 static int ite8709_set_use_inc(void *data
)
205 struct ite8709_device
*dev
;
207 if (dev
->use_count
== 0)
208 ite8709_init_hardware(dev
);
213 static void ite8709_set_use_dec(void *data
)
215 struct ite8709_device
*dev
;
218 if (dev
->use_count
== 0)
219 ite8709_drop_hardware(dev
);
222 static void ite8709_add_read_queue(struct ite8709_device
*dev
, int flag
,
227 dprintk("add a %llu usec %s\n", val
, flag
? "pulse" : "space");
229 value
= (val
> PULSE_MASK
) ? PULSE_MASK
: val
;
233 if (!lirc_buffer_full(dev
->driver
.rbuf
)) {
234 lirc_buffer_write(dev
->driver
.rbuf
, (void *) &value
);
235 wake_up(&dev
->driver
.rbuf
->wait_poll
);
239 static irqreturn_t
ite8709_interrupt(int irq
, void *dev_id
)
245 struct timeval curr_tv
;
247 /* Bit duration in microseconds */
248 const unsigned long bit_duration
= 1000000ul / (115200 / CFG_BDR
);
250 struct ite8709_device
*dev
;
254 * If device is busy, we simply discard data because we are in one of
255 * these two cases : shutting down or rearming the device, so this
256 * doesn't really matter and this avoids waiting too long in IRQ ctx
258 spin_lock(&dev
->hardware_lock
);
259 if (dev
->device_busy
) {
260 spin_unlock(&dev
->hardware_lock
);
261 return IRQ_RETVAL(IRQ_HANDLED
);
264 iir
= ite8709_read(dev
, ITE8709_IIR
);
267 case ITE8709_IIR_RFOI
:
268 dprintk("fifo overrun, scheduling forced rearm just in case\n");
269 dev
->force_rearm
= 1;
270 tasklet_schedule(&dev
->tasklet
);
271 spin_unlock(&dev
->hardware_lock
);
272 return IRQ_RETVAL(IRQ_HANDLED
);
274 case ITE8709_IIR_RDAI
:
275 rfsr
= ite8709_read(dev
, ITE8709_RFSR
);
276 fifo
= rfsr
& ITE8709_RFSR_MASK
;
279 dprintk("iir: 0x%x rfsr: 0x%x fifo: %d\n", iir
, rfsr
, fifo
);
282 do_gettimeofday(&curr_tv
);
283 dev
->acc_space
+= 1000000ull
284 * (curr_tv
.tv_sec
- dev
->last_tv
.tv_sec
)
285 + (curr_tv
.tv_usec
- dev
->last_tv
.tv_usec
);
288 for (i
= 0; i
< fifo
; i
++) {
289 data
= ite8709_read(dev
, i
+ITE8709_FIFO_START
);
292 for (bit
= 0; bit
< 8; ++bit
) {
293 if ((data
>> bit
) & 1) {
294 dev
->acc_pulse
+= bit_duration
;
295 if (dev
->lastbit
== 0) {
296 ite8709_add_read_queue(dev
, 0,
301 dev
->acc_space
+= bit_duration
;
302 if (dev
->lastbit
== 1) {
303 ite8709_add_read_queue(dev
, 1,
308 dev
->lastbit
= (data
>> bit
) & 1;
311 ite8709_write(dev
, ITE8709_RFSR
, 0);
313 if (dev
->acc_space
> CFG_TIMEOUT
) {
314 dprintk("scheduling rearm IRQ\n");
315 do_gettimeofday(&dev
->last_tv
);
316 dev
->force_rearm
= 0;
317 tasklet_schedule(&dev
->tasklet
);
320 spin_unlock(&dev
->hardware_lock
);
321 return IRQ_RETVAL(IRQ_HANDLED
);
325 dprintk("unknown IRQ (shouldn't happen) !!\n");
326 spin_unlock(&dev
->hardware_lock
);
327 return IRQ_RETVAL(IRQ_NONE
);
331 static void ite8709_rearm_irq(unsigned long data
)
333 struct ite8709_device
*dev
;
335 dev
= (struct ite8709_device
*) data
;
337 spin_lock_irqsave(&dev
->hardware_lock
, flags
);
338 dev
->device_busy
= 1;
339 spin_unlock_irqrestore(&dev
->hardware_lock
, flags
);
341 if (dev
->force_rearm
|| dev
->acc_space
> CFG_TIMEOUT
) {
342 dprintk("rearming IRQ\n");
343 ite8709_write_register(dev
, IT8512_REG_RCR
,
344 IT8512_RCR_RXACT
| CFG_DCR
);
345 ite8709_write_register(dev
, IT8512_REG_MSTCR
,
346 CFG_FIFOTL
| IT8512_MSTCR_FIFOCLR
);
347 ite8709_write_register(dev
, IT8512_REG_RCR
,
348 IT8512_RCR_RXEN
| IT8512_RCR_RXACT
| CFG_DCR
);
349 if (!dev
->force_rearm
)
351 dev
->force_rearm
= 0;
354 spin_lock_irqsave(&dev
->hardware_lock
, flags
);
355 dev
->device_busy
= 0;
356 spin_unlock_irqrestore(&dev
->hardware_lock
, flags
);
359 static int ite8709_cleanup(struct ite8709_device
*dev
, int stage
, int errno
,
363 printk(KERN_ERR LIRC_DRIVER_NAME
": %s\n", msg
);
367 if (dev
->use_count
> 0)
368 ite8709_drop_hardware(dev
);
370 free_irq(dev
->irq
, dev
);
372 release_region(dev
->io
, 2);
374 lirc_unregister_driver(dev
->driver
.minor
);
376 lirc_buffer_free(dev
->driver
.rbuf
);
377 kfree(dev
->driver
.rbuf
);
387 static int __devinit
ite8709_pnp_probe(struct pnp_dev
*dev
,
388 const struct pnp_device_id
*dev_id
)
390 struct lirc_driver
*driver
;
391 struct ite8709_device
*ite8709_dev
;
394 /* Check resources validity */
395 if (!pnp_irq_valid(dev
, 0))
396 return ite8709_cleanup(NULL
, 0, -ENODEV
, "invalid IRQ");
397 if (!pnp_port_valid(dev
, 2))
398 return ite8709_cleanup(NULL
, 0, -ENODEV
, "invalid IO port");
400 /* Allocate memory for device struct */
401 ite8709_dev
= kzalloc(sizeof(struct ite8709_device
), GFP_KERNEL
);
402 if (ite8709_dev
== NULL
)
403 return ite8709_cleanup(NULL
, 0, -ENOMEM
, "kzalloc failed");
404 pnp_set_drvdata(dev
, ite8709_dev
);
406 /* Initialize device struct */
407 ite8709_dev
->use_count
= 0;
408 ite8709_dev
->irq
= pnp_irq(dev
, 0);
409 ite8709_dev
->io
= pnp_port_start(dev
, 2);
410 ite8709_dev
->hardware_lock
=
411 __SPIN_LOCK_UNLOCKED(ite8709_dev
->hardware_lock
);
412 ite8709_dev
->acc_pulse
= 0;
413 ite8709_dev
->acc_space
= 0;
414 ite8709_dev
->lastbit
= 0;
415 do_gettimeofday(&ite8709_dev
->last_tv
);
416 tasklet_init(&ite8709_dev
->tasklet
, ite8709_rearm_irq
,
418 ite8709_dev
->force_rearm
= 0;
419 ite8709_dev
->rearmed
= 0;
420 ite8709_dev
->device_busy
= 0;
422 /* Initialize driver struct */
423 driver
= &ite8709_dev
->driver
;
424 strcpy(driver
->name
, LIRC_DRIVER_NAME
);
426 driver
->code_length
= sizeof(int) * 8;
427 driver
->sample_rate
= 0;
428 driver
->features
= LIRC_CAN_REC_MODE2
;
429 driver
->data
= ite8709_dev
;
430 driver
->add_to_buf
= NULL
;
431 driver
->set_use_inc
= ite8709_set_use_inc
;
432 driver
->set_use_dec
= ite8709_set_use_dec
;
433 driver
->dev
= &dev
->dev
;
434 driver
->owner
= THIS_MODULE
;
436 /* Initialize LIRC buffer */
437 driver
->rbuf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
439 return ite8709_cleanup(ite8709_dev
, 1, -ENOMEM
,
440 "can't allocate lirc_buffer");
441 if (lirc_buffer_init(driver
->rbuf
, BUF_CHUNK_SIZE
, BUF_SIZE
))
442 return ite8709_cleanup(ite8709_dev
, 1, -ENOMEM
,
443 "lirc_buffer_init() failed");
445 /* Register LIRC driver */
446 ret
= lirc_register_driver(driver
);
448 return ite8709_cleanup(ite8709_dev
, 2, ret
,
449 "lirc_register_driver() failed");
451 /* Reserve I/O port access */
452 if (!request_region(ite8709_dev
->io
, 2, LIRC_DRIVER_NAME
))
453 return ite8709_cleanup(ite8709_dev
, 3, -EBUSY
,
454 "i/o port already in use");
456 /* Reserve IRQ line */
457 ret
= request_irq(ite8709_dev
->irq
, ite8709_interrupt
, 0,
458 LIRC_DRIVER_NAME
, ite8709_dev
);
460 return ite8709_cleanup(ite8709_dev
, 4, ret
,
461 "IRQ already in use");
463 /* Initialize hardware */
464 ite8709_drop_hardware(ite8709_dev
); /* Shutdown hw until first use */
466 printk(KERN_INFO LIRC_DRIVER_NAME
": device found : irq=%d io=0x%x\n",
467 ite8709_dev
->irq
, ite8709_dev
->io
);
472 static void __devexit
ite8709_pnp_remove(struct pnp_dev
*dev
)
474 struct ite8709_device
*ite8709_dev
;
475 ite8709_dev
= pnp_get_drvdata(dev
);
477 ite8709_cleanup(ite8709_dev
, 6, 0, NULL
);
479 printk(KERN_INFO LIRC_DRIVER_NAME
": device removed\n");
483 static int ite8709_pnp_suspend(struct pnp_dev
*dev
, pm_message_t state
)
485 struct ite8709_device
*ite8709_dev
;
486 ite8709_dev
= pnp_get_drvdata(dev
);
488 if (ite8709_dev
->use_count
> 0)
489 ite8709_drop_hardware(ite8709_dev
);
494 static int ite8709_pnp_resume(struct pnp_dev
*dev
)
496 struct ite8709_device
*ite8709_dev
;
497 ite8709_dev
= pnp_get_drvdata(dev
);
499 if (ite8709_dev
->use_count
> 0)
500 ite8709_init_hardware(ite8709_dev
);
505 #define ite8709_pnp_suspend NULL
506 #define ite8709_pnp_resume NULL
509 static const struct pnp_device_id pnp_dev_table
[] = {
514 MODULE_DEVICE_TABLE(pnp
, pnp_dev_table
);
516 static struct pnp_driver ite8709_pnp_driver
= {
517 .name
= LIRC_DRIVER_NAME
,
518 .probe
= ite8709_pnp_probe
,
519 .remove
= __devexit_p(ite8709_pnp_remove
),
520 .suspend
= ite8709_pnp_suspend
,
521 .resume
= ite8709_pnp_resume
,
522 .id_table
= pnp_dev_table
,
525 static int __init
ite8709_init_module(void)
527 return pnp_register_driver(&ite8709_pnp_driver
);
529 module_init(ite8709_init_module
);
531 static void __exit
ite8709_cleanup_module(void)
533 pnp_unregister_driver(&ite8709_pnp_driver
);
535 module_exit(ite8709_cleanup_module
);
537 MODULE_DESCRIPTION("LIRC driver for ITE8709 CIR port");
538 MODULE_AUTHOR("Grégory Lardière");
539 MODULE_LICENSE("GPL");
541 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
542 MODULE_PARM_DESC(debug
, "Enable debugging messages");