Path: blob/master/drivers/media/video/cx25840/cx25840-ir.c
17745 views
/*1* Driver for the Conexant CX2584x Audio/Video decoder chip and related cores2*3* Integrated Consumer Infrared Controller4*5* Copyright (C) 2010 Andy Walls <[email protected]>6*7* This program is free software; you can redistribute it and/or8* modify it under the terms of the GNU General Public License9* as published by the Free Software Foundation; either version 210* of the License, or (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA20* 02110-1301, USA.21*/2223#include <linux/slab.h>24#include <linux/kfifo.h>25#include <media/cx25840.h>26#include <media/rc-core.h>2728#include "cx25840-core.h"2930static unsigned int ir_debug;31module_param(ir_debug, int, 0644);32MODULE_PARM_DESC(ir_debug, "enable integrated IR debug messages");3334#define CX25840_IR_REG_BASE 0x2003536#define CX25840_IR_CNTRL_REG 0x20037#define CNTRL_WIN_3_3 0x0000000038#define CNTRL_WIN_4_3 0x0000000139#define CNTRL_WIN_3_4 0x0000000240#define CNTRL_WIN_4_4 0x0000000341#define CNTRL_WIN 0x0000000342#define CNTRL_EDG_NONE 0x0000000043#define CNTRL_EDG_FALL 0x0000000444#define CNTRL_EDG_RISE 0x0000000845#define CNTRL_EDG_BOTH 0x0000000C46#define CNTRL_EDG 0x0000000C47#define CNTRL_DMD 0x0000001048#define CNTRL_MOD 0x0000002049#define CNTRL_RFE 0x0000004050#define CNTRL_TFE 0x0000008051#define CNTRL_RXE 0x0000010052#define CNTRL_TXE 0x0000020053#define CNTRL_RIC 0x0000040054#define CNTRL_TIC 0x0000080055#define CNTRL_CPL 0x0000100056#define CNTRL_LBM 0x0000200057#define CNTRL_R 0x000040005859#define CX25840_IR_TXCLK_REG 0x20460#define TXCLK_TCD 0x0000FFFF6162#define CX25840_IR_RXCLK_REG 0x20863#define RXCLK_RCD 0x0000FFFF6465#define CX25840_IR_CDUTY_REG 0x20C66#define CDUTY_CDC 0x0000000F6768#define CX25840_IR_STATS_REG 0x21069#define STATS_RTO 0x0000000170#define STATS_ROR 0x0000000271#define STATS_RBY 0x0000000472#define STATS_TBY 0x0000000873#define STATS_RSR 0x0000001074#define STATS_TSR 0x000000207576#define CX25840_IR_IRQEN_REG 0x21477#define IRQEN_RTE 0x0000000178#define IRQEN_ROE 0x0000000279#define IRQEN_RSE 0x0000001080#define IRQEN_TSE 0x0000002081#define IRQEN_MSK 0x000000338283#define CX25840_IR_FILTR_REG 0x21884#define FILTR_LPF 0x0000FFFF8586#define CX25840_IR_FIFO_REG 0x23C87#define FIFO_RXTX 0x0000FFFF88#define FIFO_RXTX_LVL 0x0001000089#define FIFO_RXTX_RTO 0x0001FFFF90#define FIFO_RX_NDV 0x0002000091#define FIFO_RX_DEPTH 892#define FIFO_TX_DEPTH 89394#define CX25840_VIDCLK_FREQ 108000000 /* 108 MHz, BT.656 */95#define CX25840_IR_REFCLK_FREQ (CX25840_VIDCLK_FREQ / 2)9697/*98* We use this union internally for convenience, but callers to tx_write99* and rx_read will be expecting records of type struct ir_raw_event.100* Always ensure the size of this union is dictated by struct ir_raw_event.101*/102union cx25840_ir_fifo_rec {103u32 hw_fifo_data;104struct ir_raw_event ir_core_data;105};106107#define CX25840_IR_RX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))108#define CX25840_IR_TX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))109110struct cx25840_ir_state {111struct i2c_client *c;112113struct v4l2_subdev_ir_parameters rx_params;114struct mutex rx_params_lock; /* protects Rx parameter settings cache */115atomic_t rxclk_divider;116atomic_t rx_invert;117118struct kfifo rx_kfifo;119spinlock_t rx_kfifo_lock; /* protect Rx data kfifo */120121struct v4l2_subdev_ir_parameters tx_params;122struct mutex tx_params_lock; /* protects Tx parameter settings cache */123atomic_t txclk_divider;124};125126static inline struct cx25840_ir_state *to_ir_state(struct v4l2_subdev *sd)127{128struct cx25840_state *state = to_state(sd);129return state ? state->ir_state : NULL;130}131132133/*134* Rx and Tx Clock Divider register computations135*136* Note the largest clock divider value of 0xffff corresponds to:137* (0xffff + 1) * 1000 / 108/2 MHz = 1,213,629.629... ns138* which fits in 21 bits, so we'll use unsigned int for time arguments.139*/140static inline u16 count_to_clock_divider(unsigned int d)141{142if (d > RXCLK_RCD + 1)143d = RXCLK_RCD;144else if (d < 2)145d = 1;146else147d--;148return (u16) d;149}150151static inline u16 ns_to_clock_divider(unsigned int ns)152{153return count_to_clock_divider(154DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000));155}156157static inline unsigned int clock_divider_to_ns(unsigned int divider)158{159/* Period of the Rx or Tx clock in ns */160return DIV_ROUND_CLOSEST((divider + 1) * 1000,161CX25840_IR_REFCLK_FREQ / 1000000);162}163164static inline u16 carrier_freq_to_clock_divider(unsigned int freq)165{166return count_to_clock_divider(167DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * 16));168}169170static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider)171{172return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, (divider + 1) * 16);173}174175static inline u16 freq_to_clock_divider(unsigned int freq,176unsigned int rollovers)177{178return count_to_clock_divider(179DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * rollovers));180}181182static inline unsigned int clock_divider_to_freq(unsigned int divider,183unsigned int rollovers)184{185return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ,186(divider + 1) * rollovers);187}188189/*190* Low Pass Filter register calculations191*192* Note the largest count value of 0xffff corresponds to:193* 0xffff * 1000 / 108/2 MHz = 1,213,611.11... ns194* which fits in 21 bits, so we'll use unsigned int for time arguments.195*/196static inline u16 count_to_lpf_count(unsigned int d)197{198if (d > FILTR_LPF)199d = FILTR_LPF;200else if (d < 4)201d = 0;202return (u16) d;203}204205static inline u16 ns_to_lpf_count(unsigned int ns)206{207return count_to_lpf_count(208DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000));209}210211static inline unsigned int lpf_count_to_ns(unsigned int count)212{213/* Duration of the Low Pass Filter rejection window in ns */214return DIV_ROUND_CLOSEST(count * 1000,215CX25840_IR_REFCLK_FREQ / 1000000);216}217218static inline unsigned int lpf_count_to_us(unsigned int count)219{220/* Duration of the Low Pass Filter rejection window in us */221return DIV_ROUND_CLOSEST(count, CX25840_IR_REFCLK_FREQ / 1000000);222}223224/*225* FIFO register pulse width count compuations226*/227static u32 clock_divider_to_resolution(u16 divider)228{229/*230* Resolution is the duration of 1 tick of the readable portion of231* of the pulse width counter as read from the FIFO. The two lsb's are232* not readable, hence the << 2. This function returns ns.233*/234return DIV_ROUND_CLOSEST((1 << 2) * ((u32) divider + 1) * 1000,235CX25840_IR_REFCLK_FREQ / 1000000);236}237238static u64 pulse_width_count_to_ns(u16 count, u16 divider)239{240u64 n;241u32 rem;242243/*244* The 2 lsb's of the pulse width timer count are not readable, hence245* the (count << 2) | 0x3246*/247n = (((u64) count << 2) | 0x3) * (divider + 1) * 1000; /* millicycles */248rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000); /* / MHz => ns */249if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)250n++;251return n;252}253254#if 0255/* Keep as we will need this for Transmit functionality */256static u16 ns_to_pulse_width_count(u32 ns, u16 divider)257{258u64 n;259u32 d;260u32 rem;261262/*263* The 2 lsb's of the pulse width timer count are not accessible, hence264* the (1 << 2)265*/266n = ((u64) ns) * CX25840_IR_REFCLK_FREQ / 1000000; /* millicycles */267d = (1 << 2) * ((u32) divider + 1) * 1000; /* millicycles/count */268rem = do_div(n, d);269if (rem >= d / 2)270n++;271272if (n > FIFO_RXTX)273n = FIFO_RXTX;274else if (n == 0)275n = 1;276return (u16) n;277}278279#endif280static unsigned int pulse_width_count_to_us(u16 count, u16 divider)281{282u64 n;283u32 rem;284285/*286* The 2 lsb's of the pulse width timer count are not readable, hence287* the (count << 2) | 0x3288*/289n = (((u64) count << 2) | 0x3) * (divider + 1); /* cycles */290rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000); /* / MHz => us */291if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)292n++;293return (unsigned int) n;294}295296/*297* Pulse Clocks computations: Combined Pulse Width Count & Rx Clock Counts298*299* The total pulse clock count is an 18 bit pulse width timer count as the most300* significant part and (up to) 16 bit clock divider count as a modulus.301* When the Rx clock divider ticks down to 0, it increments the 18 bit pulse302* width timer count's least significant bit.303*/304static u64 ns_to_pulse_clocks(u32 ns)305{306u64 clocks;307u32 rem;308clocks = CX25840_IR_REFCLK_FREQ / 1000000 * (u64) ns; /* millicycles */309rem = do_div(clocks, 1000); /* /1000 = cycles */310if (rem >= 1000 / 2)311clocks++;312return clocks;313}314315static u16 pulse_clocks_to_clock_divider(u64 count)316{317u32 rem;318319rem = do_div(count, (FIFO_RXTX << 2) | 0x3);320321/* net result needs to be rounded down and decremented by 1 */322if (count > RXCLK_RCD + 1)323count = RXCLK_RCD;324else if (count < 2)325count = 1;326else327count--;328return (u16) count;329}330331/*332* IR Control Register helpers333*/334enum tx_fifo_watermark {335TX_FIFO_HALF_EMPTY = 0,336TX_FIFO_EMPTY = CNTRL_TIC,337};338339enum rx_fifo_watermark {340RX_FIFO_HALF_FULL = 0,341RX_FIFO_NOT_EMPTY = CNTRL_RIC,342};343344static inline void control_tx_irq_watermark(struct i2c_client *c,345enum tx_fifo_watermark level)346{347cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_TIC, level);348}349350static inline void control_rx_irq_watermark(struct i2c_client *c,351enum rx_fifo_watermark level)352{353cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_RIC, level);354}355356static inline void control_tx_enable(struct i2c_client *c, bool enable)357{358cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~(CNTRL_TXE | CNTRL_TFE),359enable ? (CNTRL_TXE | CNTRL_TFE) : 0);360}361362static inline void control_rx_enable(struct i2c_client *c, bool enable)363{364cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~(CNTRL_RXE | CNTRL_RFE),365enable ? (CNTRL_RXE | CNTRL_RFE) : 0);366}367368static inline void control_tx_modulation_enable(struct i2c_client *c,369bool enable)370{371cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_MOD,372enable ? CNTRL_MOD : 0);373}374375static inline void control_rx_demodulation_enable(struct i2c_client *c,376bool enable)377{378cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_DMD,379enable ? CNTRL_DMD : 0);380}381382static inline void control_rx_s_edge_detection(struct i2c_client *c,383u32 edge_types)384{385cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_EDG_BOTH,386edge_types & CNTRL_EDG_BOTH);387}388389static void control_rx_s_carrier_window(struct i2c_client *c,390unsigned int carrier,391unsigned int *carrier_range_low,392unsigned int *carrier_range_high)393{394u32 v;395unsigned int c16 = carrier * 16;396397if (*carrier_range_low < DIV_ROUND_CLOSEST(c16, 16 + 3)) {398v = CNTRL_WIN_3_4;399*carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 4);400} else {401v = CNTRL_WIN_3_3;402*carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 3);403}404405if (*carrier_range_high > DIV_ROUND_CLOSEST(c16, 16 - 3)) {406v |= CNTRL_WIN_4_3;407*carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 4);408} else {409v |= CNTRL_WIN_3_3;410*carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 3);411}412cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_WIN, v);413}414415static inline void control_tx_polarity_invert(struct i2c_client *c,416bool invert)417{418cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_CPL,419invert ? CNTRL_CPL : 0);420}421422/*423* IR Rx & Tx Clock Register helpers424*/425static unsigned int txclk_tx_s_carrier(struct i2c_client *c,426unsigned int freq,427u16 *divider)428{429*divider = carrier_freq_to_clock_divider(freq);430cx25840_write4(c, CX25840_IR_TXCLK_REG, *divider);431return clock_divider_to_carrier_freq(*divider);432}433434static unsigned int rxclk_rx_s_carrier(struct i2c_client *c,435unsigned int freq,436u16 *divider)437{438*divider = carrier_freq_to_clock_divider(freq);439cx25840_write4(c, CX25840_IR_RXCLK_REG, *divider);440return clock_divider_to_carrier_freq(*divider);441}442443static u32 txclk_tx_s_max_pulse_width(struct i2c_client *c, u32 ns,444u16 *divider)445{446u64 pulse_clocks;447448if (ns > IR_MAX_DURATION)449ns = IR_MAX_DURATION;450pulse_clocks = ns_to_pulse_clocks(ns);451*divider = pulse_clocks_to_clock_divider(pulse_clocks);452cx25840_write4(c, CX25840_IR_TXCLK_REG, *divider);453return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);454}455456static u32 rxclk_rx_s_max_pulse_width(struct i2c_client *c, u32 ns,457u16 *divider)458{459u64 pulse_clocks;460461if (ns > IR_MAX_DURATION)462ns = IR_MAX_DURATION;463pulse_clocks = ns_to_pulse_clocks(ns);464*divider = pulse_clocks_to_clock_divider(pulse_clocks);465cx25840_write4(c, CX25840_IR_RXCLK_REG, *divider);466return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);467}468469/*470* IR Tx Carrier Duty Cycle register helpers471*/472static unsigned int cduty_tx_s_duty_cycle(struct i2c_client *c,473unsigned int duty_cycle)474{475u32 n;476n = DIV_ROUND_CLOSEST(duty_cycle * 100, 625); /* 16ths of 100% */477if (n != 0)478n--;479if (n > 15)480n = 15;481cx25840_write4(c, CX25840_IR_CDUTY_REG, n);482return DIV_ROUND_CLOSEST((n + 1) * 100, 16);483}484485/*486* IR Filter Register helpers487*/488static u32 filter_rx_s_min_width(struct i2c_client *c, u32 min_width_ns)489{490u32 count = ns_to_lpf_count(min_width_ns);491cx25840_write4(c, CX25840_IR_FILTR_REG, count);492return lpf_count_to_ns(count);493}494495/*496* IR IRQ Enable Register helpers497*/498static inline void irqenable_rx(struct v4l2_subdev *sd, u32 mask)499{500struct cx25840_state *state = to_state(sd);501502if (is_cx23885(state) || is_cx23887(state))503mask ^= IRQEN_MSK;504mask &= (IRQEN_RTE | IRQEN_ROE | IRQEN_RSE);505cx25840_and_or4(state->c, CX25840_IR_IRQEN_REG,506~(IRQEN_RTE | IRQEN_ROE | IRQEN_RSE), mask);507}508509static inline void irqenable_tx(struct v4l2_subdev *sd, u32 mask)510{511struct cx25840_state *state = to_state(sd);512513if (is_cx23885(state) || is_cx23887(state))514mask ^= IRQEN_MSK;515mask &= IRQEN_TSE;516cx25840_and_or4(state->c, CX25840_IR_IRQEN_REG, ~IRQEN_TSE, mask);517}518519/*520* V4L2 Subdevice IR Ops521*/522int cx25840_ir_irq_handler(struct v4l2_subdev *sd, u32 status, bool *handled)523{524struct cx25840_state *state = to_state(sd);525struct cx25840_ir_state *ir_state = to_ir_state(sd);526struct i2c_client *c = NULL;527unsigned long flags;528529union cx25840_ir_fifo_rec rx_data[FIFO_RX_DEPTH];530unsigned int i, j, k;531u32 events, v;532int tsr, rsr, rto, ror, tse, rse, rte, roe, kror;533u32 cntrl, irqen, stats;534535*handled = false;536if (ir_state == NULL)537return -ENODEV;538539c = ir_state->c;540541/* Only support the IR controller for the CX2388[57] AV Core for now */542if (!(is_cx23885(state) || is_cx23887(state)))543return -ENODEV;544545cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);546irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);547if (is_cx23885(state) || is_cx23887(state))548irqen ^= IRQEN_MSK;549stats = cx25840_read4(c, CX25840_IR_STATS_REG);550551tsr = stats & STATS_TSR; /* Tx FIFO Service Request */552rsr = stats & STATS_RSR; /* Rx FIFO Service Request */553rto = stats & STATS_RTO; /* Rx Pulse Width Timer Time Out */554ror = stats & STATS_ROR; /* Rx FIFO Over Run */555556tse = irqen & IRQEN_TSE; /* Tx FIFO Service Request IRQ Enable */557rse = irqen & IRQEN_RSE; /* Rx FIFO Service Reuqest IRQ Enable */558rte = irqen & IRQEN_RTE; /* Rx Pulse Width Timer Time Out IRQ Enable */559roe = irqen & IRQEN_ROE; /* Rx FIFO Over Run IRQ Enable */560561v4l2_dbg(2, ir_debug, sd, "IR IRQ Status: %s %s %s %s %s %s\n",562tsr ? "tsr" : " ", rsr ? "rsr" : " ",563rto ? "rto" : " ", ror ? "ror" : " ",564stats & STATS_TBY ? "tby" : " ",565stats & STATS_RBY ? "rby" : " ");566567v4l2_dbg(2, ir_debug, sd, "IR IRQ Enables: %s %s %s %s\n",568tse ? "tse" : " ", rse ? "rse" : " ",569rte ? "rte" : " ", roe ? "roe" : " ");570571/*572* Transmitter interrupt service573*/574if (tse && tsr) {575/*576* TODO:577* Check the watermark threshold setting578* Pull FIFO_TX_DEPTH or FIFO_TX_DEPTH/2 entries from tx_kfifo579* Push the data to the hardware FIFO.580* If there was nothing more to send in the tx_kfifo, disable581* the TSR IRQ and notify the v4l2_device.582* If there was something in the tx_kfifo, check the tx_kfifo583* level and notify the v4l2_device, if it is low.584*/585/* For now, inhibit TSR interrupt until Tx is implemented */586irqenable_tx(sd, 0);587events = V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ;588v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_TX_NOTIFY, &events);589*handled = true;590}591592/*593* Receiver interrupt service594*/595kror = 0;596if ((rse && rsr) || (rte && rto)) {597/*598* Receive data on RSR to clear the STATS_RSR.599* Receive data on RTO, since we may not have yet hit the RSR600* watermark when we receive the RTO.601*/602for (i = 0, v = FIFO_RX_NDV;603(v & FIFO_RX_NDV) && !kror; i = 0) {604for (j = 0;605(v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) {606v = cx25840_read4(c, CX25840_IR_FIFO_REG);607rx_data[i].hw_fifo_data = v & ~FIFO_RX_NDV;608i++;609}610if (i == 0)611break;612j = i * sizeof(union cx25840_ir_fifo_rec);613k = kfifo_in_locked(&ir_state->rx_kfifo,614(unsigned char *) rx_data, j,615&ir_state->rx_kfifo_lock);616if (k != j)617kror++; /* rx_kfifo over run */618}619*handled = true;620}621622events = 0;623v = 0;624if (kror) {625events |= V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN;626v4l2_err(sd, "IR receiver software FIFO overrun\n");627}628if (roe && ror) {629/*630* The RX FIFO Enable (CNTRL_RFE) must be toggled to clear631* the Rx FIFO Over Run status (STATS_ROR)632*/633v |= CNTRL_RFE;634events |= V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN;635v4l2_err(sd, "IR receiver hardware FIFO overrun\n");636}637if (rte && rto) {638/*639* The IR Receiver Enable (CNTRL_RXE) must be toggled to clear640* the Rx Pulse Width Timer Time Out (STATS_RTO)641*/642v |= CNTRL_RXE;643events |= V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED;644}645if (v) {646/* Clear STATS_ROR & STATS_RTO as needed by reseting hardware */647cx25840_write4(c, CX25840_IR_CNTRL_REG, cntrl & ~v);648cx25840_write4(c, CX25840_IR_CNTRL_REG, cntrl);649*handled = true;650}651spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);652if (kfifo_len(&ir_state->rx_kfifo) >= CX25840_IR_RX_KFIFO_SIZE / 2)653events |= V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ;654spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);655656if (events)657v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_RX_NOTIFY, &events);658return 0;659}660661/* Receiver */662static int cx25840_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,663ssize_t *num)664{665struct cx25840_ir_state *ir_state = to_ir_state(sd);666bool invert;667u16 divider;668unsigned int i, n;669union cx25840_ir_fifo_rec *p;670unsigned u, v;671672if (ir_state == NULL)673return -ENODEV;674675invert = (bool) atomic_read(&ir_state->rx_invert);676divider = (u16) atomic_read(&ir_state->rxclk_divider);677678n = count / sizeof(union cx25840_ir_fifo_rec)679* sizeof(union cx25840_ir_fifo_rec);680if (n == 0) {681*num = 0;682return 0;683}684685n = kfifo_out_locked(&ir_state->rx_kfifo, buf, n,686&ir_state->rx_kfifo_lock);687688n /= sizeof(union cx25840_ir_fifo_rec);689*num = n * sizeof(union cx25840_ir_fifo_rec);690691for (p = (union cx25840_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {692693if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {694/* Assume RTO was because of no IR light input */695u = 0;696v4l2_dbg(2, ir_debug, sd, "rx read: end of rx\n");697} else {698u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;699if (invert)700u = u ? 0 : 1;701}702703v = (unsigned) pulse_width_count_to_ns(704(u16) (p->hw_fifo_data & FIFO_RXTX), divider);705if (v > IR_MAX_DURATION)706v = IR_MAX_DURATION;707708init_ir_raw_event(&p->ir_core_data);709p->ir_core_data.pulse = u;710p->ir_core_data.duration = v;711712v4l2_dbg(2, ir_debug, sd, "rx read: %10u ns %s\n",713v, u ? "mark" : "space");714}715return 0;716}717718static int cx25840_ir_rx_g_parameters(struct v4l2_subdev *sd,719struct v4l2_subdev_ir_parameters *p)720{721struct cx25840_ir_state *ir_state = to_ir_state(sd);722723if (ir_state == NULL)724return -ENODEV;725726mutex_lock(&ir_state->rx_params_lock);727memcpy(p, &ir_state->rx_params,728sizeof(struct v4l2_subdev_ir_parameters));729mutex_unlock(&ir_state->rx_params_lock);730return 0;731}732733static int cx25840_ir_rx_shutdown(struct v4l2_subdev *sd)734{735struct cx25840_ir_state *ir_state = to_ir_state(sd);736struct i2c_client *c;737738if (ir_state == NULL)739return -ENODEV;740741c = ir_state->c;742mutex_lock(&ir_state->rx_params_lock);743744/* Disable or slow down all IR Rx circuits and counters */745irqenable_rx(sd, 0);746control_rx_enable(c, false);747control_rx_demodulation_enable(c, false);748control_rx_s_edge_detection(c, CNTRL_EDG_NONE);749filter_rx_s_min_width(c, 0);750cx25840_write4(c, CX25840_IR_RXCLK_REG, RXCLK_RCD);751752ir_state->rx_params.shutdown = true;753754mutex_unlock(&ir_state->rx_params_lock);755return 0;756}757758static int cx25840_ir_rx_s_parameters(struct v4l2_subdev *sd,759struct v4l2_subdev_ir_parameters *p)760{761struct cx25840_ir_state *ir_state = to_ir_state(sd);762struct i2c_client *c;763struct v4l2_subdev_ir_parameters *o;764u16 rxclk_divider;765766if (ir_state == NULL)767return -ENODEV;768769if (p->shutdown)770return cx25840_ir_rx_shutdown(sd);771772if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)773return -ENOSYS;774775c = ir_state->c;776o = &ir_state->rx_params;777778mutex_lock(&ir_state->rx_params_lock);779780o->shutdown = p->shutdown;781782p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;783o->mode = p->mode;784785p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);786o->bytes_per_data_element = p->bytes_per_data_element;787788/* Before we tweak the hardware, we have to disable the receiver */789irqenable_rx(sd, 0);790control_rx_enable(c, false);791792control_rx_demodulation_enable(c, p->modulation);793o->modulation = p->modulation;794795if (p->modulation) {796p->carrier_freq = rxclk_rx_s_carrier(c, p->carrier_freq,797&rxclk_divider);798799o->carrier_freq = p->carrier_freq;800801p->duty_cycle = 50;802o->duty_cycle = p->duty_cycle;803804control_rx_s_carrier_window(c, p->carrier_freq,805&p->carrier_range_lower,806&p->carrier_range_upper);807o->carrier_range_lower = p->carrier_range_lower;808o->carrier_range_upper = p->carrier_range_upper;809810p->max_pulse_width =811(u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);812} else {813p->max_pulse_width =814rxclk_rx_s_max_pulse_width(c, p->max_pulse_width,815&rxclk_divider);816}817o->max_pulse_width = p->max_pulse_width;818atomic_set(&ir_state->rxclk_divider, rxclk_divider);819820p->noise_filter_min_width =821filter_rx_s_min_width(c, p->noise_filter_min_width);822o->noise_filter_min_width = p->noise_filter_min_width;823824p->resolution = clock_divider_to_resolution(rxclk_divider);825o->resolution = p->resolution;826827/* FIXME - make this dependent on resolution for better performance */828control_rx_irq_watermark(c, RX_FIFO_HALF_FULL);829830control_rx_s_edge_detection(c, CNTRL_EDG_BOTH);831832o->invert_level = p->invert_level;833atomic_set(&ir_state->rx_invert, p->invert_level);834835o->interrupt_enable = p->interrupt_enable;836o->enable = p->enable;837if (p->enable) {838unsigned long flags;839840spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);841kfifo_reset(&ir_state->rx_kfifo);842spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);843if (p->interrupt_enable)844irqenable_rx(sd, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);845control_rx_enable(c, p->enable);846}847848mutex_unlock(&ir_state->rx_params_lock);849return 0;850}851852/* Transmitter */853static int cx25840_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,854ssize_t *num)855{856struct cx25840_ir_state *ir_state = to_ir_state(sd);857struct i2c_client *c;858859if (ir_state == NULL)860return -ENODEV;861862c = ir_state->c;863#if 0864/*865* FIXME - the code below is an incomplete and untested sketch of what866* may need to be done. The critical part is to get 4 (or 8) pulses867* from the tx_kfifo, or converted from ns to the proper units from the868* input, and push them off to the hardware Tx FIFO right away, if the869* HW TX fifo needs service. The rest can be pushed to the tx_kfifo in870* a less critical timeframe. Also watch out for overruning the871* tx_kfifo - don't let it happen and let the caller know not all his872* pulses were written.873*/874u32 *ns_pulse = (u32 *) buf;875unsigned int n;876u32 fifo_pulse[FIFO_TX_DEPTH];877u32 mark;878879/* Compute how much we can fit in the tx kfifo */880n = CX25840_IR_TX_KFIFO_SIZE - kfifo_len(ir_state->tx_kfifo);881n = min(n, (unsigned int) count);882n /= sizeof(u32);883884/* FIXME - turn on Tx Fifo service interrupt885* check hardware fifo level, and other stuff886*/887for (i = 0; i < n; ) {888for (j = 0; j < FIFO_TX_DEPTH / 2 && i < n; j++) {889mark = ns_pulse[i] & LEVEL_MASK;890fifo_pulse[j] = ns_to_pulse_width_count(891ns_pulse[i] &892~LEVEL_MASK,893ir_state->txclk_divider);894if (mark)895fifo_pulse[j] &= FIFO_RXTX_LVL;896i++;897}898kfifo_put(ir_state->tx_kfifo, (u8 *) fifo_pulse,899j * sizeof(u32));900}901*num = n * sizeof(u32);902#else903/* For now enable the Tx FIFO Service interrupt & pretend we did work */904irqenable_tx(sd, IRQEN_TSE);905*num = count;906#endif907return 0;908}909910static int cx25840_ir_tx_g_parameters(struct v4l2_subdev *sd,911struct v4l2_subdev_ir_parameters *p)912{913struct cx25840_ir_state *ir_state = to_ir_state(sd);914915if (ir_state == NULL)916return -ENODEV;917918mutex_lock(&ir_state->tx_params_lock);919memcpy(p, &ir_state->tx_params,920sizeof(struct v4l2_subdev_ir_parameters));921mutex_unlock(&ir_state->tx_params_lock);922return 0;923}924925static int cx25840_ir_tx_shutdown(struct v4l2_subdev *sd)926{927struct cx25840_ir_state *ir_state = to_ir_state(sd);928struct i2c_client *c;929930if (ir_state == NULL)931return -ENODEV;932933c = ir_state->c;934mutex_lock(&ir_state->tx_params_lock);935936/* Disable or slow down all IR Tx circuits and counters */937irqenable_tx(sd, 0);938control_tx_enable(c, false);939control_tx_modulation_enable(c, false);940cx25840_write4(c, CX25840_IR_TXCLK_REG, TXCLK_TCD);941942ir_state->tx_params.shutdown = true;943944mutex_unlock(&ir_state->tx_params_lock);945return 0;946}947948static int cx25840_ir_tx_s_parameters(struct v4l2_subdev *sd,949struct v4l2_subdev_ir_parameters *p)950{951struct cx25840_ir_state *ir_state = to_ir_state(sd);952struct i2c_client *c;953struct v4l2_subdev_ir_parameters *o;954u16 txclk_divider;955956if (ir_state == NULL)957return -ENODEV;958959if (p->shutdown)960return cx25840_ir_tx_shutdown(sd);961962if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)963return -ENOSYS;964965c = ir_state->c;966o = &ir_state->tx_params;967mutex_lock(&ir_state->tx_params_lock);968969o->shutdown = p->shutdown;970971p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;972o->mode = p->mode;973974p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);975o->bytes_per_data_element = p->bytes_per_data_element;976977/* Before we tweak the hardware, we have to disable the transmitter */978irqenable_tx(sd, 0);979control_tx_enable(c, false);980981control_tx_modulation_enable(c, p->modulation);982o->modulation = p->modulation;983984if (p->modulation) {985p->carrier_freq = txclk_tx_s_carrier(c, p->carrier_freq,986&txclk_divider);987o->carrier_freq = p->carrier_freq;988989p->duty_cycle = cduty_tx_s_duty_cycle(c, p->duty_cycle);990o->duty_cycle = p->duty_cycle;991992p->max_pulse_width =993(u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);994} else {995p->max_pulse_width =996txclk_tx_s_max_pulse_width(c, p->max_pulse_width,997&txclk_divider);998}999o->max_pulse_width = p->max_pulse_width;1000atomic_set(&ir_state->txclk_divider, txclk_divider);10011002p->resolution = clock_divider_to_resolution(txclk_divider);1003o->resolution = p->resolution;10041005/* FIXME - make this dependent on resolution for better performance */1006control_tx_irq_watermark(c, TX_FIFO_HALF_EMPTY);10071008control_tx_polarity_invert(c, p->invert_carrier_sense);1009o->invert_carrier_sense = p->invert_carrier_sense;10101011/*1012* FIXME: we don't have hardware help for IO pin level inversion1013* here like we have on the CX23888.1014* Act on this with some mix of logical inversion of data levels,1015* carrier polarity, and carrier duty cycle.1016*/1017o->invert_level = p->invert_level;10181019o->interrupt_enable = p->interrupt_enable;1020o->enable = p->enable;1021if (p->enable) {1022/* reset tx_fifo here */1023if (p->interrupt_enable)1024irqenable_tx(sd, IRQEN_TSE);1025control_tx_enable(c, p->enable);1026}10271028mutex_unlock(&ir_state->tx_params_lock);1029return 0;1030}103110321033/*1034* V4L2 Subdevice Core Ops support1035*/1036int cx25840_ir_log_status(struct v4l2_subdev *sd)1037{1038struct cx25840_state *state = to_state(sd);1039struct i2c_client *c = state->c;1040char *s;1041int i, j;1042u32 cntrl, txclk, rxclk, cduty, stats, irqen, filtr;10431044/* The CX23888 chip doesn't have an IR controller on the A/V core */1045if (is_cx23888(state))1046return 0;10471048cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);1049txclk = cx25840_read4(c, CX25840_IR_TXCLK_REG) & TXCLK_TCD;1050rxclk = cx25840_read4(c, CX25840_IR_RXCLK_REG) & RXCLK_RCD;1051cduty = cx25840_read4(c, CX25840_IR_CDUTY_REG) & CDUTY_CDC;1052stats = cx25840_read4(c, CX25840_IR_STATS_REG);1053irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);1054if (is_cx23885(state) || is_cx23887(state))1055irqen ^= IRQEN_MSK;1056filtr = cx25840_read4(c, CX25840_IR_FILTR_REG) & FILTR_LPF;10571058v4l2_info(sd, "IR Receiver:\n");1059v4l2_info(sd, "\tEnabled: %s\n",1060cntrl & CNTRL_RXE ? "yes" : "no");1061v4l2_info(sd, "\tDemodulation from a carrier: %s\n",1062cntrl & CNTRL_DMD ? "enabled" : "disabled");1063v4l2_info(sd, "\tFIFO: %s\n",1064cntrl & CNTRL_RFE ? "enabled" : "disabled");1065switch (cntrl & CNTRL_EDG) {1066case CNTRL_EDG_NONE:1067s = "disabled";1068break;1069case CNTRL_EDG_FALL:1070s = "falling edge";1071break;1072case CNTRL_EDG_RISE:1073s = "rising edge";1074break;1075case CNTRL_EDG_BOTH:1076s = "rising & falling edges";1077break;1078default:1079s = "??? edge";1080break;1081}1082v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);1083v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",1084cntrl & CNTRL_R ? "not loaded" : "overflow marker");1085v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",1086cntrl & CNTRL_RIC ? "not empty" : "half full or greater");1087v4l2_info(sd, "\tLoopback mode: %s\n",1088cntrl & CNTRL_LBM ? "loopback active" : "normal receive");1089if (cntrl & CNTRL_DMD) {1090v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",1091clock_divider_to_carrier_freq(rxclk));1092switch (cntrl & CNTRL_WIN) {1093case CNTRL_WIN_3_3:1094i = 3;1095j = 3;1096break;1097case CNTRL_WIN_4_3:1098i = 4;1099j = 3;1100break;1101case CNTRL_WIN_3_4:1102i = 3;1103j = 4;1104break;1105case CNTRL_WIN_4_4:1106i = 4;1107j = 4;1108break;1109default:1110i = 0;1111j = 0;1112break;1113}1114v4l2_info(sd, "\tNext carrier edge window: 16 clocks "1115"-%1d/+%1d, %u to %u Hz\n", i, j,1116clock_divider_to_freq(rxclk, 16 + j),1117clock_divider_to_freq(rxclk, 16 - i));1118}1119v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",1120pulse_width_count_to_us(FIFO_RXTX, rxclk),1121pulse_width_count_to_ns(FIFO_RXTX, rxclk));1122v4l2_info(sd, "\tLow pass filter: %s\n",1123filtr ? "enabled" : "disabled");1124if (filtr)1125v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, "1126"%u ns\n",1127lpf_count_to_us(filtr),1128lpf_count_to_ns(filtr));1129v4l2_info(sd, "\tPulse width timer timed-out: %s\n",1130stats & STATS_RTO ? "yes" : "no");1131v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",1132irqen & IRQEN_RTE ? "enabled" : "disabled");1133v4l2_info(sd, "\tFIFO overrun: %s\n",1134stats & STATS_ROR ? "yes" : "no");1135v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",1136irqen & IRQEN_ROE ? "enabled" : "disabled");1137v4l2_info(sd, "\tBusy: %s\n",1138stats & STATS_RBY ? "yes" : "no");1139v4l2_info(sd, "\tFIFO service requested: %s\n",1140stats & STATS_RSR ? "yes" : "no");1141v4l2_info(sd, "\tFIFO service request interrupt: %s\n",1142irqen & IRQEN_RSE ? "enabled" : "disabled");11431144v4l2_info(sd, "IR Transmitter:\n");1145v4l2_info(sd, "\tEnabled: %s\n",1146cntrl & CNTRL_TXE ? "yes" : "no");1147v4l2_info(sd, "\tModulation onto a carrier: %s\n",1148cntrl & CNTRL_MOD ? "enabled" : "disabled");1149v4l2_info(sd, "\tFIFO: %s\n",1150cntrl & CNTRL_TFE ? "enabled" : "disabled");1151v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",1152cntrl & CNTRL_TIC ? "not empty" : "half full or less");1153v4l2_info(sd, "\tCarrier polarity: %s\n",1154cntrl & CNTRL_CPL ? "space:burst mark:noburst"1155: "space:noburst mark:burst");1156if (cntrl & CNTRL_MOD) {1157v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",1158clock_divider_to_carrier_freq(txclk));1159v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",1160cduty + 1);1161}1162v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",1163pulse_width_count_to_us(FIFO_RXTX, txclk),1164pulse_width_count_to_ns(FIFO_RXTX, txclk));1165v4l2_info(sd, "\tBusy: %s\n",1166stats & STATS_TBY ? "yes" : "no");1167v4l2_info(sd, "\tFIFO service requested: %s\n",1168stats & STATS_TSR ? "yes" : "no");1169v4l2_info(sd, "\tFIFO service request interrupt: %s\n",1170irqen & IRQEN_TSE ? "enabled" : "disabled");11711172return 0;1173}117411751176const struct v4l2_subdev_ir_ops cx25840_ir_ops = {1177.rx_read = cx25840_ir_rx_read,1178.rx_g_parameters = cx25840_ir_rx_g_parameters,1179.rx_s_parameters = cx25840_ir_rx_s_parameters,11801181.tx_write = cx25840_ir_tx_write,1182.tx_g_parameters = cx25840_ir_tx_g_parameters,1183.tx_s_parameters = cx25840_ir_tx_s_parameters,1184};118511861187static const struct v4l2_subdev_ir_parameters default_rx_params = {1188.bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),1189.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,11901191.enable = false,1192.interrupt_enable = false,1193.shutdown = true,11941195.modulation = true,1196.carrier_freq = 36000, /* 36 kHz - RC-5, and RC-6 carrier */11971198/* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */1199/* RC-6: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */1200.noise_filter_min_width = 333333, /* ns */1201.carrier_range_lower = 35000,1202.carrier_range_upper = 37000,1203.invert_level = false,1204};12051206static const struct v4l2_subdev_ir_parameters default_tx_params = {1207.bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),1208.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,12091210.enable = false,1211.interrupt_enable = false,1212.shutdown = true,12131214.modulation = true,1215.carrier_freq = 36000, /* 36 kHz - RC-5 carrier */1216.duty_cycle = 25, /* 25 % - RC-5 carrier */1217.invert_level = false,1218.invert_carrier_sense = false,1219};12201221int cx25840_ir_probe(struct v4l2_subdev *sd)1222{1223struct cx25840_state *state = to_state(sd);1224struct cx25840_ir_state *ir_state;1225struct v4l2_subdev_ir_parameters default_params;12261227/* Only init the IR controller for the CX2388[57] AV Core for now */1228if (!(is_cx23885(state) || is_cx23887(state)))1229return 0;12301231ir_state = kzalloc(sizeof(struct cx25840_ir_state), GFP_KERNEL);1232if (ir_state == NULL)1233return -ENOMEM;12341235spin_lock_init(&ir_state->rx_kfifo_lock);1236if (kfifo_alloc(&ir_state->rx_kfifo,1237CX25840_IR_RX_KFIFO_SIZE, GFP_KERNEL)) {1238kfree(ir_state);1239return -ENOMEM;1240}12411242ir_state->c = state->c;1243state->ir_state = ir_state;12441245/* Ensure no interrupts arrive yet */1246if (is_cx23885(state) || is_cx23887(state))1247cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, IRQEN_MSK);1248else1249cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, 0);12501251mutex_init(&ir_state->rx_params_lock);1252memcpy(&default_params, &default_rx_params,1253sizeof(struct v4l2_subdev_ir_parameters));1254v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);12551256mutex_init(&ir_state->tx_params_lock);1257memcpy(&default_params, &default_tx_params,1258sizeof(struct v4l2_subdev_ir_parameters));1259v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);12601261return 0;1262}12631264int cx25840_ir_remove(struct v4l2_subdev *sd)1265{1266struct cx25840_state *state = to_state(sd);1267struct cx25840_ir_state *ir_state = to_ir_state(sd);12681269if (ir_state == NULL)1270return -ENODEV;12711272cx25840_ir_rx_shutdown(sd);1273cx25840_ir_tx_shutdown(sd);12741275kfifo_free(&ir_state->rx_kfifo);1276kfree(ir_state);1277state->ir_state = NULL;1278return 0;1279}128012811282