/drivers/media/rc/ite-cir.c
C | 1734 lines | 1074 code | 358 blank | 302 comment | 95 complexity | 7a2bbedcdedb0fab76096085bba04287 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
- /*
- * Driver for ITE Tech Inc. IT8712F/IT8512 CIR
- *
- * Copyright (C) 2010 Juan Jesús García de Soria <skandalfo@gmail.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA.
- *
- * Inspired by the original lirc_it87 and lirc_ite8709 drivers, on top of the
- * skeleton provided by the nuvoton-cir driver.
- *
- * The lirc_it87 driver was originally written by Hans-Gunter Lutke Uphues
- * <hg_lu@web.de> in 2001, with enhancements by Christoph Bartelmus
- * <lirc@bartelmus.de>, Andrew Calkin <r_tay@hotmail.com> and James Edwards
- * <jimbo-lirc@edwardsclan.net>.
- *
- * The lirc_ite8709 driver was written by Grégory Lardière
- * <spmf2004-lirc@yahoo.fr> in 2008.
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/pnp.h>
- #include <linux/io.h>
- #include <linux/interrupt.h>
- #include <linux/sched.h>
- #include <linux/delay.h>
- #include <linux/slab.h>
- #include <linux/input.h>
- #include <linux/bitops.h>
- #include <media/rc-core.h>
- #include <linux/pci_ids.h>
- #include <linux/delay.h>
- #include "ite-cir.h"
- /* module parameters */
- /* debug level */
- static int debug;
- module_param(debug, int, S_IRUGO | S_IWUSR);
- MODULE_PARM_DESC(debug, "Enable debugging output");
- /* low limit for RX carrier freq, Hz, 0 for no RX demodulation */
- static int rx_low_carrier_freq;
- module_param(rx_low_carrier_freq, int, S_IRUGO | S_IWUSR);
- MODULE_PARM_DESC(rx_low_carrier_freq, "Override low RX carrier frequency, Hz, "
- "0 for no RX demodulation");
- /* high limit for RX carrier freq, Hz, 0 for no RX demodulation */
- static int rx_high_carrier_freq;
- module_param(rx_high_carrier_freq, int, S_IRUGO | S_IWUSR);
- MODULE_PARM_DESC(rx_high_carrier_freq, "Override high RX carrier frequency, "
- "Hz, 0 for no RX demodulation");
- /* override tx carrier frequency */
- static int tx_carrier_freq;
- module_param(tx_carrier_freq, int, S_IRUGO | S_IWUSR);
- MODULE_PARM_DESC(tx_carrier_freq, "Override TX carrier frequency, Hz");
- /* override tx duty cycle */
- static int tx_duty_cycle;
- module_param(tx_duty_cycle, int, S_IRUGO | S_IWUSR);
- MODULE_PARM_DESC(tx_duty_cycle, "Override TX duty cycle, 1-100");
- /* override default sample period */
- static long sample_period;
- module_param(sample_period, long, S_IRUGO | S_IWUSR);
- MODULE_PARM_DESC(sample_period, "Override carrier sample period, us");
- /* override detected model id */
- static int model_number = -1;
- module_param(model_number, int, S_IRUGO | S_IWUSR);
- MODULE_PARM_DESC(model_number, "Use this model number, don't autodetect");
- /* HW-independent code functions */
- /* check whether carrier frequency is high frequency */
- static inline bool ite_is_high_carrier_freq(unsigned int freq)
- {
- return freq >= ITE_HCF_MIN_CARRIER_FREQ;
- }
- /* get the bits required to program the carrier frequency in CFQ bits,
- * unshifted */
- static u8 ite_get_carrier_freq_bits(unsigned int freq)
- {
- if (ite_is_high_carrier_freq(freq)) {
- if (freq < 425000)
- return ITE_CFQ_400;
- else if (freq < 465000)
- return ITE_CFQ_450;
- else if (freq < 490000)
- return ITE_CFQ_480;
- else
- return ITE_CFQ_500;
- } else {
- /* trim to limits */
- if (freq < ITE_LCF_MIN_CARRIER_FREQ)
- freq = ITE_LCF_MIN_CARRIER_FREQ;
- if (freq > ITE_LCF_MAX_CARRIER_FREQ)
- freq = ITE_LCF_MAX_CARRIER_FREQ;
- /* convert to kHz and subtract the base freq */
- freq =
- DIV_ROUND_CLOSEST(freq - ITE_LCF_MIN_CARRIER_FREQ,
- 1000);
- return (u8) freq;
- }
- }
- /* get the bits required to program the pulse with in TXMPW */
- static u8 ite_get_pulse_width_bits(unsigned int freq, int duty_cycle)
- {
- unsigned long period_ns, on_ns;
- /* sanitize freq into range */
- if (freq < ITE_LCF_MIN_CARRIER_FREQ)
- freq = ITE_LCF_MIN_CARRIER_FREQ;
- if (freq > ITE_HCF_MAX_CARRIER_FREQ)
- freq = ITE_HCF_MAX_CARRIER_FREQ;
- period_ns = 1000000000UL / freq;
- on_ns = period_ns * duty_cycle / 100;
- if (ite_is_high_carrier_freq(freq)) {
- if (on_ns < 750)
- return ITE_TXMPW_A;
- else if (on_ns < 850)
- return ITE_TXMPW_B;
- else if (on_ns < 950)
- return ITE_TXMPW_C;
- else if (on_ns < 1080)
- return ITE_TXMPW_D;
- else
- return ITE_TXMPW_E;
- } else {
- if (on_ns < 6500)
- return ITE_TXMPW_A;
- else if (on_ns < 7850)
- return ITE_TXMPW_B;
- else if (on_ns < 9650)
- return ITE_TXMPW_C;
- else if (on_ns < 11950)
- return ITE_TXMPW_D;
- else
- return ITE_TXMPW_E;
- }
- }
- /* decode raw bytes as received by the hardware, and push them to the ir-core
- * layer */
- static void ite_decode_bytes(struct ite_dev *dev, const u8 * data, int
- length)
- {
- u32 sample_period;
- unsigned long *ldata;
- unsigned int next_one, next_zero, size;
- DEFINE_IR_RAW_EVENT(ev);
- if (length == 0)
- return;
- sample_period = dev->params.sample_period;
- ldata = (unsigned long *)data;
- size = length << 3;
- next_one = find_next_bit_le(ldata, size, 0);
- if (next_one > 0) {
- ev.pulse = true;
- ev.duration =
- ITE_BITS_TO_NS(next_one, sample_period);
- ir_raw_event_store_with_filter(dev->rdev, &ev);
- }
- while (next_one < size) {
- next_zero = find_next_zero_bit_le(ldata, size, next_one + 1);
- ev.pulse = false;
- ev.duration = ITE_BITS_TO_NS(next_zero - next_one, sample_period);
- ir_raw_event_store_with_filter(dev->rdev, &ev);
- if (next_zero < size) {
- next_one =
- find_next_bit_le(ldata,
- size,
- next_zero + 1);
- ev.pulse = true;
- ev.duration =
- ITE_BITS_TO_NS(next_one - next_zero,
- sample_period);
- ir_raw_event_store_with_filter
- (dev->rdev, &ev);
- } else
- next_one = size;
- }
- ir_raw_event_handle(dev->rdev);
- ite_dbg_verbose("decoded %d bytes.", length);
- }
- /* set all the rx/tx carrier parameters; this must be called with the device
- * spinlock held */
- static void ite_set_carrier_params(struct ite_dev *dev)
- {
- unsigned int freq, low_freq, high_freq;
- int allowance;
- bool use_demodulator;
- bool for_tx = dev->transmitting;
- ite_dbg("%s called", __func__);
- if (for_tx) {
- /* we don't need no stinking calculations */
- freq = dev->params.tx_carrier_freq;
- allowance = ITE_RXDCR_DEFAULT;
- use_demodulator = false;
- } else {
- low_freq = dev->params.rx_low_carrier_freq;
- high_freq = dev->params.rx_high_carrier_freq;
- if (low_freq == 0) {
- /* don't demodulate */
-