blob: b75878c27c2a3474c28420be0c3341ce03986b0f [file] [log] [blame]
Richard Röjfors6789cb52009-09-18 21:17:20 -03001/*
2 * adv7180.c Analog Devices ADV7180 video decoder driver
3 * Copyright (c) 2009 Intel Corporation
Vladimir Barinovcccb83f2013-05-29 14:50:57 -03004 * Copyright (C) 2013 Cogent Embedded, Inc.
5 * Copyright (C) 2013 Renesas Solutions Corp.
Richard Röjfors6789cb52009-09-18 21:17:20 -03006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/errno.h>
24#include <linux/kernel.h>
25#include <linux/interrupt.h>
26#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Richard Röjfors6789cb52009-09-18 21:17:20 -030028#include <media/v4l2-ioctl.h>
29#include <linux/videodev2.h>
30#include <media/v4l2-device.h>
Federico Vagac9fbedd2012-07-11 11:29:33 -030031#include <media/v4l2-ctrls.h>
Richard Röjfors42752f72009-09-22 06:07:06 -030032#include <linux/mutex.h>
Lars-Peter Clausenc18818e2015-01-23 12:52:25 -030033#include <linux/delay.h>
Richard Röjfors6789cb52009-09-18 21:17:20 -030034
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -030035#define ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM 0x0
36#define ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM_PED 0x1
37#define ADV7180_STD_AD_PAL_N_NTSC_J_SECAM 0x2
38#define ADV7180_STD_AD_PAL_N_NTSC_M_SECAM 0x3
39#define ADV7180_STD_NTSC_J 0x4
40#define ADV7180_STD_NTSC_M 0x5
41#define ADV7180_STD_PAL60 0x6
42#define ADV7180_STD_NTSC_443 0x7
43#define ADV7180_STD_PAL_BG 0x8
44#define ADV7180_STD_PAL_N 0x9
45#define ADV7180_STD_PAL_M 0xa
46#define ADV7180_STD_PAL_M_PED 0xb
47#define ADV7180_STD_PAL_COMB_N 0xc
48#define ADV7180_STD_PAL_COMB_N_PED 0xd
49#define ADV7180_STD_PAL_SECAM 0xe
50#define ADV7180_STD_PAL_SECAM_PED 0xf
51
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030052#define ADV7180_REG_INPUT_CONTROL 0x0000
Federico Vagabca7ad12012-04-12 12:39:36 -030053#define ADV7180_INPUT_CONTROL_INSEL_MASK 0x0f
Richard Röjforsd3124292009-09-22 06:05:42 -030054
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -030055#define ADV7182_REG_INPUT_VIDSEL 0x0002
56
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030057#define ADV7180_REG_EXTENDED_OUTPUT_CONTROL 0x0004
Richard Röjfors42752f72009-09-22 06:07:06 -030058#define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5
Richard Röjfors6789cb52009-09-18 21:17:20 -030059
Lars-Peter Clausen029d6172015-01-23 12:52:23 -030060#define ADV7180_REG_AUTODETECT_ENABLE 0x07
Richard Röjfors42752f72009-09-22 06:07:06 -030061#define ADV7180_AUTODETECT_DEFAULT 0x7f
Federico Vagac9fbedd2012-07-11 11:29:33 -030062/* Contrast */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030063#define ADV7180_REG_CON 0x0008 /*Unsigned */
Federico Vagac9fbedd2012-07-11 11:29:33 -030064#define ADV7180_CON_MIN 0
65#define ADV7180_CON_DEF 128
66#define ADV7180_CON_MAX 255
67/* Brightness*/
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030068#define ADV7180_REG_BRI 0x000a /*Signed */
Federico Vagac9fbedd2012-07-11 11:29:33 -030069#define ADV7180_BRI_MIN -128
70#define ADV7180_BRI_DEF 0
71#define ADV7180_BRI_MAX 127
72/* Hue */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030073#define ADV7180_REG_HUE 0x000b /*Signed, inverted */
Federico Vagac9fbedd2012-07-11 11:29:33 -030074#define ADV7180_HUE_MIN -127
75#define ADV7180_HUE_DEF 0
76#define ADV7180_HUE_MAX 128
Federico Vagabca7ad12012-04-12 12:39:36 -030077
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030078#define ADV7180_REG_CTRL 0x000e
Lars-Peter Clausen029d6172015-01-23 12:52:23 -030079#define ADV7180_CTRL_IRQ_SPACE 0x20
Richard Röjfors6789cb52009-09-18 21:17:20 -030080
Lars-Peter Clausen029d6172015-01-23 12:52:23 -030081#define ADV7180_REG_PWR_MAN 0x0f
Federico Vagabca7ad12012-04-12 12:39:36 -030082#define ADV7180_PWR_MAN_ON 0x04
83#define ADV7180_PWR_MAN_OFF 0x24
84#define ADV7180_PWR_MAN_RES 0x80
85
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030086#define ADV7180_REG_STATUS1 0x0010
Richard Röjforsd3124292009-09-22 06:05:42 -030087#define ADV7180_STATUS1_IN_LOCK 0x01
88#define ADV7180_STATUS1_AUTOD_MASK 0x70
Richard Röjfors6789cb52009-09-18 21:17:20 -030089#define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
90#define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
91#define ADV7180_STATUS1_AUTOD_PAL_M 0x20
92#define ADV7180_STATUS1_AUTOD_PAL_60 0x30
93#define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
94#define ADV7180_STATUS1_AUTOD_SECAM 0x50
95#define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
96#define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
97
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030098#define ADV7180_REG_IDENT 0x0011
Richard Röjfors6789cb52009-09-18 21:17:20 -030099#define ADV7180_ID_7180 0x18
100
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300101#define ADV7180_REG_ICONF1 0x0040
Richard Röjfors42752f72009-09-22 06:07:06 -0300102#define ADV7180_ICONF1_ACTIVE_LOW 0x01
103#define ADV7180_ICONF1_PSYNC_ONLY 0x10
104#define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
Federico Vagac9fbedd2012-07-11 11:29:33 -0300105/* Saturation */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300106#define ADV7180_REG_SD_SAT_CB 0x00e3 /*Unsigned */
107#define ADV7180_REG_SD_SAT_CR 0x00e4 /*Unsigned */
Federico Vagac9fbedd2012-07-11 11:29:33 -0300108#define ADV7180_SAT_MIN 0
109#define ADV7180_SAT_DEF 128
110#define ADV7180_SAT_MAX 255
Federico Vagabca7ad12012-04-12 12:39:36 -0300111
Richard Röjfors42752f72009-09-22 06:07:06 -0300112#define ADV7180_IRQ1_LOCK 0x01
113#define ADV7180_IRQ1_UNLOCK 0x02
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300114#define ADV7180_REG_ISR1 0x0042
115#define ADV7180_REG_ICR1 0x0043
116#define ADV7180_REG_IMR1 0x0044
117#define ADV7180_REG_IMR2 0x0048
Richard Röjfors42752f72009-09-22 06:07:06 -0300118#define ADV7180_IRQ3_AD_CHANGE 0x08
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300119#define ADV7180_REG_ISR3 0x004A
120#define ADV7180_REG_ICR3 0x004B
121#define ADV7180_REG_IMR3 0x004C
Lars-Peter Clausen029d6172015-01-23 12:52:23 -0300122#define ADV7180_REG_IMR4 0x50
Richard Röjfors6789cb52009-09-18 21:17:20 -0300123
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300124#define ADV7180_REG_NTSC_V_BIT_END 0x00E6
Federico Vagabca7ad12012-04-12 12:39:36 -0300125#define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F
126
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300127#define ADV7180_REG_VPP_SLAVE_ADDR 0xFD
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300128#define ADV7180_REG_CSI_SLAVE_ADDR 0xFE
129
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300130#define ADV7180_REG_FLCONTROL 0x40e0
131#define ADV7180_FLCONTROL_FL_ENABLE 0x1
132
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300133#define ADV7180_CSI_REG_PWRDN 0x00
134#define ADV7180_CSI_PWRDN 0x80
135
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300136#define ADV7180_INPUT_CVBS_AIN1 0x00
137#define ADV7180_INPUT_CVBS_AIN2 0x01
138#define ADV7180_INPUT_CVBS_AIN3 0x02
139#define ADV7180_INPUT_CVBS_AIN4 0x03
140#define ADV7180_INPUT_CVBS_AIN5 0x04
141#define ADV7180_INPUT_CVBS_AIN6 0x05
142#define ADV7180_INPUT_SVIDEO_AIN1_AIN2 0x06
143#define ADV7180_INPUT_SVIDEO_AIN3_AIN4 0x07
144#define ADV7180_INPUT_SVIDEO_AIN5_AIN6 0x08
145#define ADV7180_INPUT_YPRPB_AIN1_AIN2_AIN3 0x09
146#define ADV7180_INPUT_YPRPB_AIN4_AIN5_AIN6 0x0a
147
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300148#define ADV7182_INPUT_CVBS_AIN1 0x00
149#define ADV7182_INPUT_CVBS_AIN2 0x01
150#define ADV7182_INPUT_CVBS_AIN3 0x02
151#define ADV7182_INPUT_CVBS_AIN4 0x03
152#define ADV7182_INPUT_CVBS_AIN5 0x04
153#define ADV7182_INPUT_CVBS_AIN6 0x05
154#define ADV7182_INPUT_CVBS_AIN7 0x06
155#define ADV7182_INPUT_CVBS_AIN8 0x07
156#define ADV7182_INPUT_SVIDEO_AIN1_AIN2 0x08
157#define ADV7182_INPUT_SVIDEO_AIN3_AIN4 0x09
158#define ADV7182_INPUT_SVIDEO_AIN5_AIN6 0x0a
159#define ADV7182_INPUT_SVIDEO_AIN7_AIN8 0x0b
160#define ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3 0x0c
161#define ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6 0x0d
162#define ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2 0x0e
163#define ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4 0x0f
164#define ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6 0x10
165#define ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8 0x11
166
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300167#define ADV7180_DEFAULT_CSI_I2C_ADDR 0x44
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300168#define ADV7180_DEFAULT_VPP_I2C_ADDR 0x42
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300169
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300170#define V4L2_CID_ADV_FAST_SWITCH (V4L2_CID_USER_ADV7180_BASE + 0x00)
171
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300172struct adv7180_state;
173
174#define ADV7180_FLAG_RESET_POWERED BIT(0)
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300175#define ADV7180_FLAG_V2 BIT(1)
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300176#define ADV7180_FLAG_MIPI_CSI2 BIT(2)
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300177#define ADV7180_FLAG_I2P BIT(3)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300178
179struct adv7180_chip_info {
180 unsigned int flags;
181 unsigned int valid_input_mask;
182 int (*set_std)(struct adv7180_state *st, unsigned int std);
183 int (*select_input)(struct adv7180_state *st, unsigned int input);
184 int (*init)(struct adv7180_state *state);
185};
186
Richard Röjfors6789cb52009-09-18 21:17:20 -0300187struct adv7180_state {
Federico Vagac9fbedd2012-07-11 11:29:33 -0300188 struct v4l2_ctrl_handler ctrl_hdl;
Richard Röjforsc277b602009-09-22 06:06:34 -0300189 struct v4l2_subdev sd;
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300190 struct media_pad pad;
Richard Röjfors42752f72009-09-22 06:07:06 -0300191 struct mutex mutex; /* mutual excl. when accessing chip */
192 int irq;
Richard Röjforsc277b602009-09-22 06:06:34 -0300193 v4l2_std_id curr_norm;
194 bool autodetect;
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300195 bool powered;
Federico Vagabca7ad12012-04-12 12:39:36 -0300196 u8 input;
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300197
198 struct i2c_client *client;
199 unsigned int register_page;
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300200 struct i2c_client *csi_client;
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300201 struct i2c_client *vpp_client;
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300202 const struct adv7180_chip_info *chip_info;
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300203 enum v4l2_field field;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300204};
Federico Vagac9fbedd2012-07-11 11:29:33 -0300205#define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler, \
206 struct adv7180_state, \
207 ctrl_hdl)->sd)
Richard Röjfors6789cb52009-09-18 21:17:20 -0300208
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300209static int adv7180_select_page(struct adv7180_state *state, unsigned int page)
210{
211 if (state->register_page != page) {
212 i2c_smbus_write_byte_data(state->client, ADV7180_REG_CTRL,
213 page);
214 state->register_page = page;
215 }
216
217 return 0;
218}
219
220static int adv7180_write(struct adv7180_state *state, unsigned int reg,
221 unsigned int value)
222{
223 lockdep_assert_held(&state->mutex);
224 adv7180_select_page(state, reg >> 8);
225 return i2c_smbus_write_byte_data(state->client, reg & 0xff, value);
226}
227
228static int adv7180_read(struct adv7180_state *state, unsigned int reg)
229{
230 lockdep_assert_held(&state->mutex);
231 adv7180_select_page(state, reg >> 8);
232 return i2c_smbus_read_byte_data(state->client, reg & 0xff);
233}
234
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300235static int adv7180_csi_write(struct adv7180_state *state, unsigned int reg,
236 unsigned int value)
237{
238 return i2c_smbus_write_byte_data(state->csi_client, reg, value);
239}
240
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300241static int adv7180_set_video_standard(struct adv7180_state *state,
242 unsigned int std)
243{
244 return state->chip_info->set_std(state, std);
245}
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300246
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300247static int adv7180_vpp_write(struct adv7180_state *state, unsigned int reg,
248 unsigned int value)
249{
250 return i2c_smbus_write_byte_data(state->vpp_client, reg, value);
251}
252
Richard Röjforsd3124292009-09-22 06:05:42 -0300253static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
Richard Röjfors6789cb52009-09-18 21:17:20 -0300254{
Vladimir Barinovb294a192013-04-11 18:06:46 -0300255 /* in case V4L2_IN_ST_NO_SIGNAL */
256 if (!(status1 & ADV7180_STATUS1_IN_LOCK))
257 return V4L2_STD_UNKNOWN;
258
Richard Röjfors6789cb52009-09-18 21:17:20 -0300259 switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
260 case ADV7180_STATUS1_AUTOD_NTSM_M_J:
Richard Röjforsd3124292009-09-22 06:05:42 -0300261 return V4L2_STD_NTSC;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300262 case ADV7180_STATUS1_AUTOD_NTSC_4_43:
263 return V4L2_STD_NTSC_443;
264 case ADV7180_STATUS1_AUTOD_PAL_M:
265 return V4L2_STD_PAL_M;
266 case ADV7180_STATUS1_AUTOD_PAL_60:
267 return V4L2_STD_PAL_60;
268 case ADV7180_STATUS1_AUTOD_PAL_B_G:
269 return V4L2_STD_PAL;
270 case ADV7180_STATUS1_AUTOD_SECAM:
271 return V4L2_STD_SECAM;
272 case ADV7180_STATUS1_AUTOD_PAL_COMB:
273 return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
274 case ADV7180_STATUS1_AUTOD_SECAM_525:
275 return V4L2_STD_SECAM;
276 default:
277 return V4L2_STD_UNKNOWN;
278 }
279}
280
Richard Röjforsc277b602009-09-22 06:06:34 -0300281static int v4l2_std_to_adv7180(v4l2_std_id std)
282{
283 if (std == V4L2_STD_PAL_60)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300284 return ADV7180_STD_PAL60;
Richard Röjforsc277b602009-09-22 06:06:34 -0300285 if (std == V4L2_STD_NTSC_443)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300286 return ADV7180_STD_NTSC_443;
Richard Röjforsc277b602009-09-22 06:06:34 -0300287 if (std == V4L2_STD_PAL_N)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300288 return ADV7180_STD_PAL_N;
Richard Röjforsc277b602009-09-22 06:06:34 -0300289 if (std == V4L2_STD_PAL_M)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300290 return ADV7180_STD_PAL_M;
Richard Röjforsc277b602009-09-22 06:06:34 -0300291 if (std == V4L2_STD_PAL_Nc)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300292 return ADV7180_STD_PAL_COMB_N;
Richard Röjforsc277b602009-09-22 06:06:34 -0300293
294 if (std & V4L2_STD_PAL)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300295 return ADV7180_STD_PAL_BG;
Richard Röjforsc277b602009-09-22 06:06:34 -0300296 if (std & V4L2_STD_NTSC)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300297 return ADV7180_STD_NTSC_M;
Richard Röjforsc277b602009-09-22 06:06:34 -0300298 if (std & V4L2_STD_SECAM)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300299 return ADV7180_STD_PAL_SECAM;
Richard Röjforsc277b602009-09-22 06:06:34 -0300300
301 return -EINVAL;
302}
303
Richard Röjforsd3124292009-09-22 06:05:42 -0300304static u32 adv7180_status_to_v4l2(u8 status1)
305{
306 if (!(status1 & ADV7180_STATUS1_IN_LOCK))
307 return V4L2_IN_ST_NO_SIGNAL;
308
309 return 0;
310}
311
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300312static int __adv7180_status(struct adv7180_state *state, u32 *status,
Federico Vagabca7ad12012-04-12 12:39:36 -0300313 v4l2_std_id *std)
Richard Röjforsd3124292009-09-22 06:05:42 -0300314{
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300315 int status1 = adv7180_read(state, ADV7180_REG_STATUS1);
Richard Röjforsd3124292009-09-22 06:05:42 -0300316
317 if (status1 < 0)
318 return status1;
319
320 if (status)
321 *status = adv7180_status_to_v4l2(status1);
322 if (std)
323 *std = adv7180_std_to_v4l2(status1);
324
325 return 0;
326}
327
Richard Röjfors6789cb52009-09-18 21:17:20 -0300328static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
329{
330 return container_of(sd, struct adv7180_state, sd);
331}
332
333static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
334{
Richard Röjforsc277b602009-09-22 06:06:34 -0300335 struct adv7180_state *state = to_state(sd);
Richard Röjfors42752f72009-09-22 06:07:06 -0300336 int err = mutex_lock_interruptible(&state->mutex);
337 if (err)
338 return err;
Richard Röjforsc277b602009-09-22 06:06:34 -0300339
Richard Röjfors42752f72009-09-22 06:07:06 -0300340 /* when we are interrupt driven we know the state */
341 if (!state->autodetect || state->irq > 0)
Richard Röjforsc277b602009-09-22 06:06:34 -0300342 *std = state->curr_norm;
343 else
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300344 err = __adv7180_status(state, NULL, std);
Richard Röjforsc277b602009-09-22 06:06:34 -0300345
Richard Röjfors42752f72009-09-22 06:07:06 -0300346 mutex_unlock(&state->mutex);
Richard Röjforsc277b602009-09-22 06:06:34 -0300347 return err;
Richard Röjforsd3124292009-09-22 06:05:42 -0300348}
Richard Röjfors6789cb52009-09-18 21:17:20 -0300349
Federico Vagabca7ad12012-04-12 12:39:36 -0300350static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
351 u32 output, u32 config)
352{
353 struct adv7180_state *state = to_state(sd);
354 int ret = mutex_lock_interruptible(&state->mutex);
Federico Vagabca7ad12012-04-12 12:39:36 -0300355
356 if (ret)
357 return ret;
358
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300359 if (input > 31 || !(BIT(input) & state->chip_info->valid_input_mask)) {
360 ret = -EINVAL;
Federico Vagabca7ad12012-04-12 12:39:36 -0300361 goto out;
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300362 }
Federico Vagabca7ad12012-04-12 12:39:36 -0300363
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300364 ret = state->chip_info->select_input(state, input);
Federico Vagabca7ad12012-04-12 12:39:36 -0300365
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300366 if (ret == 0)
367 state->input = input;
Federico Vagabca7ad12012-04-12 12:39:36 -0300368out:
369 mutex_unlock(&state->mutex);
370 return ret;
371}
372
Richard Röjforsd3124292009-09-22 06:05:42 -0300373static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
374{
Richard Röjfors42752f72009-09-22 06:07:06 -0300375 struct adv7180_state *state = to_state(sd);
376 int ret = mutex_lock_interruptible(&state->mutex);
377 if (ret)
378 return ret;
379
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300380 ret = __adv7180_status(state, status, NULL);
Richard Röjfors42752f72009-09-22 06:07:06 -0300381 mutex_unlock(&state->mutex);
382 return ret;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300383}
384
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300385static int adv7180_program_std(struct adv7180_state *state)
386{
387 int ret;
388
389 if (state->autodetect) {
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300390 ret = adv7180_set_video_standard(state,
391 ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM);
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300392 if (ret < 0)
393 return ret;
394
395 __adv7180_status(state, NULL, &state->curr_norm);
396 } else {
397 ret = v4l2_std_to_adv7180(state->curr_norm);
398 if (ret < 0)
399 return ret;
400
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300401 ret = adv7180_set_video_standard(state, ret);
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300402 if (ret < 0)
403 return ret;
404 }
405
406 return 0;
407}
408
Richard Röjforsc277b602009-09-22 06:06:34 -0300409static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
410{
411 struct adv7180_state *state = to_state(sd);
Richard Röjfors42752f72009-09-22 06:07:06 -0300412 int ret = mutex_lock_interruptible(&state->mutex);
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300413
Richard Röjfors42752f72009-09-22 06:07:06 -0300414 if (ret)
415 return ret;
Richard Röjforsc277b602009-09-22 06:06:34 -0300416
417 /* all standards -> autodetect */
418 if (std == V4L2_STD_ALL) {
Richard Röjforsc277b602009-09-22 06:06:34 -0300419 state->autodetect = true;
420 } else {
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300421 /* Make sure we can support this std */
Richard Röjforsc277b602009-09-22 06:06:34 -0300422 ret = v4l2_std_to_adv7180(std);
423 if (ret < 0)
424 goto out;
425
Richard Röjforsc277b602009-09-22 06:06:34 -0300426 state->curr_norm = std;
427 state->autodetect = false;
428 }
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300429
430 ret = adv7180_program_std(state);
Richard Röjforsc277b602009-09-22 06:06:34 -0300431out:
Richard Röjfors42752f72009-09-22 06:07:06 -0300432 mutex_unlock(&state->mutex);
Richard Röjforsc277b602009-09-22 06:06:34 -0300433 return ret;
434}
435
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300436static int adv7180_set_power(struct adv7180_state *state, bool on)
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300437{
438 u8 val;
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300439 int ret;
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300440
441 if (on)
442 val = ADV7180_PWR_MAN_ON;
443 else
444 val = ADV7180_PWR_MAN_OFF;
445
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300446 ret = adv7180_write(state, ADV7180_REG_PWR_MAN, val);
447 if (ret)
448 return ret;
449
450 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
451 if (on) {
452 adv7180_csi_write(state, 0xDE, 0x02);
453 adv7180_csi_write(state, 0xD2, 0xF7);
454 adv7180_csi_write(state, 0xD8, 0x65);
455 adv7180_csi_write(state, 0xE0, 0x09);
456 adv7180_csi_write(state, 0x2C, 0x00);
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300457 if (state->field == V4L2_FIELD_NONE)
458 adv7180_csi_write(state, 0x1D, 0x80);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300459 adv7180_csi_write(state, 0x00, 0x00);
460 } else {
461 adv7180_csi_write(state, 0x00, 0x80);
462 }
463 }
464
465 return 0;
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300466}
467
468static int adv7180_s_power(struct v4l2_subdev *sd, int on)
469{
470 struct adv7180_state *state = to_state(sd);
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300471 int ret;
472
473 ret = mutex_lock_interruptible(&state->mutex);
474 if (ret)
475 return ret;
476
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300477 ret = adv7180_set_power(state, on);
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300478 if (ret == 0)
479 state->powered = on;
480
481 mutex_unlock(&state->mutex);
482 return ret;
483}
484
Federico Vagac9fbedd2012-07-11 11:29:33 -0300485static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
Federico Vagabca7ad12012-04-12 12:39:36 -0300486{
Federico Vagac9fbedd2012-07-11 11:29:33 -0300487 struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
Federico Vagabca7ad12012-04-12 12:39:36 -0300488 struct adv7180_state *state = to_state(sd);
Federico Vagabca7ad12012-04-12 12:39:36 -0300489 int ret = mutex_lock_interruptible(&state->mutex);
Federico Vagac9fbedd2012-07-11 11:29:33 -0300490 int val;
491
Federico Vagabca7ad12012-04-12 12:39:36 -0300492 if (ret)
493 return ret;
Federico Vagac9fbedd2012-07-11 11:29:33 -0300494 val = ctrl->val;
Federico Vagabca7ad12012-04-12 12:39:36 -0300495 switch (ctrl->id) {
496 case V4L2_CID_BRIGHTNESS:
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300497 ret = adv7180_write(state, ADV7180_REG_BRI, val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300498 break;
499 case V4L2_CID_HUE:
Federico Vagabca7ad12012-04-12 12:39:36 -0300500 /*Hue is inverted according to HSL chart */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300501 ret = adv7180_write(state, ADV7180_REG_HUE, -val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300502 break;
503 case V4L2_CID_CONTRAST:
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300504 ret = adv7180_write(state, ADV7180_REG_CON, val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300505 break;
506 case V4L2_CID_SATURATION:
Federico Vagabca7ad12012-04-12 12:39:36 -0300507 /*
508 *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
509 *Let's not confuse the user, everybody understands saturation
510 */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300511 ret = adv7180_write(state, ADV7180_REG_SD_SAT_CB, val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300512 if (ret < 0)
513 break;
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300514 ret = adv7180_write(state, ADV7180_REG_SD_SAT_CR, val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300515 break;
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300516 case V4L2_CID_ADV_FAST_SWITCH:
517 if (ctrl->val) {
518 /* ADI required write */
519 adv7180_write(state, 0x80d9, 0x44);
520 adv7180_write(state, ADV7180_REG_FLCONTROL,
521 ADV7180_FLCONTROL_FL_ENABLE);
522 } else {
523 /* ADI required write */
524 adv7180_write(state, 0x80d9, 0xc4);
525 adv7180_write(state, ADV7180_REG_FLCONTROL, 0x00);
526 }
527 break;
Federico Vagabca7ad12012-04-12 12:39:36 -0300528 default:
529 ret = -EINVAL;
530 }
531
532 mutex_unlock(&state->mutex);
533 return ret;
534}
535
Federico Vagac9fbedd2012-07-11 11:29:33 -0300536static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
537 .s_ctrl = adv7180_s_ctrl,
538};
539
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300540static const struct v4l2_ctrl_config adv7180_ctrl_fast_switch = {
541 .ops = &adv7180_ctrl_ops,
542 .id = V4L2_CID_ADV_FAST_SWITCH,
543 .name = "Fast Switching",
544 .type = V4L2_CTRL_TYPE_BOOLEAN,
545 .min = 0,
546 .max = 1,
547 .step = 1,
548};
549
Federico Vagac9fbedd2012-07-11 11:29:33 -0300550static int adv7180_init_controls(struct adv7180_state *state)
551{
552 v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
553
554 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
555 V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
556 ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
557 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
558 V4L2_CID_CONTRAST, ADV7180_CON_MIN,
559 ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
560 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
561 V4L2_CID_SATURATION, ADV7180_SAT_MIN,
562 ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
563 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
564 V4L2_CID_HUE, ADV7180_HUE_MIN,
565 ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300566 v4l2_ctrl_new_custom(&state->ctrl_hdl, &adv7180_ctrl_fast_switch, NULL);
567
Federico Vagac9fbedd2012-07-11 11:29:33 -0300568 state->sd.ctrl_handler = &state->ctrl_hdl;
569 if (state->ctrl_hdl.error) {
570 int err = state->ctrl_hdl.error;
571
572 v4l2_ctrl_handler_free(&state->ctrl_hdl);
573 return err;
574 }
575 v4l2_ctrl_handler_setup(&state->ctrl_hdl);
576
577 return 0;
578}
579static void adv7180_exit_controls(struct adv7180_state *state)
580{
581 v4l2_ctrl_handler_free(&state->ctrl_hdl);
582}
583
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300584static int adv7180_enum_mbus_code(struct v4l2_subdev *sd,
585 struct v4l2_subdev_fh *fh,
586 struct v4l2_subdev_mbus_code_enum *code)
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300587{
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300588 if (code->index != 0)
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300589 return -EINVAL;
590
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300591 code->code = MEDIA_BUS_FMT_YUYV8_2X8;
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300592
593 return 0;
594}
595
596static int adv7180_mbus_fmt(struct v4l2_subdev *sd,
597 struct v4l2_mbus_framefmt *fmt)
598{
599 struct adv7180_state *state = to_state(sd);
600
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300601 fmt->code = MEDIA_BUS_FMT_YUYV8_2X8;
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300602 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300603 fmt->width = 720;
604 fmt->height = state->curr_norm & V4L2_STD_525_60 ? 480 : 576;
605
606 return 0;
607}
608
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300609static int adv7180_set_field_mode(struct adv7180_state *state)
610{
611 if (!(state->chip_info->flags & ADV7180_FLAG_I2P))
612 return 0;
613
614 if (state->field == V4L2_FIELD_NONE) {
615 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
616 adv7180_csi_write(state, 0x01, 0x20);
617 adv7180_csi_write(state, 0x02, 0x28);
618 adv7180_csi_write(state, 0x03, 0x38);
619 adv7180_csi_write(state, 0x04, 0x30);
620 adv7180_csi_write(state, 0x05, 0x30);
621 adv7180_csi_write(state, 0x06, 0x80);
622 adv7180_csi_write(state, 0x07, 0x70);
623 adv7180_csi_write(state, 0x08, 0x50);
624 }
625 adv7180_vpp_write(state, 0xa3, 0x00);
626 adv7180_vpp_write(state, 0x5b, 0x00);
627 adv7180_vpp_write(state, 0x55, 0x80);
628 } else {
629 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
630 adv7180_csi_write(state, 0x01, 0x18);
631 adv7180_csi_write(state, 0x02, 0x18);
632 adv7180_csi_write(state, 0x03, 0x30);
633 adv7180_csi_write(state, 0x04, 0x20);
634 adv7180_csi_write(state, 0x05, 0x28);
635 adv7180_csi_write(state, 0x06, 0x40);
636 adv7180_csi_write(state, 0x07, 0x58);
637 adv7180_csi_write(state, 0x08, 0x30);
638 }
639 adv7180_vpp_write(state, 0xa3, 0x70);
640 adv7180_vpp_write(state, 0x5b, 0x80);
641 adv7180_vpp_write(state, 0x55, 0x00);
642 }
643
644 return 0;
645}
646
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300647static int adv7180_get_pad_format(struct v4l2_subdev *sd,
648 struct v4l2_subdev_fh *fh,
649 struct v4l2_subdev_format *format)
650{
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300651 struct adv7180_state *state = to_state(sd);
652
653 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
654 format->format = *v4l2_subdev_get_try_format(fh, 0);
655 } else {
656 adv7180_mbus_fmt(sd, &format->format);
657 format->format.field = state->field;
658 }
659
660 return 0;
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300661}
662
663static int adv7180_set_pad_format(struct v4l2_subdev *sd,
664 struct v4l2_subdev_fh *fh,
665 struct v4l2_subdev_format *format)
666{
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300667 struct adv7180_state *state = to_state(sd);
668 struct v4l2_mbus_framefmt *framefmt;
669
670 switch (format->format.field) {
671 case V4L2_FIELD_NONE:
672 if (!(state->chip_info->flags & ADV7180_FLAG_I2P))
673 format->format.field = V4L2_FIELD_INTERLACED;
674 break;
675 default:
676 format->format.field = V4L2_FIELD_INTERLACED;
677 break;
678 }
679
680 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
681 framefmt = &format->format;
682 if (state->field != format->format.field) {
683 state->field = format->format.field;
684 adv7180_set_power(state, false);
685 adv7180_set_field_mode(state);
686 adv7180_set_power(state, true);
687 }
688 } else {
689 framefmt = v4l2_subdev_get_try_format(fh, 0);
690 *framefmt = format->format;
691 }
692
693 return adv7180_mbus_fmt(sd, framefmt);
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300694}
695
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300696static int adv7180_g_mbus_config(struct v4l2_subdev *sd,
697 struct v4l2_mbus_config *cfg)
698{
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300699 struct adv7180_state *state = to_state(sd);
700
701 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
702 cfg->type = V4L2_MBUS_CSI2;
703 cfg->flags = V4L2_MBUS_CSI2_1_LANE |
704 V4L2_MBUS_CSI2_CHANNEL_0 |
705 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
706 } else {
707 /*
708 * The ADV7180 sensor supports BT.601/656 output modes.
709 * The BT.656 is default and not yet configurable by s/w.
710 */
711 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
712 V4L2_MBUS_DATA_ACTIVE_HIGH;
713 cfg->type = V4L2_MBUS_BT656;
714 }
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300715
716 return 0;
717}
718
Richard Röjfors6789cb52009-09-18 21:17:20 -0300719static const struct v4l2_subdev_video_ops adv7180_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300720 .s_std = adv7180_s_std,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300721 .querystd = adv7180_querystd,
Richard Röjforsd3124292009-09-22 06:05:42 -0300722 .g_input_status = adv7180_g_input_status,
Federico Vagabca7ad12012-04-12 12:39:36 -0300723 .s_routing = adv7180_s_routing,
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300724 .g_mbus_config = adv7180_g_mbus_config,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300725};
726
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300727
Richard Röjfors6789cb52009-09-18 21:17:20 -0300728static const struct v4l2_subdev_core_ops adv7180_core_ops = {
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300729 .s_power = adv7180_s_power,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300730};
731
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300732static const struct v4l2_subdev_pad_ops adv7180_pad_ops = {
733 .enum_mbus_code = adv7180_enum_mbus_code,
734 .set_fmt = adv7180_set_pad_format,
735 .get_fmt = adv7180_get_pad_format,
736};
737
Richard Röjfors6789cb52009-09-18 21:17:20 -0300738static const struct v4l2_subdev_ops adv7180_ops = {
739 .core = &adv7180_core_ops,
740 .video = &adv7180_video_ops,
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300741 .pad = &adv7180_pad_ops,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300742};
743
Lars-Peter Clausen0c255342014-03-07 13:14:31 -0300744static irqreturn_t adv7180_irq(int irq, void *devid)
Richard Röjfors42752f72009-09-22 06:07:06 -0300745{
Lars-Peter Clausen0c255342014-03-07 13:14:31 -0300746 struct adv7180_state *state = devid;
Richard Röjfors42752f72009-09-22 06:07:06 -0300747 u8 isr3;
748
749 mutex_lock(&state->mutex);
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300750 isr3 = adv7180_read(state, ADV7180_REG_ISR3);
Richard Röjfors42752f72009-09-22 06:07:06 -0300751 /* clear */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300752 adv7180_write(state, ADV7180_REG_ICR3, isr3);
Richard Röjfors42752f72009-09-22 06:07:06 -0300753
754 if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300755 __adv7180_status(state, NULL, &state->curr_norm);
Richard Röjfors42752f72009-09-22 06:07:06 -0300756 mutex_unlock(&state->mutex);
757
Richard Röjfors42752f72009-09-22 06:07:06 -0300758 return IRQ_HANDLED;
759}
760
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300761static int adv7180_init(struct adv7180_state *state)
762{
763 int ret;
764
765 /* ITU-R BT.656-4 compatible */
766 ret = adv7180_write(state, ADV7180_REG_EXTENDED_OUTPUT_CONTROL,
767 ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
768 if (ret < 0)
769 return ret;
770
771 /* Manually set V bit end position in NTSC mode */
772 return adv7180_write(state, ADV7180_REG_NTSC_V_BIT_END,
773 ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
774}
775
776static int adv7180_set_std(struct adv7180_state *state, unsigned int std)
777{
778 return adv7180_write(state, ADV7180_REG_INPUT_CONTROL,
779 (std << 4) | state->input);
780}
781
782static int adv7180_select_input(struct adv7180_state *state, unsigned int input)
783{
784 int ret;
785
786 ret = adv7180_read(state, ADV7180_REG_INPUT_CONTROL);
787 if (ret < 0)
788 return ret;
789
790 ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
791 ret |= input;
792 return adv7180_write(state, ADV7180_REG_INPUT_CONTROL, ret);
793}
794
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300795static int adv7182_init(struct adv7180_state *state)
796{
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300797 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2)
798 adv7180_write(state, ADV7180_REG_CSI_SLAVE_ADDR,
799 ADV7180_DEFAULT_CSI_I2C_ADDR << 1);
800
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300801 if (state->chip_info->flags & ADV7180_FLAG_I2P)
802 adv7180_write(state, ADV7180_REG_VPP_SLAVE_ADDR,
803 ADV7180_DEFAULT_VPP_I2C_ADDR << 1);
804
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300805 if (state->chip_info->flags & ADV7180_FLAG_V2) {
806 /* ADI recommended writes for improved video quality */
807 adv7180_write(state, 0x0080, 0x51);
808 adv7180_write(state, 0x0081, 0x51);
809 adv7180_write(state, 0x0082, 0x68);
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300810 }
811
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300812 /* ADI required writes */
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300813 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
814 adv7180_write(state, 0x0003, 0x4e);
815 adv7180_write(state, 0x0004, 0x57);
816 adv7180_write(state, 0x001d, 0xc0);
817 } else {
818 if (state->chip_info->flags & ADV7180_FLAG_V2)
819 adv7180_write(state, 0x0004, 0x17);
820 else
821 adv7180_write(state, 0x0004, 0x07);
822 adv7180_write(state, 0x0003, 0x0c);
823 adv7180_write(state, 0x001d, 0x40);
824 }
825
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300826 adv7180_write(state, 0x0013, 0x00);
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300827
828 return 0;
829}
830
831static int adv7182_set_std(struct adv7180_state *state, unsigned int std)
832{
833 return adv7180_write(state, ADV7182_REG_INPUT_VIDSEL, std << 4);
834}
835
836enum adv7182_input_type {
837 ADV7182_INPUT_TYPE_CVBS,
838 ADV7182_INPUT_TYPE_DIFF_CVBS,
839 ADV7182_INPUT_TYPE_SVIDEO,
840 ADV7182_INPUT_TYPE_YPBPR,
841};
842
843static enum adv7182_input_type adv7182_get_input_type(unsigned int input)
844{
845 switch (input) {
846 case ADV7182_INPUT_CVBS_AIN1:
847 case ADV7182_INPUT_CVBS_AIN2:
848 case ADV7182_INPUT_CVBS_AIN3:
849 case ADV7182_INPUT_CVBS_AIN4:
850 case ADV7182_INPUT_CVBS_AIN5:
851 case ADV7182_INPUT_CVBS_AIN6:
852 case ADV7182_INPUT_CVBS_AIN7:
853 case ADV7182_INPUT_CVBS_AIN8:
854 return ADV7182_INPUT_TYPE_CVBS;
855 case ADV7182_INPUT_SVIDEO_AIN1_AIN2:
856 case ADV7182_INPUT_SVIDEO_AIN3_AIN4:
857 case ADV7182_INPUT_SVIDEO_AIN5_AIN6:
858 case ADV7182_INPUT_SVIDEO_AIN7_AIN8:
859 return ADV7182_INPUT_TYPE_SVIDEO;
860 case ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3:
861 case ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6:
862 return ADV7182_INPUT_TYPE_YPBPR;
863 case ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2:
864 case ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4:
865 case ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6:
866 case ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8:
867 return ADV7182_INPUT_TYPE_DIFF_CVBS;
868 default: /* Will never happen */
869 return 0;
870 }
871}
872
873/* ADI recommended writes to registers 0x52, 0x53, 0x54 */
874static unsigned int adv7182_lbias_settings[][3] = {
875 [ADV7182_INPUT_TYPE_CVBS] = { 0xCB, 0x4E, 0x80 },
876 [ADV7182_INPUT_TYPE_DIFF_CVBS] = { 0xC0, 0x4E, 0x80 },
877 [ADV7182_INPUT_TYPE_SVIDEO] = { 0x0B, 0xCE, 0x80 },
878 [ADV7182_INPUT_TYPE_YPBPR] = { 0x0B, 0x4E, 0xC0 },
879};
880
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300881static unsigned int adv7280_lbias_settings[][3] = {
882 [ADV7182_INPUT_TYPE_CVBS] = { 0xCD, 0x4E, 0x80 },
883 [ADV7182_INPUT_TYPE_DIFF_CVBS] = { 0xC0, 0x4E, 0x80 },
884 [ADV7182_INPUT_TYPE_SVIDEO] = { 0x0B, 0xCE, 0x80 },
885 [ADV7182_INPUT_TYPE_YPBPR] = { 0x0B, 0x4E, 0xC0 },
886};
887
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300888static int adv7182_select_input(struct adv7180_state *state, unsigned int input)
889{
890 enum adv7182_input_type input_type;
891 unsigned int *lbias;
892 unsigned int i;
893 int ret;
894
895 ret = adv7180_write(state, ADV7180_REG_INPUT_CONTROL, input);
896 if (ret)
897 return ret;
898
899 /* Reset clamp circuitry - ADI recommended writes */
900 adv7180_write(state, 0x809c, 0x00);
901 adv7180_write(state, 0x809c, 0xff);
902
903 input_type = adv7182_get_input_type(input);
904
905 switch (input_type) {
906 case ADV7182_INPUT_TYPE_CVBS:
907 case ADV7182_INPUT_TYPE_DIFF_CVBS:
908 /* ADI recommends to use the SH1 filter */
909 adv7180_write(state, 0x0017, 0x41);
910 break;
911 default:
912 adv7180_write(state, 0x0017, 0x01);
913 break;
914 }
915
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300916 if (state->chip_info->flags & ADV7180_FLAG_V2)
917 lbias = adv7280_lbias_settings[input_type];
918 else
919 lbias = adv7182_lbias_settings[input_type];
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300920
921 for (i = 0; i < ARRAY_SIZE(adv7182_lbias_settings[0]); i++)
922 adv7180_write(state, 0x0052 + i, lbias[i]);
923
924 if (input_type == ADV7182_INPUT_TYPE_DIFF_CVBS) {
925 /* ADI required writes to make differential CVBS work */
926 adv7180_write(state, 0x005f, 0xa8);
927 adv7180_write(state, 0x005a, 0x90);
928 adv7180_write(state, 0x0060, 0xb0);
929 adv7180_write(state, 0x80b6, 0x08);
930 adv7180_write(state, 0x80c0, 0xa0);
931 } else {
932 adv7180_write(state, 0x005f, 0xf0);
933 adv7180_write(state, 0x005a, 0xd0);
934 adv7180_write(state, 0x0060, 0x10);
935 adv7180_write(state, 0x80b6, 0x9c);
936 adv7180_write(state, 0x80c0, 0x00);
937 }
938
939 return 0;
940}
941
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300942static const struct adv7180_chip_info adv7180_info = {
943 .flags = ADV7180_FLAG_RESET_POWERED,
944 /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
945 * all inputs and let the card driver take care of validation
946 */
947 .valid_input_mask = BIT(ADV7180_INPUT_CVBS_AIN1) |
948 BIT(ADV7180_INPUT_CVBS_AIN2) |
949 BIT(ADV7180_INPUT_CVBS_AIN3) |
950 BIT(ADV7180_INPUT_CVBS_AIN4) |
951 BIT(ADV7180_INPUT_CVBS_AIN5) |
952 BIT(ADV7180_INPUT_CVBS_AIN6) |
953 BIT(ADV7180_INPUT_SVIDEO_AIN1_AIN2) |
954 BIT(ADV7180_INPUT_SVIDEO_AIN3_AIN4) |
955 BIT(ADV7180_INPUT_SVIDEO_AIN5_AIN6) |
956 BIT(ADV7180_INPUT_YPRPB_AIN1_AIN2_AIN3) |
957 BIT(ADV7180_INPUT_YPRPB_AIN4_AIN5_AIN6),
958 .init = adv7180_init,
959 .set_std = adv7180_set_std,
960 .select_input = adv7180_select_input,
961};
962
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300963static const struct adv7180_chip_info adv7182_info = {
964 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
965 BIT(ADV7182_INPUT_CVBS_AIN2) |
966 BIT(ADV7182_INPUT_CVBS_AIN3) |
967 BIT(ADV7182_INPUT_CVBS_AIN4) |
968 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
969 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
970 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
971 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
972 BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4),
973 .init = adv7182_init,
974 .set_std = adv7182_set_std,
975 .select_input = adv7182_select_input,
976};
977
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300978static const struct adv7180_chip_info adv7280_info = {
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300979 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_I2P,
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300980 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
981 BIT(ADV7182_INPUT_CVBS_AIN2) |
982 BIT(ADV7182_INPUT_CVBS_AIN3) |
983 BIT(ADV7182_INPUT_CVBS_AIN4) |
984 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
985 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
986 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3),
987 .init = adv7182_init,
988 .set_std = adv7182_set_std,
989 .select_input = adv7182_select_input,
990};
991
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300992static const struct adv7180_chip_info adv7280_m_info = {
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300993 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2 | ADV7180_FLAG_I2P,
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300994 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
995 BIT(ADV7182_INPUT_CVBS_AIN2) |
996 BIT(ADV7182_INPUT_CVBS_AIN3) |
997 BIT(ADV7182_INPUT_CVBS_AIN4) |
998 BIT(ADV7182_INPUT_CVBS_AIN5) |
999 BIT(ADV7182_INPUT_CVBS_AIN6) |
1000 BIT(ADV7182_INPUT_CVBS_AIN7) |
1001 BIT(ADV7182_INPUT_CVBS_AIN8) |
1002 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1003 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1004 BIT(ADV7182_INPUT_SVIDEO_AIN5_AIN6) |
1005 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1006 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
1007 BIT(ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6),
1008 .init = adv7182_init,
1009 .set_std = adv7182_set_std,
1010 .select_input = adv7182_select_input,
1011};
1012
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001013static const struct adv7180_chip_info adv7281_info = {
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001014 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2,
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001015 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1016 BIT(ADV7182_INPUT_CVBS_AIN2) |
1017 BIT(ADV7182_INPUT_CVBS_AIN7) |
1018 BIT(ADV7182_INPUT_CVBS_AIN8) |
1019 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1020 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1021 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1022 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1023 .init = adv7182_init,
1024 .set_std = adv7182_set_std,
1025 .select_input = adv7182_select_input,
1026};
1027
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001028static const struct adv7180_chip_info adv7281_m_info = {
1029 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2,
1030 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1031 BIT(ADV7182_INPUT_CVBS_AIN2) |
1032 BIT(ADV7182_INPUT_CVBS_AIN3) |
1033 BIT(ADV7182_INPUT_CVBS_AIN4) |
1034 BIT(ADV7182_INPUT_CVBS_AIN7) |
1035 BIT(ADV7182_INPUT_CVBS_AIN8) |
1036 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1037 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1038 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1039 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
1040 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1041 BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4) |
1042 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1043 .init = adv7182_init,
1044 .set_std = adv7182_set_std,
1045 .select_input = adv7182_select_input,
1046};
1047
1048static const struct adv7180_chip_info adv7281_ma_info = {
1049 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2,
1050 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1051 BIT(ADV7182_INPUT_CVBS_AIN2) |
1052 BIT(ADV7182_INPUT_CVBS_AIN3) |
1053 BIT(ADV7182_INPUT_CVBS_AIN4) |
1054 BIT(ADV7182_INPUT_CVBS_AIN5) |
1055 BIT(ADV7182_INPUT_CVBS_AIN6) |
1056 BIT(ADV7182_INPUT_CVBS_AIN7) |
1057 BIT(ADV7182_INPUT_CVBS_AIN8) |
1058 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1059 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1060 BIT(ADV7182_INPUT_SVIDEO_AIN5_AIN6) |
1061 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1062 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
1063 BIT(ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6) |
1064 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1065 BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4) |
1066 BIT(ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6) |
1067 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1068 .init = adv7182_init,
1069 .set_std = adv7182_set_std,
1070 .select_input = adv7182_select_input,
1071};
1072
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001073static const struct adv7180_chip_info adv7282_info = {
1074 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_I2P,
1075 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1076 BIT(ADV7182_INPUT_CVBS_AIN2) |
1077 BIT(ADV7182_INPUT_CVBS_AIN7) |
1078 BIT(ADV7182_INPUT_CVBS_AIN8) |
1079 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1080 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1081 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1082 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1083 .init = adv7182_init,
1084 .set_std = adv7182_set_std,
1085 .select_input = adv7182_select_input,
1086};
1087
1088static const struct adv7180_chip_info adv7282_m_info = {
1089 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2 | ADV7180_FLAG_I2P,
1090 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1091 BIT(ADV7182_INPUT_CVBS_AIN2) |
1092 BIT(ADV7182_INPUT_CVBS_AIN3) |
1093 BIT(ADV7182_INPUT_CVBS_AIN4) |
1094 BIT(ADV7182_INPUT_CVBS_AIN7) |
1095 BIT(ADV7182_INPUT_CVBS_AIN8) |
1096 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1097 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1098 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1099 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1100 BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4) |
1101 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1102 .init = adv7182_init,
1103 .set_std = adv7182_set_std,
1104 .select_input = adv7182_select_input,
1105};
1106
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001107static int init_device(struct adv7180_state *state)
Federico Vagabca7ad12012-04-12 12:39:36 -03001108{
1109 int ret;
1110
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001111 mutex_lock(&state->mutex);
1112
Lars-Peter Clausenc18818e2015-01-23 12:52:25 -03001113 adv7180_write(state, ADV7180_REG_PWR_MAN, ADV7180_PWR_MAN_RES);
1114 usleep_range(2000, 10000);
1115
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001116 ret = state->chip_info->init(state);
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -03001117 if (ret)
1118 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001119
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001120 ret = adv7180_program_std(state);
1121 if (ret)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001122 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001123
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001124 adv7180_set_field_mode(state);
1125
Federico Vagabca7ad12012-04-12 12:39:36 -03001126 /* register for interrupts */
1127 if (state->irq > 0) {
Federico Vagabca7ad12012-04-12 12:39:36 -03001128 /* config the Interrupt pin to be active low */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001129 ret = adv7180_write(state, ADV7180_REG_ICONF1,
Federico Vagabca7ad12012-04-12 12:39:36 -03001130 ADV7180_ICONF1_ACTIVE_LOW |
1131 ADV7180_ICONF1_PSYNC_ONLY);
1132 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001133 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001134
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001135 ret = adv7180_write(state, ADV7180_REG_IMR1, 0);
Federico Vagabca7ad12012-04-12 12:39:36 -03001136 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001137 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001138
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001139 ret = adv7180_write(state, ADV7180_REG_IMR2, 0);
Federico Vagabca7ad12012-04-12 12:39:36 -03001140 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001141 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001142
1143 /* enable AD change interrupts interrupts */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001144 ret = adv7180_write(state, ADV7180_REG_IMR3,
Federico Vagabca7ad12012-04-12 12:39:36 -03001145 ADV7180_IRQ3_AD_CHANGE);
1146 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001147 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001148
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001149 ret = adv7180_write(state, ADV7180_REG_IMR4, 0);
Federico Vagabca7ad12012-04-12 12:39:36 -03001150 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001151 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001152 }
1153
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001154out_unlock:
1155 mutex_unlock(&state->mutex);
Alexey Khoroshilovdf065b32014-03-14 18:04:03 -03001156
Alexey Khoroshilovdf065b32014-03-14 18:04:03 -03001157 return ret;
Federico Vagabca7ad12012-04-12 12:39:36 -03001158}
Richard Röjfors6789cb52009-09-18 21:17:20 -03001159
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001160static int adv7180_probe(struct i2c_client *client,
1161 const struct i2c_device_id *id)
Richard Röjfors6789cb52009-09-18 21:17:20 -03001162{
1163 struct adv7180_state *state;
1164 struct v4l2_subdev *sd;
1165 int ret;
1166
1167 /* Check if the adapter supports the needed features */
1168 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1169 return -EIO;
1170
1171 v4l_info(client, "chip found @ 0x%02x (%s)\n",
Federico Vagabca7ad12012-04-12 12:39:36 -03001172 client->addr, client->adapter->name);
Richard Röjfors6789cb52009-09-18 21:17:20 -03001173
Laurent Pinchartc02b2112013-05-02 08:29:43 -03001174 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Fabio Estevam7657e062014-12-16 13:49:07 -03001175 if (state == NULL)
1176 return -ENOMEM;
Richard Röjfors42752f72009-09-22 06:07:06 -03001177
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001178 state->client = client;
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001179 state->field = V4L2_FIELD_INTERLACED;
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001180 state->chip_info = (struct adv7180_chip_info *)id->driver_data;
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001181
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001182 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
1183 state->csi_client = i2c_new_dummy(client->adapter,
1184 ADV7180_DEFAULT_CSI_I2C_ADDR);
1185 if (!state->csi_client)
1186 return -ENOMEM;
1187 }
1188
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001189 if (state->chip_info->flags & ADV7180_FLAG_I2P) {
1190 state->vpp_client = i2c_new_dummy(client->adapter,
1191 ADV7180_DEFAULT_VPP_I2C_ADDR);
1192 if (!state->vpp_client) {
1193 ret = -ENOMEM;
1194 goto err_unregister_csi_client;
1195 }
1196 }
1197
Richard Röjfors42752f72009-09-22 06:07:06 -03001198 state->irq = client->irq;
Richard Röjfors42752f72009-09-22 06:07:06 -03001199 mutex_init(&state->mutex);
Richard Röjforsc277b602009-09-22 06:06:34 -03001200 state->autodetect = true;
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001201 if (state->chip_info->flags & ADV7180_FLAG_RESET_POWERED)
1202 state->powered = true;
1203 else
1204 state->powered = false;
Federico Vagabca7ad12012-04-12 12:39:36 -03001205 state->input = 0;
Richard Röjfors6789cb52009-09-18 21:17:20 -03001206 sd = &state->sd;
1207 v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001208 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
Richard Röjfors6789cb52009-09-18 21:17:20 -03001209
Federico Vagac9fbedd2012-07-11 11:29:33 -03001210 ret = adv7180_init_controls(state);
1211 if (ret)
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001212 goto err_unregister_vpp_client;
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001213
1214 state->pad.flags = MEDIA_PAD_FL_SOURCE;
1215 sd->entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;
1216 ret = media_entity_init(&sd->entity, 1, &state->pad, 0);
Federico Vagac9fbedd2012-07-11 11:29:33 -03001217 if (ret)
1218 goto err_free_ctrl;
Lars-Peter Clausenfa5b7942014-03-07 13:14:32 -03001219
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001220 ret = init_device(state);
1221 if (ret)
1222 goto err_media_entity_cleanup;
1223
Lars-Peter Clausenfa5721d2015-01-23 12:52:20 -03001224 if (state->irq) {
1225 ret = request_threaded_irq(client->irq, NULL, adv7180_irq,
Lars-Peter Clausenf3e991d2015-01-23 12:52:21 -03001226 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
1227 KBUILD_MODNAME, state);
Lars-Peter Clausenfa5721d2015-01-23 12:52:20 -03001228 if (ret)
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001229 goto err_media_entity_cleanup;
Lars-Peter Clausenfa5721d2015-01-23 12:52:20 -03001230 }
1231
Lars-Peter Clausenfa5b7942014-03-07 13:14:32 -03001232 ret = v4l2_async_register_subdev(sd);
1233 if (ret)
1234 goto err_free_irq;
1235
Richard Röjfors6789cb52009-09-18 21:17:20 -03001236 return 0;
Richard Röjfors42752f72009-09-22 06:07:06 -03001237
Lars-Peter Clausenfa5b7942014-03-07 13:14:32 -03001238err_free_irq:
1239 if (state->irq > 0)
1240 free_irq(client->irq, state);
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001241err_media_entity_cleanup:
1242 media_entity_cleanup(&sd->entity);
Federico Vagac9fbedd2012-07-11 11:29:33 -03001243err_free_ctrl:
1244 adv7180_exit_controls(state);
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001245err_unregister_vpp_client:
1246 if (state->chip_info->flags & ADV7180_FLAG_I2P)
1247 i2c_unregister_device(state->vpp_client);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001248err_unregister_csi_client:
1249 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2)
1250 i2c_unregister_device(state->csi_client);
Lars-Peter Clausen297a0ae2014-03-07 13:14:27 -03001251 mutex_destroy(&state->mutex);
Richard Röjfors42752f72009-09-22 06:07:06 -03001252 return ret;
Richard Röjfors6789cb52009-09-18 21:17:20 -03001253}
1254
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001255static int adv7180_remove(struct i2c_client *client)
Richard Röjfors6789cb52009-09-18 21:17:20 -03001256{
1257 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Richard Röjfors42752f72009-09-22 06:07:06 -03001258 struct adv7180_state *state = to_state(sd);
Richard Röjfors6789cb52009-09-18 21:17:20 -03001259
Lars-Peter Clausenfa5b7942014-03-07 13:14:32 -03001260 v4l2_async_unregister_subdev(sd);
1261
Lars-Peter Clausen0c255342014-03-07 13:14:31 -03001262 if (state->irq > 0)
Richard Röjfors42752f72009-09-22 06:07:06 -03001263 free_irq(client->irq, state);
Richard Röjfors42752f72009-09-22 06:07:06 -03001264
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001265 media_entity_cleanup(&sd->entity);
Lars-Peter Clausenb13f4af2014-03-07 13:14:28 -03001266 adv7180_exit_controls(state);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001267
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001268 if (state->chip_info->flags & ADV7180_FLAG_I2P)
1269 i2c_unregister_device(state->vpp_client);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001270 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2)
1271 i2c_unregister_device(state->csi_client);
1272
Lars-Peter Clausen297a0ae2014-03-07 13:14:27 -03001273 mutex_destroy(&state->mutex);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001274
Richard Röjfors6789cb52009-09-18 21:17:20 -03001275 return 0;
1276}
1277
1278static const struct i2c_device_id adv7180_id[] = {
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001279 { "adv7180", (kernel_ulong_t)&adv7180_info },
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -03001280 { "adv7182", (kernel_ulong_t)&adv7182_info },
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001281 { "adv7280", (kernel_ulong_t)&adv7280_info },
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001282 { "adv7280-m", (kernel_ulong_t)&adv7280_m_info },
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001283 { "adv7281", (kernel_ulong_t)&adv7281_info },
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001284 { "adv7281-m", (kernel_ulong_t)&adv7281_m_info },
1285 { "adv7281-ma", (kernel_ulong_t)&adv7281_ma_info },
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001286 { "adv7282", (kernel_ulong_t)&adv7282_info },
1287 { "adv7282-m", (kernel_ulong_t)&adv7282_m_info },
Richard Röjfors6789cb52009-09-18 21:17:20 -03001288 {},
1289};
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001290MODULE_DEVICE_TABLE(i2c, adv7180_id);
Richard Röjfors6789cb52009-09-18 21:17:20 -03001291
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001292#ifdef CONFIG_PM_SLEEP
1293static int adv7180_suspend(struct device *dev)
Federico Vagabca7ad12012-04-12 12:39:36 -03001294{
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001295 struct i2c_client *client = to_i2c_client(dev);
Lars-Peter Clausene246c332014-03-10 14:05:39 -03001296 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1297 struct adv7180_state *state = to_state(sd);
Federico Vagabca7ad12012-04-12 12:39:36 -03001298
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001299 return adv7180_set_power(state, false);
Federico Vagabca7ad12012-04-12 12:39:36 -03001300}
1301
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001302static int adv7180_resume(struct device *dev)
Federico Vagabca7ad12012-04-12 12:39:36 -03001303{
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001304 struct i2c_client *client = to_i2c_client(dev);
Federico Vagabca7ad12012-04-12 12:39:36 -03001305 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1306 struct adv7180_state *state = to_state(sd);
1307 int ret;
1308
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001309 ret = init_device(state);
Federico Vagabca7ad12012-04-12 12:39:36 -03001310 if (ret < 0)
1311 return ret;
Lars-Peter Clausenc18818e2015-01-23 12:52:25 -03001312
1313 ret = adv7180_set_power(state, state->powered);
1314 if (ret)
1315 return ret;
1316
Federico Vagabca7ad12012-04-12 12:39:36 -03001317 return 0;
1318}
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001319
1320static SIMPLE_DEV_PM_OPS(adv7180_pm_ops, adv7180_suspend, adv7180_resume);
1321#define ADV7180_PM_OPS (&adv7180_pm_ops)
1322
1323#else
1324#define ADV7180_PM_OPS NULL
Federico Vagabca7ad12012-04-12 12:39:36 -03001325#endif
1326
Richard Röjfors6789cb52009-09-18 21:17:20 -03001327static struct i2c_driver adv7180_driver = {
1328 .driver = {
Federico Vagabca7ad12012-04-12 12:39:36 -03001329 .owner = THIS_MODULE,
Federico Vagac9fbedd2012-07-11 11:29:33 -03001330 .name = KBUILD_MODNAME,
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001331 .pm = ADV7180_PM_OPS,
Federico Vagabca7ad12012-04-12 12:39:36 -03001332 },
1333 .probe = adv7180_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001334 .remove = adv7180_remove,
Federico Vagabca7ad12012-04-12 12:39:36 -03001335 .id_table = adv7180_id,
Richard Röjfors6789cb52009-09-18 21:17:20 -03001336};
1337
Axel Linc6e8d862012-02-12 06:56:32 -03001338module_i2c_driver(adv7180_driver);
Richard Röjfors6789cb52009-09-18 21:17:20 -03001339
1340MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
1341MODULE_AUTHOR("Mocean Laboratories");
1342MODULE_LICENSE("GPL v2");