2 * driver for ENE KB3926 B/C/D/E/F CIR (pnp id: ENE0XXX)
4 * Copyright (C) 2010 Maxim Levitsky <maximlevitsky@gmail.com>
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 * Sami R. <maesesami@gmail.com> for lot of help in debugging and therefore
23 * bringing to life support for transmission & learning mode.
25 * Charlie Andrews <charliethepilot@googlemail.com> for lots of help in
26 * bringing up the support of new firmware buffer that is popular
29 * ENE for partial device documentation
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/pnp.h>
39 #include <linux/interrupt.h>
40 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <media/rc-core.h>
45 static int sample_period
;
46 static bool learning_mode_force
;
50 static void ene_set_reg_addr(struct ene_device
*dev
, u16 reg
)
52 outb(reg
>> 8, dev
->hw_io
+ ENE_ADDR_HI
);
53 outb(reg
& 0xFF, dev
->hw_io
+ ENE_ADDR_LO
);
56 /* read a hardware register */
57 static u8
ene_read_reg(struct ene_device
*dev
, u16 reg
)
60 ene_set_reg_addr(dev
, reg
);
61 retval
= inb(dev
->hw_io
+ ENE_IO
);
62 dbg_regs("reg %04x == %02x", reg
, retval
);
66 /* write a hardware register */
67 static void ene_write_reg(struct ene_device
*dev
, u16 reg
, u8 value
)
69 dbg_regs("reg %04x <- %02x", reg
, value
);
70 ene_set_reg_addr(dev
, reg
);
71 outb(value
, dev
->hw_io
+ ENE_IO
);
74 /* Set bits in hardware register */
75 static void ene_set_reg_mask(struct ene_device
*dev
, u16 reg
, u8 mask
)
77 dbg_regs("reg %04x |= %02x", reg
, mask
);
78 ene_set_reg_addr(dev
, reg
);
79 outb(inb(dev
->hw_io
+ ENE_IO
) | mask
, dev
->hw_io
+ ENE_IO
);
82 /* Clear bits in hardware register */
83 static void ene_clear_reg_mask(struct ene_device
*dev
, u16 reg
, u8 mask
)
85 dbg_regs("reg %04x &= ~%02x ", reg
, mask
);
86 ene_set_reg_addr(dev
, reg
);
87 outb(inb(dev
->hw_io
+ ENE_IO
) & ~mask
, dev
->hw_io
+ ENE_IO
);
90 /* A helper to set/clear a bit in register according to boolean variable */
91 static void ene_set_clear_reg_mask(struct ene_device
*dev
, u16 reg
, u8 mask
,
95 ene_set_reg_mask(dev
, reg
, mask
);
97 ene_clear_reg_mask(dev
, reg
, mask
);
100 /* detect hardware features */
101 static int ene_hw_detect(struct ene_device
*dev
)
103 u8 chip_major
, chip_minor
;
104 u8 hw_revision
, old_ver
;
107 ene_clear_reg_mask(dev
, ENE_ECSTS
, ENE_ECSTS_RSRVD
);
108 chip_major
= ene_read_reg(dev
, ENE_ECVER_MAJOR
);
109 chip_minor
= ene_read_reg(dev
, ENE_ECVER_MINOR
);
110 ene_set_reg_mask(dev
, ENE_ECSTS
, ENE_ECSTS_RSRVD
);
112 hw_revision
= ene_read_reg(dev
, ENE_ECHV
);
113 old_ver
= ene_read_reg(dev
, ENE_HW_VER_OLD
);
115 dev
->pll_freq
= (ene_read_reg(dev
, ENE_PLLFRH
) << 4) +
116 (ene_read_reg(dev
, ENE_PLLFRL
) >> 4);
118 if (sample_period
!= ENE_DEFAULT_SAMPLE_PERIOD
)
119 dev
->rx_period_adjust
=
120 dev
->pll_freq
== ENE_DEFAULT_PLL_FREQ
? 2 : 4;
122 if (hw_revision
== 0xFF) {
123 pr_warn("device seems to be disabled\n");
124 pr_warn("send a mail to lirc-list@lists.sourceforge.net\n");
125 pr_warn("please attach output of acpidump and dmidecode\n");
129 pr_notice("chip is 0x%02x%02x - kbver = 0x%02x, rev = 0x%02x\n",
130 chip_major
, chip_minor
, old_ver
, hw_revision
);
132 pr_notice("PLL freq = %d\n", dev
->pll_freq
);
134 if (chip_major
== 0x33) {
135 pr_warn("chips 0x33xx aren't supported\n");
139 if (chip_major
== 0x39 && chip_minor
== 0x26 && hw_revision
== 0xC0) {
140 dev
->hw_revision
= ENE_HW_C
;
141 pr_notice("KB3926C detected\n");
142 } else if (old_ver
== 0x24 && hw_revision
== 0xC0) {
143 dev
->hw_revision
= ENE_HW_B
;
144 pr_notice("KB3926B detected\n");
146 dev
->hw_revision
= ENE_HW_D
;
147 pr_notice("KB3926D or higher detected\n");
150 /* detect features hardware supports */
151 if (dev
->hw_revision
< ENE_HW_C
)
154 fw_reg1
= ene_read_reg(dev
, ENE_FW1
);
155 fw_reg2
= ene_read_reg(dev
, ENE_FW2
);
157 pr_notice("Firmware regs: %02x %02x\n", fw_reg1
, fw_reg2
);
159 dev
->hw_use_gpio_0a
= !!(fw_reg2
& ENE_FW2_GP0A
);
160 dev
->hw_learning_and_tx_capable
= !!(fw_reg2
& ENE_FW2_LEARNING
);
161 dev
->hw_extra_buffer
= !!(fw_reg1
& ENE_FW1_HAS_EXTRA_BUF
);
163 if (dev
->hw_learning_and_tx_capable
)
164 dev
->hw_fan_input
= !!(fw_reg2
& ENE_FW2_FAN_INPUT
);
166 pr_notice("Hardware features:\n");
168 if (dev
->hw_learning_and_tx_capable
) {
169 pr_notice("* Supports transmitting & learning mode\n");
170 pr_notice(" This feature is rare and therefore,\n");
171 pr_notice(" you are welcome to test it,\n");
172 pr_notice(" and/or contact the author via:\n");
173 pr_notice(" lirc-list@lists.sourceforge.net\n");
174 pr_notice(" or maximlevitsky@gmail.com\n");
176 pr_notice("* Uses GPIO %s for IR raw input\n",
177 dev
->hw_use_gpio_0a
? "40" : "0A");
179 if (dev
->hw_fan_input
)
180 pr_notice("* Uses unused fan feedback input as source of demodulated IR data\n");
183 if (!dev
->hw_fan_input
)
184 pr_notice("* Uses GPIO %s for IR demodulated input\n",
185 dev
->hw_use_gpio_0a
? "0A" : "40");
187 if (dev
->hw_extra_buffer
)
188 pr_notice("* Uses new style input buffer\n");
192 /* Read properities of hw sample buffer */
193 static void ene_rx_setup_hw_buffer(struct ene_device
*dev
)
197 ene_rx_read_hw_pointer(dev
);
198 dev
->r_pointer
= dev
->w_pointer
;
200 if (!dev
->hw_extra_buffer
) {
201 dev
->buffer_len
= ENE_FW_PACKET_SIZE
* 2;
205 tmp
= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
);
206 tmp
|= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+1) << 8;
207 dev
->extra_buf1_address
= tmp
;
209 dev
->extra_buf1_len
= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 2);
211 tmp
= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 3);
212 tmp
|= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 4) << 8;
213 dev
->extra_buf2_address
= tmp
;
215 dev
->extra_buf2_len
= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 5);
217 dev
->buffer_len
= dev
->extra_buf1_len
+ dev
->extra_buf2_len
+ 8;
219 pr_notice("Hardware uses 2 extended buffers:\n");
220 pr_notice(" 0x%04x - len : %d\n",
221 dev
->extra_buf1_address
, dev
->extra_buf1_len
);
222 pr_notice(" 0x%04x - len : %d\n",
223 dev
->extra_buf2_address
, dev
->extra_buf2_len
);
225 pr_notice("Total buffer len = %d\n", dev
->buffer_len
);
227 if (dev
->buffer_len
> 64 || dev
->buffer_len
< 16)
230 if (dev
->extra_buf1_address
> 0xFBFC ||
231 dev
->extra_buf1_address
< 0xEC00)
234 if (dev
->extra_buf2_address
> 0xFBFC ||
235 dev
->extra_buf2_address
< 0xEC00)
238 if (dev
->r_pointer
> dev
->buffer_len
)
241 ene_set_reg_mask(dev
, ENE_FW1
, ENE_FW1_EXTRA_BUF_HND
);
244 pr_warn("Error validating extra buffers, device probably won't work\n");
245 dev
->hw_extra_buffer
= false;
246 ene_clear_reg_mask(dev
, ENE_FW1
, ENE_FW1_EXTRA_BUF_HND
);
250 /* Restore the pointers to extra buffers - to make module reload work*/
251 static void ene_rx_restore_hw_buffer(struct ene_device
*dev
)
253 if (!dev
->hw_extra_buffer
)
256 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 0,
257 dev
->extra_buf1_address
& 0xFF);
258 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 1,
259 dev
->extra_buf1_address
>> 8);
260 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 2, dev
->extra_buf1_len
);
262 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 3,
263 dev
->extra_buf2_address
& 0xFF);
264 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 4,
265 dev
->extra_buf2_address
>> 8);
266 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 5,
267 dev
->extra_buf2_len
);
268 ene_clear_reg_mask(dev
, ENE_FW1
, ENE_FW1_EXTRA_BUF_HND
);
271 /* Read hardware write pointer */
272 static void ene_rx_read_hw_pointer(struct ene_device
*dev
)
274 if (dev
->hw_extra_buffer
)
275 dev
->w_pointer
= ene_read_reg(dev
, ENE_FW_RX_POINTER
);
277 dev
->w_pointer
= ene_read_reg(dev
, ENE_FW2
)
278 & ENE_FW2_BUF_WPTR
? 0 : ENE_FW_PACKET_SIZE
;
280 dbg_verbose("RB: HW write pointer: %02x, driver read pointer: %02x",
281 dev
->w_pointer
, dev
->r_pointer
);
284 /* Gets address of next sample from HW ring buffer */
285 static int ene_rx_get_sample_reg(struct ene_device
*dev
)
289 if (dev
->r_pointer
== dev
->w_pointer
) {
290 dbg_verbose("RB: hit end, try update w_pointer");
291 ene_rx_read_hw_pointer(dev
);
294 if (dev
->r_pointer
== dev
->w_pointer
) {
295 dbg_verbose("RB: end of data at %d", dev
->r_pointer
);
299 dbg_verbose("RB: reading at offset %d", dev
->r_pointer
);
300 r_pointer
= dev
->r_pointer
;
303 if (dev
->r_pointer
== dev
->buffer_len
)
306 dbg_verbose("RB: next read will be from offset %d", dev
->r_pointer
);
309 dbg_verbose("RB: read at main buffer at %d", r_pointer
);
310 return ENE_FW_SAMPLE_BUFFER
+ r_pointer
;
315 if (r_pointer
< dev
->extra_buf1_len
) {
316 dbg_verbose("RB: read at 1st extra buffer at %d", r_pointer
);
317 return dev
->extra_buf1_address
+ r_pointer
;
320 r_pointer
-= dev
->extra_buf1_len
;
322 if (r_pointer
< dev
->extra_buf2_len
) {
323 dbg_verbose("RB: read at 2nd extra buffer at %d", r_pointer
);
324 return dev
->extra_buf2_address
+ r_pointer
;
327 dbg("attempt to read beyond ring buffer end");
331 /* Sense current received carrier */
332 void ene_rx_sense_carrier(struct ene_device
*dev
)
334 DEFINE_IR_RAW_EVENT(ev
);
336 int carrier
, duty_cycle
;
337 int period
= ene_read_reg(dev
, ENE_CIRCAR_PRD
);
338 int hperiod
= ene_read_reg(dev
, ENE_CIRCAR_HPRD
);
340 if (!(period
& ENE_CIRCAR_PRD_VALID
))
343 period
&= ~ENE_CIRCAR_PRD_VALID
;
348 dbg("RX: hardware carrier period = %02x", period
);
349 dbg("RX: hardware carrier pulse period = %02x", hperiod
);
351 carrier
= 2000000 / period
;
352 duty_cycle
= (hperiod
* 100) / period
;
353 dbg("RX: sensed carrier = %d Hz, duty cycle %d%%",
354 carrier
, duty_cycle
);
355 if (dev
->carrier_detect_enabled
) {
356 ev
.carrier_report
= true;
357 ev
.carrier
= carrier
;
358 ev
.duty_cycle
= duty_cycle
;
359 ir_raw_event_store(dev
->rdev
, &ev
);
363 /* this enables/disables the CIR RX engine */
364 static void ene_rx_enable_cir_engine(struct ene_device
*dev
, bool enable
)
366 ene_set_clear_reg_mask(dev
, ENE_CIRCFG
,
367 ENE_CIRCFG_RX_EN
| ENE_CIRCFG_RX_IRQ
, enable
);
370 /* this selects input for CIR engine. Ether GPIO 0A or GPIO40*/
371 static void ene_rx_select_input(struct ene_device
*dev
, bool gpio_0a
)
373 ene_set_clear_reg_mask(dev
, ENE_CIRCFG2
, ENE_CIRCFG2_GPIO0A
, gpio_0a
);
377 * this enables alternative input via fan tachometer sensor and bypasses
380 static void ene_rx_enable_fan_input(struct ene_device
*dev
, bool enable
)
382 if (!dev
->hw_fan_input
)
386 ene_write_reg(dev
, ENE_FAN_AS_IN1
, 0);
388 ene_write_reg(dev
, ENE_FAN_AS_IN1
, ENE_FAN_AS_IN1_EN
);
389 ene_write_reg(dev
, ENE_FAN_AS_IN2
, ENE_FAN_AS_IN2_EN
);
393 /* setup the receiver for RX*/
394 static void ene_rx_setup(struct ene_device
*dev
)
396 bool learning_mode
= dev
->learning_mode_enabled
||
397 dev
->carrier_detect_enabled
;
398 int sample_period_adjust
= 0;
400 dbg("RX: setup receiver, learning mode = %d", learning_mode
);
403 /* This selects RLC input and clears CFG2 settings */
404 ene_write_reg(dev
, ENE_CIRCFG2
, 0x00);
406 /* set sample period*/
407 if (sample_period
== ENE_DEFAULT_SAMPLE_PERIOD
)
408 sample_period_adjust
=
409 dev
->pll_freq
== ENE_DEFAULT_PLL_FREQ
? 1 : 2;
411 ene_write_reg(dev
, ENE_CIRRLC_CFG
,
412 (sample_period
+ sample_period_adjust
) |
413 ENE_CIRRLC_CFG_OVERFLOW
);
414 /* revB doesn't support inputs */
415 if (dev
->hw_revision
< ENE_HW_C
)
420 WARN_ON(!dev
->hw_learning_and_tx_capable
);
422 /* Enable the opposite of the normal input
423 That means that if GPIO40 is normally used, use GPIO0A
425 This input will carry non demodulated
426 signal, and we will tell the hw to demodulate it itself */
427 ene_rx_select_input(dev
, !dev
->hw_use_gpio_0a
);
428 dev
->rx_fan_input_inuse
= false;
430 /* Enable carrier demodulation */
431 ene_set_reg_mask(dev
, ENE_CIRCFG
, ENE_CIRCFG_CARR_DEMOD
);
433 /* Enable carrier detection */
434 ene_write_reg(dev
, ENE_CIRCAR_PULS
, 0x63);
435 ene_set_clear_reg_mask(dev
, ENE_CIRCFG2
, ENE_CIRCFG2_CARR_DETECT
,
436 dev
->carrier_detect_enabled
|| debug
);
438 if (dev
->hw_fan_input
)
439 dev
->rx_fan_input_inuse
= true;
441 ene_rx_select_input(dev
, dev
->hw_use_gpio_0a
);
443 /* Disable carrier detection & demodulation */
444 ene_clear_reg_mask(dev
, ENE_CIRCFG
, ENE_CIRCFG_CARR_DEMOD
);
445 ene_clear_reg_mask(dev
, ENE_CIRCFG2
, ENE_CIRCFG2_CARR_DETECT
);
449 if (dev
->rx_fan_input_inuse
) {
450 dev
->rdev
->rx_resolution
= US_TO_NS(ENE_FW_SAMPLE_PERIOD_FAN
);
452 /* Fan input doesn't support timeouts, it just ends the
453 input with a maximum sample */
454 dev
->rdev
->min_timeout
= dev
->rdev
->max_timeout
=
455 US_TO_NS(ENE_FW_SMPL_BUF_FAN_MSK
*
456 ENE_FW_SAMPLE_PERIOD_FAN
);
458 dev
->rdev
->rx_resolution
= US_TO_NS(sample_period
);
460 /* Theoreticly timeout is unlimited, but we cap it
461 * because it was seen that on one device, it
462 * would stop sending spaces after around 250 msec.
463 * Besides, this is close to 2^32 anyway and timeout is u32.
465 dev
->rdev
->min_timeout
= US_TO_NS(127 * sample_period
);
466 dev
->rdev
->max_timeout
= US_TO_NS(200000);
469 if (dev
->hw_learning_and_tx_capable
)
470 dev
->rdev
->tx_resolution
= US_TO_NS(sample_period
);
472 if (dev
->rdev
->timeout
> dev
->rdev
->max_timeout
)
473 dev
->rdev
->timeout
= dev
->rdev
->max_timeout
;
474 if (dev
->rdev
->timeout
< dev
->rdev
->min_timeout
)
475 dev
->rdev
->timeout
= dev
->rdev
->min_timeout
;
478 /* Enable the device for receive */
479 static void ene_rx_enable(struct ene_device
*dev
)
483 /* Enable system interrupt */
484 if (dev
->hw_revision
< ENE_HW_C
) {
485 ene_write_reg(dev
, ENEB_IRQ
, dev
->irq
<< 1);
486 ene_write_reg(dev
, ENEB_IRQ_UNK1
, 0x01);
488 reg_value
= ene_read_reg(dev
, ENE_IRQ
) & 0xF0;
489 reg_value
|= ENE_IRQ_UNK_EN
;
490 reg_value
&= ~ENE_IRQ_STATUS
;
491 reg_value
|= (dev
->irq
& ENE_IRQ_MASK
);
492 ene_write_reg(dev
, ENE_IRQ
, reg_value
);
496 ene_rx_enable_fan_input(dev
, dev
->rx_fan_input_inuse
);
497 ene_rx_enable_cir_engine(dev
, !dev
->rx_fan_input_inuse
);
499 /* ack any pending irqs - just in case */
502 /* enable firmware bits */
503 ene_set_reg_mask(dev
, ENE_FW1
, ENE_FW1_ENABLE
| ENE_FW1_IRQ
);
505 /* enter idle mode */
506 ir_raw_event_set_idle(dev
->rdev
, true);
507 dev
->rx_enabled
= true;
510 /* Disable the device receiver */
511 static void ene_rx_disable(struct ene_device
*dev
)
514 ene_rx_enable_cir_engine(dev
, false);
515 ene_rx_enable_fan_input(dev
, false);
517 /* disable hardware IRQ and firmware flag */
518 ene_clear_reg_mask(dev
, ENE_FW1
, ENE_FW1_ENABLE
| ENE_FW1_IRQ
);
520 ir_raw_event_set_idle(dev
->rdev
, true);
521 dev
->rx_enabled
= false;
524 /* This resets the receiver. Useful to stop stream of spaces at end of
527 static void ene_rx_reset(struct ene_device
*dev
)
529 ene_clear_reg_mask(dev
, ENE_CIRCFG
, ENE_CIRCFG_RX_EN
);
530 ene_set_reg_mask(dev
, ENE_CIRCFG
, ENE_CIRCFG_RX_EN
);
533 /* Set up the TX carrier frequency and duty cycle */
534 static void ene_tx_set_carrier(struct ene_device
*dev
)
539 spin_lock_irqsave(&dev
->hw_lock
, flags
);
541 ene_set_clear_reg_mask(dev
, ENE_CIRCFG
,
542 ENE_CIRCFG_TX_CARR
, dev
->tx_period
> 0);
547 BUG_ON(dev
->tx_duty_cycle
>= 100 || dev
->tx_duty_cycle
<= 0);
549 tx_puls_width
= dev
->tx_period
/ (100 / dev
->tx_duty_cycle
);
554 dbg("TX: pulse distance = %d * 500 ns", dev
->tx_period
);
555 dbg("TX: pulse width = %d * 500 ns", tx_puls_width
);
557 ene_write_reg(dev
, ENE_CIRMOD_PRD
, dev
->tx_period
| ENE_CIRMOD_PRD_POL
);
558 ene_write_reg(dev
, ENE_CIRMOD_HPRD
, tx_puls_width
);
560 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
563 /* Enable/disable transmitters */
564 static void ene_tx_set_transmitters(struct ene_device
*dev
)
568 spin_lock_irqsave(&dev
->hw_lock
, flags
);
569 ene_set_clear_reg_mask(dev
, ENE_GPIOFS8
, ENE_GPIOFS8_GPIO41
,
570 !!(dev
->transmitter_mask
& 0x01));
571 ene_set_clear_reg_mask(dev
, ENE_GPIOFS1
, ENE_GPIOFS1_GPIO0D
,
572 !!(dev
->transmitter_mask
& 0x02));
573 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
576 /* prepare transmission */
577 static void ene_tx_enable(struct ene_device
*dev
)
579 u8 conf1
= ene_read_reg(dev
, ENE_CIRCFG
);
580 u8 fwreg2
= ene_read_reg(dev
, ENE_FW2
);
582 dev
->saved_conf1
= conf1
;
584 /* Show information about currently connected transmitter jacks */
585 if (fwreg2
& ENE_FW2_EMMITER1_CONN
)
586 dbg("TX: Transmitter #1 is connected");
588 if (fwreg2
& ENE_FW2_EMMITER2_CONN
)
589 dbg("TX: Transmitter #2 is connected");
591 if (!(fwreg2
& (ENE_FW2_EMMITER1_CONN
| ENE_FW2_EMMITER2_CONN
)))
592 pr_warn("TX: transmitter cable isn't connected!\n");
594 /* disable receive on revc */
595 if (dev
->hw_revision
== ENE_HW_C
)
596 conf1
&= ~ENE_CIRCFG_RX_EN
;
598 /* Enable TX engine */
599 conf1
|= ENE_CIRCFG_TX_EN
| ENE_CIRCFG_TX_IRQ
;
600 ene_write_reg(dev
, ENE_CIRCFG
, conf1
);
603 /* end transmission */
604 static void ene_tx_disable(struct ene_device
*dev
)
606 ene_write_reg(dev
, ENE_CIRCFG
, dev
->saved_conf1
);
607 dev
->tx_buffer
= NULL
;
611 /* TX one sample - must be called with dev->hw_lock*/
612 static void ene_tx_sample(struct ene_device
*dev
)
616 bool pulse
= dev
->tx_sample_pulse
;
618 if (!dev
->tx_buffer
) {
619 pr_warn("TX: BUG: attempt to transmit NULL buffer\n");
623 /* Grab next TX sample */
624 if (!dev
->tx_sample
) {
626 if (dev
->tx_pos
== dev
->tx_len
) {
628 dbg("TX: no more data to send");
632 dbg("TX: last sample sent by hardware");
634 complete(&dev
->tx_complete
);
639 sample
= dev
->tx_buffer
[dev
->tx_pos
++];
640 dev
->tx_sample_pulse
= !dev
->tx_sample_pulse
;
642 dev
->tx_sample
= DIV_ROUND_CLOSEST(sample
, sample_period
);
648 raw_tx
= min(dev
->tx_sample
, (unsigned int)ENE_CIRRLC_OUT_MASK
);
649 dev
->tx_sample
-= raw_tx
;
651 dbg("TX: sample %8d (%s)", raw_tx
* sample_period
,
652 pulse
? "pulse" : "space");
654 raw_tx
|= ENE_CIRRLC_OUT_PULSE
;
657 dev
->tx_reg
? ENE_CIRRLC_OUT1
: ENE_CIRRLC_OUT0
, raw_tx
);
659 dev
->tx_reg
= !dev
->tx_reg
;
661 /* simulate TX done interrupt */
663 mod_timer(&dev
->tx_sim_timer
, jiffies
+ HZ
/ 500);
666 /* timer to simulate tx done interrupt */
667 static void ene_tx_irqsim(unsigned long data
)
669 struct ene_device
*dev
= (struct ene_device
*)data
;
672 spin_lock_irqsave(&dev
->hw_lock
, flags
);
674 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
678 /* read irq status and ack it */
679 static int ene_irq_status(struct ene_device
*dev
)
682 u8 fw_flags1
, fw_flags2
;
685 fw_flags2
= ene_read_reg(dev
, ENE_FW2
);
687 if (dev
->hw_revision
< ENE_HW_C
) {
688 irq_status
= ene_read_reg(dev
, ENEB_IRQ_STATUS
);
690 if (!(irq_status
& ENEB_IRQ_STATUS_IR
))
693 ene_clear_reg_mask(dev
, ENEB_IRQ_STATUS
, ENEB_IRQ_STATUS_IR
);
697 irq_status
= ene_read_reg(dev
, ENE_IRQ
);
698 if (!(irq_status
& ENE_IRQ_STATUS
))
701 /* original driver does that twice - a workaround ? */
702 ene_write_reg(dev
, ENE_IRQ
, irq_status
& ~ENE_IRQ_STATUS
);
703 ene_write_reg(dev
, ENE_IRQ
, irq_status
& ~ENE_IRQ_STATUS
);
705 /* check RX interrupt */
706 if (fw_flags2
& ENE_FW2_RXIRQ
) {
707 retval
|= ENE_IRQ_RX
;
708 ene_write_reg(dev
, ENE_FW2
, fw_flags2
& ~ENE_FW2_RXIRQ
);
711 /* check TX interrupt */
712 fw_flags1
= ene_read_reg(dev
, ENE_FW1
);
713 if (fw_flags1
& ENE_FW1_TXIRQ
) {
714 ene_write_reg(dev
, ENE_FW1
, fw_flags1
& ~ENE_FW1_TXIRQ
);
715 retval
|= ENE_IRQ_TX
;
721 /* interrupt handler */
722 static irqreturn_t
ene_isr(int irq
, void *data
)
725 int hw_sample
, irq_status
;
728 irqreturn_t retval
= IRQ_NONE
;
729 struct ene_device
*dev
= (struct ene_device
*)data
;
730 DEFINE_IR_RAW_EVENT(ev
);
732 spin_lock_irqsave(&dev
->hw_lock
, flags
);
734 dbg_verbose("ISR called");
735 ene_rx_read_hw_pointer(dev
);
736 irq_status
= ene_irq_status(dev
);
741 retval
= IRQ_HANDLED
;
743 if (irq_status
& ENE_IRQ_TX
) {
744 dbg_verbose("TX interrupt");
745 if (!dev
->hw_learning_and_tx_capable
) {
746 dbg("TX interrupt on unsupported device!");
752 if (!(irq_status
& ENE_IRQ_RX
))
755 dbg_verbose("RX interrupt");
757 if (dev
->hw_learning_and_tx_capable
)
758 ene_rx_sense_carrier(dev
);
760 /* On hardware that don't support extra buffer we need to trust
761 the interrupt and not track the read pointer */
762 if (!dev
->hw_extra_buffer
)
763 dev
->r_pointer
= dev
->w_pointer
== 0 ? ENE_FW_PACKET_SIZE
: 0;
767 reg
= ene_rx_get_sample_reg(dev
);
769 dbg_verbose("next sample to read at: %04x", reg
);
773 hw_value
= ene_read_reg(dev
, reg
);
775 if (dev
->rx_fan_input_inuse
) {
777 int offset
= ENE_FW_SMPL_BUF_FAN
- ENE_FW_SAMPLE_BUFFER
;
779 /* read high part of the sample */
780 hw_value
|= ene_read_reg(dev
, reg
+ offset
) << 8;
781 pulse
= hw_value
& ENE_FW_SMPL_BUF_FAN_PLS
;
783 /* clear space bit, and other unused bits */
784 hw_value
&= ENE_FW_SMPL_BUF_FAN_MSK
;
785 hw_sample
= hw_value
* ENE_FW_SAMPLE_PERIOD_FAN
;
788 pulse
= !(hw_value
& ENE_FW_SAMPLE_SPACE
);
789 hw_value
&= ~ENE_FW_SAMPLE_SPACE
;
790 hw_sample
= hw_value
* sample_period
;
792 if (dev
->rx_period_adjust
) {
794 hw_sample
/= (100 + dev
->rx_period_adjust
);
798 if (!dev
->hw_extra_buffer
&& !hw_sample
) {
799 dev
->r_pointer
= dev
->w_pointer
;
803 dbg("RX: %d (%s)", hw_sample
, pulse
? "pulse" : "space");
805 ev
.duration
= US_TO_NS(hw_sample
);
807 ir_raw_event_store_with_filter(dev
->rdev
, &ev
);
810 ir_raw_event_handle(dev
->rdev
);
812 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
816 /* Initialize default settings */
817 static void ene_setup_default_settings(struct ene_device
*dev
)
820 dev
->tx_duty_cycle
= 50; /*%*/
821 dev
->transmitter_mask
= 0x03;
822 dev
->learning_mode_enabled
= learning_mode_force
;
824 /* Set reasonable default timeout */
825 dev
->rdev
->timeout
= US_TO_NS(150000);
828 /* Upload all hardware settings at once. Used at load and resume time */
829 static void ene_setup_hw_settings(struct ene_device
*dev
)
831 if (dev
->hw_learning_and_tx_capable
) {
832 ene_tx_set_carrier(dev
);
833 ene_tx_set_transmitters(dev
);
839 /* outside interface: called on first open*/
840 static int ene_open(struct rc_dev
*rdev
)
842 struct ene_device
*dev
= rdev
->priv
;
845 spin_lock_irqsave(&dev
->hw_lock
, flags
);
847 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
851 /* outside interface: called on device close*/
852 static void ene_close(struct rc_dev
*rdev
)
854 struct ene_device
*dev
= rdev
->priv
;
856 spin_lock_irqsave(&dev
->hw_lock
, flags
);
859 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
862 /* outside interface: set transmitter mask */
863 static int ene_set_tx_mask(struct rc_dev
*rdev
, u32 tx_mask
)
865 struct ene_device
*dev
= rdev
->priv
;
866 dbg("TX: attempt to set transmitter mask %02x", tx_mask
);
869 if (!tx_mask
|| tx_mask
& ~0x03) {
870 dbg("TX: invalid mask");
871 /* return count of transmitters */
875 dev
->transmitter_mask
= tx_mask
;
876 ene_tx_set_transmitters(dev
);
880 /* outside interface : set tx carrier */
881 static int ene_set_tx_carrier(struct rc_dev
*rdev
, u32 carrier
)
883 struct ene_device
*dev
= rdev
->priv
;
884 u32 period
= 2000000 / carrier
;
886 dbg("TX: attempt to set tx carrier to %d kHz", carrier
);
888 if (period
&& (period
> ENE_CIRMOD_PRD_MAX
||
889 period
< ENE_CIRMOD_PRD_MIN
)) {
891 dbg("TX: out of range %d-%d kHz carrier",
892 2000 / ENE_CIRMOD_PRD_MIN
, 2000 / ENE_CIRMOD_PRD_MAX
);
896 dev
->tx_period
= period
;
897 ene_tx_set_carrier(dev
);
901 /*outside interface : set tx duty cycle */
902 static int ene_set_tx_duty_cycle(struct rc_dev
*rdev
, u32 duty_cycle
)
904 struct ene_device
*dev
= rdev
->priv
;
905 dbg("TX: setting duty cycle to %d%%", duty_cycle
);
906 dev
->tx_duty_cycle
= duty_cycle
;
907 ene_tx_set_carrier(dev
);
911 /* outside interface: enable learning mode */
912 static int ene_set_learning_mode(struct rc_dev
*rdev
, int enable
)
914 struct ene_device
*dev
= rdev
->priv
;
916 if (enable
== dev
->learning_mode_enabled
)
919 spin_lock_irqsave(&dev
->hw_lock
, flags
);
920 dev
->learning_mode_enabled
= enable
;
924 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
928 static int ene_set_carrier_report(struct rc_dev
*rdev
, int enable
)
930 struct ene_device
*dev
= rdev
->priv
;
933 if (enable
== dev
->carrier_detect_enabled
)
936 spin_lock_irqsave(&dev
->hw_lock
, flags
);
937 dev
->carrier_detect_enabled
= enable
;
941 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
945 /* outside interface: enable or disable idle mode */
946 static void ene_set_idle(struct rc_dev
*rdev
, bool idle
)
948 struct ene_device
*dev
= rdev
->priv
;
952 dbg("RX: end of data");
956 /* outside interface: transmit */
957 static int ene_transmit(struct rc_dev
*rdev
, unsigned *buf
, unsigned n
)
959 struct ene_device
*dev
= rdev
->priv
;
962 dev
->tx_buffer
= buf
;
968 dev
->tx_sample_pulse
= 0;
970 dbg("TX: %d samples", dev
->tx_len
);
972 spin_lock_irqsave(&dev
->hw_lock
, flags
);
976 /* Transmit first two samples */
980 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
982 if (wait_for_completion_timeout(&dev
->tx_complete
, 2 * HZ
) == 0) {
984 spin_lock_irqsave(&dev
->hw_lock
, flags
);
986 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
993 static int ene_probe(struct pnp_dev
*pnp_dev
, const struct pnp_device_id
*id
)
997 struct ene_device
*dev
;
999 /* allocate memory */
1000 dev
= kzalloc(sizeof(struct ene_device
), GFP_KERNEL
);
1001 rdev
= rc_allocate_device();
1005 /* validate resources */
1008 /* init these to -1, as 0 is valid for both */
1012 if (!pnp_port_valid(pnp_dev
, 0) ||
1013 pnp_port_len(pnp_dev
, 0) < ENE_IO_SIZE
)
1016 if (!pnp_irq_valid(pnp_dev
, 0))
1019 spin_lock_init(&dev
->hw_lock
);
1021 pnp_set_drvdata(pnp_dev
, dev
);
1022 dev
->pnp_dev
= pnp_dev
;
1024 /* don't allow too short/long sample periods */
1025 if (sample_period
< 5 || sample_period
> 0x7F)
1026 sample_period
= ENE_DEFAULT_SAMPLE_PERIOD
;
1028 /* detect hardware version and features */
1029 error
= ene_hw_detect(dev
);
1033 if (!dev
->hw_learning_and_tx_capable
&& txsim
) {
1034 dev
->hw_learning_and_tx_capable
= true;
1035 setup_timer(&dev
->tx_sim_timer
, ene_tx_irqsim
,
1036 (long unsigned int)dev
);
1037 pr_warn("Simulation of TX activated\n");
1040 if (!dev
->hw_learning_and_tx_capable
)
1041 learning_mode_force
= false;
1043 rdev
->driver_type
= RC_DRIVER_IR_RAW
;
1044 rdev
->allowed_protos
= RC_TYPE_ALL
;
1046 rdev
->open
= ene_open
;
1047 rdev
->close
= ene_close
;
1048 rdev
->s_idle
= ene_set_idle
;
1049 rdev
->driver_name
= ENE_DRIVER_NAME
;
1050 rdev
->map_name
= RC_MAP_RC6_MCE
;
1051 rdev
->input_name
= "ENE eHome Infrared Remote Receiver";
1053 if (dev
->hw_learning_and_tx_capable
) {
1054 rdev
->s_learning_mode
= ene_set_learning_mode
;
1055 init_completion(&dev
->tx_complete
);
1056 rdev
->tx_ir
= ene_transmit
;
1057 rdev
->s_tx_mask
= ene_set_tx_mask
;
1058 rdev
->s_tx_carrier
= ene_set_tx_carrier
;
1059 rdev
->s_tx_duty_cycle
= ene_set_tx_duty_cycle
;
1060 rdev
->s_carrier_report
= ene_set_carrier_report
;
1061 rdev
->input_name
= "ENE eHome Infrared Remote Transceiver";
1066 ene_rx_setup_hw_buffer(dev
);
1067 ene_setup_default_settings(dev
);
1068 ene_setup_hw_settings(dev
);
1070 device_set_wakeup_capable(&pnp_dev
->dev
, true);
1071 device_set_wakeup_enable(&pnp_dev
->dev
, true);
1073 /* claim the resources */
1075 dev
->hw_io
= pnp_port_start(pnp_dev
, 0);
1076 if (!request_region(dev
->hw_io
, ENE_IO_SIZE
, ENE_DRIVER_NAME
)) {
1082 dev
->irq
= pnp_irq(pnp_dev
, 0);
1083 if (request_irq(dev
->irq
, ene_isr
,
1084 IRQF_SHARED
, ENE_DRIVER_NAME
, (void *)dev
)) {
1089 error
= rc_register_device(rdev
);
1093 pr_notice("driver has been successfully loaded\n");
1096 if (dev
&& dev
->irq
>= 0)
1097 free_irq(dev
->irq
, dev
);
1098 if (dev
&& dev
->hw_io
>= 0)
1099 release_region(dev
->hw_io
, ENE_IO_SIZE
);
1101 rc_free_device(rdev
);
1106 /* main unload function */
1107 static void ene_remove(struct pnp_dev
*pnp_dev
)
1109 struct ene_device
*dev
= pnp_get_drvdata(pnp_dev
);
1110 unsigned long flags
;
1112 spin_lock_irqsave(&dev
->hw_lock
, flags
);
1113 ene_rx_disable(dev
);
1114 ene_rx_restore_hw_buffer(dev
);
1115 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
1117 free_irq(dev
->irq
, dev
);
1118 release_region(dev
->hw_io
, ENE_IO_SIZE
);
1119 rc_unregister_device(dev
->rdev
);
1123 /* enable wake on IR (wakes on specific button on original remote) */
1124 static void ene_enable_wake(struct ene_device
*dev
, int enable
)
1126 enable
= enable
&& device_may_wakeup(&dev
->pnp_dev
->dev
);
1127 dbg("wake on IR %s", enable
? "enabled" : "disabled");
1128 ene_set_clear_reg_mask(dev
, ENE_FW1
, ENE_FW1_WAKE
, enable
);
1132 static int ene_suspend(struct pnp_dev
*pnp_dev
, pm_message_t state
)
1134 struct ene_device
*dev
= pnp_get_drvdata(pnp_dev
);
1135 ene_enable_wake(dev
, true);
1137 /* TODO: add support for wake pattern */
1141 static int ene_resume(struct pnp_dev
*pnp_dev
)
1143 struct ene_device
*dev
= pnp_get_drvdata(pnp_dev
);
1144 ene_setup_hw_settings(dev
);
1146 if (dev
->rx_enabled
)
1149 ene_enable_wake(dev
, false);
1154 static void ene_shutdown(struct pnp_dev
*pnp_dev
)
1156 struct ene_device
*dev
= pnp_get_drvdata(pnp_dev
);
1157 ene_enable_wake(dev
, true);
1160 static const struct pnp_device_id ene_ids
[] = {
1168 static struct pnp_driver ene_driver
= {
1169 .name
= ENE_DRIVER_NAME
,
1170 .id_table
= ene_ids
,
1171 .flags
= PNP_DRIVER_RES_DO_NOT_CHANGE
,
1174 .remove
= __devexit_p(ene_remove
),
1176 .suspend
= ene_suspend
,
1177 .resume
= ene_resume
,
1179 .shutdown
= ene_shutdown
,
1182 static int __init
ene_init(void)
1184 return pnp_register_driver(&ene_driver
);
1187 static void ene_exit(void)
1189 pnp_unregister_driver(&ene_driver
);
1192 module_param(sample_period
, int, S_IRUGO
);
1193 MODULE_PARM_DESC(sample_period
, "Hardware sample period (50 us default)");
1195 module_param(learning_mode_force
, bool, S_IRUGO
);
1196 MODULE_PARM_DESC(learning_mode_force
, "Enable learning mode by default");
1198 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
1199 MODULE_PARM_DESC(debug
, "Debug level");
1201 module_param(txsim
, bool, S_IRUGO
);
1202 MODULE_PARM_DESC(txsim
,
1203 "Simulate TX features on unsupported hardware (dangerous)");
1205 MODULE_DEVICE_TABLE(pnp
, ene_ids
);
1207 ("Infrared input driver for KB3926B/C/D/E/F "
1208 "(aka ENE0100/ENE0200/ENE0201/ENE0202) CIR port");
1210 MODULE_AUTHOR("Maxim Levitsky");
1211 MODULE_LICENSE("GPL");
1213 module_init(ene_init
);
1214 module_exit(ene_exit
);