Path: blob/master/drivers/media/video/hdpvr/hdpvr-i2c.c
17680 views
1/*2* Hauppauge HD PVR USB driver3*4* Copyright (C) 2008 Janne Grunau ([email protected])5*6* IR device registration code is7* Copyright (C) 2010 Andy Walls <[email protected]>8*9* This program is free software; you can redistribute it and/or10* modify it under the terms of the GNU General Public License as11* published by the Free Software Foundation, version 2.12*13*/1415#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)1617#include <linux/i2c.h>18#include <linux/slab.h>1920#include "hdpvr.h"2122#define CTRL_READ_REQUEST 0xb823#define CTRL_WRITE_REQUEST 0x382425#define REQTYPE_I2C_READ 0xb126#define REQTYPE_I2C_WRITE 0xb027#define REQTYPE_I2C_WRITE_STATT 0xd02829#define Z8F0811_IR_TX_I2C_ADDR 0x7030#define Z8F0811_IR_RX_I2C_ADDR 0x71313233struct i2c_client *hdpvr_register_ir_tx_i2c(struct hdpvr_device *dev)34{35struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;36struct i2c_board_info hdpvr_ir_tx_i2c_board_info = {37I2C_BOARD_INFO("ir_tx_z8f0811_hdpvr", Z8F0811_IR_TX_I2C_ADDR),38};3940init_data->name = "HD-PVR";41hdpvr_ir_tx_i2c_board_info.platform_data = init_data;4243return i2c_new_device(&dev->i2c_adapter, &hdpvr_ir_tx_i2c_board_info);44}4546struct i2c_client *hdpvr_register_ir_rx_i2c(struct hdpvr_device *dev)47{48struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;49struct i2c_board_info hdpvr_ir_rx_i2c_board_info = {50I2C_BOARD_INFO("ir_rx_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR),51};5253/* Our default information for ir-kbd-i2c.c to use */54init_data->ir_codes = RC_MAP_HAUPPAUGE;55init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;56init_data->type = RC_TYPE_RC5;57init_data->name = "HD-PVR";58init_data->polling_interval = 405; /* ms, duplicated from Windows */59hdpvr_ir_rx_i2c_board_info.platform_data = init_data;6061return i2c_new_device(&dev->i2c_adapter, &hdpvr_ir_rx_i2c_board_info);62}6364static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,65unsigned char addr, char *wdata, int wlen,66char *data, int len)67{68int ret;6970if ((len > sizeof(dev->i2c_buf)) || (wlen > sizeof(dev->i2c_buf)))71return -EINVAL;7273if (wlen) {74memcpy(&dev->i2c_buf, wdata, wlen);75ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),76REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,77(bus << 8) | addr, 0, &dev->i2c_buf,78wlen, 1000);79if (ret < 0)80return ret;81}8283ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),84REQTYPE_I2C_READ, CTRL_READ_REQUEST,85(bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);8687if (ret == len) {88memcpy(data, &dev->i2c_buf, len);89ret = 0;90} else if (ret >= 0)91ret = -EIO;9293return ret;94}9596static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,97unsigned char addr, char *data, int len)98{99int ret;100101if (len > sizeof(dev->i2c_buf))102return -EINVAL;103104memcpy(&dev->i2c_buf, data, len);105ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),106REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,107(bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);108109if (ret < 0)110return ret;111112ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),113REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,1140, 0, &dev->i2c_buf, 2, 1000);115116if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))117ret = 0;118else if (ret >= 0)119ret = -EIO;120121return ret;122}123124static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,125int num)126{127struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);128int retval = 0, addr;129130if (num <= 0)131return 0;132133mutex_lock(&dev->i2c_mutex);134135addr = msgs[0].addr << 1;136137if (num == 1) {138if (msgs[0].flags & I2C_M_RD)139retval = hdpvr_i2c_read(dev, 1, addr, NULL, 0,140msgs[0].buf, msgs[0].len);141else142retval = hdpvr_i2c_write(dev, 1, addr, msgs[0].buf,143msgs[0].len);144} else if (num == 2) {145if (msgs[0].addr != msgs[1].addr) {146v4l2_warn(&dev->v4l2_dev, "refusing 2-phase i2c xfer "147"with conflicting target addresses\n");148retval = -EINVAL;149goto out;150}151152if ((msgs[0].flags & I2C_M_RD) || !(msgs[1].flags & I2C_M_RD)) {153v4l2_warn(&dev->v4l2_dev, "refusing complex xfer with "154"r0=%d, r1=%d\n", msgs[0].flags & I2C_M_RD,155msgs[1].flags & I2C_M_RD);156retval = -EINVAL;157goto out;158}159160/*161* Write followed by atomic read is the only complex xfer that162* we actually support here.163*/164retval = hdpvr_i2c_read(dev, 1, addr, msgs[0].buf, msgs[0].len,165msgs[1].buf, msgs[1].len);166} else {167v4l2_warn(&dev->v4l2_dev, "refusing %d-phase i2c xfer\n", num);168}169170out:171mutex_unlock(&dev->i2c_mutex);172173return retval ? retval : num;174}175176static u32 hdpvr_functionality(struct i2c_adapter *adapter)177{178return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;179}180181static struct i2c_algorithm hdpvr_algo = {182.master_xfer = hdpvr_transfer,183.functionality = hdpvr_functionality,184};185186static struct i2c_adapter hdpvr_i2c_adapter_template = {187.name = "Hauppage HD PVR I2C",188.owner = THIS_MODULE,189.algo = &hdpvr_algo,190};191192static int hdpvr_activate_ir(struct hdpvr_device *dev)193{194char buffer[2];195196mutex_lock(&dev->i2c_mutex);197198hdpvr_i2c_read(dev, 0, 0x54, NULL, 0, buffer, 1);199200buffer[0] = 0;201buffer[1] = 0x8;202hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);203204buffer[1] = 0x18;205hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);206207mutex_unlock(&dev->i2c_mutex);208209return 0;210}211212int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)213{214int retval = -ENOMEM;215216hdpvr_activate_ir(dev);217218memcpy(&dev->i2c_adapter, &hdpvr_i2c_adapter_template,219sizeof(struct i2c_adapter));220dev->i2c_adapter.dev.parent = &dev->udev->dev;221222i2c_set_adapdata(&dev->i2c_adapter, dev);223224retval = i2c_add_adapter(&dev->i2c_adapter);225226return retval;227}228229#endif230231232