Merge branches 'master' and 'devel' into for-linus
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / lirc / lirc_ite8709.c
CommitLineData
85cb01af
JW
1/*
2 * LIRC driver for ITE8709 CIR port
3 *
4 * Copyright (C) 2008 Grégory Lardière <spmf2004-lirc@yahoo.fr>
5 *
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.
10 *
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.
15 *
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
20 */
21
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>
27#include <linux/io.h>
28
29#include <media/lirc.h>
30#include <media/lirc_dev.h>
31
32#define LIRC_DRIVER_NAME "lirc_ite8709"
33
34#define BUF_CHUNK_SIZE sizeof(int)
35#define BUF_SIZE (128*BUF_CHUNK_SIZE)
36
37/*
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.
50 */
51
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
59
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 */
66
67/*
68 * IT8512 CIR-module registers addresses and values
69 * (from IT8512 E/F specification v0.4.1)
70 */
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 */
77
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 */
90
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 */
97
98static int debug;
99
100struct ite8709_device {
101 int use_count;
102 int io;
103 int irq;
104 spinlock_t hardware_lock;
105 unsigned long long acc_pulse;
106 unsigned long long acc_space;
107 char lastbit;
108 struct timeval last_tv;
109 struct lirc_driver driver;
110 struct tasklet_struct tasklet;
111 char force_rearm;
112 char rearmed;
113 char device_busy;
114};
115
116#define dprintk(fmt, args...) \
117 do { \
118 if (debug) \
119 printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
120 fmt, ## args); \
121 } while (0)
122
123
124static unsigned char ite8709_read(struct ite8709_device *dev,
125 unsigned char port)
126{
127 outb(port, dev->io);
128 return inb(dev->io+1);
129}
130
131static void ite8709_write(struct ite8709_device *dev, unsigned char port,
132 unsigned char data)
133{
134 outb(port, dev->io);
135 outb(data, dev->io+1);
136}
137
138static void ite8709_wait_device(struct ite8709_device *dev)
139{
140 int i = 0;
141 /*
142 * loop until device tells it's ready to continue
143 * iterations count is usually ~750 but can sometimes achieve 13000
144 */
145 for (i = 0; i < 15000; i++) {
146 udelay(2);
147 if (ite8709_read(dev, ITE8709_MODE) == ITE8709_MODE_READY)
148 break;
149 }
150}
151
152static void ite8709_write_register(struct ite8709_device *dev,
153 unsigned char reg_adr, unsigned char reg_value)
154{
155 ite8709_wait_device(dev);
156
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);
160}
161
162static void ite8709_init_hardware(struct ite8709_device *dev)
163{
164 spin_lock_irq(&dev->hardware_lock);
165 dev->device_busy = 1;
166 spin_unlock_irq(&dev->hardware_lock);
167
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);
178
179 spin_lock_irq(&dev->hardware_lock);
180 dev->device_busy = 0;
181 spin_unlock_irq(&dev->hardware_lock);
182
183 tasklet_enable(&dev->tasklet);
184}
185
186static void ite8709_drop_hardware(struct ite8709_device *dev)
187{
188 tasklet_disable(&dev->tasklet);
189
190 spin_lock_irq(&dev->hardware_lock);
191 dev->device_busy = 1;
192 spin_unlock_irq(&dev->hardware_lock);
193
194 ite8709_write_register(dev, IT8512_REG_RCR, 0);
195 ite8709_write_register(dev, IT8512_REG_MSTCR,
196 IT8512_MSTCR_RESET | IT8512_MSTCR_FIFOCLR);
197
198 spin_lock_irq(&dev->hardware_lock);
199 dev->device_busy = 0;
200 spin_unlock_irq(&dev->hardware_lock);
201}
202
203static int ite8709_set_use_inc(void *data)
204{
205 struct ite8709_device *dev;
206 dev = data;
207 if (dev->use_count == 0)
208 ite8709_init_hardware(dev);
209 dev->use_count++;
210 return 0;
211}
212
213static void ite8709_set_use_dec(void *data)
214{
215 struct ite8709_device *dev;
216 dev = data;
217 dev->use_count--;
218 if (dev->use_count == 0)
219 ite8709_drop_hardware(dev);
220}
221
222static void ite8709_add_read_queue(struct ite8709_device *dev, int flag,
223 unsigned long long val)
224{
225 int value;
226
227 dprintk("add a %llu usec %s\n", val, flag ? "pulse" : "space");
228
229 value = (val > PULSE_MASK) ? PULSE_MASK : val;
230 if (flag)
231 value |= PULSE_BIT;
232
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);
236 }
237}
238
239static irqreturn_t ite8709_interrupt(int irq, void *dev_id)
240{
241 unsigned char data;
242 int iir, rfsr, i;
243 int fifo = 0;
244 char bit;
245 struct timeval curr_tv;
246
247 /* Bit duration in microseconds */
248 const unsigned long bit_duration = 1000000ul / (115200 / CFG_BDR);
249
250 struct ite8709_device *dev;
251 dev = dev_id;
252
253 /*
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
257 */
258 spin_lock(&dev->hardware_lock);
259 if (dev->device_busy) {
260 spin_unlock(&dev->hardware_lock);
261 return IRQ_RETVAL(IRQ_HANDLED);
262 }
263
264 iir = ite8709_read(dev, ITE8709_IIR);
265
266 switch (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);
273
274 case ITE8709_IIR_RDAI:
275 rfsr = ite8709_read(dev, ITE8709_RFSR);
276 fifo = rfsr & ITE8709_RFSR_MASK;
277 if (fifo > 32)
278 fifo = 32;
279 dprintk("iir: 0x%x rfsr: 0x%x fifo: %d\n", iir, rfsr, fifo);
280
281 if (dev->rearmed) {
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);
286 dev->rearmed = 0;
287 }
288 for (i = 0; i < fifo; i++) {
289 data = ite8709_read(dev, i+ITE8709_FIFO_START);
290 data = ~data;
291 /* Loop through */
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,
297 dev->acc_space);
298 dev->acc_space = 0;
299 }
300 } else {
301 dev->acc_space += bit_duration;
302 if (dev->lastbit == 1) {
303 ite8709_add_read_queue(dev, 1,
304 dev->acc_pulse);
305 dev->acc_pulse = 0;
306 }
307 }
308 dev->lastbit = (data >> bit) & 1;
309 }
310 }
311 ite8709_write(dev, ITE8709_RFSR, 0);
312
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);
318 }
319
320 spin_unlock(&dev->hardware_lock);
321 return IRQ_RETVAL(IRQ_HANDLED);
322
323 default:
324 /* not our irq */
325 dprintk("unknown IRQ (shouldn't happen) !!\n");
326 spin_unlock(&dev->hardware_lock);
327 return IRQ_RETVAL(IRQ_NONE);
328 }
329}
330
331static void ite8709_rearm_irq(unsigned long data)
332{
333 struct ite8709_device *dev;
334 unsigned long flags;
335 dev = (struct ite8709_device *) data;
336
337 spin_lock_irqsave(&dev->hardware_lock, flags);
338 dev->device_busy = 1;
339 spin_unlock_irqrestore(&dev->hardware_lock, flags);
340
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)
350 dev->rearmed = 1;
351 dev->force_rearm = 0;
352 }
353
354 spin_lock_irqsave(&dev->hardware_lock, flags);
355 dev->device_busy = 0;
356 spin_unlock_irqrestore(&dev->hardware_lock, flags);
357}
358
359static int ite8709_cleanup(struct ite8709_device *dev, int stage, int errno,
360 char *msg)
361{
362 if (msg != NULL)
363 printk(KERN_ERR LIRC_DRIVER_NAME ": %s\n", msg);
364
365 switch (stage) {
366 case 6:
367 if (dev->use_count > 0)
368 ite8709_drop_hardware(dev);
369 case 5:
370 free_irq(dev->irq, dev);
371 case 4:
372 release_region(dev->io, 2);
373 case 3:
374 lirc_unregister_driver(dev->driver.minor);
375 case 2:
376 lirc_buffer_free(dev->driver.rbuf);
377 kfree(dev->driver.rbuf);
378 case 1:
379 kfree(dev);
380 case 0:
381 ;
382 }
383
384 return errno;
385}
386
387static int __devinit ite8709_pnp_probe(struct pnp_dev *dev,
388 const struct pnp_device_id *dev_id)
389{
390 struct lirc_driver *driver;
391 struct ite8709_device *ite8709_dev;
392 int ret;
393
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");
399
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);
405
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,
417 (long) ite8709_dev);
418 ite8709_dev->force_rearm = 0;
419 ite8709_dev->rearmed = 0;
420 ite8709_dev->device_busy = 0;
421
422 /* Initialize driver struct */
423 driver = &ite8709_dev->driver;
424 strcpy(driver->name, LIRC_DRIVER_NAME);
425 driver->minor = -1;
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;
435
436 /* Initialize LIRC buffer */
437 driver->rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
438 if (!driver->rbuf)
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");
444
445 /* Register LIRC driver */
446 ret = lirc_register_driver(driver);
447 if (ret < 0)
448 return ite8709_cleanup(ite8709_dev, 2, ret,
449 "lirc_register_driver() failed");
450
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");
455
456 /* Reserve IRQ line */
457 ret = request_irq(ite8709_dev->irq, ite8709_interrupt, 0,
458 LIRC_DRIVER_NAME, ite8709_dev);
459 if (ret < 0)
460 return ite8709_cleanup(ite8709_dev, 4, ret,
461 "IRQ already in use");
462
463 /* Initialize hardware */
464 ite8709_drop_hardware(ite8709_dev); /* Shutdown hw until first use */
465
466 printk(KERN_INFO LIRC_DRIVER_NAME ": device found : irq=%d io=0x%x\n",
467 ite8709_dev->irq, ite8709_dev->io);
468
469 return 0;
470}
471
472static void __devexit ite8709_pnp_remove(struct pnp_dev *dev)
473{
474 struct ite8709_device *ite8709_dev;
475 ite8709_dev = pnp_get_drvdata(dev);
476
477 ite8709_cleanup(ite8709_dev, 6, 0, NULL);
478
479 printk(KERN_INFO LIRC_DRIVER_NAME ": device removed\n");
480}
481
482#ifdef CONFIG_PM
483static int ite8709_pnp_suspend(struct pnp_dev *dev, pm_message_t state)
484{
485 struct ite8709_device *ite8709_dev;
486 ite8709_dev = pnp_get_drvdata(dev);
487
488 if (ite8709_dev->use_count > 0)
489 ite8709_drop_hardware(ite8709_dev);
490
491 return 0;
492}
493
494static int ite8709_pnp_resume(struct pnp_dev *dev)
495{
496 struct ite8709_device *ite8709_dev;
497 ite8709_dev = pnp_get_drvdata(dev);
498
499 if (ite8709_dev->use_count > 0)
500 ite8709_init_hardware(ite8709_dev);
501
502 return 0;
503}
504#else
505#define ite8709_pnp_suspend NULL
506#define ite8709_pnp_resume NULL
507#endif
508
509static const struct pnp_device_id pnp_dev_table[] = {
510 {"ITE8709", 0},
511 {}
512};
513
514MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
515
516static 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,
523};
524
525static int __init ite8709_init_module(void)
526{
527 return pnp_register_driver(&ite8709_pnp_driver);
528}
529module_init(ite8709_init_module);
530
531static void __exit ite8709_cleanup_module(void)
532{
533 pnp_unregister_driver(&ite8709_pnp_driver);
534}
535module_exit(ite8709_cleanup_module);
536
537MODULE_DESCRIPTION("LIRC driver for ITE8709 CIR port");
538MODULE_AUTHOR("Grégory Lardière");
539MODULE_LICENSE("GPL");
540
541module_param(debug, bool, S_IRUGO | S_IWUSR);
542MODULE_PARM_DESC(debug, "Enable debugging messages");