blob: 3e0658768135255bced783b1d613c4d3da5a7d83 [file] [log] [blame]
Janne Grunau9aba42e2009-03-18 18:10:04 -03001
2/*
Janne Grunaue86da6f2009-03-19 19:00:35 -03003 * Hauppauge HD PVR USB driver
Janne Grunau9aba42e2009-03-18 18:10:04 -03004 *
5 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
6 *
Andy Wallsea6c0602010-12-28 22:46:13 -03007 * IR device registration code is
8 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
9 *
Janne Grunau9aba42e2009-03-18 18:10:04 -030010 * 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.
13 *
14 */
15
Jarod Wilson324b04b2011-01-14 16:25:21 -030016#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
17
Janne Grunau9aba42e2009-03-18 18:10:04 -030018#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Janne Grunau9aba42e2009-03-18 18:10:04 -030020
21#include "hdpvr.h"
22
23#define CTRL_READ_REQUEST 0xb8
24#define CTRL_WRITE_REQUEST 0x38
25
26#define REQTYPE_I2C_READ 0xb1
27#define REQTYPE_I2C_WRITE 0xb0
28#define REQTYPE_I2C_WRITE_STATT 0xd0
29
Andy Wallsea6c0602010-12-28 22:46:13 -030030#define Z8F0811_IR_TX_I2C_ADDR 0x70
31#define Z8F0811_IR_RX_I2C_ADDR 0x71
32
Jarod Wilson324b04b2011-01-14 16:25:21 -030033
Jarod Wilson7f2a06d2011-01-19 18:10:14 -030034struct i2c_client *hdpvr_register_ir_tx_i2c(struct hdpvr_device *dev)
Andy Wallsea6c0602010-12-28 22:46:13 -030035{
Jarod Wilson324b04b2011-01-14 16:25:21 -030036 struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
Jarod Wilson7f2a06d2011-01-19 18:10:14 -030037 struct i2c_board_info hdpvr_ir_tx_i2c_board_info = {
38 I2C_BOARD_INFO("ir_tx_z8f0811_hdpvr", Z8F0811_IR_TX_I2C_ADDR),
39 };
40
41 init_data->name = "HD-PVR";
42 hdpvr_ir_tx_i2c_board_info.platform_data = init_data;
43
44 return i2c_new_device(&dev->i2c_adapter, &hdpvr_ir_tx_i2c_board_info);
45}
46
47struct i2c_client *hdpvr_register_ir_rx_i2c(struct hdpvr_device *dev)
48{
49 struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
50 struct i2c_board_info hdpvr_ir_rx_i2c_board_info = {
51 I2C_BOARD_INFO("ir_rx_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR),
52 };
Andy Wallsea6c0602010-12-28 22:46:13 -030053
Jarod Wilson324b04b2011-01-14 16:25:21 -030054 /* Our default information for ir-kbd-i2c.c to use */
55 init_data->ir_codes = RC_MAP_HAUPPAUGE_NEW;
56 init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
57 init_data->type = RC_TYPE_RC5;
Jarod Wilson7f2a06d2011-01-19 18:10:14 -030058 init_data->name = "HD-PVR";
59 hdpvr_ir_rx_i2c_board_info.platform_data = init_data;
Andy Wallsea6c0602010-12-28 22:46:13 -030060
Jarod Wilson7f2a06d2011-01-19 18:10:14 -030061 return i2c_new_device(&dev->i2c_adapter, &hdpvr_ir_rx_i2c_board_info);
Andy Wallsea6c0602010-12-28 22:46:13 -030062}
63
Jarod Wilson324b04b2011-01-14 16:25:21 -030064static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
Jarod Wilsonb443ac52011-03-02 13:23:52 -030065 unsigned char addr, char *wdata, int wlen,
66 char *data, int len)
Janne Grunau9aba42e2009-03-18 18:10:04 -030067{
68 int ret;
Jarod Wilson559d1622011-01-14 16:40:32 -030069
Jarod Wilsonb443ac52011-03-02 13:23:52 -030070 if ((len > sizeof(dev->i2c_buf)) || (wlen > sizeof(dev->i2c_buf)))
Jarod Wilson559d1622011-01-14 16:40:32 -030071 return -EINVAL;
Janne Grunau9aba42e2009-03-18 18:10:04 -030072
Jarod Wilsonb443ac52011-03-02 13:23:52 -030073 if (wlen) {
74 memcpy(&dev->i2c_buf, wdata, wlen);
75 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
76 REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
77 (bus << 8) | addr, 0, &dev->i2c_buf,
78 wlen, 1000);
79 if (ret < 0)
80 return ret;
81 }
82
83 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
Janne Grunau9aba42e2009-03-18 18:10:04 -030084 REQTYPE_I2C_READ, CTRL_READ_REQUEST,
Jarod Wilson559d1622011-01-14 16:40:32 -030085 (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
Janne Grunau9aba42e2009-03-18 18:10:04 -030086
87 if (ret == len) {
Jarod Wilson559d1622011-01-14 16:40:32 -030088 memcpy(data, &dev->i2c_buf, len);
Janne Grunau9aba42e2009-03-18 18:10:04 -030089 ret = 0;
90 } else if (ret >= 0)
91 ret = -EIO;
92
Janne Grunau9aba42e2009-03-18 18:10:04 -030093 return ret;
94}
95
Jarod Wilson324b04b2011-01-14 16:25:21 -030096static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
97 unsigned char addr, char *data, int len)
Janne Grunau9aba42e2009-03-18 18:10:04 -030098{
99 int ret;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300100
Jarod Wilson559d1622011-01-14 16:40:32 -0300101 if (len > sizeof(dev->i2c_buf))
102 return -EINVAL;
103
104 memcpy(&dev->i2c_buf, data, len);
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300105 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
Janne Grunau9aba42e2009-03-18 18:10:04 -0300106 REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
Jarod Wilson559d1622011-01-14 16:40:32 -0300107 (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300108
109 if (ret < 0)
Jarod Wilson559d1622011-01-14 16:40:32 -0300110 return ret;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300111
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300112 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
Janne Grunau9aba42e2009-03-18 18:10:04 -0300113 REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
Jarod Wilson559d1622011-01-14 16:40:32 -0300114 0, 0, &dev->i2c_buf, 2, 1000);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300115
Jarod Wilson559d1622011-01-14 16:40:32 -0300116 if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
Janne Grunau9aba42e2009-03-18 18:10:04 -0300117 ret = 0;
118 else if (ret >= 0)
119 ret = -EIO;
120
Janne Grunau9aba42e2009-03-18 18:10:04 -0300121 return ret;
122}
123
124static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
125 int num)
126{
127 struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300128 int retval = 0, addr;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300129
130 if (num <= 0)
131 return 0;
132
133 mutex_lock(&dev->i2c_mutex);
134
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300135 addr = msgs[0].addr << 1;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300136
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300137 if (num == 1) {
138 if (msgs[0].flags & I2C_M_RD)
139 retval = hdpvr_i2c_read(dev, 1, addr, NULL, 0,
140 msgs[0].buf, msgs[0].len);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300141 else
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300142 retval = hdpvr_i2c_write(dev, 1, addr, msgs[0].buf,
143 msgs[0].len);
144 } else if (num == 2) {
145 if (msgs[0].addr != msgs[1].addr) {
146 v4l2_warn(&dev->v4l2_dev, "refusing 2-phase i2c xfer "
147 "with conflicting target addresses\n");
148 retval = -EINVAL;
149 goto out;
150 }
151
152 if ((msgs[0].flags & I2C_M_RD) || !(msgs[1].flags & I2C_M_RD)) {
153 v4l2_warn(&dev->v4l2_dev, "refusing complex xfer with "
154 "r0=%d, r1=%d\n", msgs[0].flags & I2C_M_RD,
155 msgs[1].flags & I2C_M_RD);
156 retval = -EINVAL;
157 goto out;
158 }
159
160 /*
161 * Write followed by atomic read is the only complex xfer that
162 * we actually support here.
163 */
164 retval = hdpvr_i2c_read(dev, 1, addr, msgs[0].buf, msgs[0].len,
165 msgs[1].buf, msgs[1].len);
166 } else {
167 v4l2_warn(&dev->v4l2_dev, "refusing %d-phase i2c xfer\n", num);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300168 }
169
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300170out:
Janne Grunau9aba42e2009-03-18 18:10:04 -0300171 mutex_unlock(&dev->i2c_mutex);
172
173 return retval ? retval : num;
174}
175
176static u32 hdpvr_functionality(struct i2c_adapter *adapter)
177{
178 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
179}
180
181static struct i2c_algorithm hdpvr_algo = {
182 .master_xfer = hdpvr_transfer,
183 .functionality = hdpvr_functionality,
184};
185
Jarod Wilson324b04b2011-01-14 16:25:21 -0300186static struct i2c_adapter hdpvr_i2c_adapter_template = {
187 .name = "Hauppage HD PVR I2C",
188 .owner = THIS_MODULE,
189 .algo = &hdpvr_algo,
190};
191
192static int hdpvr_activate_ir(struct hdpvr_device *dev)
193{
194 char buffer[8];
195
196 mutex_lock(&dev->i2c_mutex);
197
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300198 hdpvr_i2c_read(dev, 0, 0x54, NULL, 0, buffer, 1);
Jarod Wilson324b04b2011-01-14 16:25:21 -0300199
200 buffer[0] = 0;
201 buffer[1] = 0x8;
202 hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
203
204 buffer[1] = 0x18;
205 hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
206
207 mutex_unlock(&dev->i2c_mutex);
208
209 return 0;
210}
211
Janne Grunau9aba42e2009-03-18 18:10:04 -0300212int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
213{
Janne Grunau9aba42e2009-03-18 18:10:04 -0300214 int retval = -ENOMEM;
215
Jarod Wilson324b04b2011-01-14 16:25:21 -0300216 hdpvr_activate_ir(dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300217
Jarod Wilson324b04b2011-01-14 16:25:21 -0300218 memcpy(&dev->i2c_adapter, &hdpvr_i2c_adapter_template,
219 sizeof(struct i2c_adapter));
220 dev->i2c_adapter.dev.parent = &dev->udev->dev;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300221
Jarod Wilson324b04b2011-01-14 16:25:21 -0300222 i2c_set_adapdata(&dev->i2c_adapter, dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300223
Jarod Wilson324b04b2011-01-14 16:25:21 -0300224 retval = i2c_add_adapter(&dev->i2c_adapter);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300225
Janne Grunau9aba42e2009-03-18 18:10:04 -0300226 return retval;
227}
Jarod Wilson324b04b2011-01-14 16:25:21 -0300228
229#endif