13ef9dff4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2cb7a01acSMauro Carvalho Chehab /* 3cb7a01acSMauro Carvalho Chehab * 4cb7a01acSMauro Carvalho Chehab * keyboard input driver for i2c IR remote controls 5cb7a01acSMauro Carvalho Chehab * 6cb7a01acSMauro Carvalho Chehab * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org> 7cb7a01acSMauro Carvalho Chehab * modified for PixelView (BT878P+W/FM) by 8cb7a01acSMauro Carvalho Chehab * Michal Kochanowicz <mkochano@pld.org.pl> 9cb7a01acSMauro Carvalho Chehab * Christoph Bartelmus <lirc@bartelmus.de> 10cb7a01acSMauro Carvalho Chehab * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by 11cb7a01acSMauro Carvalho Chehab * Ulrich Mueller <ulrich.mueller42@web.de> 12cb7a01acSMauro Carvalho Chehab * modified for em2820 based USB TV tuners by 13cb7a01acSMauro Carvalho Chehab * Markus Rechberger <mrechberger@gmail.com> 14cb7a01acSMauro Carvalho Chehab * modified for DViCO Fusion HDTV 5 RT GOLD by 15cb7a01acSMauro Carvalho Chehab * Chaogui Zhang <czhang1974@gmail.com> 16cb7a01acSMauro Carvalho Chehab * modified for MSI TV@nywhere Plus by 17cb7a01acSMauro Carvalho Chehab * Henry Wong <henry@stuffedcow.net> 18cb7a01acSMauro Carvalho Chehab * Mark Schultz <n9xmj@yahoo.com> 19cb7a01acSMauro Carvalho Chehab * Brian Rogers <brian_rogers@comcast.net> 20cb7a01acSMauro Carvalho Chehab * modified for AVerMedia Cardbus by 21cb7a01acSMauro Carvalho Chehab * Oldrich Jedlicka <oldium.pro@seznam.cz> 22acaa34bfSSean Young * Zilog Transmitter portions/ideas were derived from GPLv2+ sources: 23acaa34bfSSean Young * - drivers/char/pctv_zilogir.[ch] from Hauppauge Broadway product 24acaa34bfSSean Young * Copyright 2011 Hauppauge Computer works 25acaa34bfSSean Young * - drivers/staging/media/lirc/lirc_zilog.c 26acaa34bfSSean Young * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de> 27acaa34bfSSean Young * Michal Kochanowicz <mkochano@pld.org.pl> 28acaa34bfSSean Young * Christoph Bartelmus <lirc@bartelmus.de> 29acaa34bfSSean Young * Ulrich Mueller <ulrich.mueller42@web.de> 30acaa34bfSSean Young * Stefan Jahn <stefan@lkcc.org> 31acaa34bfSSean Young * Jerome Brock <jbrock@users.sourceforge.net> 32acaa34bfSSean Young * Thomas Reitmayr (treitmayr@yahoo.com) 33acaa34bfSSean Young * Mark Weaver <mark@npsl.co.uk> 34acaa34bfSSean Young * Jarod Wilson <jarod@redhat.com> 35acaa34bfSSean Young * Copyright (C) 2011 Andy Walls <awalls@md.metrocast.net> 36cb7a01acSMauro Carvalho Chehab */ 37cb7a01acSMauro Carvalho Chehab 3800bb8207SSean Young #include <asm/unaligned.h> 39cb7a01acSMauro Carvalho Chehab #include <linux/module.h> 40cb7a01acSMauro Carvalho Chehab #include <linux/init.h> 41cb7a01acSMauro Carvalho Chehab #include <linux/kernel.h> 42cb7a01acSMauro Carvalho Chehab #include <linux/string.h> 43cb7a01acSMauro Carvalho Chehab #include <linux/timer.h> 44cb7a01acSMauro Carvalho Chehab #include <linux/delay.h> 45cb7a01acSMauro Carvalho Chehab #include <linux/errno.h> 46cb7a01acSMauro Carvalho Chehab #include <linux/slab.h> 47cb7a01acSMauro Carvalho Chehab #include <linux/i2c.h> 48cb7a01acSMauro Carvalho Chehab #include <linux/workqueue.h> 49cb7a01acSMauro Carvalho Chehab 50cb7a01acSMauro Carvalho Chehab #include <media/rc-core.h> 51b5dcee22SMauro Carvalho Chehab #include <media/i2c/ir-kbd-i2c.h> 52cb7a01acSMauro Carvalho Chehab 53acaa34bfSSean Young #define FLAG_TX 1 54acaa34bfSSean Young #define FLAG_HDPVR 2 55cb7a01acSMauro Carvalho Chehab 561cb26030SSean Young static bool enable_hdpvr; 571cb26030SSean Young module_param(enable_hdpvr, bool, 0644); 581cb26030SSean Young 596d741bfeSSean Young static int get_key_haup_common(struct IR_i2c *ir, enum rc_proto *protocol, 6000bb8207SSean Young u32 *scancode, u8 *ptoggle, int size) 61cb7a01acSMauro Carvalho Chehab { 62cb7a01acSMauro Carvalho Chehab unsigned char buf[6]; 6300bb8207SSean Young int start, range, toggle, dev, code, ircode, vendor; 64cb7a01acSMauro Carvalho Chehab 65cb7a01acSMauro Carvalho Chehab /* poll IR chip */ 66cb7a01acSMauro Carvalho Chehab if (size != i2c_master_recv(ir->c, buf, size)) 67cb7a01acSMauro Carvalho Chehab return -EIO; 68cb7a01acSMauro Carvalho Chehab 6900bb8207SSean Young if (buf[0] & 0x80) { 7000bb8207SSean Young int offset = (size == 6) ? 3 : 0; 7100bb8207SSean Young 72cb7a01acSMauro Carvalho Chehab /* split rc5 data block ... */ 73cb7a01acSMauro Carvalho Chehab start = (buf[offset] >> 7) & 1; 74cb7a01acSMauro Carvalho Chehab range = (buf[offset] >> 6) & 1; 75cb7a01acSMauro Carvalho Chehab toggle = (buf[offset] >> 5) & 1; 76cb7a01acSMauro Carvalho Chehab dev = buf[offset] & 0x1f; 77cb7a01acSMauro Carvalho Chehab code = (buf[offset+1] >> 2) & 0x3f; 78cb7a01acSMauro Carvalho Chehab 79cb7a01acSMauro Carvalho Chehab /* rc5 has two start bits 80cb7a01acSMauro Carvalho Chehab * the first bit must be one 8100bb8207SSean Young * the second bit defines the command range: 8200bb8207SSean Young * 1 = 0-63, 0 = 64 - 127 83cb7a01acSMauro Carvalho Chehab */ 84cb7a01acSMauro Carvalho Chehab if (!start) 85cb7a01acSMauro Carvalho Chehab /* no key pressed */ 86cb7a01acSMauro Carvalho Chehab return 0; 874dd9bb91SDavid Härdeman 884dd9bb91SDavid Härdeman /* filter out invalid key presses */ 89cb7a01acSMauro Carvalho Chehab ircode = (start << 12) | (toggle << 11) | (dev << 6) | code; 90cb7a01acSMauro Carvalho Chehab if ((ircode & 0x1fff) == 0x1fff) 91cb7a01acSMauro Carvalho Chehab return 0; 92cb7a01acSMauro Carvalho Chehab 93cb7a01acSMauro Carvalho Chehab if (!range) 94cb7a01acSMauro Carvalho Chehab code += 64; 95cb7a01acSMauro Carvalho Chehab 9650a762b4SSean Young dev_dbg(&ir->rc->dev, 9750a762b4SSean Young "ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n", 98cb7a01acSMauro Carvalho Chehab start, range, toggle, dev, code); 99cb7a01acSMauro Carvalho Chehab 1006d741bfeSSean Young *protocol = RC_PROTO_RC5; 1014dd9bb91SDavid Härdeman *scancode = RC_SCANCODE_RC5(dev, code); 1024dd9bb91SDavid Härdeman *ptoggle = toggle; 10300bb8207SSean Young 104cb7a01acSMauro Carvalho Chehab return 1; 10500bb8207SSean Young } else if (size == 6 && (buf[0] & 0x40)) { 10600bb8207SSean Young code = buf[4]; 10700bb8207SSean Young dev = buf[3]; 10800bb8207SSean Young vendor = get_unaligned_be16(buf + 1); 10900bb8207SSean Young 11000bb8207SSean Young if (vendor == 0x800f) { 11100bb8207SSean Young *ptoggle = (dev & 0x80) != 0; 1126d741bfeSSean Young *protocol = RC_PROTO_RC6_MCE; 11300bb8207SSean Young dev &= 0x7f; 11450a762b4SSean Young dev_dbg(&ir->rc->dev, 11550a762b4SSean Young "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n", 1169cdbe14fSArnd Bergmann *ptoggle, vendor, dev, code); 11700bb8207SSean Young } else { 11800bb8207SSean Young *ptoggle = 0; 1196d741bfeSSean Young *protocol = RC_PROTO_RC6_6A_32; 12050a762b4SSean Young dev_dbg(&ir->rc->dev, 12150a762b4SSean Young "ir hauppauge (rc6-6a-32): vendor=%d dev=%d code=%d\n", 12200bb8207SSean Young vendor, dev, code); 12300bb8207SSean Young } 12400bb8207SSean Young 12500bb8207SSean Young *scancode = RC_SCANCODE_RC6_6A(vendor, dev, code); 12600bb8207SSean Young 12700bb8207SSean Young return 1; 12800bb8207SSean Young } 12900bb8207SSean Young 13000bb8207SSean Young return 0; 131cb7a01acSMauro Carvalho Chehab } 132cb7a01acSMauro Carvalho Chehab 1336d741bfeSSean Young static int get_key_haup(struct IR_i2c *ir, enum rc_proto *protocol, 1344dd9bb91SDavid Härdeman u32 *scancode, u8 *toggle) 135cb7a01acSMauro Carvalho Chehab { 13600bb8207SSean Young return get_key_haup_common(ir, protocol, scancode, toggle, 3); 137cb7a01acSMauro Carvalho Chehab } 138cb7a01acSMauro Carvalho Chehab 1396d741bfeSSean Young static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_proto *protocol, 1404dd9bb91SDavid Härdeman u32 *scancode, u8 *toggle) 141cb7a01acSMauro Carvalho Chehab { 142cb7a01acSMauro Carvalho Chehab int ret; 143cb7a01acSMauro Carvalho Chehab unsigned char buf[1] = { 0 }; 144cb7a01acSMauro Carvalho Chehab 145cb7a01acSMauro Carvalho Chehab /* 146cb7a01acSMauro Carvalho Chehab * This is the same apparent "are you ready?" poll command observed 147cb7a01acSMauro Carvalho Chehab * watching Windows driver traffic and implemented in lirc_zilog. With 148cb7a01acSMauro Carvalho Chehab * this added, we get far saner remote behavior with z8 chips on usb 149cb7a01acSMauro Carvalho Chehab * connected devices, even with the default polling interval of 100ms. 150cb7a01acSMauro Carvalho Chehab */ 151cb7a01acSMauro Carvalho Chehab ret = i2c_master_send(ir->c, buf, 1); 152cb7a01acSMauro Carvalho Chehab if (ret != 1) 153cb7a01acSMauro Carvalho Chehab return (ret < 0) ? ret : -EINVAL; 154cb7a01acSMauro Carvalho Chehab 15500bb8207SSean Young return get_key_haup_common(ir, protocol, scancode, toggle, 6); 156cb7a01acSMauro Carvalho Chehab } 157cb7a01acSMauro Carvalho Chehab 1586d741bfeSSean Young static int get_key_pixelview(struct IR_i2c *ir, enum rc_proto *protocol, 1594dd9bb91SDavid Härdeman u32 *scancode, u8 *toggle) 160cb7a01acSMauro Carvalho Chehab { 161c3902dabSMauro Carvalho Chehab int rc; 162cb7a01acSMauro Carvalho Chehab unsigned char b; 163cb7a01acSMauro Carvalho Chehab 164cb7a01acSMauro Carvalho Chehab /* poll IR chip */ 165c3902dabSMauro Carvalho Chehab rc = i2c_master_recv(ir->c, &b, 1); 166c3902dabSMauro Carvalho Chehab if (rc != 1) { 16750a762b4SSean Young dev_dbg(&ir->rc->dev, "read error\n"); 168c3902dabSMauro Carvalho Chehab if (rc < 0) 169c3902dabSMauro Carvalho Chehab return rc; 170cb7a01acSMauro Carvalho Chehab return -EIO; 171cb7a01acSMauro Carvalho Chehab } 1724dd9bb91SDavid Härdeman 1736d741bfeSSean Young *protocol = RC_PROTO_OTHER; 1744dd9bb91SDavid Härdeman *scancode = b; 1754dd9bb91SDavid Härdeman *toggle = 0; 176cb7a01acSMauro Carvalho Chehab return 1; 177cb7a01acSMauro Carvalho Chehab } 178cb7a01acSMauro Carvalho Chehab 1796d741bfeSSean Young static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_proto *protocol, 1804dd9bb91SDavid Härdeman u32 *scancode, u8 *toggle) 181cb7a01acSMauro Carvalho Chehab { 182c3902dabSMauro Carvalho Chehab int rc; 183cb7a01acSMauro Carvalho Chehab unsigned char buf[4]; 184cb7a01acSMauro Carvalho Chehab 185cb7a01acSMauro Carvalho Chehab /* poll IR chip */ 186c3902dabSMauro Carvalho Chehab rc = i2c_master_recv(ir->c, buf, 4); 187c3902dabSMauro Carvalho Chehab if (rc != 4) { 18850a762b4SSean Young dev_dbg(&ir->rc->dev, "read error\n"); 189c3902dabSMauro Carvalho Chehab if (rc < 0) 190c3902dabSMauro Carvalho Chehab return rc; 191cb7a01acSMauro Carvalho Chehab return -EIO; 192cb7a01acSMauro Carvalho Chehab } 193cb7a01acSMauro Carvalho Chehab 194cb7a01acSMauro Carvalho Chehab if (buf[0] != 0 || buf[1] != 0 || buf[2] != 0 || buf[3] != 0) 19550a762b4SSean Young dev_dbg(&ir->rc->dev, "%s: %*ph\n", __func__, 4, buf); 196cb7a01acSMauro Carvalho Chehab 197cb7a01acSMauro Carvalho Chehab /* no key pressed or signal from other ir remote */ 198cb7a01acSMauro Carvalho Chehab if(buf[0] != 0x1 || buf[1] != 0xfe) 199cb7a01acSMauro Carvalho Chehab return 0; 200cb7a01acSMauro Carvalho Chehab 2016d741bfeSSean Young *protocol = RC_PROTO_UNKNOWN; 2024dd9bb91SDavid Härdeman *scancode = buf[2]; 2034dd9bb91SDavid Härdeman *toggle = 0; 204cb7a01acSMauro Carvalho Chehab return 1; 205cb7a01acSMauro Carvalho Chehab } 206cb7a01acSMauro Carvalho Chehab 2076d741bfeSSean Young static int get_key_knc1(struct IR_i2c *ir, enum rc_proto *protocol, 2084dd9bb91SDavid Härdeman u32 *scancode, u8 *toggle) 209cb7a01acSMauro Carvalho Chehab { 210c3902dabSMauro Carvalho Chehab int rc; 211cb7a01acSMauro Carvalho Chehab unsigned char b; 212cb7a01acSMauro Carvalho Chehab 213cb7a01acSMauro Carvalho Chehab /* poll IR chip */ 214c3902dabSMauro Carvalho Chehab rc = i2c_master_recv(ir->c, &b, 1); 215c3902dabSMauro Carvalho Chehab if (rc != 1) { 21650a762b4SSean Young dev_dbg(&ir->rc->dev, "read error\n"); 217c3902dabSMauro Carvalho Chehab if (rc < 0) 218c3902dabSMauro Carvalho Chehab return rc; 219cb7a01acSMauro Carvalho Chehab return -EIO; 220cb7a01acSMauro Carvalho Chehab } 221cb7a01acSMauro Carvalho Chehab 222cb7a01acSMauro Carvalho Chehab /* it seems that 0xFE indicates that a button is still hold 223cb7a01acSMauro Carvalho Chehab down, while 0xff indicates that no button is hold 224cb7a01acSMauro Carvalho Chehab down. 0xfe sequences are sometimes interrupted by 0xFF */ 225cb7a01acSMauro Carvalho Chehab 22650a762b4SSean Young dev_dbg(&ir->rc->dev, "key %02x\n", b); 227cb7a01acSMauro Carvalho Chehab 228cb7a01acSMauro Carvalho Chehab if (b == 0xff) 229cb7a01acSMauro Carvalho Chehab return 0; 230cb7a01acSMauro Carvalho Chehab 231cb7a01acSMauro Carvalho Chehab if (b == 0xfe) 232cb7a01acSMauro Carvalho Chehab /* keep old data */ 233cb7a01acSMauro Carvalho Chehab return 1; 234cb7a01acSMauro Carvalho Chehab 2356d741bfeSSean Young *protocol = RC_PROTO_UNKNOWN; 2364dd9bb91SDavid Härdeman *scancode = b; 2374dd9bb91SDavid Härdeman *toggle = 0; 238cb7a01acSMauro Carvalho Chehab return 1; 239cb7a01acSMauro Carvalho Chehab } 240cb7a01acSMauro Carvalho Chehab 2416d741bfeSSean Young static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_proto *protocol, 2424dd9bb91SDavid Härdeman u32 *scancode, u8 *toggle) 243cb7a01acSMauro Carvalho Chehab { 244cb7a01acSMauro Carvalho Chehab unsigned char subaddr, key, keygroup; 245cb7a01acSMauro Carvalho Chehab struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0, 246cb7a01acSMauro Carvalho Chehab .buf = &subaddr, .len = 1}, 247cb7a01acSMauro Carvalho Chehab { .addr = ir->c->addr, .flags = I2C_M_RD, 248cb7a01acSMauro Carvalho Chehab .buf = &key, .len = 1} }; 249cb7a01acSMauro Carvalho Chehab subaddr = 0x0d; 250cb7a01acSMauro Carvalho Chehab if (2 != i2c_transfer(ir->c->adapter, msg, 2)) { 25150a762b4SSean Young dev_dbg(&ir->rc->dev, "read error\n"); 252cb7a01acSMauro Carvalho Chehab return -EIO; 253cb7a01acSMauro Carvalho Chehab } 254cb7a01acSMauro Carvalho Chehab 255cb7a01acSMauro Carvalho Chehab if (key == 0xff) 256cb7a01acSMauro Carvalho Chehab return 0; 257cb7a01acSMauro Carvalho Chehab 258cb7a01acSMauro Carvalho Chehab subaddr = 0x0b; 259cb7a01acSMauro Carvalho Chehab msg[1].buf = &keygroup; 260cb7a01acSMauro Carvalho Chehab if (2 != i2c_transfer(ir->c->adapter, msg, 2)) { 26150a762b4SSean Young dev_dbg(&ir->rc->dev, "read error\n"); 262cb7a01acSMauro Carvalho Chehab return -EIO; 263cb7a01acSMauro Carvalho Chehab } 264cb7a01acSMauro Carvalho Chehab 265cb7a01acSMauro Carvalho Chehab if (keygroup == 0xff) 266cb7a01acSMauro Carvalho Chehab return 0; 267cb7a01acSMauro Carvalho Chehab 26850a762b4SSean Young dev_dbg(&ir->rc->dev, "read key 0x%02x/0x%02x\n", key, keygroup); 26934fe2784SOndrej Zary if (keygroup < 2 || keygroup > 4) { 27050a762b4SSean Young dev_warn(&ir->rc->dev, "warning: invalid key group 0x%02x for key 0x%02x\n", 271cb7a01acSMauro Carvalho Chehab keygroup, key); 272cb7a01acSMauro Carvalho Chehab } 273cb7a01acSMauro Carvalho Chehab key |= (keygroup & 1) << 6; 274cb7a01acSMauro Carvalho Chehab 2756d741bfeSSean Young *protocol = RC_PROTO_UNKNOWN; 2764dd9bb91SDavid Härdeman *scancode = key; 2774dd9bb91SDavid Härdeman if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */ 2784dd9bb91SDavid Härdeman *scancode |= keygroup << 8; 2794dd9bb91SDavid Härdeman *toggle = 0; 280cb7a01acSMauro Carvalho Chehab return 1; 281cb7a01acSMauro Carvalho Chehab } 282cb7a01acSMauro Carvalho Chehab 283cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */ 284cb7a01acSMauro Carvalho Chehab 285cb7a01acSMauro Carvalho Chehab static int ir_key_poll(struct IR_i2c *ir) 286cb7a01acSMauro Carvalho Chehab { 2876d741bfeSSean Young enum rc_proto protocol; 2884dd9bb91SDavid Härdeman u32 scancode; 2894dd9bb91SDavid Härdeman u8 toggle; 290cb7a01acSMauro Carvalho Chehab int rc; 291cb7a01acSMauro Carvalho Chehab 29250a762b4SSean Young dev_dbg(&ir->rc->dev, "%s\n", __func__); 2934dd9bb91SDavid Härdeman rc = ir->get_key(ir, &protocol, &scancode, &toggle); 294cb7a01acSMauro Carvalho Chehab if (rc < 0) { 29550a762b4SSean Young dev_warn(&ir->rc->dev, "error %d\n", rc); 296cb7a01acSMauro Carvalho Chehab return rc; 297cb7a01acSMauro Carvalho Chehab } 298cb7a01acSMauro Carvalho Chehab 299cb7a01acSMauro Carvalho Chehab if (rc) { 30050a762b4SSean Young dev_dbg(&ir->rc->dev, "%s: proto = 0x%04x, scancode = 0x%08x\n", 301120703f9SDavid Härdeman __func__, protocol, scancode); 302120703f9SDavid Härdeman rc_keydown(ir->rc, protocol, scancode, toggle); 303cb7a01acSMauro Carvalho Chehab } 304cb7a01acSMauro Carvalho Chehab return 0; 305cb7a01acSMauro Carvalho Chehab } 306cb7a01acSMauro Carvalho Chehab 307cb7a01acSMauro Carvalho Chehab static void ir_work(struct work_struct *work) 308cb7a01acSMauro Carvalho Chehab { 309cb7a01acSMauro Carvalho Chehab int rc; 310cb7a01acSMauro Carvalho Chehab struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work); 311cb7a01acSMauro Carvalho Chehab 312acaa34bfSSean Young /* 313acaa34bfSSean Young * If the transmit code is holding the lock, skip polling for 314acaa34bfSSean Young * IR, we'll get it to it next time round 315acaa34bfSSean Young */ 316acaa34bfSSean Young if (mutex_trylock(&ir->lock)) { 317cb7a01acSMauro Carvalho Chehab rc = ir_key_poll(ir); 318acaa34bfSSean Young mutex_unlock(&ir->lock); 319cb7a01acSMauro Carvalho Chehab if (rc == -ENODEV) { 320cb7a01acSMauro Carvalho Chehab rc_unregister_device(ir->rc); 321cb7a01acSMauro Carvalho Chehab ir->rc = NULL; 322cb7a01acSMauro Carvalho Chehab return; 323cb7a01acSMauro Carvalho Chehab } 324acaa34bfSSean Young } 325cb7a01acSMauro Carvalho Chehab 326cb7a01acSMauro Carvalho Chehab schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval)); 327cb7a01acSMauro Carvalho Chehab } 328cb7a01acSMauro Carvalho Chehab 329a6927d81SSean Young static int ir_open(struct rc_dev *dev) 330a6927d81SSean Young { 331a6927d81SSean Young struct IR_i2c *ir = dev->priv; 332a6927d81SSean Young 333a6927d81SSean Young schedule_delayed_work(&ir->work, 0); 334a6927d81SSean Young 335a6927d81SSean Young return 0; 336a6927d81SSean Young } 337a6927d81SSean Young 338a6927d81SSean Young static void ir_close(struct rc_dev *dev) 339a6927d81SSean Young { 340a6927d81SSean Young struct IR_i2c *ir = dev->priv; 341a6927d81SSean Young 342a6927d81SSean Young cancel_delayed_work_sync(&ir->work); 343a6927d81SSean Young } 344a6927d81SSean Young 345acaa34bfSSean Young /* Zilog Transmit Interface */ 346acaa34bfSSean Young #define XTAL_FREQ 18432000 347acaa34bfSSean Young 348acaa34bfSSean Young #define ZILOG_SEND 0x80 349acaa34bfSSean Young #define ZILOG_UIR_END 0x40 350acaa34bfSSean Young #define ZILOG_INIT_END 0x20 351acaa34bfSSean Young #define ZILOG_LIR_END 0x10 352acaa34bfSSean Young 353acaa34bfSSean Young #define ZILOG_STATUS_OK 0x80 354acaa34bfSSean Young #define ZILOG_STATUS_TX 0x40 355acaa34bfSSean Young #define ZILOG_STATUS_SET 0x20 356acaa34bfSSean Young 357acaa34bfSSean Young /* 358acaa34bfSSean Young * As you can see here, very few different lengths of pulse and space 359acaa34bfSSean Young * can be encoded. This means that the hardware does not work well with 360acaa34bfSSean Young * recorded IR. It's best to work with generated IR, like from ir-ctl or 361acaa34bfSSean Young * the in-kernel encoders. 362acaa34bfSSean Young */ 363acaa34bfSSean Young struct code_block { 364acaa34bfSSean Young u8 length; 365acaa34bfSSean Young u16 pulse[7]; /* not aligned */ 366acaa34bfSSean Young u8 carrier_pulse; 367acaa34bfSSean Young u8 carrier_space; 368acaa34bfSSean Young u16 space[8]; /* not aligned */ 369acaa34bfSSean Young u8 codes[61]; 370acaa34bfSSean Young u8 csum[2]; 371acaa34bfSSean Young } __packed; 372acaa34bfSSean Young 373acaa34bfSSean Young static int send_data_block(struct IR_i2c *ir, int cmd, 374acaa34bfSSean Young struct code_block *code_block) 375acaa34bfSSean Young { 376acaa34bfSSean Young int i, j, ret; 377acaa34bfSSean Young u8 buf[5], *p; 378acaa34bfSSean Young 379acaa34bfSSean Young p = &code_block->length; 380acaa34bfSSean Young for (i = 0; p < code_block->csum; i++) 381acaa34bfSSean Young code_block->csum[i & 1] ^= *p++; 382acaa34bfSSean Young 383acaa34bfSSean Young p = &code_block->length; 384acaa34bfSSean Young 385acaa34bfSSean Young for (i = 0; i < sizeof(*code_block);) { 386acaa34bfSSean Young int tosend = sizeof(*code_block) - i; 387acaa34bfSSean Young 388acaa34bfSSean Young if (tosend > 4) 389acaa34bfSSean Young tosend = 4; 390acaa34bfSSean Young buf[0] = i + 1; 391acaa34bfSSean Young for (j = 0; j < tosend; ++j) 392acaa34bfSSean Young buf[1 + j] = p[i + j]; 393acaa34bfSSean Young dev_dbg(&ir->rc->dev, "%*ph", tosend + 1, buf); 394acaa34bfSSean Young ret = i2c_master_send(ir->tx_c, buf, tosend + 1); 395acaa34bfSSean Young if (ret != tosend + 1) { 396acaa34bfSSean Young dev_dbg(&ir->rc->dev, 397acaa34bfSSean Young "i2c_master_send failed with %d\n", ret); 398acaa34bfSSean Young return ret < 0 ? ret : -EIO; 399acaa34bfSSean Young } 400acaa34bfSSean Young i += tosend; 401acaa34bfSSean Young } 402acaa34bfSSean Young 403acaa34bfSSean Young buf[0] = 0; 404acaa34bfSSean Young buf[1] = cmd; 405acaa34bfSSean Young ret = i2c_master_send(ir->tx_c, buf, 2); 406acaa34bfSSean Young if (ret != 2) { 407acaa34bfSSean Young dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret); 408acaa34bfSSean Young return ret < 0 ? ret : -EIO; 409acaa34bfSSean Young } 410acaa34bfSSean Young 411acaa34bfSSean Young usleep_range(2000, 5000); 412acaa34bfSSean Young 413acaa34bfSSean Young ret = i2c_master_send(ir->tx_c, buf, 1); 414acaa34bfSSean Young if (ret != 1) { 415acaa34bfSSean Young dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret); 416acaa34bfSSean Young return ret < 0 ? ret : -EIO; 417acaa34bfSSean Young } 418acaa34bfSSean Young 419acaa34bfSSean Young return 0; 420acaa34bfSSean Young } 421acaa34bfSSean Young 422acaa34bfSSean Young static int zilog_init(struct IR_i2c *ir) 423acaa34bfSSean Young { 424acaa34bfSSean Young struct code_block code_block = { .length = sizeof(code_block) }; 425acaa34bfSSean Young u8 buf[4]; 426acaa34bfSSean Young int ret; 427acaa34bfSSean Young 428acaa34bfSSean Young put_unaligned_be16(0x1000, &code_block.pulse[3]); 429acaa34bfSSean Young 430acaa34bfSSean Young ret = send_data_block(ir, ZILOG_INIT_END, &code_block); 431acaa34bfSSean Young if (ret) 432acaa34bfSSean Young return ret; 433acaa34bfSSean Young 434acaa34bfSSean Young ret = i2c_master_recv(ir->tx_c, buf, 4); 435acaa34bfSSean Young if (ret != 4) { 436acaa34bfSSean Young dev_err(&ir->c->dev, "failed to retrieve firmware version: %d\n", 437acaa34bfSSean Young ret); 438acaa34bfSSean Young return ret < 0 ? ret : -EIO; 439acaa34bfSSean Young } 440acaa34bfSSean Young 441acaa34bfSSean Young dev_info(&ir->c->dev, "Zilog/Hauppauge IR blaster firmware version %d.%d.%d\n", 442acaa34bfSSean Young buf[1], buf[2], buf[3]); 443acaa34bfSSean Young 444acaa34bfSSean Young return 0; 445acaa34bfSSean Young } 446acaa34bfSSean Young 447acaa34bfSSean Young /* 448acaa34bfSSean Young * If the last slot for pulse is the same as the current slot for pulse, 449acaa34bfSSean Young * then use slot no 7. 450acaa34bfSSean Young */ 451acaa34bfSSean Young static void copy_codes(u8 *dst, u8 *src, unsigned int count) 452acaa34bfSSean Young { 453acaa34bfSSean Young u8 c, last = 0xff; 454acaa34bfSSean Young 455acaa34bfSSean Young while (count--) { 456acaa34bfSSean Young c = *src++; 457acaa34bfSSean Young if ((c & 0xf0) == last) { 458acaa34bfSSean Young *dst++ = 0x70 | (c & 0xf); 459acaa34bfSSean Young } else { 460acaa34bfSSean Young *dst++ = c; 461acaa34bfSSean Young last = c & 0xf0; 462acaa34bfSSean Young } 463acaa34bfSSean Young } 464acaa34bfSSean Young } 465acaa34bfSSean Young 466acaa34bfSSean Young /* 467acaa34bfSSean Young * When looking for repeats, we don't care about the trailing space. This 468acaa34bfSSean Young * is set to the shortest possible anyway. 469acaa34bfSSean Young */ 470acaa34bfSSean Young static int cmp_no_trail(u8 *a, u8 *b, unsigned int count) 471acaa34bfSSean Young { 472acaa34bfSSean Young while (--count) { 473acaa34bfSSean Young if (*a++ != *b++) 474acaa34bfSSean Young return 1; 475acaa34bfSSean Young } 476acaa34bfSSean Young 477acaa34bfSSean Young return (*a & 0xf0) - (*b & 0xf0); 478acaa34bfSSean Young } 479acaa34bfSSean Young 480acaa34bfSSean Young static int find_slot(u16 *array, unsigned int size, u16 val) 481acaa34bfSSean Young { 482acaa34bfSSean Young int i; 483acaa34bfSSean Young 484acaa34bfSSean Young for (i = 0; i < size; i++) { 485acaa34bfSSean Young if (get_unaligned_be16(&array[i]) == val) { 486acaa34bfSSean Young return i; 487acaa34bfSSean Young } else if (!array[i]) { 488acaa34bfSSean Young put_unaligned_be16(val, &array[i]); 489acaa34bfSSean Young return i; 490acaa34bfSSean Young } 491acaa34bfSSean Young } 492acaa34bfSSean Young 493acaa34bfSSean Young return -1; 494acaa34bfSSean Young } 495acaa34bfSSean Young 496acaa34bfSSean Young static int zilog_ir_format(struct rc_dev *rcdev, unsigned int *txbuf, 497acaa34bfSSean Young unsigned int count, struct code_block *code_block) 498acaa34bfSSean Young { 499acaa34bfSSean Young struct IR_i2c *ir = rcdev->priv; 500acaa34bfSSean Young int rep, i, l, p = 0, s, c = 0; 501acaa34bfSSean Young bool repeating; 502acaa34bfSSean Young u8 codes[174]; 503acaa34bfSSean Young 504acaa34bfSSean Young code_block->carrier_pulse = DIV_ROUND_CLOSEST( 505acaa34bfSSean Young ir->duty_cycle * XTAL_FREQ / 1000, ir->carrier); 506acaa34bfSSean Young code_block->carrier_space = DIV_ROUND_CLOSEST( 507acaa34bfSSean Young (100 - ir->duty_cycle) * XTAL_FREQ / 1000, ir->carrier); 508acaa34bfSSean Young 509acaa34bfSSean Young for (i = 0; i < count; i++) { 510acaa34bfSSean Young if (c >= ARRAY_SIZE(codes) - 1) { 511acaa34bfSSean Young dev_warn(&rcdev->dev, "IR too long, cannot transmit\n"); 512acaa34bfSSean Young return -EINVAL; 513acaa34bfSSean Young } 514acaa34bfSSean Young 515acaa34bfSSean Young /* 516acaa34bfSSean Young * Lengths more than 142220us cannot be encoded; also 517acaa34bfSSean Young * this checks for multiply overflow 518acaa34bfSSean Young */ 519acaa34bfSSean Young if (txbuf[i] > 142220) 520acaa34bfSSean Young return -EINVAL; 521acaa34bfSSean Young 522acaa34bfSSean Young l = DIV_ROUND_CLOSEST((XTAL_FREQ / 1000) * txbuf[i], 40000); 523acaa34bfSSean Young 524acaa34bfSSean Young if (i & 1) { 525acaa34bfSSean Young s = find_slot(code_block->space, 526acaa34bfSSean Young ARRAY_SIZE(code_block->space), l); 527acaa34bfSSean Young if (s == -1) { 528acaa34bfSSean Young dev_warn(&rcdev->dev, "Too many different lengths spaces, cannot transmit"); 529acaa34bfSSean Young return -EINVAL; 530acaa34bfSSean Young } 531acaa34bfSSean Young 532acaa34bfSSean Young /* We have a pulse and space */ 533acaa34bfSSean Young codes[c++] = (p << 4) | s; 534acaa34bfSSean Young } else { 535acaa34bfSSean Young p = find_slot(code_block->pulse, 536acaa34bfSSean Young ARRAY_SIZE(code_block->pulse), l); 537acaa34bfSSean Young if (p == -1) { 538acaa34bfSSean Young dev_warn(&rcdev->dev, "Too many different lengths pulses, cannot transmit"); 539acaa34bfSSean Young return -EINVAL; 540acaa34bfSSean Young } 541acaa34bfSSean Young } 542acaa34bfSSean Young } 543acaa34bfSSean Young 544acaa34bfSSean Young /* We have to encode the trailing pulse. Find the shortest space */ 545acaa34bfSSean Young s = 0; 546acaa34bfSSean Young for (i = 1; i < ARRAY_SIZE(code_block->space); i++) { 547acaa34bfSSean Young u16 d = get_unaligned_be16(&code_block->space[i]); 548acaa34bfSSean Young 549acaa34bfSSean Young if (get_unaligned_be16(&code_block->space[s]) > d) 550acaa34bfSSean Young s = i; 551acaa34bfSSean Young } 552acaa34bfSSean Young 553acaa34bfSSean Young codes[c++] = (p << 4) | s; 554acaa34bfSSean Young 555acaa34bfSSean Young dev_dbg(&rcdev->dev, "generated %d codes\n", c); 556acaa34bfSSean Young 557acaa34bfSSean Young /* 558acaa34bfSSean Young * Are the last N codes (so pulse + space) repeating 3 times? 559acaa34bfSSean Young * if so we can shorten the codes list and use code 0xc0 to repeat 560acaa34bfSSean Young * them. 561acaa34bfSSean Young */ 562acaa34bfSSean Young repeating = false; 563acaa34bfSSean Young 564acaa34bfSSean Young for (rep = c / 3; rep >= 1; rep--) { 565acaa34bfSSean Young if (!memcmp(&codes[c - rep * 3], &codes[c - rep * 2], rep) && 566acaa34bfSSean Young !cmp_no_trail(&codes[c - rep], &codes[c - rep * 2], rep)) { 567acaa34bfSSean Young repeating = true; 568acaa34bfSSean Young break; 569acaa34bfSSean Young } 570acaa34bfSSean Young } 571acaa34bfSSean Young 572acaa34bfSSean Young if (repeating) { 573acaa34bfSSean Young /* first copy any leading non-repeating */ 574acaa34bfSSean Young int leading = c - rep * 3; 575acaa34bfSSean Young 5769863bc49SMauro Carvalho Chehab if (leading >= ARRAY_SIZE(code_block->codes) - 3 - rep) { 577acaa34bfSSean Young dev_warn(&rcdev->dev, "IR too long, cannot transmit\n"); 578acaa34bfSSean Young return -EINVAL; 579acaa34bfSSean Young } 580acaa34bfSSean Young 581acaa34bfSSean Young dev_dbg(&rcdev->dev, "found trailing %d repeat\n", rep); 582acaa34bfSSean Young copy_codes(code_block->codes, codes, leading); 583acaa34bfSSean Young code_block->codes[leading] = 0x82; 584acaa34bfSSean Young copy_codes(code_block->codes + leading + 1, codes + leading, 585acaa34bfSSean Young rep); 586acaa34bfSSean Young c = leading + 1 + rep; 587acaa34bfSSean Young code_block->codes[c++] = 0xc0; 588acaa34bfSSean Young } else { 589acaa34bfSSean Young if (c >= ARRAY_SIZE(code_block->codes) - 3) { 590acaa34bfSSean Young dev_warn(&rcdev->dev, "IR too long, cannot transmit\n"); 591acaa34bfSSean Young return -EINVAL; 592acaa34bfSSean Young } 593acaa34bfSSean Young 594acaa34bfSSean Young dev_dbg(&rcdev->dev, "found no trailing repeat\n"); 595acaa34bfSSean Young code_block->codes[0] = 0x82; 596acaa34bfSSean Young copy_codes(code_block->codes + 1, codes, c); 597acaa34bfSSean Young c++; 598acaa34bfSSean Young code_block->codes[c++] = 0xc4; 599acaa34bfSSean Young } 600acaa34bfSSean Young 601acaa34bfSSean Young while (c < ARRAY_SIZE(code_block->codes)) 602acaa34bfSSean Young code_block->codes[c++] = 0x83; 603acaa34bfSSean Young 604acaa34bfSSean Young return 0; 605acaa34bfSSean Young } 606acaa34bfSSean Young 607acaa34bfSSean Young static int zilog_tx(struct rc_dev *rcdev, unsigned int *txbuf, 608acaa34bfSSean Young unsigned int count) 609acaa34bfSSean Young { 610acaa34bfSSean Young struct IR_i2c *ir = rcdev->priv; 611acaa34bfSSean Young struct code_block code_block = { .length = sizeof(code_block) }; 612acaa34bfSSean Young u8 buf[2]; 613acaa34bfSSean Young int ret, i; 614acaa34bfSSean Young 615acaa34bfSSean Young ret = zilog_ir_format(rcdev, txbuf, count, &code_block); 616acaa34bfSSean Young if (ret) 617acaa34bfSSean Young return ret; 618acaa34bfSSean Young 619acaa34bfSSean Young ret = mutex_lock_interruptible(&ir->lock); 620acaa34bfSSean Young if (ret) 621acaa34bfSSean Young return ret; 622acaa34bfSSean Young 623acaa34bfSSean Young ret = send_data_block(ir, ZILOG_UIR_END, &code_block); 624acaa34bfSSean Young if (ret) 625acaa34bfSSean Young goto out_unlock; 626acaa34bfSSean Young 627acaa34bfSSean Young ret = i2c_master_recv(ir->tx_c, buf, 1); 628acaa34bfSSean Young if (ret != 1) { 629acaa34bfSSean Young dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret); 630acaa34bfSSean Young goto out_unlock; 631acaa34bfSSean Young } 632acaa34bfSSean Young 633acaa34bfSSean Young dev_dbg(&ir->rc->dev, "code set status: %02x\n", buf[0]); 634acaa34bfSSean Young 635acaa34bfSSean Young if (buf[0] != (ZILOG_STATUS_OK | ZILOG_STATUS_SET)) { 636acaa34bfSSean Young dev_err(&ir->rc->dev, "unexpected IR TX response %02x\n", 637acaa34bfSSean Young buf[0]); 638acaa34bfSSean Young ret = -EIO; 639acaa34bfSSean Young goto out_unlock; 640acaa34bfSSean Young } 641acaa34bfSSean Young 642acaa34bfSSean Young buf[0] = 0x00; 643acaa34bfSSean Young buf[1] = ZILOG_SEND; 644acaa34bfSSean Young 645acaa34bfSSean Young ret = i2c_master_send(ir->tx_c, buf, 2); 646acaa34bfSSean Young if (ret != 2) { 647acaa34bfSSean Young dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret); 648acaa34bfSSean Young if (ret >= 0) 649acaa34bfSSean Young ret = -EIO; 650acaa34bfSSean Young goto out_unlock; 651acaa34bfSSean Young } 652acaa34bfSSean Young 653acaa34bfSSean Young dev_dbg(&ir->rc->dev, "send command sent\n"); 654acaa34bfSSean Young 655acaa34bfSSean Young /* 656acaa34bfSSean Young * This bit NAKs until the device is ready, so we retry it 657acaa34bfSSean Young * sleeping a bit each time. This seems to be what the windows 658acaa34bfSSean Young * driver does, approximately. 659acaa34bfSSean Young * Try for up to 1s. 660acaa34bfSSean Young */ 661acaa34bfSSean Young for (i = 0; i < 20; ++i) { 662acaa34bfSSean Young set_current_state(TASK_UNINTERRUPTIBLE); 663acaa34bfSSean Young schedule_timeout(msecs_to_jiffies(50)); 664acaa34bfSSean Young ret = i2c_master_send(ir->tx_c, buf, 1); 665acaa34bfSSean Young if (ret == 1) 666acaa34bfSSean Young break; 667acaa34bfSSean Young dev_dbg(&ir->rc->dev, 668acaa34bfSSean Young "NAK expected: i2c_master_send failed with %d (try %d)\n", 669acaa34bfSSean Young ret, i + 1); 670acaa34bfSSean Young } 671acaa34bfSSean Young 672acaa34bfSSean Young if (ret != 1) { 673acaa34bfSSean Young dev_err(&ir->rc->dev, 674acaa34bfSSean Young "IR TX chip never got ready: last i2c_master_send failed with %d\n", 675acaa34bfSSean Young ret); 676acaa34bfSSean Young if (ret >= 0) 677acaa34bfSSean Young ret = -EIO; 678acaa34bfSSean Young goto out_unlock; 679acaa34bfSSean Young } 680acaa34bfSSean Young 6819c87ae1aSChristophe JAILLET ret = i2c_master_recv(ir->tx_c, buf, 1); 6829c87ae1aSChristophe JAILLET if (ret != 1) { 683acaa34bfSSean Young dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret); 684acaa34bfSSean Young ret = -EIO; 685acaa34bfSSean Young goto out_unlock; 686acaa34bfSSean Young } else if (buf[0] != ZILOG_STATUS_OK) { 687acaa34bfSSean Young dev_err(&ir->rc->dev, "unexpected IR TX response #2: %02x\n", 688acaa34bfSSean Young buf[0]); 689acaa34bfSSean Young ret = -EIO; 690acaa34bfSSean Young goto out_unlock; 691acaa34bfSSean Young } 692acaa34bfSSean Young dev_dbg(&ir->rc->dev, "transmit complete\n"); 693acaa34bfSSean Young 694acaa34bfSSean Young /* Oh good, it worked */ 695acaa34bfSSean Young ret = count; 696acaa34bfSSean Young out_unlock: 697acaa34bfSSean Young mutex_unlock(&ir->lock); 698acaa34bfSSean Young 699acaa34bfSSean Young return ret; 700acaa34bfSSean Young } 701acaa34bfSSean Young 702acaa34bfSSean Young static int zilog_tx_carrier(struct rc_dev *dev, u32 carrier) 703acaa34bfSSean Young { 704acaa34bfSSean Young struct IR_i2c *ir = dev->priv; 705acaa34bfSSean Young 706acaa34bfSSean Young if (carrier > 500000 || carrier < 20000) 707acaa34bfSSean Young return -EINVAL; 708acaa34bfSSean Young 709acaa34bfSSean Young ir->carrier = carrier; 710acaa34bfSSean Young 711acaa34bfSSean Young return 0; 712acaa34bfSSean Young } 713acaa34bfSSean Young 714acaa34bfSSean Young static int zilog_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle) 715acaa34bfSSean Young { 716acaa34bfSSean Young struct IR_i2c *ir = dev->priv; 717acaa34bfSSean Young 718acaa34bfSSean Young ir->duty_cycle = duty_cycle; 719acaa34bfSSean Young 720acaa34bfSSean Young return 0; 721acaa34bfSSean Young } 722cb7a01acSMauro Carvalho Chehab 723cb7a01acSMauro Carvalho Chehab static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id) 724cb7a01acSMauro Carvalho Chehab { 725cb7a01acSMauro Carvalho Chehab char *ir_codes = NULL; 726cb7a01acSMauro Carvalho Chehab const char *name = NULL; 7276d741bfeSSean Young u64 rc_proto = RC_PROTO_BIT_UNKNOWN; 728cb7a01acSMauro Carvalho Chehab struct IR_i2c *ir; 729cb7a01acSMauro Carvalho Chehab struct rc_dev *rc = NULL; 730cb7a01acSMauro Carvalho Chehab struct i2c_adapter *adap = client->adapter; 731cb7a01acSMauro Carvalho Chehab unsigned short addr = client->addr; 7327bc8a0deSSean Young bool probe_tx = (id->driver_data & FLAG_TX) != 0; 733cb7a01acSMauro Carvalho Chehab int err; 734cb7a01acSMauro Carvalho Chehab 7351cb26030SSean Young if ((id->driver_data & FLAG_HDPVR) && !enable_hdpvr) { 7361cb26030SSean Young dev_err(&client->dev, "IR for HDPVR is known to cause problems during recording, use enable_hdpvr modparam to enable\n"); 7371cb26030SSean Young return -ENODEV; 7381cb26030SSean Young } 7391cb26030SSean Young 740c02b211dSLaurent Pinchart ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL); 741cb7a01acSMauro Carvalho Chehab if (!ir) 742cb7a01acSMauro Carvalho Chehab return -ENOMEM; 743cb7a01acSMauro Carvalho Chehab 744cb7a01acSMauro Carvalho Chehab ir->c = client; 745cb7a01acSMauro Carvalho Chehab ir->polling_interval = DEFAULT_POLLING_INTERVAL; 746cb7a01acSMauro Carvalho Chehab i2c_set_clientdata(client, ir); 747cb7a01acSMauro Carvalho Chehab 748cb7a01acSMauro Carvalho Chehab switch(addr) { 749cb7a01acSMauro Carvalho Chehab case 0x64: 750cb7a01acSMauro Carvalho Chehab name = "Pixelview"; 751cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_pixelview; 7526d741bfeSSean Young rc_proto = RC_PROTO_BIT_OTHER; 753cb7a01acSMauro Carvalho Chehab ir_codes = RC_MAP_EMPTY; 754cb7a01acSMauro Carvalho Chehab break; 755cb7a01acSMauro Carvalho Chehab case 0x18: 756cb7a01acSMauro Carvalho Chehab case 0x1f: 757cb7a01acSMauro Carvalho Chehab case 0x1a: 758cb7a01acSMauro Carvalho Chehab name = "Hauppauge"; 759cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_haup; 7606d741bfeSSean Young rc_proto = RC_PROTO_BIT_RC5; 761cb7a01acSMauro Carvalho Chehab ir_codes = RC_MAP_HAUPPAUGE; 762cb7a01acSMauro Carvalho Chehab break; 763cb7a01acSMauro Carvalho Chehab case 0x30: 764cb7a01acSMauro Carvalho Chehab name = "KNC One"; 765cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_knc1; 7666d741bfeSSean Young rc_proto = RC_PROTO_BIT_OTHER; 767cb7a01acSMauro Carvalho Chehab ir_codes = RC_MAP_EMPTY; 768cb7a01acSMauro Carvalho Chehab break; 769cb7a01acSMauro Carvalho Chehab case 0x6b: 770cb7a01acSMauro Carvalho Chehab name = "FusionHDTV"; 771cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_fusionhdtv; 7726d741bfeSSean Young rc_proto = RC_PROTO_BIT_UNKNOWN; 773cb7a01acSMauro Carvalho Chehab ir_codes = RC_MAP_FUSIONHDTV_MCE; 774cb7a01acSMauro Carvalho Chehab break; 775cb7a01acSMauro Carvalho Chehab case 0x40: 776cb7a01acSMauro Carvalho Chehab name = "AVerMedia Cardbus remote"; 777cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_avermedia_cardbus; 7786d741bfeSSean Young rc_proto = RC_PROTO_BIT_OTHER; 779cb7a01acSMauro Carvalho Chehab ir_codes = RC_MAP_AVERMEDIA_CARDBUS; 780cb7a01acSMauro Carvalho Chehab break; 78134fe2784SOndrej Zary case 0x41: 78234fe2784SOndrej Zary name = "AVerMedia EM78P153"; 78334fe2784SOndrej Zary ir->get_key = get_key_avermedia_cardbus; 7846d741bfeSSean Young rc_proto = RC_PROTO_BIT_OTHER; 78534fe2784SOndrej Zary /* RM-KV remote, seems to be same as RM-K6 */ 78634fe2784SOndrej Zary ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6; 78734fe2784SOndrej Zary break; 788cb7a01acSMauro Carvalho Chehab case 0x71: 789cb7a01acSMauro Carvalho Chehab name = "Hauppauge/Zilog Z8"; 790cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_haup_xvr; 7916d741bfeSSean Young rc_proto = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE | 7926d741bfeSSean Young RC_PROTO_BIT_RC6_6A_32; 793cb7a01acSMauro Carvalho Chehab ir_codes = RC_MAP_HAUPPAUGE; 794*c73ba202SSean Young ir->polling_interval = 125; 7957bc8a0deSSean Young probe_tx = true; 796cb7a01acSMauro Carvalho Chehab break; 797cb7a01acSMauro Carvalho Chehab } 798cb7a01acSMauro Carvalho Chehab 799cb7a01acSMauro Carvalho Chehab /* Let the caller override settings */ 800cb7a01acSMauro Carvalho Chehab if (client->dev.platform_data) { 801cb7a01acSMauro Carvalho Chehab const struct IR_i2c_init_data *init_data = 802cb7a01acSMauro Carvalho Chehab client->dev.platform_data; 803cb7a01acSMauro Carvalho Chehab 804cb7a01acSMauro Carvalho Chehab ir_codes = init_data->ir_codes; 805cb7a01acSMauro Carvalho Chehab rc = init_data->rc_dev; 806cb7a01acSMauro Carvalho Chehab 807cb7a01acSMauro Carvalho Chehab name = init_data->name; 808cb7a01acSMauro Carvalho Chehab if (init_data->type) 8096d741bfeSSean Young rc_proto = init_data->type; 810cb7a01acSMauro Carvalho Chehab 811cb7a01acSMauro Carvalho Chehab if (init_data->polling_interval) 812cb7a01acSMauro Carvalho Chehab ir->polling_interval = init_data->polling_interval; 813cb7a01acSMauro Carvalho Chehab 814cb7a01acSMauro Carvalho Chehab switch (init_data->internal_get_key_func) { 815cb7a01acSMauro Carvalho Chehab case IR_KBD_GET_KEY_CUSTOM: 816cb7a01acSMauro Carvalho Chehab /* The bridge driver provided us its own function */ 817cb7a01acSMauro Carvalho Chehab ir->get_key = init_data->get_key; 818cb7a01acSMauro Carvalho Chehab break; 819cb7a01acSMauro Carvalho Chehab case IR_KBD_GET_KEY_PIXELVIEW: 820cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_pixelview; 821cb7a01acSMauro Carvalho Chehab break; 822cb7a01acSMauro Carvalho Chehab case IR_KBD_GET_KEY_HAUP: 823cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_haup; 824cb7a01acSMauro Carvalho Chehab break; 825cb7a01acSMauro Carvalho Chehab case IR_KBD_GET_KEY_KNC1: 826cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_knc1; 827cb7a01acSMauro Carvalho Chehab break; 828cb7a01acSMauro Carvalho Chehab case IR_KBD_GET_KEY_FUSIONHDTV: 829cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_fusionhdtv; 830cb7a01acSMauro Carvalho Chehab break; 831cb7a01acSMauro Carvalho Chehab case IR_KBD_GET_KEY_HAUP_XVR: 832cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_haup_xvr; 833cb7a01acSMauro Carvalho Chehab break; 834cb7a01acSMauro Carvalho Chehab case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS: 835cb7a01acSMauro Carvalho Chehab ir->get_key = get_key_avermedia_cardbus; 836cb7a01acSMauro Carvalho Chehab break; 837cb7a01acSMauro Carvalho Chehab } 838cb7a01acSMauro Carvalho Chehab } 839cb7a01acSMauro Carvalho Chehab 840cb7a01acSMauro Carvalho Chehab if (!rc) { 841cb7a01acSMauro Carvalho Chehab /* 84239c1cb2bSJonathan McCrohan * If platform_data doesn't specify rc_dev, initialize it 843cb7a01acSMauro Carvalho Chehab * internally 844cb7a01acSMauro Carvalho Chehab */ 8450f7499fdSAndi Shyti rc = rc_allocate_device(RC_DRIVER_SCANCODE); 846c02b211dSLaurent Pinchart if (!rc) 847c02b211dSLaurent Pinchart return -ENOMEM; 848cb7a01acSMauro Carvalho Chehab } 849cb7a01acSMauro Carvalho Chehab ir->rc = rc; 850cb7a01acSMauro Carvalho Chehab 851cb7a01acSMauro Carvalho Chehab /* Make sure we are all setup before going on */ 8526d741bfeSSean Young if (!name || !ir->get_key || !rc_proto || !ir_codes) { 85350a762b4SSean Young dev_warn(&client->dev, "Unsupported device at address 0x%02x\n", 854cb7a01acSMauro Carvalho Chehab addr); 855cb7a01acSMauro Carvalho Chehab err = -ENODEV; 856cb7a01acSMauro Carvalho Chehab goto err_out_free; 857cb7a01acSMauro Carvalho Chehab } 858cb7a01acSMauro Carvalho Chehab 859cb7a01acSMauro Carvalho Chehab ir->ir_codes = ir_codes; 860cb7a01acSMauro Carvalho Chehab 861afc7f24cSSean Young snprintf(ir->phys, sizeof(ir->phys), "%s/%s", dev_name(&adap->dev), 862cb7a01acSMauro Carvalho Chehab dev_name(&client->dev)); 863cb7a01acSMauro Carvalho Chehab 864cb7a01acSMauro Carvalho Chehab /* 865cb7a01acSMauro Carvalho Chehab * Initialize input_dev fields 866cb7a01acSMauro Carvalho Chehab * It doesn't make sense to allow overriding them via platform_data 867cb7a01acSMauro Carvalho Chehab */ 868cb7a01acSMauro Carvalho Chehab rc->input_id.bustype = BUS_I2C; 869cb7a01acSMauro Carvalho Chehab rc->input_phys = ir->phys; 870afc7f24cSSean Young rc->device_name = name; 871afc7f24cSSean Young rc->dev.parent = &client->dev; 872a6927d81SSean Young rc->priv = ir; 873a6927d81SSean Young rc->open = ir_open; 874a6927d81SSean Young rc->close = ir_close; 875cb7a01acSMauro Carvalho Chehab 876cb7a01acSMauro Carvalho Chehab /* 877cb7a01acSMauro Carvalho Chehab * Initialize the other fields of rc_dev 878cb7a01acSMauro Carvalho Chehab */ 879cb7a01acSMauro Carvalho Chehab rc->map_name = ir->ir_codes; 8806d741bfeSSean Young rc->allowed_protocols = rc_proto; 881cb7a01acSMauro Carvalho Chehab if (!rc->driver_name) 88250a762b4SSean Young rc->driver_name = KBUILD_MODNAME; 883cb7a01acSMauro Carvalho Chehab 884acaa34bfSSean Young mutex_init(&ir->lock); 885acaa34bfSSean Young 886a6927d81SSean Young INIT_DELAYED_WORK(&ir->work, ir_work); 887a6927d81SSean Young 8887bc8a0deSSean Young if (probe_tx) { 8891b09a2afSWolfram Sang ir->tx_c = i2c_new_dummy_device(client->adapter, 0x70); 8901b09a2afSWolfram Sang if (IS_ERR(ir->tx_c)) { 891acaa34bfSSean Young dev_err(&client->dev, "failed to setup tx i2c address"); 8921b09a2afSWolfram Sang err = PTR_ERR(ir->tx_c); 8931b09a2afSWolfram Sang goto err_out_free; 894acaa34bfSSean Young } else if (!zilog_init(ir)) { 895acaa34bfSSean Young ir->carrier = 38000; 896acaa34bfSSean Young ir->duty_cycle = 40; 897acaa34bfSSean Young rc->tx_ir = zilog_tx; 898acaa34bfSSean Young rc->s_tx_carrier = zilog_tx_carrier; 899acaa34bfSSean Young rc->s_tx_duty_cycle = zilog_tx_duty_cycle; 900acaa34bfSSean Young } 901acaa34bfSSean Young } 902acaa34bfSSean Young 903cb7a01acSMauro Carvalho Chehab err = rc_register_device(rc); 904cb7a01acSMauro Carvalho Chehab if (err) 905cb7a01acSMauro Carvalho Chehab goto err_out_free; 906cb7a01acSMauro Carvalho Chehab 907cb7a01acSMauro Carvalho Chehab return 0; 908cb7a01acSMauro Carvalho Chehab 909cb7a01acSMauro Carvalho Chehab err_out_free: 9101b09a2afSWolfram Sang if (!IS_ERR(ir->tx_c)) 911acaa34bfSSean Young i2c_unregister_device(ir->tx_c); 912acaa34bfSSean Young 913cb7a01acSMauro Carvalho Chehab /* Only frees rc if it were allocated internally */ 914cb7a01acSMauro Carvalho Chehab rc_free_device(rc); 915cb7a01acSMauro Carvalho Chehab return err; 916cb7a01acSMauro Carvalho Chehab } 917cb7a01acSMauro Carvalho Chehab 918cb7a01acSMauro Carvalho Chehab static int ir_remove(struct i2c_client *client) 919cb7a01acSMauro Carvalho Chehab { 920cb7a01acSMauro Carvalho Chehab struct IR_i2c *ir = i2c_get_clientdata(client); 921cb7a01acSMauro Carvalho Chehab 922cb7a01acSMauro Carvalho Chehab cancel_delayed_work_sync(&ir->work); 923cb7a01acSMauro Carvalho Chehab 924acaa34bfSSean Young i2c_unregister_device(ir->tx_c); 925acaa34bfSSean Young 926cb7a01acSMauro Carvalho Chehab rc_unregister_device(ir->rc); 927cb7a01acSMauro Carvalho Chehab 928cb7a01acSMauro Carvalho Chehab return 0; 929cb7a01acSMauro Carvalho Chehab } 930cb7a01acSMauro Carvalho Chehab 931cb7a01acSMauro Carvalho Chehab static const struct i2c_device_id ir_kbd_id[] = { 932cb7a01acSMauro Carvalho Chehab /* Generic entry for any IR receiver */ 933cb7a01acSMauro Carvalho Chehab { "ir_video", 0 }, 934cb7a01acSMauro Carvalho Chehab /* IR device specific entries should be added here */ 935acaa34bfSSean Young { "ir_z8f0811_haup", FLAG_TX }, 936acaa34bfSSean Young { "ir_z8f0811_hdpvr", FLAG_TX | FLAG_HDPVR }, 937cb7a01acSMauro Carvalho Chehab { } 938cb7a01acSMauro Carvalho Chehab }; 9391cb26030SSean Young MODULE_DEVICE_TABLE(i2c, ir_kbd_id); 940cb7a01acSMauro Carvalho Chehab 941cb7a01acSMauro Carvalho Chehab static struct i2c_driver ir_kbd_driver = { 942cb7a01acSMauro Carvalho Chehab .driver = { 943cb7a01acSMauro Carvalho Chehab .name = "ir-kbd-i2c", 944cb7a01acSMauro Carvalho Chehab }, 945cb7a01acSMauro Carvalho Chehab .probe = ir_probe, 946cb7a01acSMauro Carvalho Chehab .remove = ir_remove, 947cb7a01acSMauro Carvalho Chehab .id_table = ir_kbd_id, 948cb7a01acSMauro Carvalho Chehab }; 949cb7a01acSMauro Carvalho Chehab 950cb7a01acSMauro Carvalho Chehab module_i2c_driver(ir_kbd_driver); 951cb7a01acSMauro Carvalho Chehab 952cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */ 953cb7a01acSMauro Carvalho Chehab 954cb7a01acSMauro Carvalho Chehab MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller"); 955cb7a01acSMauro Carvalho Chehab MODULE_DESCRIPTION("input driver for i2c IR remote controls"); 956cb7a01acSMauro Carvalho Chehab MODULE_LICENSE("GPL"); 957