ene_ir.c 30.9 KB
Newer Older
1
/*
M
Maxim Levitsky 已提交
2
 * driver for ENE KB3926 B/C/D/E/F CIR (pnp id: ENE0XXX)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Copyright (C) 2010 Maxim Levitsky <maximlevitsky@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
M
Maxim Levitsky 已提交
20 21 22 23 24 25 26 27 28 29 30
 *
 * Special thanks to:
 *   Sami R. <maesesami@gmail.com> for lot of help in debugging and therefore
 *    bringing to life support for transmission & learning mode.
 *
 *   Charlie Andrews <charliethepilot@googlemail.com> for lots of help in
 *   bringing up the support of new firmware buffer that is popular
 *   on latest notebooks
 *
 *   ENE for partial device documentation
 *
31 32 33 34 35 36 37 38
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pnp.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
39
#include <linux/slab.h>
40
#include <media/rc-core.h>
41
#include "ene_ir.h"
42

M
Maxim Levitsky 已提交
43
static int sample_period;
44
static bool learning_mode_force;
45
static int debug;
M
Maxim Levitsky 已提交
46
static bool txsim;
47

M
Maxim Levitsky 已提交
48 49 50 51 52
static void ene_set_reg_addr(struct ene_device *dev, u16 reg)
{
	outb(reg >> 8, dev->hw_io + ENE_ADDR_HI);
	outb(reg & 0xFF, dev->hw_io + ENE_ADDR_LO);
}
53 54

/* read a hardware register */
M
Maxim Levitsky 已提交
55
static u8 ene_read_reg(struct ene_device *dev, u16 reg)
56 57
{
	u8 retval;
M
Maxim Levitsky 已提交
58
	ene_set_reg_addr(dev, reg);
59
	retval = inb(dev->hw_io + ENE_IO);
M
Maxim Levitsky 已提交
60
	dbg_regs("reg %04x == %02x", reg, retval);
61 62 63 64
	return retval;
}

/* write a hardware register */
M
Maxim Levitsky 已提交
65
static void ene_write_reg(struct ene_device *dev, u16 reg, u8 value)
66
{
M
Maxim Levitsky 已提交
67 68
	dbg_regs("reg %04x <- %02x", reg, value);
	ene_set_reg_addr(dev, reg);
69 70 71
	outb(value, dev->hw_io + ENE_IO);
}

M
Maxim Levitsky 已提交
72 73
/* Set bits in hardware register */
static void ene_set_reg_mask(struct ene_device *dev, u16 reg, u8 mask)
74
{
M
Maxim Levitsky 已提交
75 76 77 78
	dbg_regs("reg %04x |= %02x", reg, mask);
	ene_set_reg_addr(dev, reg);
	outb(inb(dev->hw_io + ENE_IO) | mask, dev->hw_io + ENE_IO);
}
79

M
Maxim Levitsky 已提交
80 81 82 83 84 85 86
/* Clear bits in hardware register */
static void ene_clear_reg_mask(struct ene_device *dev, u16 reg, u8 mask)
{
	dbg_regs("reg %04x &= ~%02x ", reg, mask);
	ene_set_reg_addr(dev, reg);
	outb(inb(dev->hw_io + ENE_IO) & ~mask, dev->hw_io + ENE_IO);
}
87

M
Maxim Levitsky 已提交
88 89 90 91 92 93 94 95
/* A helper to set/clear a bit in register according to boolean variable */
static void ene_set_clear_reg_mask(struct ene_device *dev, u16 reg, u8 mask,
								bool set)
{
	if (set)
		ene_set_reg_mask(dev, reg, mask);
	else
		ene_clear_reg_mask(dev, reg, mask);
96 97 98 99 100 101 102
}

