Path: blob/master/drivers/media/video/cx23885/cx23888-ir.c
17884 views
/*1* Driver for the Conexant CX23885/7/8 PCIe bridge2*3* CX23888 Integrated Consumer Infrared Controller4*5* Copyright (C) 2009 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/kfifo.h>24#include <linux/slab.h>2526#include <media/v4l2-device.h>27#include <media/v4l2-chip-ident.h>28#include <media/rc-core.h>2930#include "cx23885.h"3132static unsigned int ir_888_debug;33module_param(ir_888_debug, int, 0644);34MODULE_PARM_DESC(ir_888_debug, "enable debug messages [CX23888 IR controller]");3536#define CX23888_IR_REG_BASE 0x17000037/*38* These CX23888 register offsets have a straightforward one to one mapping39* to the CX23885 register offsets of 0x200 through 0x21840*/41#define CX23888_IR_CNTRL_REG 0x17000042#define CNTRL_WIN_3_3 0x0000000043#define CNTRL_WIN_4_3 0x0000000144#define CNTRL_WIN_3_4 0x0000000245#define CNTRL_WIN_4_4 0x0000000346#define CNTRL_WIN 0x0000000347#define CNTRL_EDG_NONE 0x0000000048#define CNTRL_EDG_FALL 0x0000000449#define CNTRL_EDG_RISE 0x0000000850#define CNTRL_EDG_BOTH 0x0000000C51#define CNTRL_EDG 0x0000000C52#define CNTRL_DMD 0x0000001053#define CNTRL_MOD 0x0000002054#define CNTRL_RFE 0x0000004055#define CNTRL_TFE 0x0000008056#define CNTRL_RXE 0x0000010057#define CNTRL_TXE 0x0000020058#define CNTRL_RIC 0x0000040059#define CNTRL_TIC 0x0000080060#define CNTRL_CPL 0x0000100061#define CNTRL_LBM 0x0000200062#define CNTRL_R 0x0000400063/* CX23888 specific control flag */64#define CNTRL_IVO 0x000080006566#define CX23888_IR_TXCLK_REG 0x17000467#define TXCLK_TCD 0x0000FFFF6869#define CX23888_IR_RXCLK_REG 0x17000870#define RXCLK_RCD 0x0000FFFF7172#define CX23888_IR_CDUTY_REG 0x17000C73#define CDUTY_CDC 0x0000000F7475#define CX23888_IR_STATS_REG 0x17001076#define STATS_RTO 0x0000000177#define STATS_ROR 0x0000000278#define STATS_RBY 0x0000000479#define STATS_TBY 0x0000000880#define STATS_RSR 0x0000001081#define STATS_TSR 0x000000208283#define CX23888_IR_IRQEN_REG 0x17001484#define IRQEN_RTE 0x0000000185#define IRQEN_ROE 0x0000000286#define IRQEN_RSE 0x0000001087#define IRQEN_TSE 0x000000208889#define CX23888_IR_FILTR_REG 0x17001890#define FILTR_LPF 0x0000FFFF9192/* This register doesn't follow the pattern; it's 0x23C on a CX23885 */93#define CX23888_IR_FIFO_REG 0x17004094#define FIFO_RXTX 0x0000FFFF95#define FIFO_RXTX_LVL 0x0001000096#define FIFO_RXTX_RTO 0x0001FFFF97#define FIFO_RX_NDV 0x0002000098#define FIFO_RX_DEPTH 899#define FIFO_TX_DEPTH 8100101/* CX23888 unique registers */102#define CX23888_IR_SEEDP_REG 0x17001C103#define CX23888_IR_TIMOL_REG 0x170020104#define CX23888_IR_WAKE0_REG 0x170024105#define CX23888_IR_WAKE1_REG 0x170028106#define CX23888_IR_WAKE2_REG 0x17002C107#define CX23888_IR_MASK0_REG 0x170030108#define CX23888_IR_MASK1_REG 0x170034109#define CX23888_IR_MAKS2_REG 0x170038110#define CX23888_IR_DPIPG_REG 0x17003C111#define CX23888_IR_LEARN_REG 0x170044112113#define CX23888_VIDCLK_FREQ 108000000 /* 108 MHz, BT.656 */114#define CX23888_IR_REFCLK_FREQ (CX23888_VIDCLK_FREQ / 2)115116/*117* We use this union internally for convenience, but callers to tx_write118* and rx_read will be expecting records of type struct ir_raw_event.119* Always ensure the size of this union is dictated by struct ir_raw_event.120*/121union cx23888_ir_fifo_rec {122u32 hw_fifo_data;123struct ir_raw_event ir_core_data;124};125126#define CX23888_IR_RX_KFIFO_SIZE (256 * sizeof(union cx23888_ir_fifo_rec))127#define CX23888_IR_TX_KFIFO_SIZE (256 * sizeof(union cx23888_ir_fifo_rec))128129struct cx23888_ir_state {130struct v4l2_subdev sd;131struct cx23885_dev *dev;132u32 id;133u32 rev;134135struct v4l2_subdev_ir_parameters rx_params;136struct mutex rx_params_lock;137atomic_t rxclk_divider;138atomic_t rx_invert;139140struct kfifo rx_kfifo;141spinlock_t rx_kfifo_lock;142143struct v4l2_subdev_ir_parameters tx_params;144struct mutex tx_params_lock;145atomic_t txclk_divider;146};147148static inline struct cx23888_ir_state *to_state(struct v4l2_subdev *sd)149{150return v4l2_get_subdevdata(sd);151}152153/*154* IR register block read and write functions155*/156static157inline int cx23888_ir_write4(struct cx23885_dev *dev, u32 addr, u32 value)158{159cx_write(addr, value);160return 0;161}162163static inline u32 cx23888_ir_read4(struct cx23885_dev *dev, u32 addr)164{165return cx_read(addr);166}167168static inline int cx23888_ir_and_or4(struct cx23885_dev *dev, u32 addr,169u32 and_mask, u32 or_value)170{171cx_andor(addr, ~and_mask, or_value);172return 0;173}174175/*176* Rx and Tx Clock Divider register computations177*178* Note the largest clock divider value of 0xffff corresponds to:179* (0xffff + 1) * 1000 / 108/2 MHz = 1,213,629.629... ns180* which fits in 21 bits, so we'll use unsigned int for time arguments.181*/182static inline u16 count_to_clock_divider(unsigned int d)183{184if (d > RXCLK_RCD + 1)185d = RXCLK_RCD;186else if (d < 2)187d = 1;188else189d--;190return (u16) d;191}192193static inline u16 ns_to_clock_divider(unsigned int ns)194{195return count_to_clock_divider(196DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ / 1000000 * ns, 1000));197}198199static inline unsigned int clock_divider_to_ns(unsigned int divider)200{201/* Period of the Rx or Tx clock in ns */202return DIV_ROUND_CLOSEST((divider + 1) * 1000,203CX23888_IR_REFCLK_FREQ / 1000000);204}205206static inline u16 carrier_freq_to_clock_divider(unsigned int freq)207{208return count_to_clock_divider(209DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, freq * 16));210}211212static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider)213{214return DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, (divider + 1) * 16);215}216217static inline u16 freq_to_clock_divider(unsigned int freq,218unsigned int rollovers)219{220return count_to_clock_divider(221DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, freq * rollovers));222}223224static inline unsigned int clock_divider_to_freq(unsigned int divider,225unsigned int rollovers)226{227return DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ,228(divider + 1) * rollovers);229}230231/*232* Low Pass Filter register calculations233*234* Note the largest count value of 0xffff corresponds to:235* 0xffff * 1000 / 108/2 MHz = 1,213,611.11... ns236* which fits in 21 bits, so we'll use unsigned int for time arguments.237*/238static inline u16 count_to_lpf_count(unsigned int d)239{240if (d > FILTR_LPF)241d = FILTR_LPF;242else if (d < 4)243d = 0;244return (u16) d;245}246247static inline u16 ns_to_lpf_count(unsigned int ns)248{249return count_to_lpf_count(250DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ / 1000000 * ns, 1000));251}252253static inline unsigned int lpf_count_to_ns(unsigned int count)254{255/* Duration of the Low Pass Filter rejection window in ns */256return DIV_ROUND_CLOSEST(count * 1000,257CX23888_IR_REFCLK_FREQ / 1000000);258}259260static inline unsigned int lpf_count_to_us(unsigned int count)261{262/* Duration of the Low Pass Filter rejection window in us */263return DIV_ROUND_CLOSEST(count, CX23888_IR_REFCLK_FREQ / 1000000);264}265266/*267* FIFO register pulse width count compuations268*/269static u32 clock_divider_to_resolution(u16 divider)270{271/*272* Resolution is the duration of 1 tick of the readable portion of273* of the pulse width counter as read from the FIFO. The two lsb's are274* not readable, hence the << 2. This function returns ns.275*/276return DIV_ROUND_CLOSEST((1 << 2) * ((u32) divider + 1) * 1000,277CX23888_IR_REFCLK_FREQ / 1000000);278}279280static u64 pulse_width_count_to_ns(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) * 1000; /* millicycles */290rem = do_div(n, CX23888_IR_REFCLK_FREQ / 1000000); /* / MHz => ns */291if (rem >= CX23888_IR_REFCLK_FREQ / 1000000 / 2)292n++;293return n;294}295296static unsigned int pulse_width_count_to_us(u16 count, u16 divider)297{298u64 n;299u32 rem;300301/*302* The 2 lsb's of the pulse width timer count are not readable, hence303* the (count << 2) | 0x3304*/305n = (((u64) count << 2) | 0x3) * (divider + 1); /* cycles */306rem = do_div(n, CX23888_IR_REFCLK_FREQ / 1000000); /* / MHz => us */307if (rem >= CX23888_IR_REFCLK_FREQ / 1000000 / 2)308n++;309return (unsigned int) n;310}311312/*313* Pulse Clocks computations: Combined Pulse Width Count & Rx Clock Counts314*315* The total pulse clock count is an 18 bit pulse width timer count as the most316* significant part and (up to) 16 bit clock divider count as a modulus.317* When the Rx clock divider ticks down to 0, it increments the 18 bit pulse318* width timer count's least significant bit.319*/320static u64 ns_to_pulse_clocks(u32 ns)321{322u64 clocks;323u32 rem;324clocks = CX23888_IR_REFCLK_FREQ / 1000000 * (u64) ns; /* millicycles */325rem = do_div(clocks, 1000); /* /1000 = cycles */326if (rem >= 1000 / 2)327clocks++;328return clocks;329}330331static u16 pulse_clocks_to_clock_divider(u64 count)332{333u32 rem;334335rem = do_div(count, (FIFO_RXTX << 2) | 0x3);336337/* net result needs to be rounded down and decremented by 1 */338if (count > RXCLK_RCD + 1)339count = RXCLK_RCD;340else if (count < 2)341count = 1;342else343count--;344return (u16) count;345}346347/*348* IR Control Register helpers349*/350enum tx_fifo_watermark {351TX_FIFO_HALF_EMPTY = 0,352TX_FIFO_EMPTY = CNTRL_TIC,353};354355enum rx_fifo_watermark {356RX_FIFO_HALF_FULL = 0,357RX_FIFO_NOT_EMPTY = CNTRL_RIC,358};359360static inline void control_tx_irq_watermark(struct cx23885_dev *dev,361enum tx_fifo_watermark level)362{363cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_TIC, level);364}365366static inline void control_rx_irq_watermark(struct cx23885_dev *dev,367enum rx_fifo_watermark level)368{369cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_RIC, level);370}371372static inline void control_tx_enable(struct cx23885_dev *dev, bool enable)373{374cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~(CNTRL_TXE | CNTRL_TFE),375enable ? (CNTRL_TXE | CNTRL_TFE) : 0);376}377378static inline void control_rx_enable(struct cx23885_dev *dev, bool enable)379{380cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~(CNTRL_RXE | CNTRL_RFE),381enable ? (CNTRL_RXE | CNTRL_RFE) : 0);382}383384static inline void control_tx_modulation_enable(struct cx23885_dev *dev,385bool enable)386{387cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_MOD,388enable ? CNTRL_MOD : 0);389}390391static inline void control_rx_demodulation_enable(struct cx23885_dev *dev,392bool enable)393{394cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_DMD,395enable ? CNTRL_DMD : 0);396}397398static inline void control_rx_s_edge_detection(struct cx23885_dev *dev,399u32 edge_types)400{401cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_EDG_BOTH,402edge_types & CNTRL_EDG_BOTH);403}404405static void control_rx_s_carrier_window(struct cx23885_dev *dev,406unsigned int carrier,407unsigned int *carrier_range_low,408unsigned int *carrier_range_high)409{410u32 v;411unsigned int c16 = carrier * 16;412413if (*carrier_range_low < DIV_ROUND_CLOSEST(c16, 16 + 3)) {414v = CNTRL_WIN_3_4;415*carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 4);416} else {417v = CNTRL_WIN_3_3;418*carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 3);419}420421if (*carrier_range_high > DIV_ROUND_CLOSEST(c16, 16 - 3)) {422v |= CNTRL_WIN_4_3;423*carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 4);424} else {425v |= CNTRL_WIN_3_3;426*carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 3);427}428cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_WIN, v);429}430431static inline void control_tx_polarity_invert(struct cx23885_dev *dev,432bool invert)433{434cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_CPL,435invert ? CNTRL_CPL : 0);436}437438static inline void control_tx_level_invert(struct cx23885_dev *dev,439bool invert)440{441cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_IVO,442invert ? CNTRL_IVO : 0);443}444445/*446* IR Rx & Tx Clock Register helpers447*/448static unsigned int txclk_tx_s_carrier(struct cx23885_dev *dev,449unsigned int freq,450u16 *divider)451{452*divider = carrier_freq_to_clock_divider(freq);453cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, *divider);454return clock_divider_to_carrier_freq(*divider);455}456457static unsigned int rxclk_rx_s_carrier(struct cx23885_dev *dev,458unsigned int freq,459u16 *divider)460{461*divider = carrier_freq_to_clock_divider(freq);462cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, *divider);463return clock_divider_to_carrier_freq(*divider);464}465466static u32 txclk_tx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,467u16 *divider)468{469u64 pulse_clocks;470471if (ns > IR_MAX_DURATION)472ns = IR_MAX_DURATION;473pulse_clocks = ns_to_pulse_clocks(ns);474*divider = pulse_clocks_to_clock_divider(pulse_clocks);475cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, *divider);476return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);477}478479static u32 rxclk_rx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,480u16 *divider)481{482u64 pulse_clocks;483484if (ns > IR_MAX_DURATION)485ns = IR_MAX_DURATION;486pulse_clocks = ns_to_pulse_clocks(ns);487*divider = pulse_clocks_to_clock_divider(pulse_clocks);488cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, *divider);489return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);490}491492/*493* IR Tx Carrier Duty Cycle register helpers494*/495static unsigned int cduty_tx_s_duty_cycle(struct cx23885_dev *dev,496unsigned int duty_cycle)497{498u32 n;499n = DIV_ROUND_CLOSEST(duty_cycle * 100, 625); /* 16ths of 100% */500if (n != 0)501n--;502if (n > 15)503n = 15;504cx23888_ir_write4(dev, CX23888_IR_CDUTY_REG, n);505return DIV_ROUND_CLOSEST((n + 1) * 100, 16);506}507508/*509* IR Filter Register helpers510*/511static u32 filter_rx_s_min_width(struct cx23885_dev *dev, u32 min_width_ns)512{513u32 count = ns_to_lpf_count(min_width_ns);514cx23888_ir_write4(dev, CX23888_IR_FILTR_REG, count);515return lpf_count_to_ns(count);516}517518/*519* IR IRQ Enable Register helpers520*/521static inline void irqenable_rx(struct cx23885_dev *dev, u32 mask)522{523mask &= (IRQEN_RTE | IRQEN_ROE | IRQEN_RSE);524cx23888_ir_and_or4(dev, CX23888_IR_IRQEN_REG,525~(IRQEN_RTE | IRQEN_ROE | IRQEN_RSE), mask);526}527528static inline void irqenable_tx(struct cx23885_dev *dev, u32 mask)529{530mask &= IRQEN_TSE;531cx23888_ir_and_or4(dev, CX23888_IR_IRQEN_REG, ~IRQEN_TSE, mask);532}533534/*535* V4L2 Subdevice IR Ops536*/537static int cx23888_ir_irq_handler(struct v4l2_subdev *sd, u32 status,538bool *handled)539{540struct cx23888_ir_state *state = to_state(sd);541struct cx23885_dev *dev = state->dev;542unsigned long flags;543544u32 cntrl = cx23888_ir_read4(dev, CX23888_IR_CNTRL_REG);545u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);546u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);547548union cx23888_ir_fifo_rec rx_data[FIFO_RX_DEPTH];549unsigned int i, j, k;550u32 events, v;551int tsr, rsr, rto, ror, tse, rse, rte, roe, kror;552553tsr = stats & STATS_TSR; /* Tx FIFO Service Request */554rsr = stats & STATS_RSR; /* Rx FIFO Service Request */555rto = stats & STATS_RTO; /* Rx Pulse Width Timer Time Out */556ror = stats & STATS_ROR; /* Rx FIFO Over Run */557558tse = irqen & IRQEN_TSE; /* Tx FIFO Service Request IRQ Enable */559rse = irqen & IRQEN_RSE; /* Rx FIFO Service Reuqest IRQ Enable */560rte = irqen & IRQEN_RTE; /* Rx Pulse Width Timer Time Out IRQ Enable */561roe = irqen & IRQEN_ROE; /* Rx FIFO Over Run IRQ Enable */562563*handled = false;564v4l2_dbg(2, ir_888_debug, sd, "IRQ Status: %s %s %s %s %s %s\n",565tsr ? "tsr" : " ", rsr ? "rsr" : " ",566rto ? "rto" : " ", ror ? "ror" : " ",567stats & STATS_TBY ? "tby" : " ",568stats & STATS_RBY ? "rby" : " ");569570v4l2_dbg(2, ir_888_debug, sd, "IRQ Enables: %s %s %s %s\n",571tse ? "tse" : " ", rse ? "rse" : " ",572rte ? "rte" : " ", roe ? "roe" : " ");573574/*575* Transmitter interrupt service576*/577if (tse && tsr) {578/*579* TODO:580* Check the watermark threshold setting581* Pull FIFO_TX_DEPTH or FIFO_TX_DEPTH/2 entries from tx_kfifo582* Push the data to the hardware FIFO.583* If there was nothing more to send in the tx_kfifo, disable584* the TSR IRQ and notify the v4l2_device.585* If there was something in the tx_kfifo, check the tx_kfifo586* level and notify the v4l2_device, if it is low.587*/588/* For now, inhibit TSR interrupt until Tx is implemented */589irqenable_tx(dev, 0);590events = V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ;591v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_TX_NOTIFY, &events);592*handled = true;593}594595/*596* Receiver interrupt service597*/598kror = 0;599if ((rse && rsr) || (rte && rto)) {600/*601* Receive data on RSR to clear the STATS_RSR.602* Receive data on RTO, since we may not have yet hit the RSR603* watermark when we receive the RTO.604*/605for (i = 0, v = FIFO_RX_NDV;606(v & FIFO_RX_NDV) && !kror; i = 0) {607for (j = 0;608(v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) {609v = cx23888_ir_read4(dev, CX23888_IR_FIFO_REG);610rx_data[i].hw_fifo_data = v & ~FIFO_RX_NDV;611i++;612}613if (i == 0)614break;615j = i * sizeof(union cx23888_ir_fifo_rec);616k = kfifo_in_locked(&state->rx_kfifo,617(unsigned char *) rx_data, j,618&state->rx_kfifo_lock);619if (k != j)620kror++; /* rx_kfifo over run */621}622*handled = true;623}624625events = 0;626v = 0;627if (kror) {628events |= V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN;629v4l2_err(sd, "IR receiver software FIFO overrun\n");630}631if (roe && ror) {632/*633* The RX FIFO Enable (CNTRL_RFE) must be toggled to clear634* the Rx FIFO Over Run status (STATS_ROR)635*/636v |= CNTRL_RFE;637events |= V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN;638v4l2_err(sd, "IR receiver hardware FIFO overrun\n");639}640if (rte && rto) {641/*642* The IR Receiver Enable (CNTRL_RXE) must be toggled to clear643* the Rx Pulse Width Timer Time Out (STATS_RTO)644*/645v |= CNTRL_RXE;646events |= V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED;647}648if (v) {649/* Clear STATS_ROR & STATS_RTO as needed by reseting hardware */650cx23888_ir_write4(dev, CX23888_IR_CNTRL_REG, cntrl & ~v);651cx23888_ir_write4(dev, CX23888_IR_CNTRL_REG, cntrl);652*handled = true;653}654655spin_lock_irqsave(&state->rx_kfifo_lock, flags);656if (kfifo_len(&state->rx_kfifo) >= CX23888_IR_RX_KFIFO_SIZE / 2)657events |= V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ;658spin_unlock_irqrestore(&state->rx_kfifo_lock, flags);659660if (events)661v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_RX_NOTIFY, &events);662return 0;663}664665/* Receiver */666static int cx23888_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,667ssize_t *num)668{669struct cx23888_ir_state *state = to_state(sd);670bool invert = (bool) atomic_read(&state->rx_invert);671u16 divider = (u16) atomic_read(&state->rxclk_divider);672673unsigned int i, n;674union cx23888_ir_fifo_rec *p;675unsigned u, v;676677n = count / sizeof(union cx23888_ir_fifo_rec)678* sizeof(union cx23888_ir_fifo_rec);679if (n == 0) {680*num = 0;681return 0;682}683684n = kfifo_out_locked(&state->rx_kfifo, buf, n, &state->rx_kfifo_lock);685686n /= sizeof(union cx23888_ir_fifo_rec);687*num = n * sizeof(union cx23888_ir_fifo_rec);688689for (p = (union cx23888_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {690691if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {692/* Assume RTO was because of no IR light input */693u = 0;694v4l2_dbg(2, ir_888_debug, sd, "rx read: end of rx\n");695} else {696u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;697if (invert)698u = u ? 0 : 1;699}700701v = (unsigned) pulse_width_count_to_ns(702(u16) (p->hw_fifo_data & FIFO_RXTX), divider);703if (v > IR_MAX_DURATION)704v = IR_MAX_DURATION;705706init_ir_raw_event(&p->ir_core_data);707p->ir_core_data.pulse = u;708p->ir_core_data.duration = v;709710v4l2_dbg(2, ir_888_debug, sd, "rx read: %10u ns %s\n",711v, u ? "mark" : "space");712}713return 0;714}715716static int cx23888_ir_rx_g_parameters(struct v4l2_subdev *sd,717struct v4l2_subdev_ir_parameters *p)718{719struct cx23888_ir_state *state = to_state(sd);720mutex_lock(&state->rx_params_lock);721memcpy(p, &state->rx_params, sizeof(struct v4l2_subdev_ir_parameters));722mutex_unlock(&state->rx_params_lock);723return 0;724}725726static int cx23888_ir_rx_shutdown(struct v4l2_subdev *sd)727{728struct cx23888_ir_state *state = to_state(sd);729struct cx23885_dev *dev = state->dev;730731mutex_lock(&state->rx_params_lock);732733/* Disable or slow down all IR Rx circuits and counters */734irqenable_rx(dev, 0);735control_rx_enable(dev, false);736control_rx_demodulation_enable(dev, false);737control_rx_s_edge_detection(dev, CNTRL_EDG_NONE);738filter_rx_s_min_width(dev, 0);739cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, RXCLK_RCD);740741state->rx_params.shutdown = true;742743mutex_unlock(&state->rx_params_lock);744return 0;745}746747static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,748struct v4l2_subdev_ir_parameters *p)749{750struct cx23888_ir_state *state = to_state(sd);751struct cx23885_dev *dev = state->dev;752struct v4l2_subdev_ir_parameters *o = &state->rx_params;753u16 rxclk_divider;754755if (p->shutdown)756return cx23888_ir_rx_shutdown(sd);757758if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)759return -ENOSYS;760761mutex_lock(&state->rx_params_lock);762763o->shutdown = p->shutdown;764765o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;766767o->bytes_per_data_element = p->bytes_per_data_element768= sizeof(union cx23888_ir_fifo_rec);769770/* Before we tweak the hardware, we have to disable the receiver */771irqenable_rx(dev, 0);772control_rx_enable(dev, false);773774control_rx_demodulation_enable(dev, p->modulation);775o->modulation = p->modulation;776777if (p->modulation) {778p->carrier_freq = rxclk_rx_s_carrier(dev, p->carrier_freq,779&rxclk_divider);780781o->carrier_freq = p->carrier_freq;782783o->duty_cycle = p->duty_cycle = 50;784785control_rx_s_carrier_window(dev, p->carrier_freq,786&p->carrier_range_lower,787&p->carrier_range_upper);788o->carrier_range_lower = p->carrier_range_lower;789o->carrier_range_upper = p->carrier_range_upper;790791p->max_pulse_width =792(u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);793} else {794p->max_pulse_width =795rxclk_rx_s_max_pulse_width(dev, p->max_pulse_width,796&rxclk_divider);797}798o->max_pulse_width = p->max_pulse_width;799atomic_set(&state->rxclk_divider, rxclk_divider);800801p->noise_filter_min_width =802filter_rx_s_min_width(dev, p->noise_filter_min_width);803o->noise_filter_min_width = p->noise_filter_min_width;804805p->resolution = clock_divider_to_resolution(rxclk_divider);806o->resolution = p->resolution;807808/* FIXME - make this dependent on resolution for better performance */809control_rx_irq_watermark(dev, RX_FIFO_HALF_FULL);810811control_rx_s_edge_detection(dev, CNTRL_EDG_BOTH);812813o->invert_level = p->invert_level;814atomic_set(&state->rx_invert, p->invert_level);815816o->interrupt_enable = p->interrupt_enable;817o->enable = p->enable;818if (p->enable) {819unsigned long flags;820821spin_lock_irqsave(&state->rx_kfifo_lock, flags);822kfifo_reset(&state->rx_kfifo);823/* reset tx_fifo too if there is one... */824spin_unlock_irqrestore(&state->rx_kfifo_lock, flags);825if (p->interrupt_enable)826irqenable_rx(dev, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);827control_rx_enable(dev, p->enable);828}829830mutex_unlock(&state->rx_params_lock);831return 0;832}833834/* Transmitter */835static int cx23888_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,836ssize_t *num)837{838struct cx23888_ir_state *state = to_state(sd);839struct cx23885_dev *dev = state->dev;840/* For now enable the Tx FIFO Service interrupt & pretend we did work */841irqenable_tx(dev, IRQEN_TSE);842*num = count;843return 0;844}845846static int cx23888_ir_tx_g_parameters(struct v4l2_subdev *sd,847struct v4l2_subdev_ir_parameters *p)848{849struct cx23888_ir_state *state = to_state(sd);850mutex_lock(&state->tx_params_lock);851memcpy(p, &state->tx_params, sizeof(struct v4l2_subdev_ir_parameters));852mutex_unlock(&state->tx_params_lock);853return 0;854}855856static int cx23888_ir_tx_shutdown(struct v4l2_subdev *sd)857{858struct cx23888_ir_state *state = to_state(sd);859struct cx23885_dev *dev = state->dev;860861mutex_lock(&state->tx_params_lock);862863/* Disable or slow down all IR Tx circuits and counters */864irqenable_tx(dev, 0);865control_tx_enable(dev, false);866control_tx_modulation_enable(dev, false);867cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, TXCLK_TCD);868869state->tx_params.shutdown = true;870871mutex_unlock(&state->tx_params_lock);872return 0;873}874875static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,876struct v4l2_subdev_ir_parameters *p)877{878struct cx23888_ir_state *state = to_state(sd);879struct cx23885_dev *dev = state->dev;880struct v4l2_subdev_ir_parameters *o = &state->tx_params;881u16 txclk_divider;882883if (p->shutdown)884return cx23888_ir_tx_shutdown(sd);885886if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)887return -ENOSYS;888889mutex_lock(&state->tx_params_lock);890891o->shutdown = p->shutdown;892893o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;894895o->bytes_per_data_element = p->bytes_per_data_element896= sizeof(union cx23888_ir_fifo_rec);897898/* Before we tweak the hardware, we have to disable the transmitter */899irqenable_tx(dev, 0);900control_tx_enable(dev, false);901902control_tx_modulation_enable(dev, p->modulation);903o->modulation = p->modulation;904905if (p->modulation) {906p->carrier_freq = txclk_tx_s_carrier(dev, p->carrier_freq,907&txclk_divider);908o->carrier_freq = p->carrier_freq;909910p->duty_cycle = cduty_tx_s_duty_cycle(dev, p->duty_cycle);911o->duty_cycle = p->duty_cycle;912913p->max_pulse_width =914(u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);915} else {916p->max_pulse_width =917txclk_tx_s_max_pulse_width(dev, p->max_pulse_width,918&txclk_divider);919}920o->max_pulse_width = p->max_pulse_width;921atomic_set(&state->txclk_divider, txclk_divider);922923p->resolution = clock_divider_to_resolution(txclk_divider);924o->resolution = p->resolution;925926/* FIXME - make this dependent on resolution for better performance */927control_tx_irq_watermark(dev, TX_FIFO_HALF_EMPTY);928929control_tx_polarity_invert(dev, p->invert_carrier_sense);930o->invert_carrier_sense = p->invert_carrier_sense;931932control_tx_level_invert(dev, p->invert_level);933o->invert_level = p->invert_level;934935o->interrupt_enable = p->interrupt_enable;936o->enable = p->enable;937if (p->enable) {938if (p->interrupt_enable)939irqenable_tx(dev, IRQEN_TSE);940control_tx_enable(dev, p->enable);941}942943mutex_unlock(&state->tx_params_lock);944return 0;945}946947948/*949* V4L2 Subdevice Core Ops950*/951static int cx23888_ir_log_status(struct v4l2_subdev *sd)952{953struct cx23888_ir_state *state = to_state(sd);954struct cx23885_dev *dev = state->dev;955char *s;956int i, j;957958u32 cntrl = cx23888_ir_read4(dev, CX23888_IR_CNTRL_REG);959u32 txclk = cx23888_ir_read4(dev, CX23888_IR_TXCLK_REG) & TXCLK_TCD;960u32 rxclk = cx23888_ir_read4(dev, CX23888_IR_RXCLK_REG) & RXCLK_RCD;961u32 cduty = cx23888_ir_read4(dev, CX23888_IR_CDUTY_REG) & CDUTY_CDC;962u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);963u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);964u32 filtr = cx23888_ir_read4(dev, CX23888_IR_FILTR_REG) & FILTR_LPF;965966v4l2_info(sd, "IR Receiver:\n");967v4l2_info(sd, "\tEnabled: %s\n",968cntrl & CNTRL_RXE ? "yes" : "no");969v4l2_info(sd, "\tDemodulation from a carrier: %s\n",970cntrl & CNTRL_DMD ? "enabled" : "disabled");971v4l2_info(sd, "\tFIFO: %s\n",972cntrl & CNTRL_RFE ? "enabled" : "disabled");973switch (cntrl & CNTRL_EDG) {974case CNTRL_EDG_NONE:975s = "disabled";976break;977case CNTRL_EDG_FALL:978s = "falling edge";979break;980case CNTRL_EDG_RISE:981s = "rising edge";982break;983case CNTRL_EDG_BOTH:984s = "rising & falling edges";985break;986default:987s = "??? edge";988break;989}990v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);991v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",992cntrl & CNTRL_R ? "not loaded" : "overflow marker");993v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",994cntrl & CNTRL_RIC ? "not empty" : "half full or greater");995v4l2_info(sd, "\tLoopback mode: %s\n",996cntrl & CNTRL_LBM ? "loopback active" : "normal receive");997if (cntrl & CNTRL_DMD) {998v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",999clock_divider_to_carrier_freq(rxclk));1000switch (cntrl & CNTRL_WIN) {1001case CNTRL_WIN_3_3:1002i = 3;1003j = 3;1004break;1005case CNTRL_WIN_4_3:1006i = 4;1007j = 3;1008break;1009case CNTRL_WIN_3_4:1010i = 3;1011j = 4;1012break;1013case CNTRL_WIN_4_4:1014i = 4;1015j = 4;1016break;1017default:1018i = 0;1019j = 0;1020break;1021}1022v4l2_info(sd, "\tNext carrier edge window: 16 clocks "1023"-%1d/+%1d, %u to %u Hz\n", i, j,1024clock_divider_to_freq(rxclk, 16 + j),1025clock_divider_to_freq(rxclk, 16 - i));1026}1027v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",1028pulse_width_count_to_us(FIFO_RXTX, rxclk),1029pulse_width_count_to_ns(FIFO_RXTX, rxclk));1030v4l2_info(sd, "\tLow pass filter: %s\n",1031filtr ? "enabled" : "disabled");1032if (filtr)1033v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, "1034"%u ns\n",1035lpf_count_to_us(filtr),1036lpf_count_to_ns(filtr));1037v4l2_info(sd, "\tPulse width timer timed-out: %s\n",1038stats & STATS_RTO ? "yes" : "no");1039v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",1040irqen & IRQEN_RTE ? "enabled" : "disabled");1041v4l2_info(sd, "\tFIFO overrun: %s\n",1042stats & STATS_ROR ? "yes" : "no");1043v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",1044irqen & IRQEN_ROE ? "enabled" : "disabled");1045v4l2_info(sd, "\tBusy: %s\n",1046stats & STATS_RBY ? "yes" : "no");1047v4l2_info(sd, "\tFIFO service requested: %s\n",1048stats & STATS_RSR ? "yes" : "no");1049v4l2_info(sd, "\tFIFO service request interrupt: %s\n",1050irqen & IRQEN_RSE ? "enabled" : "disabled");10511052v4l2_info(sd, "IR Transmitter:\n");1053v4l2_info(sd, "\tEnabled: %s\n",1054cntrl & CNTRL_TXE ? "yes" : "no");1055v4l2_info(sd, "\tModulation onto a carrier: %s\n",1056cntrl & CNTRL_MOD ? "enabled" : "disabled");1057v4l2_info(sd, "\tFIFO: %s\n",1058cntrl & CNTRL_TFE ? "enabled" : "disabled");1059v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",1060cntrl & CNTRL_TIC ? "not empty" : "half full or less");1061v4l2_info(sd, "\tOutput pin level inversion %s\n",1062cntrl & CNTRL_IVO ? "yes" : "no");1063v4l2_info(sd, "\tCarrier polarity: %s\n",1064cntrl & CNTRL_CPL ? "space:burst mark:noburst"1065: "space:noburst mark:burst");1066if (cntrl & CNTRL_MOD) {1067v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",1068clock_divider_to_carrier_freq(txclk));1069v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",1070cduty + 1);1071}1072v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",1073pulse_width_count_to_us(FIFO_RXTX, txclk),1074pulse_width_count_to_ns(FIFO_RXTX, txclk));1075v4l2_info(sd, "\tBusy: %s\n",1076stats & STATS_TBY ? "yes" : "no");1077v4l2_info(sd, "\tFIFO service requested: %s\n",1078stats & STATS_TSR ? "yes" : "no");1079v4l2_info(sd, "\tFIFO service request interrupt: %s\n",1080irqen & IRQEN_TSE ? "enabled" : "disabled");10811082return 0;1083}10841085static inline int cx23888_ir_dbg_match(const struct v4l2_dbg_match *match)1086{1087return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 2;1088}10891090static int cx23888_ir_g_chip_ident(struct v4l2_subdev *sd,1091struct v4l2_dbg_chip_ident *chip)1092{1093struct cx23888_ir_state *state = to_state(sd);10941095if (cx23888_ir_dbg_match(&chip->match)) {1096chip->ident = state->id;1097chip->revision = state->rev;1098}1099return 0;1100}11011102#ifdef CONFIG_VIDEO_ADV_DEBUG1103static int cx23888_ir_g_register(struct v4l2_subdev *sd,1104struct v4l2_dbg_register *reg)1105{1106struct cx23888_ir_state *state = to_state(sd);1107u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;11081109if (!cx23888_ir_dbg_match(®->match))1110return -EINVAL;1111if ((addr & 0x3) != 0)1112return -EINVAL;1113if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)1114return -EINVAL;1115if (!capable(CAP_SYS_ADMIN))1116return -EPERM;1117reg->size = 4;1118reg->val = cx23888_ir_read4(state->dev, addr);1119return 0;1120}11211122static int cx23888_ir_s_register(struct v4l2_subdev *sd,1123struct v4l2_dbg_register *reg)1124{1125struct cx23888_ir_state *state = to_state(sd);1126u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;11271128if (!cx23888_ir_dbg_match(®->match))1129return -EINVAL;1130if ((addr & 0x3) != 0)1131return -EINVAL;1132if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)1133return -EINVAL;1134if (!capable(CAP_SYS_ADMIN))1135return -EPERM;1136cx23888_ir_write4(state->dev, addr, reg->val);1137return 0;1138}1139#endif11401141static const struct v4l2_subdev_core_ops cx23888_ir_core_ops = {1142.g_chip_ident = cx23888_ir_g_chip_ident,1143.log_status = cx23888_ir_log_status,1144#ifdef CONFIG_VIDEO_ADV_DEBUG1145.g_register = cx23888_ir_g_register,1146.s_register = cx23888_ir_s_register,1147#endif1148.interrupt_service_routine = cx23888_ir_irq_handler,1149};11501151static const struct v4l2_subdev_ir_ops cx23888_ir_ir_ops = {1152.rx_read = cx23888_ir_rx_read,1153.rx_g_parameters = cx23888_ir_rx_g_parameters,1154.rx_s_parameters = cx23888_ir_rx_s_parameters,11551156.tx_write = cx23888_ir_tx_write,1157.tx_g_parameters = cx23888_ir_tx_g_parameters,1158.tx_s_parameters = cx23888_ir_tx_s_parameters,1159};11601161static const struct v4l2_subdev_ops cx23888_ir_controller_ops = {1162.core = &cx23888_ir_core_ops,1163.ir = &cx23888_ir_ir_ops,1164};11651166static const struct v4l2_subdev_ir_parameters default_rx_params = {1167.bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),1168.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,11691170.enable = false,1171.interrupt_enable = false,1172.shutdown = true,11731174.modulation = true,1175.carrier_freq = 36000, /* 36 kHz - RC-5, RC-6, and RC-6A carrier */11761177/* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */1178/* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */1179.noise_filter_min_width = 333333, /* ns */1180.carrier_range_lower = 35000,1181.carrier_range_upper = 37000,1182.invert_level = false,1183};11841185static const struct v4l2_subdev_ir_parameters default_tx_params = {1186.bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),1187.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,11881189.enable = false,1190.interrupt_enable = false,1191.shutdown = true,11921193.modulation = true,1194.carrier_freq = 36000, /* 36 kHz - RC-5 carrier */1195.duty_cycle = 25, /* 25 % - RC-5 carrier */1196.invert_level = false,1197.invert_carrier_sense = false,1198};11991200int cx23888_ir_probe(struct cx23885_dev *dev)1201{1202struct cx23888_ir_state *state;1203struct v4l2_subdev *sd;1204struct v4l2_subdev_ir_parameters default_params;1205int ret;12061207state = kzalloc(sizeof(struct cx23888_ir_state), GFP_KERNEL);1208if (state == NULL)1209return -ENOMEM;12101211spin_lock_init(&state->rx_kfifo_lock);1212if (kfifo_alloc(&state->rx_kfifo, CX23888_IR_RX_KFIFO_SIZE, GFP_KERNEL))1213return -ENOMEM;12141215state->dev = dev;1216state->id = V4L2_IDENT_CX23888_IR;1217state->rev = 0;1218sd = &state->sd;12191220v4l2_subdev_init(sd, &cx23888_ir_controller_ops);1221v4l2_set_subdevdata(sd, state);1222/* FIXME - fix the formatting of dev->v4l2_dev.name and use it */1223snprintf(sd->name, sizeof(sd->name), "%s/888-ir", dev->name);1224sd->grp_id = CX23885_HW_888_IR;12251226ret = v4l2_device_register_subdev(&dev->v4l2_dev, sd);1227if (ret == 0) {1228/*1229* Ensure no interrupts arrive from '888 specific conditions,1230* since we ignore them in this driver to have commonality with1231* similar IR controller cores.1232*/1233cx23888_ir_write4(dev, CX23888_IR_IRQEN_REG, 0);12341235mutex_init(&state->rx_params_lock);1236memcpy(&default_params, &default_rx_params,1237sizeof(struct v4l2_subdev_ir_parameters));1238v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);12391240mutex_init(&state->tx_params_lock);1241memcpy(&default_params, &default_tx_params,1242sizeof(struct v4l2_subdev_ir_parameters));1243v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);1244} else {1245kfifo_free(&state->rx_kfifo);1246}1247return ret;1248}12491250int cx23888_ir_remove(struct cx23885_dev *dev)1251{1252struct v4l2_subdev *sd;1253struct cx23888_ir_state *state;12541255sd = cx23885_find_hw(dev, CX23885_HW_888_IR);1256if (sd == NULL)1257return -ENODEV;12581259cx23888_ir_rx_shutdown(sd);1260cx23888_ir_tx_shutdown(sd);12611262state = to_state(sd);1263v4l2_device_unregister_subdev(sd);1264kfifo_free(&state->rx_kfifo);1265kfree(state);1266/* Nothing more to free() as state held the actual v4l2_subdev object */1267return 0;1268}126912701271