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