1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/module.h>
32#include <linux/types.h>
33#include <linux/ioctl.h>
34#include <asm/uaccess.h>
35#include <linux/i2c.h>
36#include <linux/i2c-id.h>
37#include <linux/videodev2.h>
38#include <media/v4l2-device.h>
39#include <media/v4l2-chip-ident.h>
40#include <media/v4l2-i2c-drv.h>
41
42MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
43MODULE_AUTHOR("Mike Bernson & Dave Perks");
44MODULE_LICENSE("GPL");
45
46static int debug;
47module_param(debug, int, 0);
48MODULE_PARM_DESC(debug, "Debug level (0-1)");
49
50
51
52
53#define BT856_REG_OFFSET 0xDA
54#define BT856_NR_REG 6
55
56struct bt856 {
57 struct v4l2_subdev sd;
58 unsigned char reg[BT856_NR_REG];
59
60 v4l2_std_id norm;
61};
62
63static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
64{
65 return container_of(sd, struct bt856, sd);
66}
67
68
69
70static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
71{
72 struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
73
74 encoder->reg[reg - BT856_REG_OFFSET] = value;
75 return i2c_smbus_write_byte_data(client, reg, value);
76}
77
78static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
79{
80 return bt856_write(encoder, reg,
81 (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
82 (value ? (1 << bit) : 0));
83}
84
85static void bt856_dump(struct bt856 *encoder)
86{
87 int i;
88
89 v4l2_info(&encoder->sd, "register dump:\n");
90 for (i = 0; i < BT856_NR_REG; i += 2)
91 printk(KERN_CONT " %02x", encoder->reg[i]);
92 printk(KERN_CONT "\n");
93}
94
95
96
97static int bt856_init(struct v4l2_subdev *sd, u32 arg)
98{
99 struct bt856 *encoder = to_bt856(sd);
100
101
102 v4l2_dbg(1, debug, sd, "init\n");
103 bt856_write(encoder, 0xdc, 0x18);
104 bt856_write(encoder, 0xda, 0);
105 bt856_write(encoder, 0xde, 0);
106
107 bt856_setbit(encoder, 0xdc, 3, 1);
108
109 bt856_setbit(encoder, 0xdc, 4, 1);
110
111 if (encoder->norm & V4L2_STD_NTSC)
112 bt856_setbit(encoder, 0xdc, 2, 0);
113 else
114 bt856_setbit(encoder, 0xdc, 2, 1);
115
116 bt856_setbit(encoder, 0xdc, 1, 1);
117 bt856_setbit(encoder, 0xde, 4, 0);
118 bt856_setbit(encoder, 0xde, 3, 1);
119 if (debug != 0)
120 bt856_dump(encoder);
121 return 0;
122}
123
124static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
125{
126 struct bt856 *encoder = to_bt856(sd);
127
128 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
129
130 if (std & V4L2_STD_NTSC) {
131 bt856_setbit(encoder, 0xdc, 2, 0);
132 } else if (std & V4L2_STD_PAL) {
133 bt856_setbit(encoder, 0xdc, 2, 1);
134 bt856_setbit(encoder, 0xda, 0, 0);
135
136 } else {
137 return -EINVAL;
138 }
139 encoder->norm = std;
140 if (debug != 0)
141 bt856_dump(encoder);
142 return 0;
143}
144
145static int bt856_s_routing(struct v4l2_subdev *sd,
146 u32 input, u32 output, u32 config)
147{
148 struct bt856 *encoder = to_bt856(sd);
149
150 v4l2_dbg(1, debug, sd, "set input %d\n", input);
151
152
153
154
155 switch (input) {
156 case 0:
157 bt856_setbit(encoder, 0xde, 4, 0);
158 bt856_setbit(encoder, 0xde, 3, 1);
159 bt856_setbit(encoder, 0xdc, 3, 1);
160 bt856_setbit(encoder, 0xdc, 6, 0);
161 break;
162 case 1:
163 bt856_setbit(encoder, 0xde, 4, 0);
164 bt856_setbit(encoder, 0xde, 3, 1);
165 bt856_setbit(encoder, 0xdc, 3, 1);
166 bt856_setbit(encoder, 0xdc, 6, 1);
167 break;
168 case 2:
169 bt856_setbit(encoder, 0xdc, 3, 0);
170 bt856_setbit(encoder, 0xde, 4, 1);
171 break;
172 default:
173 return -EINVAL;
174 }
175
176 if (debug != 0)
177 bt856_dump(encoder);
178 return 0;
179}
180
181static int bt856_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
182{
183 struct i2c_client *client = v4l2_get_subdevdata(sd);
184
185 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_BT856, 0);
186}
187
188
189
190static const struct v4l2_subdev_core_ops bt856_core_ops = {
191 .g_chip_ident = bt856_g_chip_ident,
192 .init = bt856_init,
193};
194
195static const struct v4l2_subdev_video_ops bt856_video_ops = {
196 .s_std_output = bt856_s_std_output,
197 .s_routing = bt856_s_routing,
198};
199
200static const struct v4l2_subdev_ops bt856_ops = {
201 .core = &bt856_core_ops,
202 .video = &bt856_video_ops,
203};
204
205
206
207static int bt856_probe(struct i2c_client *client,
208 const struct i2c_device_id *id)
209{
210 struct bt856 *encoder;
211 struct v4l2_subdev *sd;
212
213
214 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
215 return -ENODEV;
216
217 v4l_info(client, "chip found @ 0x%x (%s)\n",
218 client->addr << 1, client->adapter->name);
219
220 encoder = kzalloc(sizeof(struct bt856), GFP_KERNEL);
221 if (encoder == NULL)
222 return -ENOMEM;
223 sd = &encoder->sd;
224 v4l2_i2c_subdev_init(sd, client, &bt856_ops);
225 encoder->norm = V4L2_STD_NTSC;
226
227 bt856_write(encoder, 0xdc, 0x18);
228 bt856_write(encoder, 0xda, 0);
229 bt856_write(encoder, 0xde, 0);
230
231 bt856_setbit(encoder, 0xdc, 3, 1);
232
233 bt856_setbit(encoder, 0xdc, 4, 1);
234
235 if (encoder->norm & V4L2_STD_NTSC)
236 bt856_setbit(encoder, 0xdc, 2, 0);
237 else
238 bt856_setbit(encoder, 0xdc, 2, 1);
239
240 bt856_setbit(encoder, 0xdc, 1, 1);
241 bt856_setbit(encoder, 0xde, 4, 0);
242 bt856_setbit(encoder, 0xde, 3, 1);
243
244 if (debug != 0)
245 bt856_dump(encoder);
246 return 0;
247}
248
249static int bt856_remove(struct i2c_client *client)
250{
251 struct v4l2_subdev *sd = i2c_get_clientdata(client);
252
253 v4l2_device_unregister_subdev(sd);
254 kfree(to_bt856(sd));
255 return 0;
256}
257
258static const struct i2c_device_id bt856_id[] = {
259 { "bt856", 0 },
260 { }
261};
262MODULE_DEVICE_TABLE(i2c, bt856_id);
263
264static struct v4l2_i2c_driver_data v4l2_i2c_data = {
265 .name = "bt856",
266 .probe = bt856_probe,
267 .remove = bt856_remove,
268 .id_table = bt856_id,
269};
270