1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * driver for ENE KB3926 B/C/D/E/F CIR (pnp id: ENE0XXX)
5 * Copyright (C) 2010 Maxim Levitsky <maximlevitsky@gmail.com>
8 * Sami R. <maesesami@gmail.com> for lot of help in debugging and therefore
9 * bringing to life support for transmission & learning mode.
11 * Charlie Andrews <charliethepilot@googlemail.com> for lots of help in
12 * bringing up the support of new firmware buffer that is popular
15 * ENE for partial device documentation
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/pnp.h>
24 #include <linux/interrupt.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <media/rc-core.h>
30 static int sample_period
;
31 static bool learning_mode_force
;
35 static void ene_set_reg_addr(struct ene_device
*dev
, u16 reg
)
37 outb(reg
>> 8, dev
->hw_io
+ ENE_ADDR_HI
);
38 outb(reg
& 0xFF, dev
->hw_io
+ ENE_ADDR_LO
);
41 /* read a hardware register */
42 static u8
ene_read_reg(struct ene_device
*dev
, u16 reg
)
45 ene_set_reg_addr(dev
, reg
);
46 retval
= inb(dev
->hw_io
+ ENE_IO
);
47 dbg_regs("reg %04x == %02x", reg
, retval
);
51 /* write a hardware register */
52 static void ene_write_reg(struct ene_device
*dev
, u16 reg
, u8 value
)
54 dbg_regs("reg %04x <- %02x", reg
, value
);
55 ene_set_reg_addr(dev
, reg
);
56 outb(value
, dev
->hw_io
+ ENE_IO
);
59 /* Set bits in hardware register */
60 static void ene_set_reg_mask(struct ene_device
*dev
, u16 reg
, u8 mask
)
62 dbg_regs("reg %04x |= %02x", reg
, mask
);
63 ene_set_reg_addr(dev
, reg
);
64 outb(inb(dev
->hw_io
+ ENE_IO
) | mask
, dev
->hw_io
+ ENE_IO
);
67 /* Clear bits in hardware register */
68 static void ene_clear_reg_mask(struct ene_device
*dev
, u16 reg
, u8 mask
)
70 dbg_regs("reg %04x &= ~%02x ", reg
, mask
);
71 ene_set_reg_addr(dev
, reg
);
72 outb(inb(dev
->hw_io
+ ENE_IO
) & ~mask
, dev
->hw_io
+ ENE_IO
);
75 /* A helper to set/clear a bit in register according to boolean variable */
76 static void ene_set_clear_reg_mask(struct ene_device
*dev
, u16 reg
, u8 mask
,
80 ene_set_reg_mask(dev
, reg
, mask
);
82 ene_clear_reg_mask(dev
, reg
, mask
);
85 /* detect hardware features */
86 static int ene_hw_detect(struct ene_device
*dev
)
88 u8 chip_major
, chip_minor
;
89 u8 hw_revision
, old_ver
;
92 ene_clear_reg_mask(dev
, ENE_ECSTS
, ENE_ECSTS_RSRVD
);
93 chip_major
= ene_read_reg(dev
, ENE_ECVER_MAJOR
);
94 chip_minor
= ene_read_reg(dev
, ENE_ECVER_MINOR
);
95 ene_set_reg_mask(dev
, ENE_ECSTS
, ENE_ECSTS_RSRVD
);
97 hw_revision
= ene_read_reg(dev
, ENE_ECHV
);
98 old_ver
= ene_read_reg(dev
, ENE_HW_VER_OLD
);
100 dev
->pll_freq
= (ene_read_reg(dev
, ENE_PLLFRH
) << 4) +
101 (ene_read_reg(dev
, ENE_PLLFRL
) >> 4);
103 if (sample_period
!= ENE_DEFAULT_SAMPLE_PERIOD
)
104 dev
->rx_period_adjust
=
105 dev
->pll_freq
== ENE_DEFAULT_PLL_FREQ
? 2 : 4;
107 if (hw_revision
== 0xFF) {
108 pr_warn("device seems to be disabled\n");
109 pr_warn("send a mail to lirc-list@lists.sourceforge.net\n");
110 pr_warn("please attach output of acpidump and dmidecode\n");
114 pr_notice("chip is 0x%02x%02x - kbver = 0x%02x, rev = 0x%02x\n",
115 chip_major
, chip_minor
, old_ver
, hw_revision
);
117 pr_notice("PLL freq = %d\n", dev
->pll_freq
);
119 if (chip_major
== 0x33) {
120 pr_warn("chips 0x33xx aren't supported\n");
124 if (chip_major
== 0x39 && chip_minor
== 0x26 && hw_revision
== 0xC0) {
125 dev
->hw_revision
= ENE_HW_C
;
126 pr_notice("KB3926C detected\n");
127 } else if (old_ver
== 0x24 && hw_revision
== 0xC0) {
128 dev
->hw_revision
= ENE_HW_B
;
129 pr_notice("KB3926B detected\n");
131 dev
->hw_revision
= ENE_HW_D
;
132 pr_notice("KB3926D or higher detected\n");
135 /* detect features hardware supports */
136 if (dev
->hw_revision
< ENE_HW_C
)
139 fw_reg1
= ene_read_reg(dev
, ENE_FW1
);
140 fw_reg2
= ene_read_reg(dev
, ENE_FW2
);
142 pr_notice("Firmware regs: %02x %02x\n", fw_reg1
, fw_reg2
);
144 dev
->hw_use_gpio_0a
= !!(fw_reg2
& ENE_FW2_GP0A
);
145 dev
->hw_learning_and_tx_capable
= !!(fw_reg2
& ENE_FW2_LEARNING
);
146 dev
->hw_extra_buffer
= !!(fw_reg1
& ENE_FW1_HAS_EXTRA_BUF
);
148 if (dev
->hw_learning_and_tx_capable
)
149 dev
->hw_fan_input
= !!(fw_reg2
& ENE_FW2_FAN_INPUT
);
151 pr_notice("Hardware features:\n");
153 if (dev
->hw_learning_and_tx_capable
) {
154 pr_notice("* Supports transmitting & learning mode\n");
155 pr_notice(" This feature is rare and therefore,\n");
156 pr_notice(" you are welcome to test it,\n");
157 pr_notice(" and/or contact the author via:\n");
158 pr_notice(" lirc-list@lists.sourceforge.net\n");
159 pr_notice(" or maximlevitsky@gmail.com\n");
161 pr_notice("* Uses GPIO %s for IR raw input\n",
162 dev
->hw_use_gpio_0a
? "40" : "0A");
164 if (dev
->hw_fan_input
)
165 pr_notice("* Uses unused fan feedback input as source of demodulated IR data\n");
168 if (!dev
->hw_fan_input
)
169 pr_notice("* Uses GPIO %s for IR demodulated input\n",
170 dev
->hw_use_gpio_0a
? "0A" : "40");
172 if (dev
->hw_extra_buffer
)
173 pr_notice("* Uses new style input buffer\n");
177 /* Read properties of hw sample buffer */
178 static void ene_rx_setup_hw_buffer(struct ene_device
*dev
)
182 ene_rx_read_hw_pointer(dev
);
183 dev
->r_pointer
= dev
->w_pointer
;
185 if (!dev
->hw_extra_buffer
) {
186 dev
->buffer_len
= ENE_FW_PACKET_SIZE
* 2;
190 tmp
= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
);
191 tmp
|= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+1) << 8;
192 dev
->extra_buf1_address
= tmp
;
194 dev
->extra_buf1_len
= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 2);
196 tmp
= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 3);
197 tmp
|= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 4) << 8;
198 dev
->extra_buf2_address
= tmp
;
200 dev
->extra_buf2_len
= ene_read_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 5);
202 dev
->buffer_len
= dev
->extra_buf1_len
+ dev
->extra_buf2_len
+ 8;
204 pr_notice("Hardware uses 2 extended buffers:\n");
205 pr_notice(" 0x%04x - len : %d\n",
206 dev
->extra_buf1_address
, dev
->extra_buf1_len
);
207 pr_notice(" 0x%04x - len : %d\n",
208 dev
->extra_buf2_address
, dev
->extra_buf2_len
);
210 pr_notice("Total buffer len = %d\n", dev
->buffer_len
);
212 if (dev
->buffer_len
> 64 || dev
->buffer_len
< 16)
215 if (dev
->extra_buf1_address
> 0xFBFC ||
216 dev
->extra_buf1_address
< 0xEC00)
219 if (dev
->extra_buf2_address
> 0xFBFC ||
220 dev
->extra_buf2_address
< 0xEC00)
223 if (dev
->r_pointer
> dev
->buffer_len
)
226 ene_set_reg_mask(dev
, ENE_FW1
, ENE_FW1_EXTRA_BUF_HND
);
229 pr_warn("Error validating extra buffers, device probably won't work\n");
230 dev
->hw_extra_buffer
= false;
231 ene_clear_reg_mask(dev
, ENE_FW1
, ENE_FW1_EXTRA_BUF_HND
);
235 /* Restore the pointers to extra buffers - to make module reload work*/
236 static void ene_rx_restore_hw_buffer(struct ene_device
*dev
)
238 if (!dev
->hw_extra_buffer
)
241 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 0,
242 dev
->extra_buf1_address
& 0xFF);
243 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 1,
244 dev
->extra_buf1_address
>> 8);
245 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 2, dev
->extra_buf1_len
);
247 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 3,
248 dev
->extra_buf2_address
& 0xFF);
249 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 4,
250 dev
->extra_buf2_address
>> 8);
251 ene_write_reg(dev
, ENE_FW_SAMPLE_BUFFER
+ 5,
252 dev
->extra_buf2_len
);
253 ene_clear_reg_mask(dev
, ENE_FW1
, ENE_FW1_EXTRA_BUF_HND
);
256 /* Read hardware write pointer */
257 static void ene_rx_read_hw_pointer(struct ene_device
*dev
)
259 if (dev
->hw_extra_buffer
)
260 dev
->w_pointer
= ene_read_reg(dev
, ENE_FW_RX_POINTER
);
262 dev
->w_pointer
= ene_read_reg(dev
, ENE_FW2
)
263 & ENE_FW2_BUF_WPTR
? 0 : ENE_FW_PACKET_SIZE
;
265 dbg_verbose("RB: HW write pointer: %02x, driver read pointer: %02x",
266 dev
->w_pointer
, dev
->r_pointer
);
269 /* Gets address of next sample from HW ring buffer */
270 static int ene_rx_get_sample_reg(struct ene_device
*dev
)
274 if (dev
->r_pointer
== dev
->w_pointer
) {
275 dbg_verbose("RB: hit end, try update w_pointer");
276 ene_rx_read_hw_pointer(dev
);
279 if (dev
->r_pointer
== dev
->w_pointer
) {
280 dbg_verbose("RB: end of data at %d", dev
->r_pointer
);
284 dbg_verbose("RB: reading at offset %d", dev
->r_pointer
);
285 r_pointer
= dev
->r_pointer
;
288 if (dev
->r_pointer
== dev
->buffer_len
)
291 dbg_verbose("RB: next read will be from offset %d", dev
->r_pointer
);
294 dbg_verbose("RB: read at main buffer at %d", r_pointer
);
295 return ENE_FW_SAMPLE_BUFFER
+ r_pointer
;
300 if (r_pointer
< dev
->extra_buf1_len
) {
301 dbg_verbose("RB: read at 1st extra buffer at %d", r_pointer
);
302 return dev
->extra_buf1_address
+ r_pointer
;
305 r_pointer
-= dev
->extra_buf1_len
;
307 if (r_pointer
< dev
->extra_buf2_len
) {
308 dbg_verbose("RB: read at 2nd extra buffer at %d", r_pointer
);
309 return dev
->extra_buf2_address
+ r_pointer
;
312 dbg("attempt to read beyond ring buffer end");
316 /* Sense current received carrier */
317 static void ene_rx_sense_carrier(struct ene_device
*dev
)
319 int carrier
, duty_cycle
;
320 int period
= ene_read_reg(dev
, ENE_CIRCAR_PRD
);
321 int hperiod
= ene_read_reg(dev
, ENE_CIRCAR_HPRD
);
323 if (!(period
& ENE_CIRCAR_PRD_VALID
))
326 period
&= ~ENE_CIRCAR_PRD_VALID
;
331 dbg("RX: hardware carrier period = %02x", period
);
332 dbg("RX: hardware carrier pulse period = %02x", hperiod
);
334 carrier
= 2000000 / period
;
335 duty_cycle
= (hperiod
* 100) / period
;
336 dbg("RX: sensed carrier = %d Hz, duty cycle %d%%",
337 carrier
, duty_cycle
);
338 if (dev
->carrier_detect_enabled
) {
339 struct ir_raw_event ev
= {
340 .carrier_report
= true,
342 .duty_cycle
= duty_cycle
344 ir_raw_event_store(dev
->rdev
, &ev
);
348 /* this enables/disables the CIR RX engine */
349 static void ene_rx_enable_cir_engine(struct ene_device
*dev
, bool enable
)
351 ene_set_clear_reg_mask(dev
, ENE_CIRCFG
,
352 ENE_CIRCFG_RX_EN
| ENE_CIRCFG_RX_IRQ
, enable
);
355 /* this selects input for CIR engine. Ether GPIO 0A or GPIO40*/
356 static void ene_rx_select_input(struct ene_device
*dev
, bool gpio_0a
)
358 ene_set_clear_reg_mask(dev
, ENE_CIRCFG2
, ENE_CIRCFG2_GPIO0A
, gpio_0a
);
362 * this enables alternative input via fan tachometer sensor and bypasses
365 static void ene_rx_enable_fan_input(struct ene_device
*dev
, bool enable
)
367 if (!dev
->hw_fan_input
)
371 ene_write_reg(dev
, ENE_FAN_AS_IN1
, 0);
373 ene_write_reg(dev
, ENE_FAN_AS_IN1
, ENE_FAN_AS_IN1_EN
);
374 ene_write_reg(dev
, ENE_FAN_AS_IN2
, ENE_FAN_AS_IN2_EN
);
378 /* setup the receiver for RX*/
379 static void ene_rx_setup(struct ene_device
*dev
)
381 bool learning_mode
= dev
->learning_mode_enabled
||
382 dev
->carrier_detect_enabled
;
383 int sample_period_adjust
= 0;
385 dbg("RX: setup receiver, learning mode = %d", learning_mode
);
388 /* This selects RLC input and clears CFG2 settings */
389 ene_write_reg(dev
, ENE_CIRCFG2
, 0x00);
391 /* set sample period*/
392 if (sample_period
== ENE_DEFAULT_SAMPLE_PERIOD
)
393 sample_period_adjust
=
394 dev
->pll_freq
== ENE_DEFAULT_PLL_FREQ
? 1 : 2;
396 ene_write_reg(dev
, ENE_CIRRLC_CFG
,
397 (sample_period
+ sample_period_adjust
) |
398 ENE_CIRRLC_CFG_OVERFLOW
);
399 /* revB doesn't support inputs */
400 if (dev
->hw_revision
< ENE_HW_C
)
405 WARN_ON(!dev
->hw_learning_and_tx_capable
);
407 /* Enable the opposite of the normal input
408 That means that if GPIO40 is normally used, use GPIO0A
410 This input will carry non demodulated
411 signal, and we will tell the hw to demodulate it itself */
412 ene_rx_select_input(dev
, !dev
->hw_use_gpio_0a
);
413 dev
->rx_fan_input_inuse
= false;
415 /* Enable carrier demodulation */
416 ene_set_reg_mask(dev
, ENE_CIRCFG
, ENE_CIRCFG_CARR_DEMOD
);
418 /* Enable carrier detection */
419 ene_write_reg(dev
, ENE_CIRCAR_PULS
, 0x63);
420 ene_set_clear_reg_mask(dev
, ENE_CIRCFG2
, ENE_CIRCFG2_CARR_DETECT
,
421 dev
->carrier_detect_enabled
|| debug
);
423 if (dev
->hw_fan_input
)
424 dev
->rx_fan_input_inuse
= true;
426 ene_rx_select_input(dev
, dev
->hw_use_gpio_0a
);
428 /* Disable carrier detection & demodulation */
429 ene_clear_reg_mask(dev
, ENE_CIRCFG
, ENE_CIRCFG_CARR_DEMOD
);
430 ene_clear_reg_mask(dev
, ENE_CIRCFG2
, ENE_CIRCFG2_CARR_DETECT
);
434 if (dev
->rx_fan_input_inuse
) {
435 dev
->rdev
->rx_resolution
= ENE_FW_SAMPLE_PERIOD_FAN
;
437 /* Fan input doesn't support timeouts, it just ends the
438 input with a maximum sample */
439 dev
->rdev
->min_timeout
= dev
->rdev
->max_timeout
=
440 ENE_FW_SMPL_BUF_FAN_MSK
*
441 ENE_FW_SAMPLE_PERIOD_FAN
;
443 dev
->rdev
->rx_resolution
= sample_period
;
445 /* Theoreticly timeout is unlimited, but we cap it
446 * because it was seen that on one device, it
447 * would stop sending spaces after around 250 msec.
448 * Besides, this is close to 2^32 anyway and timeout is u32.
450 dev
->rdev
->min_timeout
= 127 * sample_period
;
451 dev
->rdev
->max_timeout
= 200000;
454 if (dev
->rdev
->timeout
> dev
->rdev
->max_timeout
)
455 dev
->rdev
->timeout
= dev
->rdev
->max_timeout
;
456 if (dev
->rdev
->timeout
< dev
->rdev
->min_timeout
)
457 dev
->rdev
->timeout
= dev
->rdev
->min_timeout
;
460 /* Enable the device for receive */
461 static void ene_rx_enable_hw(struct ene_device
*dev
)
465 /* Enable system interrupt */
466 if (dev
->hw_revision
< ENE_HW_C
) {
467 ene_write_reg(dev
, ENEB_IRQ
, dev
->irq
<< 1);
468 ene_write_reg(dev
, ENEB_IRQ_UNK1
, 0x01);
470 reg_value
= ene_read_reg(dev
, ENE_IRQ
) & 0xF0;
471 reg_value
|= ENE_IRQ_UNK_EN
;
472 reg_value
&= ~ENE_IRQ_STATUS
;
473 reg_value
|= (dev
->irq
& ENE_IRQ_MASK
);
474 ene_write_reg(dev
, ENE_IRQ
, reg_value
);
478 ene_rx_enable_fan_input(dev
, dev
->rx_fan_input_inuse
);
479 ene_rx_enable_cir_engine(dev
, !dev
->rx_fan_input_inuse
);
481 /* ack any pending irqs - just in case */
484 /* enable firmware bits */
485 ene_set_reg_mask(dev
, ENE_FW1
, ENE_FW1_ENABLE
| ENE_FW1_IRQ
);
487 /* enter idle mode */
488 ir_raw_event_set_idle(dev
->rdev
, true);
491 /* Enable the device for receive - wrapper to track the state*/
492 static void ene_rx_enable(struct ene_device
*dev
)
494 ene_rx_enable_hw(dev
);
495 dev
->rx_enabled
= true;
498 /* Disable the device receiver */
499 static void ene_rx_disable_hw(struct ene_device
*dev
)
502 ene_rx_enable_cir_engine(dev
, false);
503 ene_rx_enable_fan_input(dev
, false);
505 /* disable hardware IRQ and firmware flag */
506 ene_clear_reg_mask(dev
, ENE_FW1
, ENE_FW1_ENABLE
| ENE_FW1_IRQ
);
507 ir_raw_event_set_idle(dev
->rdev
, true);
510 /* Disable the device receiver - wrapper to track the state */
511 static void ene_rx_disable(struct ene_device
*dev
)
513 ene_rx_disable_hw(dev
);
514 dev
->rx_enabled
= false;
517 /* This resets the receiver. Useful to stop stream of spaces at end of
520 static void ene_rx_reset(struct ene_device
*dev
)
522 ene_clear_reg_mask(dev
, ENE_CIRCFG
, ENE_CIRCFG_RX_EN
);
523 ene_set_reg_mask(dev
, ENE_CIRCFG
, ENE_CIRCFG_RX_EN
);
526 /* Set up the TX carrier frequency and duty cycle */
527 static void ene_tx_set_carrier(struct ene_device
*dev
)
532 spin_lock_irqsave(&dev
->hw_lock
, flags
);
534 ene_set_clear_reg_mask(dev
, ENE_CIRCFG
,
535 ENE_CIRCFG_TX_CARR
, dev
->tx_period
> 0);
540 BUG_ON(dev
->tx_duty_cycle
>= 100 || dev
->tx_duty_cycle
<= 0);
542 tx_puls_width
= dev
->tx_period
/ (100 / dev
->tx_duty_cycle
);
547 dbg("TX: pulse distance = %d * 500 ns", dev
->tx_period
);
548 dbg("TX: pulse width = %d * 500 ns", tx_puls_width
);
550 ene_write_reg(dev
, ENE_CIRMOD_PRD
, dev
->tx_period
| ENE_CIRMOD_PRD_POL
);
551 ene_write_reg(dev
, ENE_CIRMOD_HPRD
, tx_puls_width
);
553 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
556 /* Enable/disable transmitters */
557 static void ene_tx_set_transmitters(struct ene_device
*dev
)
561 spin_lock_irqsave(&dev
->hw_lock
, flags
);
562 ene_set_clear_reg_mask(dev
, ENE_GPIOFS8
, ENE_GPIOFS8_GPIO41
,
563 !!(dev
->transmitter_mask
& 0x01));
564 ene_set_clear_reg_mask(dev
, ENE_GPIOFS1
, ENE_GPIOFS1_GPIO0D
,
565 !!(dev
->transmitter_mask
& 0x02));
566 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
569 /* prepare transmission */
570 static void ene_tx_enable(struct ene_device
*dev
)
572 u8 conf1
= ene_read_reg(dev
, ENE_CIRCFG
);
573 u8 fwreg2
= ene_read_reg(dev
, ENE_FW2
);
575 dev
->saved_conf1
= conf1
;
577 /* Show information about currently connected transmitter jacks */
578 if (fwreg2
& ENE_FW2_EMMITER1_CONN
)
579 dbg("TX: Transmitter #1 is connected");
581 if (fwreg2
& ENE_FW2_EMMITER2_CONN
)
582 dbg("TX: Transmitter #2 is connected");
584 if (!(fwreg2
& (ENE_FW2_EMMITER1_CONN
| ENE_FW2_EMMITER2_CONN
)))
585 pr_warn("TX: transmitter cable isn't connected!\n");
587 /* disable receive on revc */
588 if (dev
->hw_revision
== ENE_HW_C
)
589 conf1
&= ~ENE_CIRCFG_RX_EN
;
591 /* Enable TX engine */
592 conf1
|= ENE_CIRCFG_TX_EN
| ENE_CIRCFG_TX_IRQ
;
593 ene_write_reg(dev
, ENE_CIRCFG
, conf1
);
596 /* end transmission */
597 static void ene_tx_disable(struct ene_device
*dev
)
599 ene_write_reg(dev
, ENE_CIRCFG
, dev
->saved_conf1
);
600 dev
->tx_buffer
= NULL
;
604 /* TX one sample - must be called with dev->hw_lock*/
605 static void ene_tx_sample(struct ene_device
*dev
)
609 bool pulse
= dev
->tx_sample_pulse
;
611 if (!dev
->tx_buffer
) {
612 pr_warn("TX: BUG: attempt to transmit NULL buffer\n");
616 /* Grab next TX sample */
617 if (!dev
->tx_sample
) {
619 if (dev
->tx_pos
== dev
->tx_len
) {
621 dbg("TX: no more data to send");
625 dbg("TX: last sample sent by hardware");
627 complete(&dev
->tx_complete
);
632 sample
= dev
->tx_buffer
[dev
->tx_pos
++];
633 dev
->tx_sample_pulse
= !dev
->tx_sample_pulse
;
635 dev
->tx_sample
= DIV_ROUND_CLOSEST(sample
, sample_period
);
641 raw_tx
= min(dev
->tx_sample
, (unsigned int)ENE_CIRRLC_OUT_MASK
);
642 dev
->tx_sample
-= raw_tx
;
644 dbg("TX: sample %8d (%s)", raw_tx
* sample_period
,
645 pulse
? "pulse" : "space");
647 raw_tx
|= ENE_CIRRLC_OUT_PULSE
;
650 dev
->tx_reg
? ENE_CIRRLC_OUT1
: ENE_CIRRLC_OUT0
, raw_tx
);
652 dev
->tx_reg
= !dev
->tx_reg
;
654 /* simulate TX done interrupt */
656 mod_timer(&dev
->tx_sim_timer
, jiffies
+ HZ
/ 500);
659 /* timer to simulate tx done interrupt */
660 static void ene_tx_irqsim(struct timer_list
*t
)
662 struct ene_device
*dev
= from_timer(dev
, t
, tx_sim_timer
);
665 spin_lock_irqsave(&dev
->hw_lock
, flags
);
667 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
671 /* read irq status and ack it */
672 static int ene_irq_status(struct ene_device
*dev
)
675 u8 fw_flags1
, fw_flags2
;
678 fw_flags2
= ene_read_reg(dev
, ENE_FW2
);
680 if (dev
->hw_revision
< ENE_HW_C
) {
681 irq_status
= ene_read_reg(dev
, ENEB_IRQ_STATUS
);
683 if (!(irq_status
& ENEB_IRQ_STATUS_IR
))
686 ene_clear_reg_mask(dev
, ENEB_IRQ_STATUS
, ENEB_IRQ_STATUS_IR
);
690 irq_status
= ene_read_reg(dev
, ENE_IRQ
);
691 if (!(irq_status
& ENE_IRQ_STATUS
))
694 /* original driver does that twice - a workaround ? */
695 ene_write_reg(dev
, ENE_IRQ
, irq_status
& ~ENE_IRQ_STATUS
);
696 ene_write_reg(dev
, ENE_IRQ
, irq_status
& ~ENE_IRQ_STATUS
);
698 /* check RX interrupt */
699 if (fw_flags2
& ENE_FW2_RXIRQ
) {
700 retval
|= ENE_IRQ_RX
;
701 ene_write_reg(dev
, ENE_FW2
, fw_flags2
& ~ENE_FW2_RXIRQ
);
704 /* check TX interrupt */
705 fw_flags1
= ene_read_reg(dev
, ENE_FW1
);
706 if (fw_flags1
& ENE_FW1_TXIRQ
) {
707 ene_write_reg(dev
, ENE_FW1
, fw_flags1
& ~ENE_FW1_TXIRQ
);
708 retval
|= ENE_IRQ_TX
;
714 /* interrupt handler */
715 static irqreturn_t
ene_isr(int irq
, void *data
)
718 int hw_sample
, irq_status
;
721 irqreturn_t retval
= IRQ_NONE
;
722 struct ene_device
*dev
= (struct ene_device
*)data
;
723 struct ir_raw_event ev
= {};
725 spin_lock_irqsave(&dev
->hw_lock
, flags
);
727 dbg_verbose("ISR called");
728 ene_rx_read_hw_pointer(dev
);
729 irq_status
= ene_irq_status(dev
);
734 retval
= IRQ_HANDLED
;
736 if (irq_status
& ENE_IRQ_TX
) {
737 dbg_verbose("TX interrupt");
738 if (!dev
->hw_learning_and_tx_capable
) {
739 dbg("TX interrupt on unsupported device!");
745 if (!(irq_status
& ENE_IRQ_RX
))
748 dbg_verbose("RX interrupt");
750 if (dev
->hw_learning_and_tx_capable
)
751 ene_rx_sense_carrier(dev
);
753 /* On hardware that don't support extra buffer we need to trust
754 the interrupt and not track the read pointer */
755 if (!dev
->hw_extra_buffer
)
756 dev
->r_pointer
= dev
->w_pointer
== 0 ? ENE_FW_PACKET_SIZE
: 0;
760 reg
= ene_rx_get_sample_reg(dev
);
762 dbg_verbose("next sample to read at: %04x", reg
);
766 hw_value
= ene_read_reg(dev
, reg
);
768 if (dev
->rx_fan_input_inuse
) {
770 int offset
= ENE_FW_SMPL_BUF_FAN
- ENE_FW_SAMPLE_BUFFER
;
772 /* read high part of the sample */
773 hw_value
|= ene_read_reg(dev
, reg
+ offset
) << 8;
774 pulse
= hw_value
& ENE_FW_SMPL_BUF_FAN_PLS
;
776 /* clear space bit, and other unused bits */
777 hw_value
&= ENE_FW_SMPL_BUF_FAN_MSK
;
778 hw_sample
= hw_value
* ENE_FW_SAMPLE_PERIOD_FAN
;
781 pulse
= !(hw_value
& ENE_FW_SAMPLE_SPACE
);
782 hw_value
&= ~ENE_FW_SAMPLE_SPACE
;
783 hw_sample
= hw_value
* sample_period
;
785 if (dev
->rx_period_adjust
) {
787 hw_sample
/= (100 + dev
->rx_period_adjust
);
791 if (!dev
->hw_extra_buffer
&& !hw_sample
) {
792 dev
->r_pointer
= dev
->w_pointer
;
796 dbg("RX: %d (%s)", hw_sample
, pulse
? "pulse" : "space");
798 ev
.duration
= hw_sample
;
800 ir_raw_event_store_with_filter(dev
->rdev
, &ev
);
803 ir_raw_event_handle(dev
->rdev
);
805 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
809 /* Initialize default settings */
810 static void ene_setup_default_settings(struct ene_device
*dev
)
813 dev
->tx_duty_cycle
= 50; /*%*/
814 dev
->transmitter_mask
= 0x03;
815 dev
->learning_mode_enabled
= learning_mode_force
;
817 /* Set reasonable default timeout */
818 dev
->rdev
->timeout
= MS_TO_US(150);
821 /* Upload all hardware settings at once. Used at load and resume time */
822 static void ene_setup_hw_settings(struct ene_device
*dev
)
824 if (dev
->hw_learning_and_tx_capable
) {
825 ene_tx_set_carrier(dev
);
826 ene_tx_set_transmitters(dev
);
832 /* outside interface: called on first open*/
833 static int ene_open(struct rc_dev
*rdev
)
835 struct ene_device
*dev
= rdev
->priv
;
838 spin_lock_irqsave(&dev
->hw_lock
, flags
);
840 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
844 /* outside interface: called on device close*/
845 static void ene_close(struct rc_dev
*rdev
)
847 struct ene_device
*dev
= rdev
->priv
;
849 spin_lock_irqsave(&dev
->hw_lock
, flags
);
852 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
855 /* outside interface: set transmitter mask */
856 static int ene_set_tx_mask(struct rc_dev
*rdev
, u32 tx_mask
)
858 struct ene_device
*dev
= rdev
->priv
;
859 dbg("TX: attempt to set transmitter mask %02x", tx_mask
);
862 if (!tx_mask
|| tx_mask
& ~0x03) {
863 dbg("TX: invalid mask");
864 /* return count of transmitters */
868 dev
->transmitter_mask
= tx_mask
;
869 ene_tx_set_transmitters(dev
);
873 /* outside interface : set tx carrier */
874 static int ene_set_tx_carrier(struct rc_dev
*rdev
, u32 carrier
)
876 struct ene_device
*dev
= rdev
->priv
;
879 dbg("TX: attempt to set tx carrier to %d kHz", carrier
);
883 period
= 2000000 / carrier
;
884 if (period
&& (period
> ENE_CIRMOD_PRD_MAX
||
885 period
< ENE_CIRMOD_PRD_MIN
)) {
887 dbg("TX: out of range %d-%d kHz carrier",
888 2000 / ENE_CIRMOD_PRD_MIN
, 2000 / ENE_CIRMOD_PRD_MAX
);
892 dev
->tx_period
= period
;
893 ene_tx_set_carrier(dev
);
897 /*outside interface : set tx duty cycle */
898 static int ene_set_tx_duty_cycle(struct rc_dev
*rdev
, u32 duty_cycle
)
900 struct ene_device
*dev
= rdev
->priv
;
901 dbg("TX: setting duty cycle to %d%%", duty_cycle
);
902 dev
->tx_duty_cycle
= duty_cycle
;
903 ene_tx_set_carrier(dev
);
907 /* outside interface: enable learning mode */
908 static int ene_set_learning_mode(struct rc_dev
*rdev
, int enable
)
910 struct ene_device
*dev
= rdev
->priv
;
912 if (enable
== dev
->learning_mode_enabled
)
915 spin_lock_irqsave(&dev
->hw_lock
, flags
);
916 dev
->learning_mode_enabled
= enable
;
920 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
924 static int ene_set_carrier_report(struct rc_dev
*rdev
, int enable
)
926 struct ene_device
*dev
= rdev
->priv
;
929 if (enable
== dev
->carrier_detect_enabled
)
932 spin_lock_irqsave(&dev
->hw_lock
, flags
);
933 dev
->carrier_detect_enabled
= enable
;
937 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
941 /* outside interface: enable or disable idle mode */
942 static void ene_set_idle(struct rc_dev
*rdev
, bool idle
)
944 struct ene_device
*dev
= rdev
->priv
;
948 dbg("RX: end of data");
952 /* outside interface: transmit */
953 static int ene_transmit(struct rc_dev
*rdev
, unsigned *buf
, unsigned n
)
955 struct ene_device
*dev
= rdev
->priv
;
958 dev
->tx_buffer
= buf
;
964 dev
->tx_sample_pulse
= false;
966 dbg("TX: %d samples", dev
->tx_len
);
968 spin_lock_irqsave(&dev
->hw_lock
, flags
);
972 /* Transmit first two samples */
976 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
978 if (wait_for_completion_timeout(&dev
->tx_complete
, 2 * HZ
) == 0) {
980 spin_lock_irqsave(&dev
->hw_lock
, flags
);
982 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
989 static int ene_probe(struct pnp_dev
*pnp_dev
, const struct pnp_device_id
*id
)
993 struct ene_device
*dev
;
995 /* allocate memory */
996 dev
= kzalloc(sizeof(struct ene_device
), GFP_KERNEL
);
997 rdev
= rc_allocate_device(RC_DRIVER_IR_RAW
);
999 goto exit_free_dev_rdev
;
1001 /* validate resources */
1004 /* init these to -1, as 0 is valid for both */
1008 if (!pnp_port_valid(pnp_dev
, 0) ||
1009 pnp_port_len(pnp_dev
, 0) < ENE_IO_SIZE
)
1010 goto exit_free_dev_rdev
;
1012 if (!pnp_irq_valid(pnp_dev
, 0))
1013 goto exit_free_dev_rdev
;
1015 spin_lock_init(&dev
->hw_lock
);
1017 dev
->hw_io
= pnp_port_start(pnp_dev
, 0);
1018 dev
->irq
= pnp_irq(pnp_dev
, 0);
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
);
1031 goto exit_free_dev_rdev
;
1033 if (!dev
->hw_learning_and_tx_capable
&& txsim
) {
1034 dev
->hw_learning_and_tx_capable
= true;
1035 timer_setup(&dev
->tx_sim_timer
, ene_tx_irqsim
, 0);
1036 pr_warn("Simulation of TX activated\n");
1039 if (!dev
->hw_learning_and_tx_capable
)
1040 learning_mode_force
= false;
1042 rdev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
1044 rdev
->open
= ene_open
;
1045 rdev
->close
= ene_close
;
1046 rdev
->s_idle
= ene_set_idle
;
1047 rdev
->driver_name
= ENE_DRIVER_NAME
;
1048 rdev
->map_name
= RC_MAP_RC6_MCE
;
1049 rdev
->device_name
= "ENE eHome Infrared Remote Receiver";
1051 if (dev
->hw_learning_and_tx_capable
) {
1052 rdev
->s_wideband_receiver
= ene_set_learning_mode
;
1053 init_completion(&dev
->tx_complete
);
1054 rdev
->tx_ir
= ene_transmit
;
1055 rdev
->s_tx_mask
= ene_set_tx_mask
;
1056 rdev
->s_tx_carrier
= ene_set_tx_carrier
;
1057 rdev
->s_tx_duty_cycle
= ene_set_tx_duty_cycle
;
1058 rdev
->s_carrier_report
= ene_set_carrier_report
;
1059 rdev
->device_name
= "ENE eHome Infrared Remote Transceiver";
1064 ene_rx_setup_hw_buffer(dev
);
1065 ene_setup_default_settings(dev
);
1066 ene_setup_hw_settings(dev
);
1068 device_set_wakeup_capable(&pnp_dev
->dev
, true);
1069 device_set_wakeup_enable(&pnp_dev
->dev
, true);
1071 error
= rc_register_device(rdev
);
1073 goto exit_free_dev_rdev
;
1075 /* claim the resources */
1077 if (!request_region(dev
->hw_io
, ENE_IO_SIZE
, ENE_DRIVER_NAME
)) {
1078 goto exit_unregister_device
;
1081 if (request_irq(dev
->irq
, ene_isr
,
1082 IRQF_SHARED
, ENE_DRIVER_NAME
, (void *)dev
)) {
1083 goto exit_release_hw_io
;
1086 pr_notice("driver has been successfully loaded\n");
1090 release_region(dev
->hw_io
, ENE_IO_SIZE
);
1091 exit_unregister_device
:
1092 rc_unregister_device(rdev
);
1095 rc_free_device(rdev
);
1100 /* main unload function */
1101 static void ene_remove(struct pnp_dev
*pnp_dev
)
1103 struct ene_device
*dev
= pnp_get_drvdata(pnp_dev
);
1104 unsigned long flags
;
1106 rc_unregister_device(dev
->rdev
);
1107 del_timer_sync(&dev
->tx_sim_timer
);
1108 spin_lock_irqsave(&dev
->hw_lock
, flags
);
1109 ene_rx_disable(dev
);
1110 ene_rx_restore_hw_buffer(dev
);
1111 spin_unlock_irqrestore(&dev
->hw_lock
, flags
);
1113 free_irq(dev
->irq
, dev
);
1114 release_region(dev
->hw_io
, ENE_IO_SIZE
);
1118 /* enable wake on IR (wakes on specific button on original remote) */
1119 static void ene_enable_wake(struct ene_device
*dev
, bool enable
)
1121 dbg("wake on IR %s", enable
? "enabled" : "disabled");
1122 ene_set_clear_reg_mask(dev
, ENE_FW1
, ENE_FW1_WAKE
, enable
);
1126 static int ene_suspend(struct pnp_dev
*pnp_dev
, pm_message_t state
)
1128 struct ene_device
*dev
= pnp_get_drvdata(pnp_dev
);
1129 bool wake
= device_may_wakeup(&dev
->pnp_dev
->dev
);
1131 if (!wake
&& dev
->rx_enabled
)
1132 ene_rx_disable_hw(dev
);
1134 ene_enable_wake(dev
, wake
);
1138 static int ene_resume(struct pnp_dev
*pnp_dev
)
1140 struct ene_device
*dev
= pnp_get_drvdata(pnp_dev
);
1141 ene_setup_hw_settings(dev
);
1143 if (dev
->rx_enabled
)
1146 ene_enable_wake(dev
, false);
1151 static void ene_shutdown(struct pnp_dev
*pnp_dev
)
1153 struct ene_device
*dev
= pnp_get_drvdata(pnp_dev
);
1154 ene_enable_wake(dev
, true);
1157 static const struct pnp_device_id ene_ids
[] = {
1165 static struct pnp_driver ene_driver
= {
1166 .name
= ENE_DRIVER_NAME
,
1167 .id_table
= ene_ids
,
1168 .flags
= PNP_DRIVER_RES_DO_NOT_CHANGE
,
1171 .remove
= ene_remove
,
1173 .suspend
= ene_suspend
,
1174 .resume
= ene_resume
,
1176 .shutdown
= ene_shutdown
,
1179 module_param(sample_period
, int, S_IRUGO
);
1180 MODULE_PARM_DESC(sample_period
, "Hardware sample period (50 us default)");
1182 module_param(learning_mode_force
, bool, S_IRUGO
);
1183 MODULE_PARM_DESC(learning_mode_force
, "Enable learning mode by default");
1185 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
1186 MODULE_PARM_DESC(debug
, "Debug level");
1188 module_param(txsim
, bool, S_IRUGO
);
1189 MODULE_PARM_DESC(txsim
,
1190 "Simulate TX features on unsupported hardware (dangerous)");
1192 MODULE_DEVICE_TABLE(pnp
, ene_ids
);
1194 ("Infrared input driver for KB3926B/C/D/E/F (aka ENE0100/ENE0200/ENE0201/ENE0202) CIR port");
1196 MODULE_AUTHOR("Maxim Levitsky");
1197 MODULE_LICENSE("GPL");
1199 module_pnp_driver(ene_driver
);