1*c942fddfSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 232cf86f6SMauro Carvalho Chehab /* 332cf86f6SMauro Carvalho Chehab * driver for ENE KB3926 B/C/D/E/F CIR (also known as ENE0XXX) 432cf86f6SMauro Carvalho Chehab * 532cf86f6SMauro Carvalho Chehab * Copyright (C) 2010 Maxim Levitsky <maximlevitsky@gmail.com> 632cf86f6SMauro Carvalho Chehab */ 732cf86f6SMauro Carvalho Chehab #include <linux/spinlock.h> 832cf86f6SMauro Carvalho Chehab 932cf86f6SMauro Carvalho Chehab 1032cf86f6SMauro Carvalho Chehab /* hardware address */ 1132cf86f6SMauro Carvalho Chehab #define ENE_STATUS 0 /* hardware status - unused */ 1232cf86f6SMauro Carvalho Chehab #define ENE_ADDR_HI 1 /* hi byte of register address */ 1332cf86f6SMauro Carvalho Chehab #define ENE_ADDR_LO 2 /* low byte of register address */ 1432cf86f6SMauro Carvalho Chehab #define ENE_IO 3 /* read/write window */ 1532cf86f6SMauro Carvalho Chehab #define ENE_IO_SIZE 4 1632cf86f6SMauro Carvalho Chehab 1732cf86f6SMauro Carvalho Chehab /* 8 bytes of samples, divided in 2 packets*/ 1832cf86f6SMauro Carvalho Chehab #define ENE_FW_SAMPLE_BUFFER 0xF8F0 /* sample buffer */ 1932cf86f6SMauro Carvalho Chehab #define ENE_FW_SAMPLE_SPACE 0x80 /* sample is space */ 2032cf86f6SMauro Carvalho Chehab #define ENE_FW_PACKET_SIZE 4 2132cf86f6SMauro Carvalho Chehab 2232cf86f6SMauro Carvalho Chehab /* first firmware flag register */ 2332cf86f6SMauro Carvalho Chehab #define ENE_FW1 0xF8F8 /* flagr */ 2432cf86f6SMauro Carvalho Chehab #define ENE_FW1_ENABLE 0x01 /* enable fw processing */ 2532cf86f6SMauro Carvalho Chehab #define ENE_FW1_TXIRQ 0x02 /* TX interrupt pending */ 2632cf86f6SMauro Carvalho Chehab #define ENE_FW1_HAS_EXTRA_BUF 0x04 /* fw uses extra buffer*/ 2732cf86f6SMauro Carvalho Chehab #define ENE_FW1_EXTRA_BUF_HND 0x08 /* extra buffer handshake bit*/ 2832cf86f6SMauro Carvalho Chehab #define ENE_FW1_LED_ON 0x10 /* turn on a led */ 2932cf86f6SMauro Carvalho Chehab 3032cf86f6SMauro Carvalho Chehab #define ENE_FW1_WPATTERN 0x20 /* enable wake pattern */ 3132cf86f6SMauro Carvalho Chehab #define ENE_FW1_WAKE 0x40 /* enable wake from S3 */ 3232cf86f6SMauro Carvalho Chehab #define ENE_FW1_IRQ 0x80 /* enable interrupt */ 3332cf86f6SMauro Carvalho Chehab 3432cf86f6SMauro Carvalho Chehab /* second firmware flag register */ 3532cf86f6SMauro Carvalho Chehab #define ENE_FW2 0xF8F9 /* flagw */ 3632cf86f6SMauro Carvalho Chehab #define ENE_FW2_BUF_WPTR 0x01 /* which half of the buffer to read */ 3732cf86f6SMauro Carvalho Chehab #define ENE_FW2_RXIRQ 0x04 /* RX IRQ pending*/ 3832cf86f6SMauro Carvalho Chehab #define ENE_FW2_GP0A 0x08 /* Use GPIO0A for demodulated input */ 3932cf86f6SMauro Carvalho Chehab #define ENE_FW2_EMMITER1_CONN 0x10 /* TX emmiter 1 connected */ 4032cf86f6SMauro Carvalho Chehab #define ENE_FW2_EMMITER2_CONN 0x20 /* TX emmiter 2 connected */ 4132cf86f6SMauro Carvalho Chehab 4232cf86f6SMauro Carvalho Chehab #define ENE_FW2_FAN_INPUT 0x40 /* fan input used for demodulated data*/ 4332cf86f6SMauro Carvalho Chehab #define ENE_FW2_LEARNING 0x80 /* hardware supports learning and TX */ 4432cf86f6SMauro Carvalho Chehab 4532cf86f6SMauro Carvalho Chehab /* firmware RX pointer for new style buffer */ 4632cf86f6SMauro Carvalho Chehab #define ENE_FW_RX_POINTER 0xF8FA 4732cf86f6SMauro Carvalho Chehab 4832cf86f6SMauro Carvalho Chehab /* high parts of samples for fan input (8 samples)*/ 4932cf86f6SMauro Carvalho Chehab #define ENE_FW_SMPL_BUF_FAN 0xF8FB 5032cf86f6SMauro Carvalho Chehab #define ENE_FW_SMPL_BUF_FAN_PLS 0x8000 /* combined sample is pulse */ 5132cf86f6SMauro Carvalho Chehab #define ENE_FW_SMPL_BUF_FAN_MSK 0x0FFF /* combined sample maximum value */ 5232cf86f6SMauro Carvalho Chehab #define ENE_FW_SAMPLE_PERIOD_FAN 61 /* fan input has fixed sample period */ 5332cf86f6SMauro Carvalho Chehab 5432cf86f6SMauro Carvalho Chehab /* transmitter ports */ 5532cf86f6SMauro Carvalho Chehab #define ENE_GPIOFS1 0xFC01 5632cf86f6SMauro Carvalho Chehab #define ENE_GPIOFS1_GPIO0D 0x20 /* enable tx output on GPIO0D */ 5732cf86f6SMauro Carvalho Chehab #define ENE_GPIOFS8 0xFC08 5832cf86f6SMauro Carvalho Chehab #define ENE_GPIOFS8_GPIO41 0x02 /* enable tx output on GPIO40 */ 5932cf86f6SMauro Carvalho Chehab 6032cf86f6SMauro Carvalho Chehab /* IRQ registers block (for revision B) */ 6132cf86f6SMauro Carvalho Chehab #define ENEB_IRQ 0xFD09 /* IRQ number */ 6232cf86f6SMauro Carvalho Chehab #define ENEB_IRQ_UNK1 0xFD17 /* unknown setting = 1 */ 6332cf86f6SMauro Carvalho Chehab #define ENEB_IRQ_STATUS 0xFD80 /* irq status */ 6432cf86f6SMauro Carvalho Chehab #define ENEB_IRQ_STATUS_IR 0x20 /* IR irq */ 6532cf86f6SMauro Carvalho Chehab 6632cf86f6SMauro Carvalho Chehab /* fan as input settings */ 6732cf86f6SMauro Carvalho Chehab #define ENE_FAN_AS_IN1 0xFE30 /* fan init reg 1 */ 6832cf86f6SMauro Carvalho Chehab #define ENE_FAN_AS_IN1_EN 0xCD 6932cf86f6SMauro Carvalho Chehab #define ENE_FAN_AS_IN2 0xFE31 /* fan init reg 2 */ 7032cf86f6SMauro Carvalho Chehab #define ENE_FAN_AS_IN2_EN 0x03 7132cf86f6SMauro Carvalho Chehab 7232cf86f6SMauro Carvalho Chehab /* IRQ registers block (for revision C,D) */ 7332cf86f6SMauro Carvalho Chehab #define ENE_IRQ 0xFE9B /* new irq settings register */ 7432cf86f6SMauro Carvalho Chehab #define ENE_IRQ_MASK 0x0F /* irq number mask */ 7532cf86f6SMauro Carvalho Chehab #define ENE_IRQ_UNK_EN 0x10 /* always enabled */ 7632cf86f6SMauro Carvalho Chehab #define ENE_IRQ_STATUS 0x20 /* irq status and ACK */ 7732cf86f6SMauro Carvalho Chehab 7832cf86f6SMauro Carvalho Chehab /* CIR Config register #1 */ 7932cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG 0xFEC0 8032cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG_RX_EN 0x01 /* RX enable */ 8132cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG_RX_IRQ 0x02 /* Enable hardware interrupt */ 8232cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG_REV_POL 0x04 /* Input polarity reversed */ 8332cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG_CARR_DEMOD 0x08 /* Enable carrier demodulator */ 8432cf86f6SMauro Carvalho Chehab 8532cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG_TX_EN 0x10 /* TX enable */ 8632cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG_TX_IRQ 0x20 /* Send interrupt on TX done */ 8732cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG_TX_POL_REV 0x40 /* TX polarity reversed */ 8832cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG_TX_CARR 0x80 /* send TX carrier or not */ 8932cf86f6SMauro Carvalho Chehab 9032cf86f6SMauro Carvalho Chehab /* CIR config register #2 */ 9132cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG2 0xFEC1 9232cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG2_RLC 0x00 9332cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG2_RC5 0x01 9432cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG2_RC6 0x02 9532cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG2_NEC 0x03 9632cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG2_CARR_DETECT 0x10 /* Enable carrier detection */ 9732cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG2_GPIO0A 0x20 /* Use GPIO0A instead of GPIO40 for input */ 9832cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG2_FAST_SAMPL1 0x40 /* Fast leading pulse detection for RC6 */ 9932cf86f6SMauro Carvalho Chehab #define ENE_CIRCFG2_FAST_SAMPL2 0x80 /* Fast data detection for RC6 */ 10032cf86f6SMauro Carvalho Chehab 10132cf86f6SMauro Carvalho Chehab /* Knobs for protocol decoding - will document when/if will use them */ 10232cf86f6SMauro Carvalho Chehab #define ENE_CIRPF 0xFEC2 10332cf86f6SMauro Carvalho Chehab #define ENE_CIRHIGH 0xFEC3 10432cf86f6SMauro Carvalho Chehab #define ENE_CIRBIT 0xFEC4 10532cf86f6SMauro Carvalho Chehab #define ENE_CIRSTART 0xFEC5 10632cf86f6SMauro Carvalho Chehab #define ENE_CIRSTART2 0xFEC6 10732cf86f6SMauro Carvalho Chehab 10832cf86f6SMauro Carvalho Chehab /* Actual register which contains RLC RX data - read by firmware */ 10932cf86f6SMauro Carvalho Chehab #define ENE_CIRDAT_IN 0xFEC7 11032cf86f6SMauro Carvalho Chehab 11132cf86f6SMauro Carvalho Chehab 11204ad3011SMauro Carvalho Chehab /* RLC configuration - sample period (1us resolution) + idle mode */ 11332cf86f6SMauro Carvalho Chehab #define ENE_CIRRLC_CFG 0xFEC8 11432cf86f6SMauro Carvalho Chehab #define ENE_CIRRLC_CFG_OVERFLOW 0x80 /* interrupt on overflows if set */ 11532cf86f6SMauro Carvalho Chehab #define ENE_DEFAULT_SAMPLE_PERIOD 50 11632cf86f6SMauro Carvalho Chehab 11732cf86f6SMauro Carvalho Chehab /* Two byte RLC TX buffer */ 11832cf86f6SMauro Carvalho Chehab #define ENE_CIRRLC_OUT0 0xFEC9 11932cf86f6SMauro Carvalho Chehab #define ENE_CIRRLC_OUT1 0xFECA 12032cf86f6SMauro Carvalho Chehab #define ENE_CIRRLC_OUT_PULSE 0x80 /* Transmitted sample is pulse */ 12132cf86f6SMauro Carvalho Chehab #define ENE_CIRRLC_OUT_MASK 0x7F 12232cf86f6SMauro Carvalho Chehab 12332cf86f6SMauro Carvalho Chehab 12432cf86f6SMauro Carvalho Chehab /* Carrier detect setting 12532cf86f6SMauro Carvalho Chehab * Low nibble - number of carrier pulses to average 12632cf86f6SMauro Carvalho Chehab * High nibble - number of initial carrier pulses to discard 12732cf86f6SMauro Carvalho Chehab */ 12832cf86f6SMauro Carvalho Chehab #define ENE_CIRCAR_PULS 0xFECB 12932cf86f6SMauro Carvalho Chehab 13032cf86f6SMauro Carvalho Chehab /* detected RX carrier period (resolution: 500 ns) */ 13132cf86f6SMauro Carvalho Chehab #define ENE_CIRCAR_PRD 0xFECC 13232cf86f6SMauro Carvalho Chehab #define ENE_CIRCAR_PRD_VALID 0x80 /* data valid content valid */ 13332cf86f6SMauro Carvalho Chehab 13432cf86f6SMauro Carvalho Chehab /* detected RX carrier pulse width (resolution: 500 ns) */ 13532cf86f6SMauro Carvalho Chehab #define ENE_CIRCAR_HPRD 0xFECD 13632cf86f6SMauro Carvalho Chehab 13732cf86f6SMauro Carvalho Chehab /* TX period (resolution: 500 ns, minimum 2)*/ 13832cf86f6SMauro Carvalho Chehab #define ENE_CIRMOD_PRD 0xFECE 13932cf86f6SMauro Carvalho Chehab #define ENE_CIRMOD_PRD_POL 0x80 /* TX carrier polarity*/ 14032cf86f6SMauro Carvalho Chehab 14132cf86f6SMauro Carvalho Chehab #define ENE_CIRMOD_PRD_MAX 0x7F /* 15.87 kHz */ 14232cf86f6SMauro Carvalho Chehab #define ENE_CIRMOD_PRD_MIN 0x02 /* 1 Mhz */ 14332cf86f6SMauro Carvalho Chehab 14432cf86f6SMauro Carvalho Chehab /* TX pulse width (resolution: 500 ns)*/ 14532cf86f6SMauro Carvalho Chehab #define ENE_CIRMOD_HPRD 0xFECF 14632cf86f6SMauro Carvalho Chehab 14732cf86f6SMauro Carvalho Chehab /* Hardware versions */ 14832cf86f6SMauro Carvalho Chehab #define ENE_ECHV 0xFF00 /* hardware revision */ 14932cf86f6SMauro Carvalho Chehab #define ENE_PLLFRH 0xFF16 15032cf86f6SMauro Carvalho Chehab #define ENE_PLLFRL 0xFF17 15132cf86f6SMauro Carvalho Chehab #define ENE_DEFAULT_PLL_FREQ 1000 15232cf86f6SMauro Carvalho Chehab 15332cf86f6SMauro Carvalho Chehab #define ENE_ECSTS 0xFF1D 15432cf86f6SMauro Carvalho Chehab #define ENE_ECSTS_RSRVD 0x04 15532cf86f6SMauro Carvalho Chehab 15632cf86f6SMauro Carvalho Chehab #define ENE_ECVER_MAJOR 0xFF1E /* chip version */ 15732cf86f6SMauro Carvalho Chehab #define ENE_ECVER_MINOR 0xFF1F 15832cf86f6SMauro Carvalho Chehab #define ENE_HW_VER_OLD 0xFD00 15932cf86f6SMauro Carvalho Chehab 16032cf86f6SMauro Carvalho Chehab /******************************************************************************/ 16132cf86f6SMauro Carvalho Chehab 16232cf86f6SMauro Carvalho Chehab #define ENE_DRIVER_NAME "ene_ir" 16332cf86f6SMauro Carvalho Chehab 16432cf86f6SMauro Carvalho Chehab #define ENE_IRQ_RX 1 16532cf86f6SMauro Carvalho Chehab #define ENE_IRQ_TX 2 16632cf86f6SMauro Carvalho Chehab 16732cf86f6SMauro Carvalho Chehab #define ENE_HW_B 1 /* 3926B */ 16832cf86f6SMauro Carvalho Chehab #define ENE_HW_C 2 /* 3926C */ 16932cf86f6SMauro Carvalho Chehab #define ENE_HW_D 3 /* 3926D or later */ 17032cf86f6SMauro Carvalho Chehab 17132cf86f6SMauro Carvalho Chehab #define __dbg(level, format, ...) \ 17232cf86f6SMauro Carvalho Chehab do { \ 17332cf86f6SMauro Carvalho Chehab if (debug >= level) \ 174408ed992SMaxim Levitsky pr_info(format "\n", ## __VA_ARGS__); \ 17532cf86f6SMauro Carvalho Chehab } while (0) 17632cf86f6SMauro Carvalho Chehab 17732cf86f6SMauro Carvalho Chehab #define dbg(format, ...) __dbg(1, format, ## __VA_ARGS__) 17832cf86f6SMauro Carvalho Chehab #define dbg_verbose(format, ...) __dbg(2, format, ## __VA_ARGS__) 17932cf86f6SMauro Carvalho Chehab #define dbg_regs(format, ...) __dbg(3, format, ## __VA_ARGS__) 18032cf86f6SMauro Carvalho Chehab 18132cf86f6SMauro Carvalho Chehab struct ene_device { 18232cf86f6SMauro Carvalho Chehab struct pnp_dev *pnp_dev; 183d8b4b582SDavid Härdeman struct rc_dev *rdev; 18432cf86f6SMauro Carvalho Chehab 18532cf86f6SMauro Carvalho Chehab /* hw IO settings */ 18632cf86f6SMauro Carvalho Chehab long hw_io; 18732cf86f6SMauro Carvalho Chehab int irq; 18832cf86f6SMauro Carvalho Chehab spinlock_t hw_lock; 18932cf86f6SMauro Carvalho Chehab 19032cf86f6SMauro Carvalho Chehab /* HW features */ 19132cf86f6SMauro Carvalho Chehab int hw_revision; /* hardware revision */ 19232cf86f6SMauro Carvalho Chehab bool hw_use_gpio_0a; /* gpio0a is demodulated input*/ 19332cf86f6SMauro Carvalho Chehab bool hw_extra_buffer; /* hardware has 'extra buffer' */ 19432cf86f6SMauro Carvalho Chehab bool hw_fan_input; /* fan input is IR data source */ 19532cf86f6SMauro Carvalho Chehab bool hw_learning_and_tx_capable; /* learning & tx capable */ 19632cf86f6SMauro Carvalho Chehab int pll_freq; 19732cf86f6SMauro Carvalho Chehab int buffer_len; 19832cf86f6SMauro Carvalho Chehab 19932cf86f6SMauro Carvalho Chehab /* Extra RX buffer location */ 20032cf86f6SMauro Carvalho Chehab int extra_buf1_address; 20132cf86f6SMauro Carvalho Chehab int extra_buf1_len; 20232cf86f6SMauro Carvalho Chehab int extra_buf2_address; 20332cf86f6SMauro Carvalho Chehab int extra_buf2_len; 20432cf86f6SMauro Carvalho Chehab 20532cf86f6SMauro Carvalho Chehab /* HW state*/ 20632cf86f6SMauro Carvalho Chehab int r_pointer; /* pointer to next sample to read */ 20732cf86f6SMauro Carvalho Chehab int w_pointer; /* pointer to next sample hw will write */ 20832cf86f6SMauro Carvalho Chehab bool rx_fan_input_inuse; /* is fan input in use for rx*/ 20932cf86f6SMauro Carvalho Chehab int tx_reg; /* current reg used for TX */ 21032cf86f6SMauro Carvalho Chehab u8 saved_conf1; /* saved FEC0 reg */ 21132cf86f6SMauro Carvalho Chehab unsigned int tx_sample; /* current sample for TX */ 21232cf86f6SMauro Carvalho Chehab bool tx_sample_pulse; /* current sample is pulse */ 21332cf86f6SMauro Carvalho Chehab 21432cf86f6SMauro Carvalho Chehab /* TX buffer */ 2155588dc2bSDavid Härdeman unsigned *tx_buffer; /* input samples buffer*/ 21690802ed9SPaul Bolle int tx_pos; /* position in that buffer */ 21732cf86f6SMauro Carvalho Chehab int tx_len; /* current len of tx buffer */ 21832cf86f6SMauro Carvalho Chehab int tx_done; /* done transmitting */ 21932cf86f6SMauro Carvalho Chehab /* one more sample pending*/ 22032cf86f6SMauro Carvalho Chehab struct completion tx_complete; /* TX completion */ 22132cf86f6SMauro Carvalho Chehab struct timer_list tx_sim_timer; 22232cf86f6SMauro Carvalho Chehab 22332cf86f6SMauro Carvalho Chehab /* TX settings */ 22432cf86f6SMauro Carvalho Chehab int tx_period; 22532cf86f6SMauro Carvalho Chehab int tx_duty_cycle; 22632cf86f6SMauro Carvalho Chehab int transmitter_mask; 22732cf86f6SMauro Carvalho Chehab 22832cf86f6SMauro Carvalho Chehab /* RX settings */ 22932cf86f6SMauro Carvalho Chehab bool learning_mode_enabled; /* learning input enabled */ 23032cf86f6SMauro Carvalho Chehab bool carrier_detect_enabled; /* carrier detect enabled */ 23132cf86f6SMauro Carvalho Chehab int rx_period_adjust; 23232cf86f6SMauro Carvalho Chehab bool rx_enabled; 23332cf86f6SMauro Carvalho Chehab }; 23432cf86f6SMauro Carvalho Chehab 23532cf86f6SMauro Carvalho Chehab static int ene_irq_status(struct ene_device *dev); 23632cf86f6SMauro Carvalho Chehab static void ene_rx_read_hw_pointer(struct ene_device *dev); 237