Added lirc.
[irreco.git] / lirc-0.8.4a / drivers / lirc_ite8709 / lirc_ite8709.c
blob5c640b99eb2f8fcac4f55fa72225a93d04af5425
1 /*
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
19 * USA
22 #include <linux/version.h>
23 #include <linux/module.h>
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <linux/autoconf.h>
31 #include <linux/pnp.h>
32 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
33 #include <asm/io.h>
34 #else
35 #include <linux/io.h>
36 #endif
38 #include "drivers/lirc.h"
39 #include "drivers/lirc_dev/lirc_dev.h"
40 #include "drivers/kcompat.h"
42 #define LIRC_DRIVER_NAME "lirc_ite8709"
44 #define BUF_CHUNK_SIZE sizeof(lirc_t)
45 #define BUF_SIZE (128*BUF_CHUNK_SIZE)
47 /*******************************************************************************
48 * The ITE8709 device seems to be the combination of IT8512 superIO chip and *
49 * a specific firmware running on the IT8512's embedded micro-controller. *
50 * In addition of the embedded micro-controller, the IT8512 chip contains a *
51 * CIR module and several other modules. A few modules are directly accessible *
52 * by the host CPU, but most of them are only accessible by the *
53 * micro-controller. The CIR module is only accessible by the micro-controller. *
54 * The battery-backed SRAM module is accessible by the host CPU and the *
55 * micro-controller. So one of the MC's firmware role is to act as a bridge *
56 * between the host CPU and the CIR module. The firmware implements a kind of *
57 * communication protocol using the SRAM module as a shared memory. The IT8512 *
58 * specification is publicly available on ITE's web site, but the communication *
59 * protocol is not, so it was reverse-engineered. *
60 *******************************************************************************/
62 /* ITE8709 Registers addresses and values (reverse-engineered) */
63 #define ITE8709_MODE 0x1a
64 #define ITE8709_REG_ADR 0x1b
65 #define ITE8709_REG_VAL 0x1c
66 #define ITE8709_IIR 0x1e /* Interrupt identification register */
67 #define ITE8709_RFSR 0x1f /* Receiver FIFO status register */
68 #define ITE8709_FIFO_START 0x20
70 #define ITE8709_MODE_READY 0X00
71 #define ITE8709_MODE_WRITE 0X01
72 #define ITE8709_MODE_READ 0X02
73 #define ITE8709_IIR_RDAI 0x02 /* Receiver data available interrupt */
74 #define ITE8709_IIR_RFOI 0x04 /* Receiver FIFO overrun interrupt */
75 #define ITE8709_RFSR_MASK 0x3f /* FIFO byte count mask */
77 /* IT8512 CIR-module registers addresses and values (from IT8512 E/F */
78 /* specification v0.4.1) */
79 #define IT8512_REG_MSTCR 0x01 /* Master control register */
80 #define IT8512_REG_IER 0x02 /* Interrupt enable register */
81 #define IT8512_REG_CFR 0x04 /* Carrier frequency register */
82 #define IT8512_REG_RCR 0x05 /* Receive control register */
83 #define IT8512_REG_BDLR 0x08 /* Baud rate divisor low byte register */
84 #define IT8512_REG_BDHR 0x09 /* Baud rate divisor high byte register */
86 #define IT8512_MSTCR_RESET 0x01 /* Reset registers to default value */
87 #define IT8512_MSTCR_FIFOCLR 0x02 /* Clear FIFO */
88 #define IT8512_MSTCR_FIFOTL_7 0x04 /* FIFO threshold level : 7 */
89 #define IT8512_MSTCR_FIFOTL_25 0x0c /* FIFO threshold level : 25 */
90 #define IT8512_IER_RDAIE 0x02 /* Enable data interrupt request */
91 #define IT8512_IER_RFOIE 0x04 /* Enable FIFO overrun interrupt req */
92 #define IT8512_IER_IEC 0x80 /* Enable interrupt request */
93 #define IT8512_CFR_CF_36KHZ 0x09 /* Carrier freq : low speed, 36kHz */
94 #define IT8512_RCR_RXDCR_1 0x01 /* Demodulation carrier range : 1 */
95 #define IT8512_RCR_RXACT 0x08 /* Receiver active */
96 #define IT8512_RCR_RXEN 0x80 /* Receiver enable */
97 #define IT8512_BDR_6 6 /* Baud rate divisor : 6 */
99 /* Actual values used by this driver */
100 #define CFG_FIFOTL IT8512_MSTCR_FIFOTL_25
101 #define CFG_CR_FREQ IT8512_CFR_CF_36KHZ
102 #define CFG_DCR IT8512_RCR_RXDCR_1
103 #define CFG_BDR IT8512_BDR_6
104 #define CFG_TIMEOUT 100000 /* Rearm interrupt when a space is > 100 ms */
106 static int debug;
108 struct ite8709_device {
109 int use_count;
110 int io;
111 int irq;
112 spinlock_t hardware_lock;
113 unsigned long long acc_pulse;
114 unsigned long long acc_space;
115 char lastbit;
116 struct timeval last_tv;
117 struct lirc_plugin plugin;
118 struct lirc_buffer buffer;
119 struct tasklet_struct tasklet;
120 char force_rearm;
121 char rearmed;
122 char device_busy;
125 #define dprintk(fmt, args...) \
126 do { \
127 if (debug) \
128 printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
129 fmt, ## args); \
130 } while (0)
133 static unsigned char ite8709_read(struct ite8709_device *dev,
134 unsigned char port)
136 outb(port, dev->io);
137 return inb(dev->io+1);
140 static void ite8709_write(struct ite8709_device *dev, unsigned char port,
141 unsigned char data)
143 outb(port, dev->io);
144 outb(data, dev->io+1);
147 static void ite8709_wait_device(struct ite8709_device *dev)
149 int i = 0;
150 /* loop until device tells it's ready to continue */
151 /* iterations count is usually ~750 but can sometimes achieve 13000 */
152 for (i = 0; i < 15000; i++) {
153 udelay(2);
154 if (ite8709_read(dev, ITE8709_MODE) == ITE8709_MODE_READY)
155 break;
159 static void ite8709_write_register(struct ite8709_device *dev,
160 unsigned char reg_adr, unsigned char reg_value)
162 ite8709_wait_device(dev);
164 ite8709_write(dev, ITE8709_REG_VAL, reg_value);
165 ite8709_write(dev, ITE8709_REG_ADR, reg_adr);
166 ite8709_write(dev, ITE8709_MODE, ITE8709_MODE_WRITE);
169 static unsigned char ite8709_read_register(struct ite8709_device *dev,
170 unsigned char reg_adr)
172 ite8709_wait_device(dev);
174 ite8709_write(dev, ITE8709_REG_ADR, reg_adr);
175 ite8709_write(dev, ITE8709_MODE, ITE8709_MODE_READ);
177 ite8709_wait_device(dev);
179 return ite8709_read(dev, ITE8709_REG_VAL);
182 static void ite8709_init_hardware(struct ite8709_device *dev)
184 spin_lock_irq(&dev->hardware_lock);
185 dev->device_busy = 1;
186 spin_unlock_irq(&dev->hardware_lock);
188 ite8709_write_register(dev, IT8512_REG_BDHR, (CFG_BDR >> 8) & 0xff);
189 ite8709_write_register(dev, IT8512_REG_BDLR, CFG_BDR & 0xff);
190 ite8709_write_register(dev, IT8512_REG_CFR, CFG_CR_FREQ);
191 ite8709_write_register(dev, IT8512_REG_IER,
192 IT8512_IER_IEC | IT8512_IER_RFOIE | IT8512_IER_RDAIE);
193 ite8709_write_register(dev, IT8512_REG_RCR, CFG_DCR);
194 ite8709_write_register(dev, IT8512_REG_MSTCR,
195 CFG_FIFOTL | IT8512_MSTCR_FIFOCLR);
196 ite8709_write_register(dev, IT8512_REG_RCR,
197 IT8512_RCR_RXEN | IT8512_RCR_RXACT | CFG_DCR);
199 spin_lock_irq(&dev->hardware_lock);
200 dev->device_busy = 0;
201 spin_unlock_irq(&dev->hardware_lock);
203 tasklet_enable(&dev->tasklet);
206 static void ite8709_drop_hardware(struct ite8709_device *dev)
208 tasklet_disable(&dev->tasklet);
210 spin_lock_irq(&dev->hardware_lock);
211 dev->device_busy = 1;
212 spin_unlock_irq(&dev->hardware_lock);
214 ite8709_write_register(dev, IT8512_REG_RCR, 0);
215 ite8709_write_register(dev, IT8512_REG_MSTCR,
216 IT8512_MSTCR_RESET | IT8512_MSTCR_FIFOCLR);
218 spin_lock_irq(&dev->hardware_lock);
219 dev->device_busy = 0;
220 spin_unlock_irq(&dev->hardware_lock);
223 static int ite8709_set_use_inc(void *data)
225 struct ite8709_device *dev;
226 MOD_INC_USE_COUNT;
227 dev = data;
228 if (dev->use_count == 0)
229 ite8709_init_hardware(dev);
230 dev->use_count++;
231 return 0;
234 static void ite8709_set_use_dec(void *data)
236 struct ite8709_device *dev;
237 MOD_DEC_USE_COUNT;
238 dev = data;
239 dev->use_count--;
240 if (dev->use_count == 0)
241 ite8709_drop_hardware(dev);
244 static void ite8709_add_read_queue(struct ite8709_device *dev, int flag,
245 unsigned long long val)
247 lirc_t value;
249 dprintk("add a %llu usec %s\n", val, flag ? "pulse" : "space");
251 value = (val > PULSE_MASK) ? PULSE_MASK : val;
252 if (flag)
253 value |= PULSE_BIT;
255 if (!lirc_buffer_full(&dev->buffer)) {
256 lirc_buffer_write_1(&dev->buffer, (void *) &value);
257 wake_up(&dev->buffer.wait_poll);
261 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
262 static irqreturn_t ite8709_interrupt(int irq, void *dev_id,
263 struct pt_regs *regs)
264 #else
265 static irqreturn_t ite8709_interrupt(int irq, void *dev_id)
266 #endif
268 unsigned char data;
269 int iir, rfsr, i;
270 int fifo = 0;
271 char bit;
272 struct timeval curr_tv;
274 struct ite8709_device *dev;
275 dev = dev_id;
277 /* Bit duration in microseconds */
278 const unsigned long bit_duration = 1000000ul / (115200 / CFG_BDR);
280 /* If device is busy, we simply discard data because we are in one of */
281 /* these two cases : shutting down or rearming the device, so this */
282 /* doesn't really matter and this avoids waiting too long in IRQ ctx */
283 spin_lock(&dev->hardware_lock);
284 if (dev->device_busy) {
285 spin_unlock(&dev->hardware_lock);
286 return IRQ_RETVAL(IRQ_HANDLED);
289 iir = ite8709_read(dev, ITE8709_IIR);
291 switch (iir) {
292 case ITE8709_IIR_RFOI:
293 dprintk("fifo overrun, scheduling forced rearm just in case\n");
294 dev->force_rearm = 1;
295 tasklet_schedule(&dev->tasklet);
296 spin_unlock(&dev->hardware_lock);
297 return IRQ_RETVAL(IRQ_HANDLED);
299 case ITE8709_IIR_RDAI:
300 rfsr = ite8709_read(dev, ITE8709_RFSR);
301 fifo = rfsr & ITE8709_RFSR_MASK;
302 if (fifo > 32)
303 fifo = 32;
304 dprintk("iir: 0x%x rfsr: 0x%x fifo: %d\n", iir, rfsr, fifo);
306 if (dev->rearmed) {
307 do_gettimeofday(&curr_tv);
308 dev->acc_space += 1000000ull
309 * (curr_tv.tv_sec - dev->last_tv.tv_sec)
310 + (curr_tv.tv_usec - dev->last_tv.tv_usec);
311 dev->rearmed = 0;
313 for (i = 0; i < fifo; i++) {
314 data = ite8709_read(dev, i+ITE8709_FIFO_START);
315 data = ~data;
316 /* Loop through */
317 for (bit = 0; bit < 8; ++bit) {
318 if ((data >> bit) & 1) {
319 dev->acc_pulse += bit_duration;
320 if (dev->lastbit == 0) {
321 ite8709_add_read_queue(dev, 0,
322 dev->acc_space);
323 dev->acc_space = 0;
325 } else {
326 dev->acc_space += bit_duration;
327 if (dev->lastbit == 1) {
328 ite8709_add_read_queue(dev, 1,
329 dev->acc_pulse);
330 dev->acc_pulse = 0;
333 dev->lastbit = (data >> bit) & 1;
336 ite8709_write(dev, ITE8709_RFSR, 0);
338 if (dev->acc_space > CFG_TIMEOUT) {
339 dprintk("scheduling rearm IRQ\n");
340 do_gettimeofday(&dev->last_tv);
341 dev->force_rearm = 0;
342 tasklet_schedule(&dev->tasklet);
345 spin_unlock(&dev->hardware_lock);
346 return IRQ_RETVAL(IRQ_HANDLED);
348 default:
349 /* not our irq */
350 dprintk("unknown IRQ (shouldn't happen) !!\n");
351 spin_unlock(&dev->hardware_lock);
352 return IRQ_RETVAL(IRQ_NONE);
356 static void ite8709_rearm_irq(unsigned long data)
358 struct ite8709_device *dev;
359 unsigned long flags;
360 dev = (struct ite8709_device *) data;
362 spin_lock_irqsave(&dev->hardware_lock, flags);
363 dev->device_busy = 1;
364 spin_unlock_irqrestore(&dev->hardware_lock, flags);
366 if (dev->force_rearm || dev->acc_space > CFG_TIMEOUT) {
367 dprintk("rearming IRQ\n");
368 ite8709_write_register(dev, IT8512_REG_RCR,
369 IT8512_RCR_RXACT | CFG_DCR);
370 ite8709_write_register(dev, IT8512_REG_MSTCR,
371 CFG_FIFOTL | IT8512_MSTCR_FIFOCLR);
372 ite8709_write_register(dev, IT8512_REG_RCR,
373 IT8512_RCR_RXEN | IT8512_RCR_RXACT | CFG_DCR);
374 if (!dev->force_rearm)
375 dev->rearmed = 1;
376 dev->force_rearm = 0;
379 spin_lock_irqsave(&dev->hardware_lock, flags);
380 dev->device_busy = 0;
381 spin_unlock_irqrestore(&dev->hardware_lock, flags);
384 static int ite8709_cleanup(struct ite8709_device *dev, int stage, int errno,
385 char *msg)
387 if (msg != NULL)
388 printk(KERN_ERR LIRC_DRIVER_NAME ": %s\n", msg);
390 switch (stage) {
391 case 6:
392 if (dev->use_count > 0)
393 ite8709_drop_hardware(dev);
394 case 5:
395 free_irq(dev->irq, dev);
396 case 4:
397 release_region(dev->io, 2);
398 case 3:
399 lirc_unregister_plugin(dev->plugin.minor);
400 case 2:
401 lirc_buffer_free(dev->plugin.rbuf);
402 case 1:
403 kfree(dev);
404 case 0:
408 return errno;
411 static int __devinit ite8709_pnp_probe(struct pnp_dev *dev,
412 const struct pnp_device_id *dev_id)
414 struct lirc_plugin *plugin;
415 struct ite8709_device *ite8709_dev;
416 int ret;
418 /* Check resources validity */
419 if (!pnp_irq_valid(dev, 0))
420 return ite8709_cleanup(NULL, 0, -ENODEV, "invalid IRQ");
421 if (!pnp_port_valid(dev, 2))
422 return ite8709_cleanup(NULL, 0, -ENODEV, "invalid IO port");
424 /* Allocate memory for device struct */
425 ite8709_dev = kzalloc(sizeof(struct ite8709_device), GFP_KERNEL);
426 if (ite8709_dev == NULL)
427 return ite8709_cleanup(NULL, 0, -ENOMEM, "kzalloc failed");
428 pnp_set_drvdata(dev, ite8709_dev);
430 /* Initialize device struct */
431 ite8709_dev->use_count = 0;
432 ite8709_dev->irq = pnp_irq(dev, 0);
433 ite8709_dev->io = pnp_port_start(dev, 2);
434 ite8709_dev->hardware_lock = __SPIN_LOCK_UNLOCKED(
435 ite8709_dev->hardware_lock);
436 ite8709_dev->acc_pulse = 0;
437 ite8709_dev->acc_space = 0;
438 ite8709_dev->lastbit = 0;
439 do_gettimeofday(&ite8709_dev->last_tv);
440 tasklet_init(&ite8709_dev->tasklet, ite8709_rearm_irq,
441 (long) ite8709_dev);
442 ite8709_dev->force_rearm = 0;
443 ite8709_dev->rearmed = 0;
444 ite8709_dev->device_busy = 0;
446 /* Initialize plugin struct */
447 plugin = &ite8709_dev->plugin;
448 strcpy(plugin->name, LIRC_DRIVER_NAME);
449 plugin->minor = -1;
450 plugin->code_length = sizeof(lirc_t) * 8;
451 plugin->sample_rate = 0;
452 plugin->features = LIRC_CAN_REC_MODE2;
453 plugin->data = ite8709_dev;
454 plugin->add_to_buf = NULL;
455 plugin->get_queue = NULL;
456 plugin->rbuf = &ite8709_dev->buffer;
457 plugin->set_use_inc = ite8709_set_use_inc;
458 plugin->set_use_dec = ite8709_set_use_dec;
459 plugin->ioctl = NULL;
460 plugin->fops = NULL;
461 plugin->dev = &dev->dev;
462 plugin->owner = THIS_MODULE;
464 /* Initialize LIRC buffer */
465 if (lirc_buffer_init(plugin->rbuf, BUF_CHUNK_SIZE, BUF_SIZE))
466 return ite8709_cleanup(ite8709_dev, 1, -ENOMEM,
467 "lirc_buffer_init() failed");
469 /* Register LIRC plugin */
470 ret = lirc_register_plugin(plugin);
471 if (ret < 0)
472 return ite8709_cleanup(ite8709_dev, 2, ret,
473 "lirc_register_plugin() failed");
475 /* Reserve I/O port access */
476 if (!request_region(ite8709_dev->io, 2, LIRC_DRIVER_NAME))
477 return ite8709_cleanup(ite8709_dev, 3, -EBUSY,
478 "i/o port already in use");
480 /* Reserve IRQ line */
481 ret = request_irq(ite8709_dev->irq, ite8709_interrupt, 0,
482 LIRC_DRIVER_NAME, ite8709_dev);
483 if (ret < 0)
484 return ite8709_cleanup(ite8709_dev, 4, ret,
485 "IRQ already in use");
487 /* Initialize hardware */
488 ite8709_drop_hardware(ite8709_dev); /* Shutdown hw until first use */
490 printk(KERN_INFO LIRC_DRIVER_NAME ": device found : irq=%d io=0x%x\n",
491 ite8709_dev->irq, ite8709_dev->io);
493 return 0;
496 static void __devexit ite8709_pnp_remove(struct pnp_dev *dev)
498 struct ite8709_device *ite8709_dev;
499 ite8709_dev = pnp_get_drvdata(dev);
501 ite8709_cleanup(ite8709_dev, 6, 0, NULL);
503 printk(KERN_INFO LIRC_DRIVER_NAME ": device removed\n");
506 #ifdef CONFIG_PM
507 static int ite8709_pnp_suspend(struct pnp_dev *dev, pm_message_t state)
509 struct ite8709_device *ite8709_dev;
510 ite8709_dev = pnp_get_drvdata(dev);
512 if (ite8709_dev->use_count > 0)
513 ite8709_drop_hardware(ite8709_dev);
515 return 0;
518 static int ite8709_pnp_resume(struct pnp_dev *dev)
520 struct ite8709_device *ite8709_dev;
521 ite8709_dev = pnp_get_drvdata(dev);
523 if (ite8709_dev->use_count > 0)
524 ite8709_init_hardware(ite8709_dev);
526 return 0;
528 #else
529 #define ite8709_pnp_suspend NULL
530 #define ite8709_pnp_resume NULL
531 #endif
533 static const struct pnp_device_id pnp_dev_table[] = {
534 {"ITE8709", 0},
538 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
540 static struct pnp_driver ite8709_pnp_driver = {
541 .name = LIRC_DRIVER_NAME,
542 .probe = ite8709_pnp_probe,
543 .remove = __devexit_p(ite8709_pnp_remove),
544 .suspend = ite8709_pnp_suspend,
545 .resume = ite8709_pnp_resume,
546 .id_table = pnp_dev_table,
549 int init_module(void)
551 return pnp_register_driver(&ite8709_pnp_driver);
554 void cleanup_module(void)
556 pnp_unregister_driver(&ite8709_pnp_driver);
559 MODULE_DESCRIPTION("LIRC driver for ITE8709 CIR port");
560 MODULE_AUTHOR("Grégory Lardière");
561 MODULE_LICENSE("GPL");
563 module_param(debug, bool, 0644);
564 MODULE_PARM_DESC(debug, "Enable debugging messages");
566 EXPORT_NO_SYMBOLS;
569 * Overrides for Emacs so that we follow Linus's tabbing style.
570 * ---------------------------------------------------------------------------
571 * Local variables:
572 * c-basic-offset: 8
573 * End: