33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/kernel.h>
36 #include <linux/module.h>
40 #include <linux/sched.h>
41 #include <linux/slab.h>
45 static int sample_period;
46 static bool learning_mode_force;
60 ene_set_reg_addr(dev, reg);
62 dbg_regs(
"reg %04x == %02x", reg, retval);
69 dbg_regs(
"reg %04x <- %02x", reg, value);
70 ene_set_reg_addr(dev, reg);
77 dbg_regs(
"reg %04x |= %02x", reg, mask);
78 ene_set_reg_addr(dev, reg);
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);
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);
101 static int ene_hw_detect(
struct ene_device *dev)
103 u8 chip_major, chip_minor;
104 u8 hw_revision, old_ver;
112 hw_revision = ene_read_reg(dev,
ENE_ECHV);
122 if (hw_revision == 0xFF) {
123 pr_warn(
"device seems to be disabled\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);
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) {
142 }
else if (old_ver == 0x24 && hw_revision == 0xC0) {
147 pr_notice(
"KB3926D or higher detected\n");
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);
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");
176 pr_notice(
"* Uses GPIO %s for IR raw input\n",
180 pr_notice(
"* Uses unused fan feedback input as source of demodulated IR data\n");
184 pr_notice(
"* Uses GPIO %s for IR demodulated input\n",
188 pr_notice(
"* Uses new style input buffer\n");
193 static void ene_rx_setup_hw_buffer(
struct ene_device *dev)
197 ene_rx_read_hw_pointer(dev);
219 pr_notice(
"Hardware uses 2 extended buffers:\n");
244 pr_warn(
"Error validating extra buffers, device probably won't work\n");
251 static void ene_rx_restore_hw_buffer(
struct ene_device *dev)
272 static void ene_rx_read_hw_pointer(
struct ene_device *dev)
280 dbg_verbose(
"RB: HW write pointer: %02x, driver read pointer: %02x",
285 static int ene_rx_get_sample_reg(
struct ene_device *dev)
291 ene_rx_read_hw_pointer(dev);
309 dbg_verbose(
"RB: read at main buffer at %d", r_pointer);
315 if (r_pointer < dev->extra_buf1_len) {
316 dbg_verbose(
"RB: read at 1st extra buffer at %d", r_pointer);
322 if (r_pointer < dev->extra_buf2_len) {
323 dbg_verbose(
"RB: read at 2nd extra buffer at %d", r_pointer);
327 dbg(
"attempt to read beyond ring buffer end");
336 int carrier, duty_cycle;
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);
356 ev.carrier_report =
true;
357 ev.carrier = carrier;
358 ev.duty_cycle = duty_cycle;
371 static void ene_rx_select_input(
struct ene_device *dev,
bool gpio_0a)
394 static void ene_rx_setup(
struct ene_device *dev)
398 int sample_period_adjust = 0;
400 dbg(
"RX: setup receiver, learning mode = %d", learning_mode);
408 sample_period_adjust =
412 (sample_period + sample_period_adjust) |
454 dev->
rdev->min_timeout = dev->
rdev->max_timeout =
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;
479 static void ene_rx_enable(
struct ene_device *dev)
488 reg_value = ene_read_reg(dev,
ENE_IRQ) & 0xF0;
492 ene_write_reg(dev,
ENE_IRQ, reg_value);
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);
527 static void ene_rx_reset(
struct ene_device *dev)
534 static void ene_tx_set_carrier(
struct ene_device *dev)
554 dbg(
"TX: pulse distance = %d * 500 ns", dev->
tx_period);
555 dbg(
"TX: pulse width = %d * 500 ns", tx_puls_width);
560 spin_unlock_irqrestore(&dev->
hw_lock, flags);
564 static void ene_tx_set_transmitters(
struct ene_device *dev)
573 spin_unlock_irqrestore(&dev->
hw_lock, flags);
577 static void ene_tx_enable(
struct ene_device *dev)
586 dbg(
"TX: Transmitter #1 is connected");
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");
604 static void ene_tx_disable(
struct ene_device *dev)
612 static void ene_tx_sample(
struct ene_device *dev)
619 pr_warn(
"TX: BUG: attempt to transmit NULL buffer\n");
628 dbg(
"TX: no more data to send");
632 dbg(
"TX: last sample sent by hardware");
651 dbg(
"TX: sample %8d (%s)", raw_tx * sample_period,
652 pulse ?
"pulse" :
"space");
667 static void ene_tx_irqsim(
unsigned long data)
674 spin_unlock_irqrestore(&dev->
hw_lock, flags);
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);
697 irq_status = ene_read_reg(dev,
ENE_IRQ);
702 ene_write_reg(dev,
ENE_IRQ, irq_status & ~ENE_IRQ_STATUS);
703 ene_write_reg(dev,
ENE_IRQ, irq_status & ~ENE_IRQ_STATUS);
708 ene_write_reg(dev,
ENE_FW2, fw_flags2 & ~ENE_FW2_RXIRQ);
712 fw_flags1 = ene_read_reg(dev,
ENE_FW1);
714 ene_write_reg(dev,
ENE_FW1, fw_flags1 & ~ENE_FW1_TXIRQ);
725 int hw_sample, irq_status;
735 ene_rx_read_hw_pointer(dev);
736 irq_status = ene_irq_status(dev);
746 dbg(
"TX interrupt on unsupported device!");
767 reg = ene_rx_get_sample_reg(dev);
773 hw_value = ene_read_reg(dev, reg);
780 hw_value |= ene_read_reg(dev, reg + offset) << 8;
790 hw_sample = hw_value * sample_period;
803 dbg(
"RX: %d (%s)", hw_sample, pulse ?
"pulse" :
"space");
812 spin_unlock_irqrestore(&dev->
hw_lock, flags);
817 static void ene_setup_default_settings(
struct ene_device *dev)
829 static void ene_setup_hw_settings(
struct ene_device *dev)
832 ene_tx_set_carrier(dev);
833 ene_tx_set_transmitters(dev);
847 spin_unlock_irqrestore(&dev->
hw_lock, flags);
859 spin_unlock_irqrestore(&dev->
hw_lock, flags);
866 dbg(
"TX: attempt to set transmitter mask %02x", tx_mask);
869 if (!tx_mask || tx_mask & ~0x03) {
870 dbg(
"TX: invalid mask");
876 ene_tx_set_transmitters(dev);
881 static int ene_set_tx_carrier(
struct rc_dev *rdev,
u32 carrier)
886 dbg(
"TX: attempt to set tx carrier to %d kHz", carrier);
890 period = 2000000 / carrier;
894 dbg(
"TX: out of range %d-%d kHz carrier",
900 ene_tx_set_carrier(dev);
905 static int ene_set_tx_duty_cycle(
struct rc_dev *rdev,
u32 duty_cycle)
908 dbg(
"TX: setting duty cycle to %d%%", duty_cycle);
910 ene_tx_set_carrier(dev);
915 static int ene_set_learning_mode(
struct rc_dev *rdev,
int enable)
927 spin_unlock_irqrestore(&dev->
hw_lock, flags);
931 static int ene_set_carrier_report(
struct rc_dev *rdev,
int enable)
944 spin_unlock_irqrestore(&dev->
hw_lock, flags);
949 static void ene_set_idle(
struct rc_dev *rdev,
bool idle)
955 dbg(
"RX: end of data");
960 static int ene_transmit(
struct rc_dev *rdev,
unsigned *
buf,
unsigned n)
983 spin_unlock_irqrestore(&dev->
hw_lock, flags);
989 spin_unlock_irqrestore(&dev->
hw_lock, flags);
1015 if (!pnp_port_valid(pnp_dev, 0) ||
1019 if (!pnp_irq_valid(pnp_dev, 0))
1024 dev->
hw_io = pnp_port_start(pnp_dev, 0);
1026 pnp_set_drvdata(pnp_dev, dev);
1030 if (sample_period < 5 || sample_period > 0x7F)
1034 error = ene_hw_detect(dev);
1041 (
long unsigned int)dev);
1042 pr_warn(
"Simulation of TX activated\n");
1046 learning_mode_force =
false;
1051 rdev->
open = ene_open;
1052 rdev->
close = ene_close;
1053 rdev->
s_idle = ene_set_idle;
1056 rdev->
input_name =
"ENE eHome Infrared Remote Receiver";
1061 rdev->
tx_ir = ene_transmit;
1066 rdev->
input_name =
"ENE eHome Infrared Remote Transceiver";
1071 ene_rx_setup_hw_buffer(dev);
1072 ene_setup_default_settings(dev);
1073 ene_setup_hw_settings(dev);
1097 pr_notice(
"driver has been successfully loaded\n");
1100 if (dev && dev->
irq >= 0)
1102 if (dev && dev->
hw_io >= 0)
1111 static void ene_remove(
struct pnp_dev *pnp_dev)
1113 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
1114 unsigned long flags;
1117 ene_rx_disable(dev);
1118 ene_rx_restore_hw_buffer(dev);
1119 spin_unlock_irqrestore(&dev->
hw_lock, flags);
1128 static void ene_enable_wake(
struct ene_device *dev,
int enable)
1130 enable = enable && device_may_wakeup(&dev->
pnp_dev->dev);
1131 dbg(
"wake on IR %s", enable ?
"enabled" :
"disabled");
1138 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
1139 ene_enable_wake(dev,
true);
1145 static int ene_resume(
struct pnp_dev *pnp_dev)
1147 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
1148 ene_setup_hw_settings(dev);
1153 ene_enable_wake(dev,
false);
1158 static void ene_shutdown(
struct pnp_dev *pnp_dev)
1160 struct ene_device *dev = pnp_get_drvdata(pnp_dev);
1161 ene_enable_wake(dev,
true);
1174 .id_table = ene_ids,
1180 .suspend = ene_suspend,
1181 .resume = ene_resume,
1183 .shutdown = ene_shutdown,
1186 static int __init ene_init(
void)
1191 static void ene_exit(
void)
1207 "Simulate TX features on unsupported hardware (dangerous)");
1211 (
"Infrared input driver for KB3926B/C/D/E/F "
1212 "(aka ENE0100/ENE0200/ENE0201/ENE0202) CIR port");