Merge tag 'dlm-4.8' of git://git.kernel.org/pub/scm/linux/kernel/git/teigland/linux-dlm
[deliverable/linux.git] / drivers / media / i2c / bt856.c
CommitLineData
d56410e0 1/*
1da177e4
LT
2 * bt856 - BT856A Digital Video Encoder (Rockwell Part)
3 *
4 * Copyright (C) 1999 Mike Bernson <mike@mlb.org>
5 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
6 *
7 * Modifications for LML33/DC10plus unified driver
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * This code was modify/ported from the saa7111 driver written
11 * by Dave Perks.
12 *
13 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
14 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31#include <linux/module.h>
18f3fa1e 32#include <linux/types.h>
5a0e3ad6 33#include <linux/slab.h>
a8c26dfe 34#include <linux/ioctl.h>
18f3fa1e 35#include <asm/uaccess.h>
a8c26dfe 36#include <linux/i2c.h>
a91f56e7
HV
37#include <linux/videodev2.h>
38#include <media/v4l2-device.h>
1da177e4
LT
39
40MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
41MODULE_AUTHOR("Mike Bernson & Dave Perks");
42MODULE_LICENSE("GPL");
43
ff699e6b 44static int debug;
1da177e4
LT
45module_param(debug, int, 0);
46MODULE_PARM_DESC(debug, "Debug level (0-1)");
47
a91f56e7 48
1da177e4
LT
49/* ----------------------------------------------------------------------- */
50
187565c4
HV
51#define BT856_REG_OFFSET 0xDA
52#define BT856_NR_REG 6
1da177e4
LT
53
54struct bt856 {
a91f56e7 55 struct v4l2_subdev sd;
f49a5eae 56 unsigned char reg[BT856_NR_REG];
1da177e4 57
107063c6 58 v4l2_std_id norm;
1da177e4
LT
59};
60
a91f56e7
HV
61static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
62{
63 return container_of(sd, struct bt856, sd);
64}
65
1da177e4
LT
66/* ----------------------------------------------------------------------- */
67
a91f56e7 68static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
1da177e4 69{
a91f56e7 70 struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
1da177e4 71
187565c4 72 encoder->reg[reg - BT856_REG_OFFSET] = value;
1da177e4
LT
73 return i2c_smbus_write_byte_data(client, reg, value);
74}
75
a91f56e7 76static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
1da177e4 77{
a91f56e7 78 return bt856_write(encoder, reg,
a8c26dfe
HV
79 (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
80 (value ? (1 << bit) : 0));
1da177e4
LT
81}
82
a91f56e7 83static void bt856_dump(struct bt856 *encoder)
1da177e4
LT
84{
85 int i;
1da177e4 86
a91f56e7 87 v4l2_info(&encoder->sd, "register dump:\n");
f49a5eae 88 for (i = 0; i < BT856_NR_REG; i += 2)
a8c26dfe
HV
89 printk(KERN_CONT " %02x", encoder->reg[i]);
90 printk(KERN_CONT "\n");
1da177e4
LT
91}
92
93/* ----------------------------------------------------------------------- */
94
a91f56e7 95static int bt856_init(struct v4l2_subdev *sd, u32 arg)
1da177e4 96{
a91f56e7 97 struct bt856 *encoder = to_bt856(sd);
1da177e4 98
a91f56e7
HV
99 /* This is just for testing!!! */
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);
1da177e4 104
a91f56e7
HV
105 bt856_setbit(encoder, 0xdc, 3, 1);
106 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
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
cc5cef8e 126 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
a91f56e7
HV
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 /*bt856_setbit(encoder, 0xda, 0, 1);*/
134 } else {
135 return -EINVAL;
a8c26dfe 136 }
a91f56e7
HV
137 encoder->norm = std;
138 if (debug != 0)
139 bt856_dump(encoder);
140 return 0;
141}
1da177e4 142
5325b427
HV
143static int bt856_s_routing(struct v4l2_subdev *sd,
144 u32 input, u32 output, u32 config)
a91f56e7
HV
145{
146 struct bt856 *encoder = to_bt856(sd);
147
5325b427 148 v4l2_dbg(1, debug, sd, "set input %d\n", input);
a91f56e7
HV
149
150 /* We only have video bus.
5325b427
HV
151 * input= 0: input is from bt819
152 * input= 1: input is from ZR36060 */
153 switch (input) {
a91f56e7
HV
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: /* Color bar */
167 bt856_setbit(encoder, 0xdc, 3, 0);
168 bt856_setbit(encoder, 0xde, 4, 1);
169 break;
1da177e4
LT
170 default:
171 return -EINVAL;
172 }
173
a91f56e7
HV
174 if (debug != 0)
175 bt856_dump(encoder);
1da177e4
LT
176 return 0;
177}
178
179/* ----------------------------------------------------------------------- */
180
a91f56e7 181static const struct v4l2_subdev_core_ops bt856_core_ops = {
a91f56e7
HV
182 .init = bt856_init,
183};
1da177e4 184
a91f56e7
HV
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/* ----------------------------------------------------------------------- */
1da177e4 196
a8c26dfe
HV
197static int bt856_probe(struct i2c_client *client,
198 const struct i2c_device_id *id)
1da177e4 199{
1da177e4 200 struct bt856 *encoder;
a91f56e7 201 struct v4l2_subdev *sd;
1da177e4 202
1da177e4 203 /* Check if the adapter supports the needed features */
a8c26dfe
HV
204 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
205 return -ENODEV;
1da177e4 206
a8c26dfe
HV
207 v4l_info(client, "chip found @ 0x%x (%s)\n",
208 client->addr << 1, client->adapter->name);
1da177e4 209
c02b211d 210 encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
a8c26dfe 211 if (encoder == NULL)
1da177e4 212 return -ENOMEM;
a91f56e7
HV
213 sd = &encoder->sd;
214 v4l2_i2c_subdev_init(sd, client, &bt856_ops);
107063c6 215 encoder->norm = V4L2_STD_NTSC;
1da177e4 216
a91f56e7
HV
217 bt856_write(encoder, 0xdc, 0x18);
218 bt856_write(encoder, 0xda, 0);
219 bt856_write(encoder, 0xde, 0);
1da177e4 220
a91f56e7
HV
221 bt856_setbit(encoder, 0xdc, 3, 1);
222 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
223 bt856_setbit(encoder, 0xdc, 4, 1);
1da177e4 224
107063c6 225 if (encoder->norm & V4L2_STD_NTSC)
a91f56e7 226 bt856_setbit(encoder, 0xdc, 2, 0);
107063c6 227 else
a91f56e7 228 bt856_setbit(encoder, 0xdc, 2, 1);
1da177e4 229
a91f56e7
HV
230 bt856_setbit(encoder, 0xdc, 1, 1);
231 bt856_setbit(encoder, 0xde, 4, 0);
232 bt856_setbit(encoder, 0xde, 3, 1);
1da177e4
LT
233
234 if (debug != 0)
a91f56e7 235 bt856_dump(encoder);
1da177e4
LT
236 return 0;
237}
238
a8c26dfe 239static int bt856_remove(struct i2c_client *client)
1da177e4 240{
a91f56e7
HV
241 struct v4l2_subdev *sd = i2c_get_clientdata(client);
242
243 v4l2_device_unregister_subdev(sd);
1da177e4
LT
244 return 0;
245}
246
a8c26dfe
HV
247static const struct i2c_device_id bt856_id[] = {
248 { "bt856", 0 },
249 { }
250};
251MODULE_DEVICE_TABLE(i2c, bt856_id);
1da177e4 252
3f417200
HV
253static struct i2c_driver bt856_driver = {
254 .driver = {
3f417200
HV
255 .name = "bt856",
256 },
257 .probe = bt856_probe,
258 .remove = bt856_remove,
259 .id_table = bt856_id,
1da177e4 260};
3f417200 261
c6e8d86f 262module_i2c_driver(bt856_driver);
This page took 1.36948 seconds and 5 git commands to generate.