Merge branch 'agp-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[sfrench/cifs-2.6.git] / drivers / media / video / adv7180.c
1 /*
2  * adv7180.c Analog Devices ADV7180 video decoder driver
3  * Copyright (c) 2009 Intel Corporation
4  *
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.
8  *
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.
13  *
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.
17  */
18
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>
30
31 #define DRIVER_NAME "adv7180"
32
33 #define ADV7180_INPUT_CONTROL_REG       0x00
34 #define ADV7180_INPUT_CONTROL_PAL_BG_NTSC_J_SECAM       0x00
35 #define ADV7180_AUTODETECT_ENABLE_REG   0x07
36 #define ADV7180_AUTODETECT_DEFAULT      0x7f
37
38
39 #define ADV7180_STATUS1_REG 0x10
40 #define ADV7180_STATUS1_AUTOD_MASK 0x70
41 #define ADV7180_STATUS1_AUTOD_NTSM_M_J  0x00
42 #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
43 #define ADV7180_STATUS1_AUTOD_PAL_M     0x20
44 #define ADV7180_STATUS1_AUTOD_PAL_60    0x30
45 #define ADV7180_STATUS1_AUTOD_PAL_B_G   0x40
46 #define ADV7180_STATUS1_AUTOD_SECAM     0x50
47 #define ADV7180_STATUS1_AUTOD_PAL_COMB  0x60
48 #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
49
50 #define ADV7180_IDENT_REG 0x11
51 #define ADV7180_ID_7180 0x18
52
53
54 struct adv7180_state {
55         struct v4l2_subdev sd;
56 };
57
58 static v4l2_std_id determine_norm(struct i2c_client *client)
59 {
60         u8 status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
61
62         switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
63         case ADV7180_STATUS1_AUTOD_NTSM_M_J:
64                 return V4L2_STD_NTSC_M_JP;
65         case ADV7180_STATUS1_AUTOD_NTSC_4_43:
66                 return V4L2_STD_NTSC_443;
67         case ADV7180_STATUS1_AUTOD_PAL_M:
68                 return V4L2_STD_PAL_M;
69         case ADV7180_STATUS1_AUTOD_PAL_60:
70                 return V4L2_STD_PAL_60;
71         case ADV7180_STATUS1_AUTOD_PAL_B_G:
72                 return V4L2_STD_PAL;
73         case ADV7180_STATUS1_AUTOD_SECAM:
74                 return V4L2_STD_SECAM;
75         case ADV7180_STATUS1_AUTOD_PAL_COMB:
76                 return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
77         case ADV7180_STATUS1_AUTOD_SECAM_525:
78                 return V4L2_STD_SECAM;
79         default:
80                 return V4L2_STD_UNKNOWN;
81         }
82 }
83
84 static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
85 {
86         return container_of(sd, struct adv7180_state, sd);
87 }
88
89 static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
90 {
91         struct i2c_client *client = v4l2_get_subdevdata(sd);
92
93         *std = determine_norm(client);
94         return 0;
95 }
96
97 static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
98         struct v4l2_dbg_chip_ident *chip)
99 {
100         struct i2c_client *client = v4l2_get_subdevdata(sd);
101
102         return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
103 }
104
105 static const struct v4l2_subdev_video_ops adv7180_video_ops = {
106         .querystd = adv7180_querystd,
107 };
108
109 static const struct v4l2_subdev_core_ops adv7180_core_ops = {
110         .g_chip_ident = adv7180_g_chip_ident,
111 };
112
113 static const struct v4l2_subdev_ops adv7180_ops = {
114         .core = &adv7180_core_ops,
115         .video = &adv7180_video_ops,
116 };
117
118 /*
119  * Generic i2c probe
120  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
121  */
122
123 static int adv7180_probe(struct i2c_client *client,
124                         const struct i2c_device_id *id)
125 {
126         struct adv7180_state *state;
127         struct v4l2_subdev *sd;
128         int ret;
129
130         /* Check if the adapter supports the needed features */
131         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
132                 return -EIO;
133
134         v4l_info(client, "chip found @ 0x%02x (%s)\n",
135                         client->addr << 1, client->adapter->name);
136
137         state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
138         if (state == NULL)
139                 return -ENOMEM;
140         sd = &state->sd;
141         v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
142
143         /* Initialize adv7180 */
144         /* enable autodetection */
145         ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
146                 ADV7180_INPUT_CONTROL_PAL_BG_NTSC_J_SECAM);
147         if (ret > 0)
148                 ret = i2c_smbus_write_byte_data(client,
149                         ADV7180_AUTODETECT_ENABLE_REG,
150                         ADV7180_AUTODETECT_DEFAULT);
151         if (ret < 0) {
152                 printk(KERN_ERR DRIVER_NAME
153                         ": Failed to communicate to chip: %d\n", ret);
154                 return ret;
155         }
156
157         return 0;
158 }
159
160 static int adv7180_remove(struct i2c_client *client)
161 {
162         struct v4l2_subdev *sd = i2c_get_clientdata(client);
163
164         v4l2_device_unregister_subdev(sd);
165         kfree(to_state(sd));
166         return 0;
167 }
168
169 static const struct i2c_device_id adv7180_id[] = {
170         {DRIVER_NAME, 0},
171         {},
172 };
173
174 MODULE_DEVICE_TABLE(i2c, adv7180_id);
175
176 static struct i2c_driver adv7180_driver = {
177         .driver = {
178                 .owner  = THIS_MODULE,
179                 .name   = DRIVER_NAME,
180         },
181         .probe          = adv7180_probe,
182         .remove         = adv7180_remove,
183         .id_table       = adv7180_id,
184 };
185
186 static __init int adv7180_init(void)
187 {
188         return i2c_add_driver(&adv7180_driver);
189 }
190
191 static __exit void adv7180_exit(void)
192 {
193         i2c_del_driver(&adv7180_driver);
194 }
195
196 module_init(adv7180_init);
197 module_exit(adv7180_exit);
198
199 MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
200 MODULE_AUTHOR("Mocean Laboratories");
201 MODULE_LICENSE("GPL v2");
202