blob: 82e819fa91c0814047074b867ae2bca713570b75 [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>
Paul Gortmaker35a24632011-08-01 15:26:38 -040020#include <linux/export.h>
Janne Grunau9aba42e2009-03-18 18:10:04 -030021
22#include "hdpvr.h"
23
24#define CTRL_READ_REQUEST 0xb8
25#define CTRL_WRITE_REQUEST 0x38
26
27#define REQTYPE_I2C_READ 0xb1
28#define REQTYPE_I2C_WRITE 0xb0
29#define REQTYPE_I2C_WRITE_STATT 0xd0
30
Andy Wallsea6c0602010-12-28 22:46:13 -030031#define Z8F0811_IR_TX_I2C_ADDR 0x70
32#define Z8F0811_IR_RX_I2C_ADDR 0x71
33
Jarod Wilson324b04b2011-01-14 16:25:21 -030034
Jarod Wilson7f2a06d2011-01-19 18:10:14 -030035struct i2c_client *hdpvr_register_ir_tx_i2c(struct hdpvr_device *dev)
Andy Wallsea6c0602010-12-28 22:46:13 -030036{
Jarod Wilson324b04b2011-01-14 16:25:21 -030037 struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
Jarod Wilson7f2a06d2011-01-19 18:10:14 -030038 struct i2c_board_info hdpvr_ir_tx_i2c_board_info = {
39 I2C_BOARD_INFO("ir_tx_z8f0811_hdpvr", Z8F0811_IR_TX_I2C_ADDR),
40 };
41
42 init_data->name = "HD-PVR";
43 hdpvr_ir_tx_i2c_board_info.platform_data = init_data;
44
45 return i2c_new_device(&dev->i2c_adapter, &hdpvr_ir_tx_i2c_board_info);
46}
47
48struct i2c_client *hdpvr_register_ir_rx_i2c(struct hdpvr_device *dev)
49{
50 struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
51 struct i2c_board_info hdpvr_ir_rx_i2c_board_info = {
52 I2C_BOARD_INFO("ir_rx_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR),
53 };
Andy Wallsea6c0602010-12-28 22:46:13 -030054
Jarod Wilson324b04b2011-01-14 16:25:21 -030055 /* Our default information for ir-kbd-i2c.c to use */
Mauro Carvalho Chehabaf86ce72011-01-24 12:18:48 -030056 init_data->ir_codes = RC_MAP_HAUPPAUGE;
Jarod Wilson324b04b2011-01-14 16:25:21 -030057 init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
58 init_data->type = RC_TYPE_RC5;
Jarod Wilson7f2a06d2011-01-19 18:10:14 -030059 init_data->name = "HD-PVR";
Jarod Wilsondc8e2aa2011-03-04 17:31:11 -030060 init_data->polling_interval = 405; /* ms, duplicated from Windows */
Jarod Wilson7f2a06d2011-01-19 18:10:14 -030061 hdpvr_ir_rx_i2c_board_info.platform_data = init_data;
Andy Wallsea6c0602010-12-28 22:46:13 -030062
Jarod Wilson7f2a06d2011-01-19 18:10:14 -030063 return i2c_new_device(&dev->i2c_adapter, &hdpvr_ir_rx_i2c_board_info);
Andy Wallsea6c0602010-12-28 22:46:13 -030064}
65
Jarod Wilson324b04b2011-01-14 16:25:21 -030066static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
Jarod Wilsonb443ac52011-03-02 13:23:52 -030067 unsigned char addr, char *wdata, int wlen,
68 char *data, int len)
Janne Grunau9aba42e2009-03-18 18:10:04 -030069{
70 int ret;
Jarod Wilson559d162e2011-01-14 16:40:32 -030071
Jarod Wilsonb443ac52011-03-02 13:23:52 -030072 if ((len > sizeof(dev->i2c_buf)) || (wlen > sizeof(dev->i2c_buf)))
Jarod Wilson559d162e2011-01-14 16:40:32 -030073 return -EINVAL;
Janne Grunau9aba42e2009-03-18 18:10:04 -030074
Jarod Wilsonb443ac52011-03-02 13:23:52 -030075 if (wlen) {
76 memcpy(&dev->i2c_buf, wdata, wlen);
77 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
78 REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
79 (bus << 8) | addr, 0, &dev->i2c_buf,
80 wlen, 1000);
81 if (ret < 0)
82 return ret;
83 }
84
85 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
Janne Grunau9aba42e2009-03-18 18:10:04 -030086 REQTYPE_I2C_READ, CTRL_READ_REQUEST,
Jarod Wilson559d162e2011-01-14 16:40:32 -030087 (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
Janne Grunau9aba42e2009-03-18 18:10:04 -030088
89 if (ret == len) {
Jarod Wilson559d162e2011-01-14 16:40:32 -030090 memcpy(data, &dev->i2c_buf, len);
Janne Grunau9aba42e2009-03-18 18:10:04 -030091 ret = 0;
92 } else if (ret >= 0)
93 ret = -EIO;
94
Janne Grunau9aba42e2009-03-18 18:10:04 -030095 return ret;
96}
97
Jarod Wilson324b04b2011-01-14 16:25:21 -030098static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
99 unsigned char addr, char *data, int len)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300100{
101 int ret;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300102
Jarod Wilson559d162e2011-01-14 16:40:32 -0300103 if (len > sizeof(dev->i2c_buf))
104 return -EINVAL;
105
106 memcpy(&dev->i2c_buf, data, len);
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300107 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
Janne Grunau9aba42e2009-03-18 18:10:04 -0300108 REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
Jarod Wilson559d162e2011-01-14 16:40:32 -0300109 (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300110
111 if (ret < 0)
Jarod Wilson559d162e2011-01-14 16:40:32 -0300112 return ret;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300113
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300114 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
Janne Grunau9aba42e2009-03-18 18:10:04 -0300115 REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
Jarod Wilson559d162e2011-01-14 16:40:32 -0300116 0, 0, &dev->i2c_buf, 2, 1000);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300117
Jarod Wilson559d162e2011-01-14 16:40:32 -0300118 if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
Janne Grunau9aba42e2009-03-18 18:10:04 -0300119 ret = 0;
120 else if (ret >= 0)
121 ret = -EIO;
122
Janne Grunau9aba42e2009-03-18 18:10:04 -0300123 return ret;
124}
125
126static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
127 int num)
128{
129 struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300130 int retval = 0, addr;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300131
132 if (num <= 0)
133 return 0;
134
135 mutex_lock(&dev->i2c_mutex);
136
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300137 addr = msgs[0].addr << 1;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300138
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300139 if (num == 1) {
140 if (msgs[0].flags & I2C_M_RD)
141 retval = hdpvr_i2c_read(dev, 1, addr, NULL, 0,
142 msgs[0].buf, msgs[0].len);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300143 else
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300144 retval = hdpvr_i2c_write(dev, 1, addr, msgs[0].buf,
145 msgs[0].len);
146 } else if (num == 2) {
147 if (msgs[0].addr != msgs[1].addr) {
148 v4l2_warn(&dev->v4l2_dev, "refusing 2-phase i2c xfer "
149 "with conflicting target addresses\n");
150 retval = -EINVAL;
151 goto out;
152 }
153
154 if ((msgs[0].flags & I2C_M_RD) || !(msgs[1].flags & I2C_M_RD)) {
155 v4l2_warn(&dev->v4l2_dev, "refusing complex xfer with "
156 "r0=%d, r1=%d\n", msgs[0].flags & I2C_M_RD,
157 msgs[1].flags & I2C_M_RD);
158 retval = -EINVAL;
159 goto out;
160 }
161
162 /*
163 * Write followed by atomic read is the only complex xfer that
164 * we actually support here.
165 */
166 retval = hdpvr_i2c_read(dev, 1, addr, msgs[0].buf, msgs[0].len,
167 msgs[1].buf, msgs[1].len);
168 } else {
169 v4l2_warn(&dev->v4l2_dev, "refusing %d-phase i2c xfer\n", num);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300170 }
171
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300172out:
Janne Grunau9aba42e2009-03-18 18:10:04 -0300173 mutex_unlock(&dev->i2c_mutex);
174
175 return retval ? retval : num;
176}
177
178static u32 hdpvr_functionality(struct i2c_adapter *adapter)
179{
180 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
181}
182
183static struct i2c_algorithm hdpvr_algo = {
184 .master_xfer = hdpvr_transfer,
185 .functionality = hdpvr_functionality,
186};
187
Jarod Wilson324b04b2011-01-14 16:25:21 -0300188static struct i2c_adapter hdpvr_i2c_adapter_template = {
189 .name = "Hauppage HD PVR I2C",
190 .owner = THIS_MODULE,
191 .algo = &hdpvr_algo,
192};
193
194static int hdpvr_activate_ir(struct hdpvr_device *dev)
195{
Jarod Wilsondc8e2aa2011-03-04 17:31:11 -0300196 char buffer[2];
Jarod Wilson324b04b2011-01-14 16:25:21 -0300197
198 mutex_lock(&dev->i2c_mutex);
199
Jarod Wilsonb443ac52011-03-02 13:23:52 -0300200 hdpvr_i2c_read(dev, 0, 0x54, NULL, 0, buffer, 1);
Jarod Wilson324b04b2011-01-14 16:25:21 -0300201
202 buffer[0] = 0;
203 buffer[1] = 0x8;
204 hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
205
206 buffer[1] = 0x18;
207 hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
208
209 mutex_unlock(&dev->i2c_mutex);
210
211 return 0;
212}
213
Janne Grunau9aba42e2009-03-18 18:10:04 -0300214int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
215{
Janne Grunau9aba42e2009-03-18 18:10:04 -0300216 int retval = -ENOMEM;
217
Jarod Wilson324b04b2011-01-14 16:25:21 -0300218 hdpvr_activate_ir(dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300219
Jarod Wilson324b04b2011-01-14 16:25:21 -0300220 memcpy(&dev->i2c_adapter, &hdpvr_i2c_adapter_template,
221 sizeof(struct i2c_adapter));
222 dev->i2c_adapter.dev.parent = &dev->udev->dev;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300223
Jarod Wilson324b04b2011-01-14 16:25:21 -0300224 i2c_set_adapdata(&dev->i2c_adapter, dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300225
Jarod Wilson324b04b2011-01-14 16:25:21 -0300226 retval = i2c_add_adapter(&dev->i2c_adapter);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300227
Janne Grunau9aba42e2009-03-18 18:10:04 -0300228 return retval;
229}
Jarod Wilson324b04b2011-01-14 16:25:21 -0300230
231#endif