Merge branch 'drm-nouveau-next' of git://git.freedesktop.org/git/nouveau/linux-2...
[pandora-kernel.git] / drivers / media / video / hdpvr / hdpvr-i2c.c
1
2 /*
3  * Hauppauge HD PVR USB driver
4  *
5  * Copyright (C) 2008      Janne Grunau (j@jannau.net)
6  *
7  * IR device registration code is
8  * Copyright (C) 2010   Andy Walls <awalls@md.metrocast.net>
9  *
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.
13  *
14  */
15
16 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
17
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20
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
30 #define Z8F0811_IR_TX_I2C_ADDR  0x70
31 #define Z8F0811_IR_RX_I2C_ADDR  0x71
32
33
34 static struct i2c_board_info hdpvr_i2c_board_info = {
35         I2C_BOARD_INFO("ir_tx_z8f0811_hdpvr", Z8F0811_IR_TX_I2C_ADDR),
36         I2C_BOARD_INFO("ir_rx_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR),
37 };
38
39 int hdpvr_register_i2c_ir(struct hdpvr_device *dev)
40 {
41         struct i2c_client *c;
42         struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
43
44         /* Our default information for ir-kbd-i2c.c to use */
45         init_data->ir_codes = RC_MAP_HAUPPAUGE_NEW;
46         init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
47         init_data->type = RC_TYPE_RC5;
48         init_data->name = "HD PVR";
49         hdpvr_i2c_board_info.platform_data = init_data;
50
51         c = i2c_new_device(&dev->i2c_adapter, &hdpvr_i2c_board_info);
52
53         return (c == NULL) ? -ENODEV : 0;
54 }
55
56 static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
57                           unsigned char addr, char *data, int len)
58 {
59         int ret;
60
61         if (len > sizeof(dev->i2c_buf))
62                 return -EINVAL;
63
64         ret = usb_control_msg(dev->udev,
65                               usb_rcvctrlpipe(dev->udev, 0),
66                               REQTYPE_I2C_READ, CTRL_READ_REQUEST,
67                               (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
68
69         if (ret == len) {
70                 memcpy(data, &dev->i2c_buf, len);
71                 ret = 0;
72         } else if (ret >= 0)
73                 ret = -EIO;
74
75         return ret;
76 }
77
78 static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
79                            unsigned char addr, char *data, int len)
80 {
81         int ret;
82
83         if (len > sizeof(dev->i2c_buf))
84                 return -EINVAL;
85
86         memcpy(&dev->i2c_buf, data, len);
87         ret = usb_control_msg(dev->udev,
88                               usb_sndctrlpipe(dev->udev, 0),
89                               REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
90                               (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
91
92         if (ret < 0)
93                 return ret;
94
95         ret = usb_control_msg(dev->udev,
96                               usb_rcvctrlpipe(dev->udev, 0),
97                               REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
98                               0, 0, &dev->i2c_buf, 2, 1000);
99
100         if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
101                 ret = 0;
102         else if (ret >= 0)
103                 ret = -EIO;
104
105         return ret;
106 }
107
108 static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
109                           int num)
110 {
111         struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
112         int retval = 0, i, addr;
113
114         if (num <= 0)
115                 return 0;
116
117         mutex_lock(&dev->i2c_mutex);
118
119         for (i = 0; i < num && !retval; i++) {
120                 addr = msgs[i].addr << 1;
121
122                 if (msgs[i].flags & I2C_M_RD)
123                         retval = hdpvr_i2c_read(dev, 1, addr, msgs[i].buf,
124                                                 msgs[i].len);
125                 else
126                         retval = hdpvr_i2c_write(dev, 1, addr, msgs[i].buf,
127                                                  msgs[i].len);
128         }
129
130         mutex_unlock(&dev->i2c_mutex);
131
132         return retval ? retval : num;
133 }
134
135 static u32 hdpvr_functionality(struct i2c_adapter *adapter)
136 {
137         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
138 }
139
140 static struct i2c_algorithm hdpvr_algo = {
141         .master_xfer   = hdpvr_transfer,
142         .functionality = hdpvr_functionality,
143 };
144
145 static struct i2c_adapter hdpvr_i2c_adapter_template = {
146         .name   = "Hauppage HD PVR I2C",
147         .owner  = THIS_MODULE,
148         .algo   = &hdpvr_algo,
149 };
150
151 static int hdpvr_activate_ir(struct hdpvr_device *dev)
152 {
153         char buffer[8];
154
155         mutex_lock(&dev->i2c_mutex);
156
157         hdpvr_i2c_read(dev, 0, 0x54, buffer, 1);
158
159         buffer[0] = 0;
160         buffer[1] = 0x8;
161         hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
162
163         buffer[1] = 0x18;
164         hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
165
166         mutex_unlock(&dev->i2c_mutex);
167
168         return 0;
169 }
170
171 int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
172 {
173         int retval = -ENOMEM;
174
175         hdpvr_activate_ir(dev);
176
177         memcpy(&dev->i2c_adapter, &hdpvr_i2c_adapter_template,
178                sizeof(struct i2c_adapter));
179         dev->i2c_adapter.dev.parent = &dev->udev->dev;
180
181         i2c_set_adapdata(&dev->i2c_adapter, dev);
182
183         retval = i2c_add_adapter(&dev->i2c_adapter);
184
185         return retval;
186 }
187
188 #endif