/* detect hardware features */
static int ene_hw_detect(struct ene_device *dev)
{
	u8 chip_major, chip_minor;
	u8 hw_revision, old_ver;
M
Maxim Levitsky 已提交
103
	u8 fw_reg2, fw_reg1;
104

M
Maxim Levitsky 已提交
105 106 107 108
	ene_clear_reg_mask(dev, ENE_ECSTS, ENE_ECSTS_RSRVD);
	chip_major = ene_read_reg(dev, ENE_ECVER_MAJOR);
	chip_minor = ene_read_reg(dev, ENE_ECVER_MINOR);
	ene_set_reg_mask(dev, ENE_ECSTS, ENE_ECSTS_RSRVD);
109

M
Maxim Levitsky 已提交
110 111
	hw_revision = ene_read_reg(dev, ENE_ECHV);
	old_ver = ene_read_reg(dev, ENE_HW_VER_OLD);
112

M
Maxim Levitsky 已提交
113 114
	dev->pll_freq = (ene_read_reg(dev, ENE_PLLFRH) << 4) +
		(ene_read_reg(dev, ENE_PLLFRL) >> 4);
115

M
Maxim Levitsky 已提交
116 117 118
	if (sample_period != ENE_DEFAULT_SAMPLE_PERIOD)
		dev->rx_period_adjust =
			dev->pll_freq == ENE_DEFAULT_PLL_FREQ ? 2 : 4;
119

120
	if (hw_revision == 0xFF) {
M
Maxim Levitsky 已提交
121 122 123
		ene_warn("device seems to be disabled");
		ene_warn("send a mail to lirc-list@lists.sourceforge.net");
		ene_warn("please attach output of acpidump and dmidecode");
124 125 126
		return -ENODEV;
	}

M
Maxim Levitsky 已提交
127 128 129 130 131
	ene_notice("chip is 0x%02x%02x - kbver = 0x%02x, rev = 0x%02x",
		chip_major, chip_minor, old_ver, hw_revision);

	ene_notice("PLL freq = %d", dev->pll_freq);

132
	if (chip_major == 0x33) {
M
Maxim Levitsky 已提交
133
		ene_warn("chips 0x33xx aren't supported");
134 135 136 137 138
		return -ENODEV;
	}

	if (chip_major == 0x39 && chip_minor == 0x26 && hw_revision == 0xC0) {
		dev->hw_revision = ENE_HW_C;
M
Maxim Levitsky 已提交
139
		ene_notice("KB3926C detected");
140 141
	} else if (old_ver == 0x24 && hw_revision == 0xC0) {
		dev->hw_revision = ENE_HW_B;
M
Maxim Levitsky 已提交
142
		ene_notice("KB3926B detected");
143 144
	} else {
		dev->hw_revision = ENE_HW_D;
M
Maxim Levitsky 已提交
145
		ene_notice("KB3926D or higher detected");
146 147 148 149 150 151
	}

	/* detect features hardware supports */
	if (dev->hw_revision < ENE_HW_C)
		return 0;

M
Maxim Levitsky 已提交
152 153
	fw_reg1 = ene_read_reg(dev, ENE_FW1);
	fw_reg2 = ene_read_reg(dev, ENE_FW2);
154

M
Maxim Levitsky 已提交
155
	ene_notice("Firmware regs: %02x %02x", fw_reg1, fw_reg2);
156

M
Maxim Levitsky 已提交
157 158 159 160 161 162
	dev->hw_use_gpio_0a = !!(fw_reg2 & ENE_FW2_GP0A);
	dev->hw_learning_and_tx_capable = !!(fw_reg2 & ENE_FW2_LEARNING);
	dev->hw_extra_buffer = !!(fw_reg1 & ENE_FW1_HAS_EXTRA_BUF);

	if (dev->hw_learning_and_tx_capable)
		dev->hw_fan_input = !!(fw_reg2 & ENE_FW2_FAN_INPUT);
163

M
Maxim Levitsky 已提交
164
	ene_notice("Hardware features:");
165 166

	if (dev->hw_learning_and_tx_capable) {
M
Maxim Levitsky 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179
		ene_notice("* Supports transmitting & learning mode");
		ene_notice("   This feature is rare and therefore,");
		ene_notice("   you are welcome to test it,");
		ene_notice("   and/or contact the author via:");
		ene_notice("   lirc-list@lists.sourceforge.net");
		ene_notice("   or maximlevitsky@gmail.com");

		ene_notice("* Uses GPIO %s for IR raw input",
			dev->hw_use_gpio_0a ? "40" : "0A");

		if (dev->hw_fan_input)
			ene_notice("* Uses unused fan feedback input as source"
					" of demodulated IR data");
180
	}
M
Maxim Levitsky 已提交
181 182 183 184 185 186 187

	if (!dev->hw_fan_input)
		ene_notice("* Uses GPIO %s for IR demodulated input",
			dev->hw_use_gpio_0a ? "0A" : "40");

	if (dev->hw_extra_buffer)
		ene_notice("* Uses new style input buffer");
188 189 190
	return 0;
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
/* Read properities of hw sample buffer */
static void ene_rx_setup_hw_buffer(struct ene_device *dev)
{
	u16 tmp;

	ene_rx_read_hw_pointer(dev);
	dev->r_pointer = dev->w_pointer;

	if (!dev->hw_extra_buffer) {
		dev->buffer_len = ENE_FW_PACKET_SIZE * 2;
		return;
	}

	tmp = ene_read_reg(dev, ENE_FW_SAMPLE_BUFFER);
	tmp |= ene_read_reg(dev, ENE_FW_SAMPLE_BUFFER+1) << 8;
	dev->extra_buf1_address = tmp;

	dev->extra_buf1_len = ene_read_reg(dev, ENE_FW_SAMPLE_BUFFER + 2);

	tmp = ene_read_reg(dev, ENE_FW_SAMPLE_BUFFER + 3);
	tmp |= ene_read_reg(dev, ENE_FW_SAMPLE_BUFFER + 4) << 8;
	dev->extra_buf2_address = tmp;

	dev->extra_buf2_len = ene_read_reg(dev, ENE_FW_SAMPLE_BUFFER + 5);

	dev->buffer_len = dev->extra_buf1_len + dev->extra_buf2_len + 8;

	ene_notice("Hardware uses 2 extended buffers:");
	ene_notice("  0x%04x - len : %d", dev->extra_buf1_address,
						dev->extra_buf1_len);
	ene_notice("  0x%04x - len : %d", dev->extra_buf2_address,
						dev->extra_buf2_len);

	ene_notice("Total buffer len = %d", dev->buffer_len);

	if (dev->buffer_len > 64 || dev->buffer_len < 16)
		goto error;

	if (dev->extra_buf1_address > 0xFBFC ||
					dev->extra_buf1_address < 0xEC00)
		goto error;

	if (dev->extra_buf2_address > 0xFBFC ||
					dev->extra_buf2_address < 0xEC00)
		goto error;

	if (dev->r_pointer > dev->buffer_len)
		goto error;

	ene_set_reg_mask(dev, ENE_FW1, ENE_FW1_EXTRA_BUF_HND);
	return;
error:
	ene_warn("Error validating extra buffers, device probably won't work");
	dev->hw_extra_buffer = false;
	ene_clear_reg_mask(dev, ENE_FW1, ENE_FW1_EXTRA_BUF_HND);
}


/* Restore the pointers to extra buffers - to make module reload work*/
static void ene_rx_restore_hw_buffer(struct ene_device *dev)
{
	if (!dev->hw_extra_buffer)
		return;

	ene_write_reg(dev, ENE_FW_SAMPLE_BUFFER + 0,
				dev->extra_buf1_address & 0xFF);
	ene_write_reg(dev, ENE_FW_SAMPLE_BUFFER + 1,
				dev->extra_buf1_address >> 8);
	ene_write_reg(dev, ENE_FW_SAMPLE_BUFFER + 2, dev->extra_buf1_len);

	ene_write_reg(dev, ENE_FW_SAMPLE_BUFFER + 3,
				dev->extra_buf2_address & 0xFF);
	ene_write_reg(dev, ENE_FW_SAMPLE_BUFFER + 4,
				dev->extra_buf2_address >> 8);
	ene_write_reg(dev, ENE_FW_SAMPLE_BUFFER + 5,
				dev->extra_buf2_len);
	ene_clear_reg_mask(dev, ENE_FW1, ENE_FW1_EXTRA_BUF_HND);
}

/* Read hardware write pointer */
static void ene_rx_read_hw_pointer(struct ene_device *dev)
{
	if (dev->hw_extra_buffer)
		dev->w_pointer = ene_read_reg(dev, ENE_FW_RX_POINTER);
	else
		dev->w_pointer = ene_read_reg(dev, ENE_FW2)
			& ENE_FW2_BUF_WPTR ? 0 : ENE_FW_PACKET_SIZE;

	dbg_verbose("RB: HW write pointer: %02x, driver read pointer: %02x",
		dev->w_pointer, dev->r_pointer);
}

/* Gets address of next sample from HW ring buffer */
static int ene_rx_get_sample_reg(struct ene_device *dev)
{
	int r_pointer;

	if (dev->r_pointer == dev->w_pointer) {
		dbg_verbose("RB: hit end, try update w_pointer");
		ene_rx_read_hw_pointer(dev);
	}

	if (dev->r_pointer == dev->w_pointer) {
		dbg_verbose("RB: end of data at %d", dev->r_pointer);
		return 0;
	}

	dbg_verbose("RB: reading at offset %d", dev->r_pointer);
	r_pointer = dev->r_pointer;

	dev->r_pointer++;
	if (dev->r_pointer == dev->buffer_len)
		dev->r_pointer = 0;

	dbg_verbose("RB: next read will be from offset %d", dev->r_pointer);

	if (r_pointer < 8) {
		dbg_verbose("RB: read at main buffer at %d", r_pointer);
		return ENE_FW_SAMPLE_BUFFER + r_pointer;
	}

	r_pointer -= 8;

	if (r_pointer < dev->extra_buf1_len) {
		dbg_verbose("RB: read at 1st extra buffer at %d", r_pointer);
		return dev->extra_buf1_address + r_pointer;
	}

	r_pointer -= dev->extra_buf1_len;

	if (r_pointer < dev->extra_buf2_len) {
		dbg_verbose("RB: read at 2nd extra buffer at %d", r_pointer);
		return dev->extra_buf2_address + r_pointer;
	}

	dbg("attempt to read beyong ring bufer end");
	return 0;
}

M
Maxim Levitsky 已提交
330 331
/* Sense current received carrier */
void ene_rx_sense_carrier(struct ene_device *dev)
332
{
333 334 335
	DEFINE_IR_RAW_EVENT(ev);

	int carrier, duty_cycle;
M
Maxim Levitsky 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	int period = ene_read_reg(dev, ENE_CIRCAR_PRD);
	int hperiod = ene_read_reg(dev, ENE_CIRCAR_HPRD);

	if (!(period & ENE_CIRCAR_PRD_VALID))
		return;

	period &= ~ENE_CIRCAR_PRD_VALID;

	if (!period)
		return;

	dbg("RX: hardware carrier period = %02x", period);
	dbg("RX: hardware carrier pulse period = %02x", hperiod);

	carrier = 2000000 / period;
	duty_cycle = (hperiod * 100) / period;
	dbg("RX: sensed carrier = %d Hz, duty cycle %d%%",
353 354 355 356 357
						carrier, duty_cycle);
	if (dev->carrier_detect_enabled) {
		ev.carrier_report = true;
		ev.carrier = carrier;
		ev.duty_cycle = duty_cycle;
358
		ir_raw_event_store(dev->rdev, &ev);
359
	}
360 361
}

M
Maxim Levitsky 已提交
362
/* this enables/disables the CIR RX engine */
363
static void ene_rx_enable_cir_engine(struct ene_device *dev, bool enable)
364
{
M
Maxim Levitsky 已提交
365 366
	ene_set_clear_reg_mask(dev, ENE_CIRCFG,
			ENE_CIRCFG_RX_EN | ENE_CIRCFG_RX_IRQ, enable);
367 368
}

M
Maxim Levitsky 已提交
369
/* this selects input for CIR engine. Ether GPIO 0A or GPIO40*/
370
static void ene_rx_select_input(struct ene_device *dev, bool gpio_0a)
371
{
M
Maxim Levitsky 已提交
372
	ene_set_clear_reg_mask(dev, ENE_CIRCFG2, ENE_CIRCFG2_GPIO0A, gpio_0a);
373 374
}

M
Maxim Levitsky 已提交
375 376 377 378
/*
 * this enables alternative input via fan tachometer sensor and bypasses
 * the hw CIR engine
 */
379
static void ene_rx_enable_fan_input(struct ene_device *dev, bool enable)
380
{
M
Maxim Levitsky 已提交
381 382
	if (!dev->hw_fan_input)
		return;
383

M
Maxim Levitsky 已提交
384 385 386 387 388 389
	if (!enable)
		ene_write_reg(dev, ENE_FAN_AS_IN1, 0);
	else {
		ene_write_reg(dev, ENE_FAN_AS_IN1, ENE_FAN_AS_IN1_EN);
		ene_write_reg(dev, ENE_FAN_AS_IN2, ENE_FAN_AS_IN2_EN);
	}
390 391
}

M
Maxim Levitsky 已提交
392 393
/* setup the receiver for RX*/
static void ene_rx_setup(struct ene_device *dev)
394
{
395
	bool learning_mode = dev->learning_mode_enabled ||
M
Maxim Levitsky 已提交
396 397
					dev->carrier_detect_enabled;
	int sample_period_adjust = 0;
398

399 400 401
	dbg("RX: setup receiver, learning mode = %d", learning_mode);


M
Maxim Levitsky 已提交
402 403
	/* This selects RLC input and clears CFG2 settings */
	ene_write_reg(dev, ENE_CIRCFG2, 0x00);
404

M
Maxim Levitsky 已提交
405 406 407 408
	/* set sample period*/
	if (sample_period == ENE_DEFAULT_SAMPLE_PERIOD)
		sample_period_adjust =
			dev->pll_freq == ENE_DEFAULT_PLL_FREQ ? 1 : 2;
409

M
Maxim Levitsky 已提交
410 411 412 413 414 415
	ene_write_reg(dev, ENE_CIRRLC_CFG,
			(sample_period + sample_period_adjust) |
						ENE_CIRRLC_CFG_OVERFLOW);
	/* revB doesn't support inputs */
	if (dev->hw_revision < ENE_HW_C)
		goto select_timeout;
416

M
Maxim Levitsky 已提交
417 418 419
	if (learning_mode) {

		WARN_ON(!dev->hw_learning_and_tx_capable);
420

M
Maxim Levitsky 已提交
421 422 423 424 425
		/* Enable the opposite of the normal input
		That means that if GPIO40 is normally used, use GPIO0A
		and vice versa.
		This input will carry non demodulated
		signal, and we will tell the hw to demodulate it itself */
426
		ene_rx_select_input(dev, !dev->hw_use_gpio_0a);
M
Maxim Levitsky 已提交
427
		dev->rx_fan_input_inuse = false;
428

M
Maxim Levitsky 已提交
429 430
		/* Enable carrier demodulation */
		ene_set_reg_mask(dev, ENE_CIRCFG, ENE_CIRCFG_CARR_DEMOD);
431

M
Maxim Levitsky 已提交
432
		/* Enable carrier detection */
M
Maxim Levitsky 已提交
433
		ene_write_reg(dev, ENE_CIRCAR_PULS, 0x63);
M
Maxim Levitsky 已提交
434 435
		ene_set_clear_reg_mask(dev, ENE_CIRCFG2, ENE_CIRCFG2_CARR_DETECT,
			dev->carrier_detect_enabled || debug);
436
	} else {
M
Maxim Levitsky 已提交
437 438 439
		if (dev->hw_fan_input)
			dev->rx_fan_input_inuse = true;
		else
440
			ene_rx_select_input(dev, dev->hw_use_gpio_0a);
M
Maxim Levitsky 已提交
441 442 443 444

		/* Disable carrier detection & demodulation */
		ene_clear_reg_mask(dev, ENE_CIRCFG, ENE_CIRCFG_CARR_DEMOD);
		ene_clear_reg_mask(dev, ENE_CIRCFG2, ENE_CIRCFG2_CARR_DETECT);
445 446
	}

M
Maxim Levitsky 已提交
447
select_timeout:
448
	if (dev->rx_fan_input_inuse) {
449
		dev->rdev->rx_resolution = MS_TO_NS(ENE_FW_SAMPLE_PERIOD_FAN);
450

M
Maxim Levitsky 已提交
451 452
		/* Fan input doesn't support timeouts, it just ends the
			input with a maximum sample */
453
		dev->rdev->min_timeout = dev->rdev->max_timeout =
M
Maxim Levitsky 已提交
454 455
			MS_TO_NS(ENE_FW_SMPL_BUF_FAN_MSK *
				ENE_FW_SAMPLE_PERIOD_FAN);
456
	} else {
457
		dev->rdev->rx_resolution = MS_TO_NS(sample_period);
M
Maxim Levitsky 已提交
458 459 460 461 462 463

		/* Theoreticly timeout is unlimited, but we cap it
		 * because it was seen that on one device, it
		 * would stop sending spaces after around 250 msec.
		 * Besides, this is close to 2^32 anyway and timeout is u32.
		 */
464 465
		dev->rdev->min_timeout = MS_TO_NS(127 * sample_period);
		dev->rdev->max_timeout = MS_TO_NS(200000);
466
	}
M
Maxim Levitsky 已提交
467 468

	if (dev->hw_learning_and_tx_capable)
469
		dev->rdev->tx_resolution = MS_TO_NS(sample_period);
M
Maxim Levitsky 已提交
470

471 472 473 474
	if (dev->rdev->timeout > dev->rdev->max_timeout)
		dev->rdev->timeout = dev->rdev->max_timeout;
	if (dev->rdev->timeout < dev->rdev->min_timeout)
		dev->rdev->timeout = dev->rdev->min_timeout;
475 476 477 478 479 480 481
}

/* Enable the device for receive */
static void ene_rx_enable(struct ene_device *dev)
{
	u8 reg_value;

M
Maxim Levitsky 已提交
482
	/* Enable system interrupt */
483
	if (dev->hw_revision < ENE_HW_C) {
M
Maxim Levitsky 已提交
484 485
		ene_write_reg(dev, ENEB_IRQ, dev->irq << 1);
		ene_write_reg(dev, ENEB_IRQ_UNK1, 0x01);
486
	} else {
M
Maxim Levitsky 已提交
487 488 489 490 491
		reg_value = ene_read_reg(dev, ENE_IRQ) & 0xF0;
		reg_value |= ENE_IRQ_UNK_EN;
		reg_value &= ~ENE_IRQ_STATUS;
		reg_value |= (dev->irq & ENE_IRQ_MASK);
		ene_write_reg(dev, ENE_IRQ, reg_value);
492 493
	}

M
Maxim Levitsky 已提交
494
	/* Enable inputs */
495 496
	ene_rx_enable_fan_input(dev, dev->rx_fan_input_inuse);
	ene_rx_enable_cir_engine(dev, !dev->rx_fan_input_inuse);
497 498 499 500 501

	/* ack any pending irqs - just in case */
	ene_irq_status(dev);

	/* enable firmware bits */
M
Maxim Levitsky 已提交
502
	ene_set_reg_mask(dev, ENE_FW1, ENE_FW1_ENABLE | ENE_FW1_IRQ);
503 504

	/* enter idle mode */
505
	ir_raw_event_set_idle(dev->rdev, true);
M
Maxim Levitsky 已提交
506
	dev->rx_enabled = true;
507 508
}

509
/* Disable the device receiver */
510 511 512
static void ene_rx_disable(struct ene_device *dev)
{
	/* disable inputs */
513 514
	ene_rx_enable_cir_engine(dev, false);
	ene_rx_enable_fan_input(dev, false);
515 516

	/* disable hardware IRQ and firmware flag */
M
Maxim Levitsky 已提交
517
	ene_clear_reg_mask(dev, ENE_FW1, ENE_FW1_ENABLE | ENE_FW1_IRQ);
518

519
	ir_raw_event_set_idle(dev->rdev, true);
M
Maxim Levitsky 已提交
520
	dev->rx_enabled = false;
521 522
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
/* This resets the receiver. Usefull to stop stream of spaces at end of
 * transmission
 */
static void ene_rx_reset(struct ene_device *dev)
{
	ene_clear_reg_mask(dev, ENE_CIRCFG, ENE_CIRCFG_RX_EN);
	ene_set_reg_mask(dev, ENE_CIRCFG, ENE_CIRCFG_RX_EN);
}

/* Set up the TX carrier frequency and duty cycle */
static void ene_tx_set_carrier(struct ene_device *dev)
{
	u8 tx_puls_width;
	unsigned long flags;

	spin_lock_irqsave(&dev->hw_lock, flags);

	ene_set_clear_reg_mask(dev, ENE_CIRCFG,
		ENE_CIRCFG_TX_CARR, dev->tx_period > 0);

	if (!dev->tx_period)
		goto unlock;

	BUG_ON(dev->tx_duty_cycle >= 100 || dev->tx_duty_cycle <= 0);

	tx_puls_width = dev->tx_period / (100 / dev->tx_duty_cycle);

	if (!tx_puls_width)
		tx_puls_width = 1;

	dbg("TX: pulse distance = %d * 500 ns", dev->tx_period);
	dbg("TX: pulse width = %d * 500 ns", tx_puls_width);

	ene_write_reg(dev, ENE_CIRMOD_PRD, dev->tx_period | ENE_CIRMOD_PRD_POL);
	ene_write_reg(dev, ENE_CIRMOD_HPRD, tx_puls_width);
unlock:
	spin_unlock_irqrestore(&dev->hw_lock, flags);
}

/* Enable/disable transmitters */
static void ene_tx_set_transmitters(struct ene_device *dev)
{
	unsigned long flags;

	spin_lock_irqsave(&dev->hw_lock, flags);
	ene_set_clear_reg_mask(dev, ENE_GPIOFS8, ENE_GPIOFS8_GPIO41,
					!!(dev->transmitter_mask & 0x01));
	ene_set_clear_reg_mask(dev, ENE_GPIOFS1, ENE_GPIOFS1_GPIO0D,
					!!(dev->transmitter_mask & 0x02));
	spin_unlock_irqrestore(&dev->hw_lock, flags);
}

575
/* prepare transmission */
576
static void ene_tx_enable(struct ene_device *dev)
577
{
M
Maxim Levitsky 已提交
578 579
	u8 conf1 = ene_read_reg(dev, ENE_CIRCFG);
	u8 fwreg2 = ene_read_reg(dev, ENE_FW2);
580 581 582

	dev->saved_conf1 = conf1;

M
Maxim Levitsky 已提交
583 584 585
	/* Show information about currently connected transmitter jacks */
	if (fwreg2 & ENE_FW2_EMMITER1_CONN)
		dbg("TX: Transmitter #1 is connected");
586

M
Maxim Levitsky 已提交
587 588
	if (fwreg2 & ENE_FW2_EMMITER2_CONN)
		dbg("TX: Transmitter #2 is connected");
589

M
Maxim Levitsky 已提交
590 591 592 593 594 595
	if (!(fwreg2 & (ENE_FW2_EMMITER1_CONN | ENE_FW2_EMMITER2_CONN)))
		ene_warn("TX: transmitter cable isn't connected!");

	/* disable receive on revc */
	if (dev->hw_revision == ENE_HW_C)
		conf1 &= ~ENE_CIRCFG_RX_EN;
596

M
Maxim Levitsky 已提交
597 598 599
	/* Enable TX engine */
	conf1 |= ENE_CIRCFG_TX_EN | ENE_CIRCFG_TX_IRQ;
	ene_write_reg(dev, ENE_CIRCFG, conf1);
600 601 602
}

/* end transmission */
603
static void ene_tx_disable(struct ene_device *dev)
604
{
M
Maxim Levitsky 已提交
605
	ene_write_reg(dev, ENE_CIRCFG, dev->saved_conf1);
606
	dev->tx_buffer = NULL;
607 608 609 610 611 612 613 614
}


/* TX one sample - must be called with dev->hw_lock*/
static void ene_tx_sample(struct ene_device *dev)
{
	u8 raw_tx;
	u32 sample;
M
Maxim Levitsky 已提交
615
	bool pulse = dev->tx_sample_pulse;
616

617
	if (!dev->tx_buffer) {
M
Maxim Levitsky 已提交
618
		ene_warn("TX: BUG: attempt to transmit NULL buffer");
619 620 621 622 623
		return;
	}

	/* Grab next TX sample */
	if (!dev->tx_sample) {
M
Maxim Levitsky 已提交
624 625

		if (dev->tx_pos == dev->tx_len) {
626
			if (!dev->tx_done) {
M
Maxim Levitsky 已提交
627 628
				dbg("TX: no more data to send");
				dev->tx_done = true;
629 630
				goto exit;
			} else {
M
Maxim Levitsky 已提交
631
				dbg("TX: last sample sent by hardware");
632
				ene_tx_disable(dev);
633 634 635 636 637 638 639 640
				complete(&dev->tx_complete);
				return;
			}
		}

		sample = dev->tx_buffer[dev->tx_pos++];
		dev->tx_sample_pulse = !dev->tx_sample_pulse;

M
Maxim Levitsky 已提交
641
		dev->tx_sample = DIV_ROUND_CLOSEST(sample, sample_period);
642 643

		if (!dev->tx_sample)
M
Maxim Levitsky 已提交
644
			dev->tx_sample = 1;
645 646
	}

M
Maxim Levitsky 已提交
647
	raw_tx = min(dev->tx_sample , (unsigned int)ENE_CIRRLC_OUT_MASK);
648 649
	dev->tx_sample -= raw_tx;

M
Maxim Levitsky 已提交
650 651 652 653 654 655 656
	dbg("TX: sample %8d (%s)", raw_tx * sample_period,
						pulse ? "pulse" : "space");
	if (pulse)
		raw_tx |= ENE_CIRRLC_OUT_PULSE;

	ene_write_reg(dev,
		dev->tx_reg ? ENE_CIRRLC_OUT1 : ENE_CIRRLC_OUT0, raw_tx);
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

	dev->tx_reg = !dev->tx_reg;
exit:
	/* simulate TX done interrupt */
	if (txsim)
		mod_timer(&dev->tx_sim_timer, jiffies + HZ / 500);
}

/* timer to simulate tx done interrupt */
static void ene_tx_irqsim(unsigned long data)
{
	struct ene_device *dev = (struct ene_device *)data;
	unsigned long flags;

	spin_lock_irqsave(&dev->hw_lock, flags);
	ene_tx_sample(dev);
	spin_unlock_irqrestore(&dev->hw_lock, flags);
}


/* read irq status and ack it */
static int ene_irq_status(struct ene_device *dev)
{
	u8 irq_status;
	u8 fw_flags1, fw_flags2;
	int retval = 0;

M
Maxim Levitsky 已提交
684
	fw_flags2 = ene_read_reg(dev, ENE_FW2);
685 686

	if (dev->hw_revision < ENE_HW_C) {
M
Maxim Levitsky 已提交
687
		irq_status = ene_read_reg(dev, ENEB_IRQ_STATUS);
688 689 690 691

		if (!(irq_status & ENEB_IRQ_STATUS_IR))
			return 0;

M
Maxim Levitsky 已提交
692
		ene_clear_reg_mask(dev, ENEB_IRQ_STATUS, ENEB_IRQ_STATUS_IR);
693 694 695
		return ENE_IRQ_RX;
	}

M
Maxim Levitsky 已提交
696 697
	irq_status = ene_read_reg(dev, ENE_IRQ);
	if (!(irq_status & ENE_IRQ_STATUS))
698 699 700
		return 0;

	/* original driver does that twice - a workaround ? */
M
Maxim Levitsky 已提交
701 702
	ene_write_reg(dev, ENE_IRQ, irq_status & ~ENE_IRQ_STATUS);
	ene_write_reg(dev, ENE_IRQ, irq_status & ~ENE_IRQ_STATUS);
703

M
Maxim Levitsky 已提交
704 705 706 707 708
	/* check RX interrupt */
	if (fw_flags2 & ENE_FW2_RXIRQ) {
		retval |= ENE_IRQ_RX;
		ene_write_reg(dev, ENE_FW2, fw_flags2 & ~ENE_FW2_RXIRQ);
	}
709

M
Maxim Levitsky 已提交
710 711
	/* check TX interrupt */
	fw_flags1 = ene_read_reg(dev, ENE_FW1);
712
	if (fw_flags1 & ENE_FW1_TXIRQ) {
M
Maxim Levitsky 已提交
713
		ene_write_reg(dev, ENE_FW1, fw_flags1 & ~ENE_FW1_TXIRQ);
714 715 716
		retval |= ENE_IRQ_TX;
	}

M
Maxim Levitsky 已提交
717 718
	return retval;
}
719 720 721 722

/* interrupt handler */
static irqreturn_t ene_isr(int irq, void *data)
{
M
Maxim Levitsky 已提交
723 724 725
	u16 hw_value, reg;
	int hw_sample, irq_status;
	bool pulse;
726 727 728
	unsigned long flags;
	irqreturn_t retval = IRQ_NONE;
	struct ene_device *dev = (struct ene_device *)data;
729
	DEFINE_IR_RAW_EVENT(ev);
730 731

	spin_lock_irqsave(&dev->hw_lock, flags);
M
Maxim Levitsky 已提交
732 733

	dbg_verbose("ISR called");
734
	ene_rx_read_hw_pointer(dev);
735 736 737 738 739 740 741 742
	irq_status = ene_irq_status(dev);

	if (!irq_status)
		goto unlock;

	retval = IRQ_HANDLED;

	if (irq_status & ENE_IRQ_TX) {
M
Maxim Levitsky 已提交
743
		dbg_verbose("TX interrupt");
744
		if (!dev->hw_learning_and_tx_capable) {
M
Maxim Levitsky 已提交
745
			dbg("TX interrupt on unsupported device!");
746 747 748 749 750 751 752 753
			goto unlock;
		}
		ene_tx_sample(dev);
	}

	if (!(irq_status & ENE_IRQ_RX))
		goto unlock;

M
Maxim Levitsky 已提交
754
	dbg_verbose("RX interrupt");
755

756
	if (dev->hw_learning_and_tx_capable)
M
Maxim Levitsky 已提交
757 758 759 760 761 762 763 764
		ene_rx_sense_carrier(dev);

	/* On hardware that don't support extra buffer we need to trust
		the interrupt and not track the read pointer */
	if (!dev->hw_extra_buffer)
		dev->r_pointer = dev->w_pointer == 0 ? ENE_FW_PACKET_SIZE : 0;

	while (1) {
765

766
		reg = ene_rx_get_sample_reg(dev);
M
Maxim Levitsky 已提交
767 768 769 770 771 772

		dbg_verbose("next sample to read at: %04x", reg);
		if (!reg)
			break;

		hw_value = ene_read_reg(dev, reg);
773 774

		if (dev->rx_fan_input_inuse) {
M
Maxim Levitsky 已提交
775 776 777

			int offset = ENE_FW_SMPL_BUF_FAN - ENE_FW_SAMPLE_BUFFER;

778
			/* read high part of the sample */
M
Maxim Levitsky 已提交
779 780
			hw_value |= ene_read_reg(dev, reg + offset) << 8;
			pulse = hw_value & ENE_FW_SMPL_BUF_FAN_PLS;
781 782

			/* clear space bit, and other unused bits */
M
Maxim Levitsky 已提交
783 784
			hw_value &= ENE_FW_SMPL_BUF_FAN_MSK;
			hw_sample = hw_value * ENE_FW_SAMPLE_PERIOD_FAN;
785 786

		} else {
M
Maxim Levitsky 已提交
787 788
			pulse = !(hw_value & ENE_FW_SAMPLE_SPACE);
			hw_value &= ~ENE_FW_SAMPLE_SPACE;
789
			hw_sample = hw_value * sample_period;
790 791

			if (dev->rx_period_adjust) {
M
Maxim Levitsky 已提交
792 793
				hw_sample *= 100;
				hw_sample /= (100 + dev->rx_period_adjust);
794
			}
795 796
		}

M
Maxim Levitsky 已提交
797 798 799 800
		if (!dev->hw_extra_buffer && !hw_sample) {
			dev->r_pointer = dev->w_pointer;
			continue;
		}
801

M
Maxim Levitsky 已提交
802
		dbg("RX: %d (%s)", hw_sample, pulse ? "pulse" : "space");
803

M
Maxim Levitsky 已提交
804
		ev.duration = MS_TO_NS(hw_sample);
805
		ev.pulse = pulse;
806
		ir_raw_event_store_with_filter(dev->rdev, &ev);
807
	}
808

809
	ir_raw_event_handle(dev->rdev);
810 811 812 813 814 815
unlock:
	spin_unlock_irqrestore(&dev->hw_lock, flags);
	return retval;
}

/* Initialize default settings */
816
static void ene_setup_default_settings(struct ene_device *dev)
817 818
{
	dev->tx_period = 32;
M
Maxim Levitsky 已提交
819 820
	dev->tx_duty_cycle = 50; /*%*/
	dev->transmitter_mask = 0x03;
821
	dev->learning_mode_enabled = learning_mode_force;
822

M
Maxim Levitsky 已提交
823
	/* Set reasonable default timeout */
824
	dev->rdev->timeout = MS_TO_NS(150000);
825 826
}

827 828 829 830 831 832 833 834 835 836 837
/* Upload all hardware settings at once. Used at load and resume time */
static void ene_setup_hw_settings(struct ene_device *dev)
{
	if (dev->hw_learning_and_tx_capable) {
		ene_tx_set_carrier(dev);
		ene_tx_set_transmitters(dev);
	}

	ene_rx_setup(dev);
}

838
/* outside interface: called on first open*/
839
static int ene_open(struct rc_dev *rdev)
840
{
841
	struct ene_device *dev = rdev->priv;
842 843 844 845 846 847 848 849 850
	unsigned long flags;

	spin_lock_irqsave(&dev->hw_lock, flags);
	ene_rx_enable(dev);
	spin_unlock_irqrestore(&dev->hw_lock, flags);
	return 0;
}

/* outside interface: called on device close*/
851
static void ene_close(struct rc_dev *rdev)
852
{
853
	struct ene_device *dev = rdev->priv;
854 855 856 857 858 859 860
	unsigned long flags;
	spin_lock_irqsave(&dev->hw_lock, flags);

	ene_rx_disable(dev);
	spin_unlock_irqrestore(&dev->hw_lock, flags);
}

861
/* outside interface: set transmitter mask */
862
static int ene_set_tx_mask(struct rc_dev *rdev, u32 tx_mask)
863
{
864
	struct ene_device *dev = rdev->priv;
M
Maxim Levitsky 已提交
865
	dbg("TX: attempt to set transmitter mask %02x", tx_mask);
866 867

	/* invalid txmask */
M
Maxim Levitsky 已提交
868 869
	if (!tx_mask || tx_mask & ~0x03) {
		dbg("TX: invalid mask");
870 871
		/* return count of transmitters */
		return 2;
872 873
	}

874
	dev->transmitter_mask = tx_mask;
875
	ene_tx_set_transmitters(dev);
876 877
	return 0;
}
878

879
/* outside interface : set tx carrier */
880
static int ene_set_tx_carrier(struct rc_dev *rdev, u32 carrier)
881
{
882
	struct ene_device *dev = rdev->priv;
M
Maxim Levitsky 已提交
883
	u32 period = 2000000 / carrier;
884

M
Maxim Levitsky 已提交
885
	dbg("TX: attempt to set tx carrier to %d kHz", carrier);
886

M
Maxim Levitsky 已提交
887 888
	if (period && (period > ENE_CIRMOD_PRD_MAX ||
			period < ENE_CIRMOD_PRD_MIN)) {
889

M
Maxim Levitsky 已提交
890
		dbg("TX: out of range %d-%d kHz carrier",
891
			2000 / ENE_CIRMOD_PRD_MIN, 2000 / ENE_CIRMOD_PRD_MAX);
M
Maxim Levitsky 已提交
892
		return -1;
893 894
	}

895
	dev->tx_period = period;
896
	ene_tx_set_carrier(dev);
897
	return 0;
898 899
}

M
Maxim Levitsky 已提交
900
/*outside interface : set tx duty cycle */
901
static int ene_set_tx_duty_cycle(struct rc_dev *rdev, u32 duty_cycle)
M
Maxim Levitsky 已提交
902
{
903
	struct ene_device *dev = rdev->priv;
M
Maxim Levitsky 已提交
904 905
	dbg("TX: setting duty cycle to %d%%", duty_cycle);
	dev->tx_duty_cycle = duty_cycle;
906
	ene_tx_set_carrier(dev);
M
Maxim Levitsky 已提交
907 908
	return 0;
}
909 910

/* outside interface: enable learning mode */
911
static int ene_set_learning_mode(struct rc_dev *rdev, int enable)
912
{
913
	struct ene_device *dev = rdev->priv;
914
	unsigned long flags;
915
	if (enable == dev->learning_mode_enabled)
916
		return 0;
917

918
	spin_lock_irqsave(&dev->hw_lock, flags);
919
	dev->learning_mode_enabled = enable;
M
Maxim Levitsky 已提交
920 921 922
	ene_rx_disable(dev);
	ene_rx_setup(dev);
	ene_rx_enable(dev);
923 924 925
	spin_unlock_irqrestore(&dev->hw_lock, flags);
	return 0;
}
926

927
static int ene_set_carrier_report(struct rc_dev *rdev, int enable)
928
{
929
	struct ene_device *dev = rdev->priv;
930 931 932 933 934 935 936 937 938 939 940 941 942 943
	unsigned long flags;

	if (enable == dev->carrier_detect_enabled)
		return 0;

	spin_lock_irqsave(&dev->hw_lock, flags);
	dev->carrier_detect_enabled = enable;
	ene_rx_disable(dev);
	ene_rx_setup(dev);
	ene_rx_enable(dev);
	spin_unlock_irqrestore(&dev->hw_lock, flags);
	return 0;
}

944
/* outside interface: enable or disable idle mode */
945
static void ene_set_idle(struct rc_dev *rdev, bool idle)
946
{
947 948
	struct ene_device *dev = rdev->priv;

949
	if (idle) {
950
		ene_rx_reset(dev);
951 952
		dbg("RX: end of data");
	}
M
Maxim Levitsky 已提交
953
}
954

955
/* outside interface: transmit */
956
static int ene_transmit(struct rc_dev *rdev, int *buf, u32 n)
957
{
958
	struct ene_device *dev = rdev->priv;
959 960 961
	unsigned long flags;

	dev->tx_buffer = buf;
962 963 964 965 966 967 968
	dev->tx_len = n / sizeof(int);
	dev->tx_pos = 0;
	dev->tx_reg = 0;
	dev->tx_done = 0;
	dev->tx_sample = 0;
	dev->tx_sample_pulse = 0;

M
Maxim Levitsky 已提交
969
	dbg("TX: %d samples", dev->tx_len);
970 971 972

	spin_lock_irqsave(&dev->hw_lock, flags);

973
	ene_tx_enable(dev);
974 975 976 977 978 979 980 981

	/* Transmit first two samples */
	ene_tx_sample(dev);
	ene_tx_sample(dev);

	spin_unlock_irqrestore(&dev->hw_lock, flags);

	if (wait_for_completion_timeout(&dev->tx_complete, 2 * HZ) == 0) {
M
Maxim Levitsky 已提交
982
		dbg("TX: timeout");
983
		spin_lock_irqsave(&dev->hw_lock, flags);
984
		ene_tx_disable(dev);
985 986
		spin_unlock_irqrestore(&dev->hw_lock, flags);
	} else
M
Maxim Levitsky 已提交
987
		dbg("TX: done");
988 989 990
	return n;
}

991 992
/* probe entry */
static int ene_probe(struct pnp_dev *pnp_dev, const struct pnp_device_id *id)
993 994
{
	int error = -ENOMEM;
995
	struct rc_dev *rdev;
996
	struct ene_device *dev;
997

998
	/* allocate memory */
999
	dev = kzalloc(sizeof(struct ene_device), GFP_KERNEL);
1000 1001
	rdev = rc_allocate_device();
	if (!dev || !rdev)
1002
		goto error1;
1003 1004

	/* validate resources */
1005 1006
	error = -ENODEV;

1007
	if (!pnp_port_valid(pnp_dev, 0) ||
M
Maxim Levitsky 已提交
1008
	    pnp_port_len(pnp_dev, 0) < ENE_IO_SIZE)
1009
		goto error;
1010 1011

	if (!pnp_irq_valid(pnp_dev, 0))
1012
		goto error;
1013 1014 1015 1016 1017

	spin_lock_init(&dev->hw_lock);

	/* claim the resources */
	error = -EBUSY;
M
Maxim Levitsky 已提交
1018 1019 1020 1021
	dev->hw_io = pnp_port_start(pnp_dev, 0);
	if (!request_region(dev->hw_io, ENE_IO_SIZE, ENE_DRIVER_NAME)) {
		dev->hw_io = -1;
		dev->irq = -1;
1022
		goto error;
M
Maxim Levitsky 已提交
1023
	}
1024

M
Maxim Levitsky 已提交
1025
	dev->irq = pnp_irq(pnp_dev, 0);
1026
	if (request_irq(dev->irq, ene_isr,
M
Maxim Levitsky 已提交
1027 1028
			IRQF_SHARED, ENE_DRIVER_NAME, (void *)dev)) {
		dev->irq = -1;
1029
		goto error;
M
Maxim Levitsky 已提交
1030
	}
1031 1032 1033

	pnp_set_drvdata(pnp_dev, dev);
	dev->pnp_dev = pnp_dev;
1034

M
Maxim Levitsky 已提交
1035 1036 1037 1038
	/* don't allow too short/long sample periods */
	if (sample_period < 5 || sample_period > 0x7F)
		sample_period = ENE_DEFAULT_SAMPLE_PERIOD;

1039 1040 1041
	/* detect hardware version and features */
	error = ene_hw_detect(dev);
	if (error)
1042
		goto error;
1043 1044

	if (!dev->hw_learning_and_tx_capable && txsim) {
M
Maxim Levitsky 已提交
1045
		dev->hw_learning_and_tx_capable = true;
1046 1047
		setup_timer(&dev->tx_sim_timer, ene_tx_irqsim,
						(long unsigned int)dev);
M
Maxim Levitsky 已提交
1048
		ene_warn("Simulation of TX activated");
1049 1050
	}

M
Maxim Levitsky 已提交
1051
	if (!dev->hw_learning_and_tx_capable)
1052
		learning_mode_force = false;
M
Maxim Levitsky 已提交
1053

1054 1055 1056 1057 1058 1059 1060 1061 1062
	rdev->driver_type = RC_DRIVER_IR_RAW;
	rdev->allowed_protos = IR_TYPE_ALL;
	rdev->priv = dev;
	rdev->open = ene_open;
	rdev->close = ene_close;
	rdev->s_idle = ene_set_idle;
	rdev->driver_name = ENE_DRIVER_NAME;
	rdev->map_name = RC_MAP_RC6_MCE;
	rdev->input_name = "ENE eHome Infrared Remote Receiver";
1063

1064
	if (dev->hw_learning_and_tx_capable) {
1065
		rdev->s_learning_mode = ene_set_learning_mode;
1066
		init_completion(&dev->tx_complete);
1067 1068 1069 1070 1071 1072
		rdev->tx_ir = ene_transmit;
		rdev->s_tx_mask = ene_set_tx_mask;
		rdev->s_tx_carrier = ene_set_tx_carrier;
		rdev->s_tx_duty_cycle = ene_set_tx_duty_cycle;
		rdev->s_carrier_report = ene_set_carrier_report;
		rdev->input_name = "ENE eHome Infrared Remote Transceiver";
1073 1074
	}

1075 1076 1077
	ene_rx_setup_hw_buffer(dev);
	ene_setup_default_settings(dev);
	ene_setup_hw_settings(dev);
1078

M
Maxim Levitsky 已提交
1079 1080
	device_set_wakeup_capable(&pnp_dev->dev, true);
	device_set_wakeup_enable(&pnp_dev->dev, true);
1081

1082 1083
	error = rc_register_device(rdev);
	if (error < 0)
1084 1085
		goto error;

1086
	dev->rdev = rdev;
M
Maxim Levitsky 已提交
1087
	ene_notice("driver has been succesfully loaded");
1088
	return 0;
1089
error:
M
Maxim Levitsky 已提交
1090
	if (dev && dev->irq >= 0)
1091
		free_irq(dev->irq, dev);
M
Maxim Levitsky 已提交
1092 1093
	if (dev && dev->hw_io >= 0)
		release_region(dev->hw_io, ENE_IO_SIZE);
1094
error1:
1095
	rc_free_device(rdev);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	kfree(dev);
	return error;
}

/* main unload function */
static void ene_remove(struct pnp_dev *pnp_dev)
{
	struct ene_device *dev = pnp_get_drvdata(pnp_dev);
	unsigned long flags;

	spin_lock_irqsave(&dev->hw_lock, flags);
	ene_rx_disable(dev);
1108
	ene_rx_restore_hw_buffer(dev);
1109 1110 1111
	spin_unlock_irqrestore(&dev->hw_lock, flags);

	free_irq(dev->irq, dev);
M
Maxim Levitsky 已提交
1112
	release_region(dev->hw_io, ENE_IO_SIZE);
1113
	rc_unregister_device(dev->rdev);
1114 1115 1116 1117 1118 1119 1120
	kfree(dev);
}

/* enable wake on IR (wakes on specific button on original remote) */
static void ene_enable_wake(struct ene_device *dev, int enable)
{
	enable = enable && device_may_wakeup(&dev->pnp_dev->dev);
M
Maxim Levitsky 已提交
1121 1122
	dbg("wake on IR %s", enable ? "enabled" : "disabled");
	ene_set_clear_reg_mask(dev, ENE_FW1, ENE_FW1_WAKE, enable);
1123 1124 1125 1126 1127 1128
}

#ifdef CONFIG_PM
static int ene_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
{
	struct ene_device *dev = pnp_get_drvdata(pnp_dev);
M
Maxim Levitsky 已提交
1129 1130 1131
	ene_enable_wake(dev, true);

	/* TODO: add support for wake pattern */
1132 1133 1134 1135 1136 1137
	return 0;
}

static int ene_resume(struct pnp_dev *pnp_dev)
{
	struct ene_device *dev = pnp_get_drvdata(pnp_dev);
1138 1139 1140
	ene_setup_hw_settings(dev);

	if (dev->rx_enabled)
1141
		ene_rx_enable(dev);
1142

M
Maxim Levitsky 已提交
1143
	ene_enable_wake(dev, false);
1144 1145 1146 1147 1148 1149 1150
	return 0;
}
#endif

static void ene_shutdown(struct pnp_dev *pnp_dev)
{
	struct ene_device *dev = pnp_get_drvdata(pnp_dev);
M
Maxim Levitsky 已提交
1151
	ene_enable_wake(dev, true);
1152 1153 1154 1155 1156 1157
}

static const struct pnp_device_id ene_ids[] = {
	{.id = "ENE0100",},
	{.id = "ENE0200",},
	{.id = "ENE0201",},
1158
	{.id = "ENE0202",},
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	{},
};

static struct pnp_driver ene_driver = {
	.name = ENE_DRIVER_NAME,
	.id_table = ene_ids,
	.flags = PNP_DRIVER_RES_DO_NOT_CHANGE,

	.probe = ene_probe,
	.remove = __devexit_p(ene_remove),
#ifdef CONFIG_PM
	.suspend = ene_suspend,
	.resume = ene_resume,
#endif
	.shutdown = ene_shutdown,
};

static int __init ene_init(void)
{
	return pnp_register_driver(&ene_driver);
}

static void ene_exit(void)
{
	pnp_unregister_driver(&ene_driver);
}

module_param(sample_period, int, S_IRUGO);
MODULE_PARM_DESC(sample_period, "Hardware sample period (50 us default)");

1189 1190
module_param(learning_mode_force, bool, S_IRUGO);
MODULE_PARM_DESC(learning_mode_force, "Enable learning mode by default");
1191 1192

module_param(debug, int, S_IRUGO | S_IWUSR);
M
Maxim Levitsky 已提交
1193
MODULE_PARM_DESC(debug, "Debug level");
1194 1195 1196 1197 1198 1199 1200

module_param(txsim, bool, S_IRUGO);
MODULE_PARM_DESC(txsim,
	"Simulate TX features on unsupported hardware (dangerous)");

MODULE_DEVICE_TABLE(pnp, ene_ids);
MODULE_DESCRIPTION
M
Maxim Levitsky 已提交
1201 1202
	("Infrared input driver for KB3926B/C/D/E/F "
	"(aka ENE0100/ENE0200/ENE0201/ENE0202) CIR port");
1203 1204 1205 1206 1207 1208

MODULE_AUTHOR("Maxim Levitsky");
MODULE_LICENSE("GPL");

module_init(ene_init);
module_exit(ene_exit);