Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / media / usb / hdpvr / hdpvr-i2c.c
blob4720d79b0282398d06feb6c85dbdfd42d06e5ea8
2 /*
3 * Hauppauge HD PVR USB driver
5 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
7 * IR device registration code is
8 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2.
16 #if IS_ENABLED(CONFIG_I2C)
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/export.h>
22 #include "hdpvr.h"
24 #define CTRL_READ_REQUEST 0xb8
25 #define CTRL_WRITE_REQUEST 0x38
27 #define REQTYPE_I2C_READ 0xb1
28 #define REQTYPE_I2C_WRITE 0xb0
29 #define REQTYPE_I2C_WRITE_STATT 0xd0
31 #define Z8F0811_IR_TX_I2C_ADDR 0x70
32 #define Z8F0811_IR_RX_I2C_ADDR 0x71
35 struct i2c_client *hdpvr_register_ir_i2c(struct hdpvr_device *dev)
37 struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
38 struct i2c_board_info info = {
39 I2C_BOARD_INFO("ir_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR),
42 /* Our default information for ir-kbd-i2c.c to use */
43 init_data->ir_codes = RC_MAP_HAUPPAUGE;
44 init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
45 init_data->type = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
46 RC_PROTO_BIT_RC6_6A_32;
47 init_data->name = "HD-PVR";
48 init_data->polling_interval = 405; /* ms, duplicated from Windows */
49 info.platform_data = init_data;
51 return i2c_new_device(&dev->i2c_adapter, &info);
54 static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
55 unsigned char addr, char *wdata, int wlen,
56 char *data, int len)
58 int ret;
60 if ((len > sizeof(dev->i2c_buf)) || (wlen > sizeof(dev->i2c_buf)))
61 return -EINVAL;
63 if (wlen) {
64 memcpy(&dev->i2c_buf, wdata, wlen);
65 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
66 REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
67 (bus << 8) | addr, 0, &dev->i2c_buf,
68 wlen, 1000);
69 if (ret < 0)
70 return ret;
73 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
74 REQTYPE_I2C_READ, CTRL_READ_REQUEST,
75 (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
77 if (ret == len) {
78 memcpy(data, &dev->i2c_buf, len);
79 ret = 0;
80 } else if (ret >= 0)
81 ret = -EIO;
83 return ret;
86 static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
87 unsigned char addr, char *data, int len)
89 int ret;
91 if (len > sizeof(dev->i2c_buf))
92 return -EINVAL;
94 memcpy(&dev->i2c_buf, data, len);
95 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
96 REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
97 (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
99 if (ret < 0)
100 return ret;
102 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
103 REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
104 0, 0, &dev->i2c_buf, 2, 1000);
106 if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
107 ret = 0;
108 else if (ret >= 0)
109 ret = -EIO;
111 return ret;
114 static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
115 int num)
117 struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
118 int retval = 0, addr;
120 if (num <= 0)
121 return 0;
123 mutex_lock(&dev->i2c_mutex);
125 addr = msgs[0].addr << 1;
127 if (num == 1) {
128 if (msgs[0].flags & I2C_M_RD)
129 retval = hdpvr_i2c_read(dev, 1, addr, NULL, 0,
130 msgs[0].buf, msgs[0].len);
131 else
132 retval = hdpvr_i2c_write(dev, 1, addr, msgs[0].buf,
133 msgs[0].len);
134 } else if (num == 2) {
135 if (msgs[0].addr != msgs[1].addr) {
136 v4l2_warn(&dev->v4l2_dev, "refusing 2-phase i2c xfer with conflicting target addresses\n");
137 retval = -EINVAL;
138 goto out;
141 if ((msgs[0].flags & I2C_M_RD) || !(msgs[1].flags & I2C_M_RD)) {
142 v4l2_warn(&dev->v4l2_dev, "refusing complex xfer with r0=%d, r1=%d\n",
143 msgs[0].flags & I2C_M_RD,
144 msgs[1].flags & I2C_M_RD);
145 retval = -EINVAL;
146 goto out;
150 * Write followed by atomic read is the only complex xfer that
151 * we actually support here.
153 retval = hdpvr_i2c_read(dev, 1, addr, msgs[0].buf, msgs[0].len,
154 msgs[1].buf, msgs[1].len);
155 } else {
156 v4l2_warn(&dev->v4l2_dev, "refusing %d-phase i2c xfer\n", num);
159 out:
160 mutex_unlock(&dev->i2c_mutex);
162 return retval ? retval : num;
165 static u32 hdpvr_functionality(struct i2c_adapter *adapter)
167 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
170 static const struct i2c_algorithm hdpvr_algo = {
171 .master_xfer = hdpvr_transfer,
172 .functionality = hdpvr_functionality,
175 static const struct i2c_adapter hdpvr_i2c_adapter_template = {
176 .name = "Hauppage HD PVR I2C",
177 .owner = THIS_MODULE,
178 .algo = &hdpvr_algo,
181 static int hdpvr_activate_ir(struct hdpvr_device *dev)
183 char buffer[2];
185 mutex_lock(&dev->i2c_mutex);
187 hdpvr_i2c_read(dev, 0, 0x54, NULL, 0, buffer, 1);
189 buffer[0] = 0;
190 buffer[1] = 0x8;
191 hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
193 buffer[1] = 0x18;
194 hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
196 mutex_unlock(&dev->i2c_mutex);
198 return 0;
201 int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
203 int retval = -ENOMEM;
205 hdpvr_activate_ir(dev);
207 dev->i2c_adapter = hdpvr_i2c_adapter_template;
208 dev->i2c_adapter.dev.parent = &dev->udev->dev;
210 i2c_set_adapdata(&dev->i2c_adapter, dev);
212 retval = i2c_add_adapter(&dev->i2c_adapter);
214 return retval;
217 #endif