blob: 2e17abc7731065e913a84037e5a93690883c73a0 [file] [log] [blame]
Chaithrika U S40199c52009-05-07 09:29:25 -03001/*
Lad, Prabhakar88da0182013-02-18 07:56:41 -03002 * ths7303/53- THS7303/53 Video Amplifier driver
Chaithrika U S40199c52009-05-07 09:29:25 -03003 *
4 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
Lad, Prabhakar88da0182013-02-18 07:56:41 -03005 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates.
6 *
7 * Author: Chaithrika U S <chaithrika@ti.com>
8 *
9 * Contributors:
10 * Hans Verkuil <hans.verkuil@cisco.com>
11 * Lad, Prabhakar <prabhakar.lad@ti.com>
12 * Martin Bugge <marbugge@cisco.com>
Chaithrika U S40199c52009-05-07 09:29:25 -030013 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation version 2.
17 *
18 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
19 * kind, whether express or implied; without even the implied warranty
20 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
Chaithrika U S40199c52009-05-07 09:29:25 -030024#include <linux/i2c.h>
Chaithrika U S40199c52009-05-07 09:29:25 -030025#include <linux/module.h>
Lad, Prabhakar88da0182013-02-18 07:56:41 -030026#include <linux/slab.h>
Chaithrika U S40199c52009-05-07 09:29:25 -030027
Lad, Prabhakar88da0182013-02-18 07:56:41 -030028#include <media/ths7303.h>
Lad, Prabhakar88da0182013-02-18 07:56:41 -030029#include <media/v4l2-device.h>
Chaithrika U S40199c52009-05-07 09:29:25 -030030
Manjunath Hadliad7dcb32012-10-01 11:46:35 -030031#define THS7303_CHANNEL_1 1
32#define THS7303_CHANNEL_2 2
33#define THS7303_CHANNEL_3 3
34
Lad, Prabhakar88da0182013-02-18 07:56:41 -030035struct ths7303_state {
36 struct v4l2_subdev sd;
Lad, Prabhakardd8c3932013-05-25 13:39:36 -030037 const struct ths7303_platform_data *pdata;
Lad, Prabhakar88da0182013-02-18 07:56:41 -030038 struct v4l2_bt_timings bt;
39 int std_id;
40 int stream_on;
41 int driver_data;
42};
43
Manjunath Hadliad7dcb32012-10-01 11:46:35 -030044enum ths7303_filter_mode {
45 THS7303_FILTER_MODE_480I_576I,
46 THS7303_FILTER_MODE_480P_576P,
47 THS7303_FILTER_MODE_720P_1080I,
48 THS7303_FILTER_MODE_1080P,
49 THS7303_FILTER_MODE_DISABLE
50};
51
Chaithrika U S40199c52009-05-07 09:29:25 -030052MODULE_DESCRIPTION("TI THS7303 video amplifier driver");
53MODULE_AUTHOR("Chaithrika U S");
54MODULE_LICENSE("GPL");
55
56static int debug;
57module_param(debug, int, 0644);
58MODULE_PARM_DESC(debug, "Debug level 0-1");
59
Lad, Prabhakar88da0182013-02-18 07:56:41 -030060static inline struct ths7303_state *to_state(struct v4l2_subdev *sd)
61{
62 return container_of(sd, struct ths7303_state, sd);
63}
64
65static int ths7303_read(struct v4l2_subdev *sd, u8 reg)
66{
67 struct i2c_client *client = v4l2_get_subdevdata(sd);
68
69 return i2c_smbus_read_byte_data(client, reg);
70}
71
72static int ths7303_write(struct v4l2_subdev *sd, u8 reg, u8 val)
73{
74 struct i2c_client *client = v4l2_get_subdevdata(sd);
75 int ret;
76 int i;
77
78 for (i = 0; i < 3; i++) {
79 ret = i2c_smbus_write_byte_data(client, reg, val);
80 if (ret == 0)
81 return 0;
82 }
83 return ret;
84}
85
Chaithrika U S40199c52009-05-07 09:29:25 -030086/* following function is used to set ths7303 */
Manjunath Hadliad7dcb32012-10-01 11:46:35 -030087int ths7303_setval(struct v4l2_subdev *sd, enum ths7303_filter_mode mode)
Chaithrika U S40199c52009-05-07 09:29:25 -030088{
Manjunath Hadliad7dcb32012-10-01 11:46:35 -030089 struct i2c_client *client = v4l2_get_subdevdata(sd);
Lad, Prabhakar88da0182013-02-18 07:56:41 -030090 struct ths7303_state *state = to_state(sd);
Lad, Prabhakardd8c3932013-05-25 13:39:36 -030091 const struct ths7303_platform_data *pdata = state->pdata;
Lad, Prabhakar88da0182013-02-18 07:56:41 -030092 u8 val, sel = 0;
93 int err, disable = 0;
Chaithrika U S40199c52009-05-07 09:29:25 -030094
Manjunath Hadliad7dcb32012-10-01 11:46:35 -030095 if (!client)
96 return -EINVAL;
97
98 switch (mode) {
99 case THS7303_FILTER_MODE_1080P:
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300100 sel = 0x3; /*1080p and SXGA/UXGA */
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300101 break;
102 case THS7303_FILTER_MODE_720P_1080I:
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300103 sel = 0x2; /*720p, 1080i and SVGA/XGA */
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300104 break;
105 case THS7303_FILTER_MODE_480P_576P:
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300106 sel = 0x1; /* EDTV 480p/576p and VGA */
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300107 break;
108 case THS7303_FILTER_MODE_480I_576I:
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300109 sel = 0x0; /* SDTV, S-Video, 480i/576i */
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300110 break;
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300111 default:
112 /* disable all channels */
113 disable = 1;
Chaithrika U S40199c52009-05-07 09:29:25 -0300114 }
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300115
116 val = (sel << 6) | (sel << 3);
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300117 if (!disable)
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300118 val |= (pdata->ch_1 & 0x27);
119 err = ths7303_write(sd, THS7303_CHANNEL_1, val);
Chaithrika U S40199c52009-05-07 09:29:25 -0300120 if (err)
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300121 goto out;
Chaithrika U S40199c52009-05-07 09:29:25 -0300122
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300123 val = (sel << 6) | (sel << 3);
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300124 if (!disable)
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300125 val |= (pdata->ch_2 & 0x27);
126 err = ths7303_write(sd, THS7303_CHANNEL_2, val);
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300127 if (err)
128 goto out;
129
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300130 val = (sel << 6) | (sel << 3);
131 if (!disable)
132 val |= (pdata->ch_3 & 0x27);
133 err = ths7303_write(sd, THS7303_CHANNEL_3, val);
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300134 if (err)
135 goto out;
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300136
137 return 0;
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300138out:
139 pr_info("write byte data failed\n");
Chaithrika U S40199c52009-05-07 09:29:25 -0300140 return err;
141}
142
143static int ths7303_s_std_output(struct v4l2_subdev *sd, v4l2_std_id norm)
144{
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300145 struct ths7303_state *state = to_state(sd);
146
147 if (norm & (V4L2_STD_ALL & ~V4L2_STD_SECAM)) {
148 state->std_id = 1;
149 state->bt.pixelclock = 0;
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300150 return ths7303_setval(sd, THS7303_FILTER_MODE_480I_576I);
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300151 }
152
153 return ths7303_setval(sd, THS7303_FILTER_MODE_DISABLE);
154}
155
156static int ths7303_config(struct v4l2_subdev *sd)
157{
158 struct ths7303_state *state = to_state(sd);
159 int res;
160
161 if (!state->stream_on) {
162 ths7303_write(sd, THS7303_CHANNEL_1,
163 (ths7303_read(sd, THS7303_CHANNEL_1) & 0xf8) |
164 0x00);
165 ths7303_write(sd, THS7303_CHANNEL_2,
166 (ths7303_read(sd, THS7303_CHANNEL_2) & 0xf8) |
167 0x00);
168 ths7303_write(sd, THS7303_CHANNEL_3,
169 (ths7303_read(sd, THS7303_CHANNEL_3) & 0xf8) |
170 0x00);
171 return 0;
172 }
173
174 if (state->bt.pixelclock > 120000000)
175 res = ths7303_setval(sd, THS7303_FILTER_MODE_1080P);
176 else if (state->bt.pixelclock > 70000000)
177 res = ths7303_setval(sd, THS7303_FILTER_MODE_720P_1080I);
178 else if (state->bt.pixelclock > 20000000)
179 res = ths7303_setval(sd, THS7303_FILTER_MODE_480P_576P);
180 else if (state->std_id)
181 res = ths7303_setval(sd, THS7303_FILTER_MODE_480I_576I);
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300182 else
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300183 /* disable all channels */
184 res = ths7303_setval(sd, THS7303_FILTER_MODE_DISABLE);
185
186 return res;
187
188}
189
190static int ths7303_s_stream(struct v4l2_subdev *sd, int enable)
191{
192 struct ths7303_state *state = to_state(sd);
193
194 state->stream_on = enable;
195
196 return ths7303_config(sd);
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300197}
198
199/* for setting filter for HD output */
200static int ths7303_s_dv_timings(struct v4l2_subdev *sd,
201 struct v4l2_dv_timings *dv_timings)
202{
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300203 struct ths7303_state *state = to_state(sd);
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300204
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300205 if (!dv_timings || dv_timings->type != V4L2_DV_BT_656_1120)
206 return -EINVAL;
Manjunath Hadliad7dcb32012-10-01 11:46:35 -0300207
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300208 state->bt = dv_timings->bt;
209 state->std_id = 0;
210
211 return ths7303_config(sd);
Chaithrika U S40199c52009-05-07 09:29:25 -0300212}
213
Chaithrika U S40199c52009-05-07 09:29:25 -0300214static const struct v4l2_subdev_video_ops ths7303_video_ops = {
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300215 .s_stream = ths7303_s_stream,
Chaithrika U S40199c52009-05-07 09:29:25 -0300216 .s_std_output = ths7303_s_std_output,
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300217 .s_dv_timings = ths7303_s_dv_timings,
Chaithrika U S40199c52009-05-07 09:29:25 -0300218};
219
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300220#ifdef CONFIG_VIDEO_ADV_DEBUG
221
222static int ths7303_g_register(struct v4l2_subdev *sd,
223 struct v4l2_dbg_register *reg)
224{
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300225 reg->size = 1;
226 reg->val = ths7303_read(sd, reg->reg);
227 return 0;
228}
229
230static int ths7303_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300231 const struct v4l2_dbg_register *reg)
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300232{
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300233 ths7303_write(sd, reg->reg, reg->val);
234 return 0;
235}
236#endif
237
238static const char * const stc_lpf_sel_txt[4] = {
239 "500-kHz Filter",
240 "2.5-MHz Filter",
241 "5-MHz Filter",
242 "5-MHz Filter",
243};
244
245static const char * const in_mux_sel_txt[2] = {
246 "Input A Select",
247 "Input B Select",
248};
249
250static const char * const lpf_freq_sel_txt[4] = {
251 "9-MHz LPF",
252 "16-MHz LPF",
253 "35-MHz LPF",
254 "Bypass LPF",
255};
256
257static const char * const in_bias_sel_dis_cont_txt[8] = {
258 "Disable Channel",
259 "Mute Function - No Output",
260 "DC Bias Select",
261 "DC Bias + 250 mV Offset Select",
262 "AC Bias Select",
263 "Sync Tip Clamp with low bias",
264 "Sync Tip Clamp with mid bias",
265 "Sync Tip Clamp with high bias",
266};
267
268static void ths7303_log_channel_status(struct v4l2_subdev *sd, u8 reg)
269{
270 u8 val = ths7303_read(sd, reg);
271
272 if ((val & 0x7) == 0) {
273 v4l2_info(sd, "Channel %d Off\n", reg);
274 return;
275 }
276
277 v4l2_info(sd, "Channel %d On\n", reg);
278 v4l2_info(sd, " value 0x%x\n", val);
279 v4l2_info(sd, " %s\n", stc_lpf_sel_txt[(val >> 6) & 0x3]);
280 v4l2_info(sd, " %s\n", in_mux_sel_txt[(val >> 5) & 0x1]);
281 v4l2_info(sd, " %s\n", lpf_freq_sel_txt[(val >> 3) & 0x3]);
282 v4l2_info(sd, " %s\n", in_bias_sel_dis_cont_txt[(val >> 0) & 0x7]);
283}
284
285static int ths7303_log_status(struct v4l2_subdev *sd)
286{
287 struct ths7303_state *state = to_state(sd);
288
289 v4l2_info(sd, "stream %s\n", state->stream_on ? "On" : "Off");
290
291 if (state->bt.pixelclock) {
292 struct v4l2_bt_timings *bt = bt = &state->bt;
293 u32 frame_width, frame_height;
294
295 frame_width = bt->width + bt->hfrontporch +
296 bt->hsync + bt->hbackporch;
297 frame_height = bt->height + bt->vfrontporch +
298 bt->vsync + bt->vbackporch;
299 v4l2_info(sd,
300 "timings: %dx%d%s%d (%dx%d). Pix freq. = %d Hz. Polarities = 0x%x\n",
301 bt->width, bt->height, bt->interlaced ? "i" : "p",
302 (frame_height * frame_width) > 0 ?
303 (int)bt->pixelclock /
304 (frame_height * frame_width) : 0,
305 frame_width, frame_height,
306 (int)bt->pixelclock, bt->polarities);
307 } else {
308 v4l2_info(sd, "no timings set\n");
309 }
310
311 ths7303_log_channel_status(sd, THS7303_CHANNEL_1);
312 ths7303_log_channel_status(sd, THS7303_CHANNEL_2);
313 ths7303_log_channel_status(sd, THS7303_CHANNEL_3);
314
315 return 0;
316}
317
Chaithrika U S40199c52009-05-07 09:29:25 -0300318static const struct v4l2_subdev_core_ops ths7303_core_ops = {
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300319 .log_status = ths7303_log_status,
320#ifdef CONFIG_VIDEO_ADV_DEBUG
321 .g_register = ths7303_g_register,
322 .s_register = ths7303_s_register,
323#endif
Chaithrika U S40199c52009-05-07 09:29:25 -0300324};
325
326static const struct v4l2_subdev_ops ths7303_ops = {
327 .core = &ths7303_core_ops,
328 .video = &ths7303_video_ops,
329};
330
331static int ths7303_probe(struct i2c_client *client,
332 const struct i2c_device_id *id)
333{
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300334 struct ths7303_platform_data *pdata = client->dev.platform_data;
335 struct ths7303_state *state;
Chaithrika U S40199c52009-05-07 09:29:25 -0300336 struct v4l2_subdev *sd;
Chaithrika U S40199c52009-05-07 09:29:25 -0300337
Lad, Prabhakardd8c3932013-05-25 13:39:36 -0300338 if (pdata == NULL) {
339 dev_err(&client->dev, "No platform data\n");
340 return -EINVAL;
341 }
342
Chaithrika U S40199c52009-05-07 09:29:25 -0300343 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
344 return -ENODEV;
345
346 v4l_info(client, "chip found @ 0x%x (%s)\n",
347 client->addr << 1, client->adapter->name);
348
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300349 state = devm_kzalloc(&client->dev, sizeof(struct ths7303_state),
350 GFP_KERNEL);
351 if (!state)
Chaithrika U S40199c52009-05-07 09:29:25 -0300352 return -ENOMEM;
353
Lad, Prabhakardd8c3932013-05-25 13:39:36 -0300354 state->pdata = pdata;
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300355 sd = &state->sd;
Chaithrika U S40199c52009-05-07 09:29:25 -0300356 v4l2_i2c_subdev_init(sd, client, &ths7303_ops);
357
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300358 /* store the driver data to differntiate the chip */
359 state->driver_data = (int)id->driver_data;
360
Lad, Prabhakar8524ce52013-05-25 13:39:35 -0300361 /* set to default 480I_576I filter mode */
362 if (ths7303_setval(sd, THS7303_FILTER_MODE_480I_576I) < 0) {
363 v4l_err(client, "Setting to 480I_576I filter mode failed!\n");
364 return -EINVAL;
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300365 }
366
367 return 0;
Chaithrika U S40199c52009-05-07 09:29:25 -0300368}
369
370static int ths7303_remove(struct i2c_client *client)
371{
372 struct v4l2_subdev *sd = i2c_get_clientdata(client);
373
374 v4l2_device_unregister_subdev(sd);
Chaithrika U S40199c52009-05-07 09:29:25 -0300375
376 return 0;
377}
378
379static const struct i2c_device_id ths7303_id[] = {
Hans Verkuile1277112013-05-29 06:59:51 -0300380 {"ths7303", 0},
381 {"ths7353", 0},
Chaithrika U S40199c52009-05-07 09:29:25 -0300382 {},
383};
384
385MODULE_DEVICE_TABLE(i2c, ths7303_id);
386
387static struct i2c_driver ths7303_driver = {
388 .driver = {
389 .owner = THIS_MODULE,
Lad, Prabhakar88da0182013-02-18 07:56:41 -0300390 .name = "ths73x3",
Chaithrika U S40199c52009-05-07 09:29:25 -0300391 },
392 .probe = ths7303_probe,
393 .remove = ths7303_remove,
394 .id_table = ths7303_id,
395};
396
Axel Linc6e8d862012-02-12 06:56:32 -0300397module_i2c_driver(ths7303_driver);