2 * adv7180.c Analog Devices ADV7180 video decoder driver
3 * Copyright (c) 2009 Intel Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/interrupt.h>
24 #include <linux/i2c.h>
25 #include <linux/i2c-id.h>
26 #include <media/v4l2-ioctl.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-chip-ident.h>
31 #define DRIVER_NAME "adv7180"
33 #define ADV7180_INPUT_CONTROL_REG 0x00
34 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00
35 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
36 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20
37 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30
38 #define ADV7180_INPUT_CONTROL_NTSC_J 0x40
39 #define ADV7180_INPUT_CONTROL_NTSC_M 0x50
40 #define ADV7180_INPUT_CONTROL_PAL60 0x60
41 #define ADV7180_INPUT_CONTROL_NTSC_443 0x70
42 #define ADV7180_INPUT_CONTROL_PAL_BG 0x80
43 #define ADV7180_INPUT_CONTROL_PAL_N 0x90
44 #define ADV7180_INPUT_CONTROL_PAL_M 0xa0
45 #define ADV7180_INPUT_CONTROL_PAL_M_PED 0xb0
46 #define ADV7180_INPUT_CONTROL_PAL_COMB_N 0xc0
47 #define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED 0xd0
48 #define ADV7180_INPUT_CONTROL_PAL_SECAM 0xe0
49 #define ADV7180_INPUT_CONTROL_PAL_SECAM_PED 0xf0
51 #define ADV7180_AUTODETECT_ENABLE_REG 0x07
52 #define ADV7180_AUTODETECT_DEFAULT 0x7f
55 #define ADV7180_STATUS1_REG 0x10
56 #define ADV7180_STATUS1_IN_LOCK 0x01
57 #define ADV7180_STATUS1_AUTOD_MASK 0x70
58 #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
59 #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
60 #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
61 #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
62 #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
63 #define ADV7180_STATUS1_AUTOD_SECAM 0x50
64 #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
65 #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
67 #define ADV7180_IDENT_REG 0x11
68 #define ADV7180_ID_7180 0x18
71 struct adv7180_state
{
72 struct v4l2_subdev sd
;
73 v4l2_std_id curr_norm
;
77 static v4l2_std_id
adv7180_std_to_v4l2(u8 status1
)
79 switch (status1
& ADV7180_STATUS1_AUTOD_MASK
) {
80 case ADV7180_STATUS1_AUTOD_NTSM_M_J
:
82 case ADV7180_STATUS1_AUTOD_NTSC_4_43
:
83 return V4L2_STD_NTSC_443
;
84 case ADV7180_STATUS1_AUTOD_PAL_M
:
85 return V4L2_STD_PAL_M
;
86 case ADV7180_STATUS1_AUTOD_PAL_60
:
87 return V4L2_STD_PAL_60
;
88 case ADV7180_STATUS1_AUTOD_PAL_B_G
:
90 case ADV7180_STATUS1_AUTOD_SECAM
:
91 return V4L2_STD_SECAM
;
92 case ADV7180_STATUS1_AUTOD_PAL_COMB
:
93 return V4L2_STD_PAL_Nc
| V4L2_STD_PAL_N
;
94 case ADV7180_STATUS1_AUTOD_SECAM_525
:
95 return V4L2_STD_SECAM
;
97 return V4L2_STD_UNKNOWN
;
101 static int v4l2_std_to_adv7180(v4l2_std_id std
)
103 if (std
== V4L2_STD_PAL_60
)
104 return ADV7180_INPUT_CONTROL_PAL60
;
105 if (std
== V4L2_STD_NTSC_443
)
106 return ADV7180_INPUT_CONTROL_NTSC_443
;
107 if (std
== V4L2_STD_PAL_N
)
108 return ADV7180_INPUT_CONTROL_PAL_N
;
109 if (std
== V4L2_STD_PAL_M
)
110 return ADV7180_INPUT_CONTROL_PAL_M
;
111 if (std
== V4L2_STD_PAL_Nc
)
112 return ADV7180_INPUT_CONTROL_PAL_COMB_N
;
114 if (std
& V4L2_STD_PAL
)
115 return ADV7180_INPUT_CONTROL_PAL_BG
;
116 if (std
& V4L2_STD_NTSC
)
117 return ADV7180_INPUT_CONTROL_NTSC_M
;
118 if (std
& V4L2_STD_SECAM
)
119 return ADV7180_INPUT_CONTROL_PAL_SECAM
;
124 static u32
adv7180_status_to_v4l2(u8 status1
)
126 if (!(status1
& ADV7180_STATUS1_IN_LOCK
))
127 return V4L2_IN_ST_NO_SIGNAL
;
132 static int __adv7180_status(struct i2c_client
*client
, u32
*status
,
135 int status1
= i2c_smbus_read_byte_data(client
, ADV7180_STATUS1_REG
);
141 *status
= adv7180_status_to_v4l2(status1
);
143 *std
= adv7180_std_to_v4l2(status1
);
148 static inline struct adv7180_state
*to_state(struct v4l2_subdev
*sd
)
150 return container_of(sd
, struct adv7180_state
, sd
);
153 static int adv7180_querystd(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
155 struct adv7180_state
*state
= to_state(sd
);
158 if (!state
->autodetect
)
159 *std
= state
->curr_norm
;
161 err
= __adv7180_status(v4l2_get_subdevdata(sd
), NULL
, std
);
166 static int adv7180_g_input_status(struct v4l2_subdev
*sd
, u32
*status
)
168 return __adv7180_status(v4l2_get_subdevdata(sd
), status
, NULL
);
171 static int adv7180_g_chip_ident(struct v4l2_subdev
*sd
,
172 struct v4l2_dbg_chip_ident
*chip
)
174 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
176 return v4l2_chip_ident_i2c_client(client
, chip
, V4L2_IDENT_ADV7180
, 0);
179 static int adv7180_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
181 struct adv7180_state
*state
= to_state(sd
);
182 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
185 /* all standards -> autodetect */
186 if (std
== V4L2_STD_ALL
) {
187 ret
= i2c_smbus_write_byte_data(client
,
188 ADV7180_INPUT_CONTROL_REG
,
189 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
);
193 state
->autodetect
= true;
195 ret
= v4l2_std_to_adv7180(std
);
199 ret
= i2c_smbus_write_byte_data(client
,
200 ADV7180_INPUT_CONTROL_REG
, ret
);
204 state
->curr_norm
= std
;
205 state
->autodetect
= false;
212 static const struct v4l2_subdev_video_ops adv7180_video_ops
= {
213 .querystd
= adv7180_querystd
,
214 .g_input_status
= adv7180_g_input_status
,
217 static const struct v4l2_subdev_core_ops adv7180_core_ops
= {
218 .g_chip_ident
= adv7180_g_chip_ident
,
219 .s_std
= adv7180_s_std
,
222 static const struct v4l2_subdev_ops adv7180_ops
= {
223 .core
= &adv7180_core_ops
,
224 .video
= &adv7180_video_ops
,
229 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
232 static int adv7180_probe(struct i2c_client
*client
,
233 const struct i2c_device_id
*id
)
235 struct adv7180_state
*state
;
236 struct v4l2_subdev
*sd
;
239 /* Check if the adapter supports the needed features */
240 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
243 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
244 client
->addr
<< 1, client
->adapter
->name
);
246 state
= kzalloc(sizeof(struct adv7180_state
), GFP_KERNEL
);
249 state
->autodetect
= true;
251 v4l2_i2c_subdev_init(sd
, client
, &adv7180_ops
);
253 /* Initialize adv7180 */
254 /* enable autodetection */
255 ret
= i2c_smbus_write_byte_data(client
, ADV7180_INPUT_CONTROL_REG
,
256 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
);
258 ret
= i2c_smbus_write_byte_data(client
,
259 ADV7180_AUTODETECT_ENABLE_REG
,
260 ADV7180_AUTODETECT_DEFAULT
);
262 printk(KERN_ERR DRIVER_NAME
263 ": Failed to communicate to chip: %d\n", ret
);
270 static int adv7180_remove(struct i2c_client
*client
)
272 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
274 v4l2_device_unregister_subdev(sd
);
279 static const struct i2c_device_id adv7180_id
[] = {
284 MODULE_DEVICE_TABLE(i2c
, adv7180_id
);
286 static struct i2c_driver adv7180_driver
= {
288 .owner
= THIS_MODULE
,
291 .probe
= adv7180_probe
,
292 .remove
= adv7180_remove
,
293 .id_table
= adv7180_id
,
296 static __init
int adv7180_init(void)
298 return i2c_add_driver(&adv7180_driver
);
301 static __exit
void adv7180_exit(void)
303 i2c_del_driver(&adv7180_driver
);
306 module_init(adv7180_init
);
307 module_exit(adv7180_exit
);
309 MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
310 MODULE_AUTHOR("Mocean Laboratories");
311 MODULE_LICENSE("GPL v2");