blob: 95cbc857f36e981145a01e221281767afa6adaa9 [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>
Ben Dooks250121d2015-06-03 10:59:50 -030028#include <linux/of.h>
Richard Röjfors6789cb52009-09-18 21:17:20 -030029#include <linux/videodev2.h>
Hans Verkuil937feee2016-04-22 10:03:37 -030030#include <media/v4l2-ioctl.h>
31#include <media/v4l2-event.h>
Richard Röjfors6789cb52009-09-18 21:17:20 -030032#include <media/v4l2-device.h>
Federico Vagac9fbedd2012-07-11 11:29:33 -030033#include <media/v4l2-ctrls.h>
Richard Röjfors42752f72009-09-22 06:07:06 -030034#include <linux/mutex.h>
Lars-Peter Clausenc18818e2015-01-23 12:52:25 -030035#include <linux/delay.h>
Richard Röjfors6789cb52009-09-18 21:17:20 -030036
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -030037#define ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM 0x0
38#define ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM_PED 0x1
39#define ADV7180_STD_AD_PAL_N_NTSC_J_SECAM 0x2
40#define ADV7180_STD_AD_PAL_N_NTSC_M_SECAM 0x3
41#define ADV7180_STD_NTSC_J 0x4
42#define ADV7180_STD_NTSC_M 0x5
43#define ADV7180_STD_PAL60 0x6
44#define ADV7180_STD_NTSC_443 0x7
45#define ADV7180_STD_PAL_BG 0x8
46#define ADV7180_STD_PAL_N 0x9
47#define ADV7180_STD_PAL_M 0xa
48#define ADV7180_STD_PAL_M_PED 0xb
49#define ADV7180_STD_PAL_COMB_N 0xc
50#define ADV7180_STD_PAL_COMB_N_PED 0xd
51#define ADV7180_STD_PAL_SECAM 0xe
52#define ADV7180_STD_PAL_SECAM_PED 0xf
53
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030054#define ADV7180_REG_INPUT_CONTROL 0x0000
Federico Vagabca7ad12012-04-12 12:39:36 -030055#define ADV7180_INPUT_CONTROL_INSEL_MASK 0x0f
Richard Röjforsd3124292009-09-22 06:05:42 -030056
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -030057#define ADV7182_REG_INPUT_VIDSEL 0x0002
58
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030059#define ADV7180_REG_EXTENDED_OUTPUT_CONTROL 0x0004
Richard Röjfors42752f72009-09-22 06:07:06 -030060#define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5
Richard Röjfors6789cb52009-09-18 21:17:20 -030061
Lars-Peter Clausen029d6172015-01-23 12:52:23 -030062#define ADV7180_REG_AUTODETECT_ENABLE 0x07
Richard Röjfors42752f72009-09-22 06:07:06 -030063#define ADV7180_AUTODETECT_DEFAULT 0x7f
Federico Vagac9fbedd2012-07-11 11:29:33 -030064/* Contrast */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030065#define ADV7180_REG_CON 0x0008 /*Unsigned */
Federico Vagac9fbedd2012-07-11 11:29:33 -030066#define ADV7180_CON_MIN 0
67#define ADV7180_CON_DEF 128
68#define ADV7180_CON_MAX 255
69/* Brightness*/
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030070#define ADV7180_REG_BRI 0x000a /*Signed */
Federico Vagac9fbedd2012-07-11 11:29:33 -030071#define ADV7180_BRI_MIN -128
72#define ADV7180_BRI_DEF 0
73#define ADV7180_BRI_MAX 127
74/* Hue */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030075#define ADV7180_REG_HUE 0x000b /*Signed, inverted */
Federico Vagac9fbedd2012-07-11 11:29:33 -030076#define ADV7180_HUE_MIN -127
77#define ADV7180_HUE_DEF 0
78#define ADV7180_HUE_MAX 128
Federico Vagabca7ad12012-04-12 12:39:36 -030079
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030080#define ADV7180_REG_CTRL 0x000e
Lars-Peter Clausen029d6172015-01-23 12:52:23 -030081#define ADV7180_CTRL_IRQ_SPACE 0x20
Richard Röjfors6789cb52009-09-18 21:17:20 -030082
Lars-Peter Clausen029d6172015-01-23 12:52:23 -030083#define ADV7180_REG_PWR_MAN 0x0f
Federico Vagabca7ad12012-04-12 12:39:36 -030084#define ADV7180_PWR_MAN_ON 0x04
85#define ADV7180_PWR_MAN_OFF 0x24
86#define ADV7180_PWR_MAN_RES 0x80
87
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -030088#define ADV7180_REG_STATUS1 0x0010
Richard Röjforsd3124292009-09-22 06:05:42 -030089#define ADV7180_STATUS1_IN_LOCK 0x01
90#define ADV7180_STATUS1_AUTOD_MASK 0x70
Richard Röjfors6789cb52009-09-18 21:17:20 -030091#define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
92#define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
93#define ADV7180_STATUS1_AUTOD_PAL_M 0x20
94#define ADV7180_STATUS1_AUTOD_PAL_60 0x30
95#define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
96#define ADV7180_STATUS1_AUTOD_SECAM 0x50
97#define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
98#define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
99
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300100#define ADV7180_REG_IDENT 0x0011
Richard Röjfors6789cb52009-09-18 21:17:20 -0300101#define ADV7180_ID_7180 0x18
102
Steve Longerbeam52e37f02016-07-19 21:03:29 -0300103#define ADV7180_REG_ICONF1 0x2040
Richard Röjfors42752f72009-09-22 06:07:06 -0300104#define ADV7180_ICONF1_ACTIVE_LOW 0x01
105#define ADV7180_ICONF1_PSYNC_ONLY 0x10
106#define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
Federico Vagac9fbedd2012-07-11 11:29:33 -0300107/* Saturation */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300108#define ADV7180_REG_SD_SAT_CB 0x00e3 /*Unsigned */
109#define ADV7180_REG_SD_SAT_CR 0x00e4 /*Unsigned */
Federico Vagac9fbedd2012-07-11 11:29:33 -0300110#define ADV7180_SAT_MIN 0
111#define ADV7180_SAT_DEF 128
112#define ADV7180_SAT_MAX 255
Federico Vagabca7ad12012-04-12 12:39:36 -0300113
Richard Röjfors42752f72009-09-22 06:07:06 -0300114#define ADV7180_IRQ1_LOCK 0x01
115#define ADV7180_IRQ1_UNLOCK 0x02
Steve Longerbeam52e37f02016-07-19 21:03:29 -0300116#define ADV7180_REG_ISR1 0x2042
117#define ADV7180_REG_ICR1 0x2043
118#define ADV7180_REG_IMR1 0x2044
119#define ADV7180_REG_IMR2 0x2048
Richard Röjfors42752f72009-09-22 06:07:06 -0300120#define ADV7180_IRQ3_AD_CHANGE 0x08
Steve Longerbeam52e37f02016-07-19 21:03:29 -0300121#define ADV7180_REG_ISR3 0x204A
122#define ADV7180_REG_ICR3 0x204B
123#define ADV7180_REG_IMR3 0x204C
124#define ADV7180_REG_IMR4 0x2050
Richard Röjfors6789cb52009-09-18 21:17:20 -0300125
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300126#define ADV7180_REG_NTSC_V_BIT_END 0x00E6
Federico Vagabca7ad12012-04-12 12:39:36 -0300127#define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F
128
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300129#define ADV7180_REG_VPP_SLAVE_ADDR 0xFD
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300130#define ADV7180_REG_CSI_SLAVE_ADDR 0xFE
131
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300132#define ADV7180_REG_FLCONTROL 0x40e0
133#define ADV7180_FLCONTROL_FL_ENABLE 0x1
134
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300135#define ADV7180_CSI_REG_PWRDN 0x00
136#define ADV7180_CSI_PWRDN 0x80
137
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300138#define ADV7180_INPUT_CVBS_AIN1 0x00
139#define ADV7180_INPUT_CVBS_AIN2 0x01
140#define ADV7180_INPUT_CVBS_AIN3 0x02
141#define ADV7180_INPUT_CVBS_AIN4 0x03
142#define ADV7180_INPUT_CVBS_AIN5 0x04
143#define ADV7180_INPUT_CVBS_AIN6 0x05
144#define ADV7180_INPUT_SVIDEO_AIN1_AIN2 0x06
145#define ADV7180_INPUT_SVIDEO_AIN3_AIN4 0x07
146#define ADV7180_INPUT_SVIDEO_AIN5_AIN6 0x08
147#define ADV7180_INPUT_YPRPB_AIN1_AIN2_AIN3 0x09
148#define ADV7180_INPUT_YPRPB_AIN4_AIN5_AIN6 0x0a
149
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300150#define ADV7182_INPUT_CVBS_AIN1 0x00
151#define ADV7182_INPUT_CVBS_AIN2 0x01
152#define ADV7182_INPUT_CVBS_AIN3 0x02
153#define ADV7182_INPUT_CVBS_AIN4 0x03
154#define ADV7182_INPUT_CVBS_AIN5 0x04
155#define ADV7182_INPUT_CVBS_AIN6 0x05
156#define ADV7182_INPUT_CVBS_AIN7 0x06
157#define ADV7182_INPUT_CVBS_AIN8 0x07
158#define ADV7182_INPUT_SVIDEO_AIN1_AIN2 0x08
159#define ADV7182_INPUT_SVIDEO_AIN3_AIN4 0x09
160#define ADV7182_INPUT_SVIDEO_AIN5_AIN6 0x0a
161#define ADV7182_INPUT_SVIDEO_AIN7_AIN8 0x0b
162#define ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3 0x0c
163#define ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6 0x0d
164#define ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2 0x0e
165#define ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4 0x0f
166#define ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6 0x10
167#define ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8 0x11
168
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300169#define ADV7180_DEFAULT_CSI_I2C_ADDR 0x44
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300170#define ADV7180_DEFAULT_VPP_I2C_ADDR 0x42
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300171
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300172#define V4L2_CID_ADV_FAST_SWITCH (V4L2_CID_USER_ADV7180_BASE + 0x00)
173
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300174struct adv7180_state;
175
176#define ADV7180_FLAG_RESET_POWERED BIT(0)
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300177#define ADV7180_FLAG_V2 BIT(1)
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300178#define ADV7180_FLAG_MIPI_CSI2 BIT(2)
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300179#define ADV7180_FLAG_I2P BIT(3)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300180
181struct adv7180_chip_info {
182 unsigned int flags;
183 unsigned int valid_input_mask;
184 int (*set_std)(struct adv7180_state *st, unsigned int std);
185 int (*select_input)(struct adv7180_state *st, unsigned int input);
186 int (*init)(struct adv7180_state *state);
187};
188
Richard Röjfors6789cb52009-09-18 21:17:20 -0300189struct adv7180_state {
Federico Vagac9fbedd2012-07-11 11:29:33 -0300190 struct v4l2_ctrl_handler ctrl_hdl;
Richard Röjforsc277b602009-09-22 06:06:34 -0300191 struct v4l2_subdev sd;
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300192 struct media_pad pad;
Richard Röjfors42752f72009-09-22 06:07:06 -0300193 struct mutex mutex; /* mutual excl. when accessing chip */
194 int irq;
Richard Röjforsc277b602009-09-22 06:06:34 -0300195 v4l2_std_id curr_norm;
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300196 bool powered;
Hans Verkuil937feee2016-04-22 10:03:37 -0300197 bool streaming;
Federico Vagabca7ad12012-04-12 12:39:36 -0300198 u8 input;
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300199
200 struct i2c_client *client;
201 unsigned int register_page;
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300202 struct i2c_client *csi_client;
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300203 struct i2c_client *vpp_client;
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300204 const struct adv7180_chip_info *chip_info;
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300205 enum v4l2_field field;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300206};
Federico Vagac9fbedd2012-07-11 11:29:33 -0300207#define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler, \
208 struct adv7180_state, \
209 ctrl_hdl)->sd)
Richard Röjfors6789cb52009-09-18 21:17:20 -0300210
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300211static int adv7180_select_page(struct adv7180_state *state, unsigned int page)
212{
213 if (state->register_page != page) {
214 i2c_smbus_write_byte_data(state->client, ADV7180_REG_CTRL,
215 page);
216 state->register_page = page;
217 }
218
219 return 0;
220}
221
222static int adv7180_write(struct adv7180_state *state, unsigned int reg,
223 unsigned int value)
224{
225 lockdep_assert_held(&state->mutex);
226 adv7180_select_page(state, reg >> 8);
227 return i2c_smbus_write_byte_data(state->client, reg & 0xff, value);
228}
229
230static int adv7180_read(struct adv7180_state *state, unsigned int reg)
231{
232 lockdep_assert_held(&state->mutex);
233 adv7180_select_page(state, reg >> 8);
234 return i2c_smbus_read_byte_data(state->client, reg & 0xff);
235}
236
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300237static int adv7180_csi_write(struct adv7180_state *state, unsigned int reg,
238 unsigned int value)
239{
240 return i2c_smbus_write_byte_data(state->csi_client, reg, value);
241}
242
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300243static int adv7180_set_video_standard(struct adv7180_state *state,
244 unsigned int std)
245{
246 return state->chip_info->set_std(state, std);
247}
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300248
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300249static int adv7180_vpp_write(struct adv7180_state *state, unsigned int reg,
250 unsigned int value)
251{
252 return i2c_smbus_write_byte_data(state->vpp_client, reg, value);
253}
254
Richard Röjforsd3124292009-09-22 06:05:42 -0300255static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
Richard Röjfors6789cb52009-09-18 21:17:20 -0300256{
Vladimir Barinovb294a192013-04-11 18:06:46 -0300257 /* in case V4L2_IN_ST_NO_SIGNAL */
258 if (!(status1 & ADV7180_STATUS1_IN_LOCK))
259 return V4L2_STD_UNKNOWN;
260
Richard Röjfors6789cb52009-09-18 21:17:20 -0300261 switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
262 case ADV7180_STATUS1_AUTOD_NTSM_M_J:
Richard Röjforsd3124292009-09-22 06:05:42 -0300263 return V4L2_STD_NTSC;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300264 case ADV7180_STATUS1_AUTOD_NTSC_4_43:
265 return V4L2_STD_NTSC_443;
266 case ADV7180_STATUS1_AUTOD_PAL_M:
267 return V4L2_STD_PAL_M;
268 case ADV7180_STATUS1_AUTOD_PAL_60:
269 return V4L2_STD_PAL_60;
270 case ADV7180_STATUS1_AUTOD_PAL_B_G:
271 return V4L2_STD_PAL;
272 case ADV7180_STATUS1_AUTOD_SECAM:
273 return V4L2_STD_SECAM;
274 case ADV7180_STATUS1_AUTOD_PAL_COMB:
275 return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
276 case ADV7180_STATUS1_AUTOD_SECAM_525:
277 return V4L2_STD_SECAM;
278 default:
279 return V4L2_STD_UNKNOWN;
280 }
281}
282
Richard Röjforsc277b602009-09-22 06:06:34 -0300283static int v4l2_std_to_adv7180(v4l2_std_id std)
284{
285 if (std == V4L2_STD_PAL_60)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300286 return ADV7180_STD_PAL60;
Richard Röjforsc277b602009-09-22 06:06:34 -0300287 if (std == V4L2_STD_NTSC_443)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300288 return ADV7180_STD_NTSC_443;
Richard Röjforsc277b602009-09-22 06:06:34 -0300289 if (std == V4L2_STD_PAL_N)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300290 return ADV7180_STD_PAL_N;
Richard Röjforsc277b602009-09-22 06:06:34 -0300291 if (std == V4L2_STD_PAL_M)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300292 return ADV7180_STD_PAL_M;
Richard Röjforsc277b602009-09-22 06:06:34 -0300293 if (std == V4L2_STD_PAL_Nc)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300294 return ADV7180_STD_PAL_COMB_N;
Richard Röjforsc277b602009-09-22 06:06:34 -0300295
296 if (std & V4L2_STD_PAL)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300297 return ADV7180_STD_PAL_BG;
Richard Röjforsc277b602009-09-22 06:06:34 -0300298 if (std & V4L2_STD_NTSC)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300299 return ADV7180_STD_NTSC_M;
Richard Röjforsc277b602009-09-22 06:06:34 -0300300 if (std & V4L2_STD_SECAM)
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300301 return ADV7180_STD_PAL_SECAM;
Richard Röjforsc277b602009-09-22 06:06:34 -0300302
303 return -EINVAL;
304}
305
Richard Röjforsd3124292009-09-22 06:05:42 -0300306static u32 adv7180_status_to_v4l2(u8 status1)
307{
308 if (!(status1 & ADV7180_STATUS1_IN_LOCK))
309 return V4L2_IN_ST_NO_SIGNAL;
310
311 return 0;
312}
313
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300314static int __adv7180_status(struct adv7180_state *state, u32 *status,
Federico Vagabca7ad12012-04-12 12:39:36 -0300315 v4l2_std_id *std)
Richard Röjforsd3124292009-09-22 06:05:42 -0300316{
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300317 int status1 = adv7180_read(state, ADV7180_REG_STATUS1);
Richard Röjforsd3124292009-09-22 06:05:42 -0300318
319 if (status1 < 0)
320 return status1;
321
322 if (status)
323 *status = adv7180_status_to_v4l2(status1);
324 if (std)
325 *std = adv7180_std_to_v4l2(status1);
326
327 return 0;
328}
329
Richard Röjfors6789cb52009-09-18 21:17:20 -0300330static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
331{
332 return container_of(sd, struct adv7180_state, sd);
333}
334
335static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
336{
Richard Röjforsc277b602009-09-22 06:06:34 -0300337 struct adv7180_state *state = to_state(sd);
Richard Röjfors42752f72009-09-22 06:07:06 -0300338 int err = mutex_lock_interruptible(&state->mutex);
339 if (err)
340 return err;
Richard Röjforsc277b602009-09-22 06:06:34 -0300341
Hans Verkuil937feee2016-04-22 10:03:37 -0300342 if (state->streaming) {
343 err = -EBUSY;
344 goto unlock;
345 }
Richard Röjforsc277b602009-09-22 06:06:34 -0300346
Hans Verkuil937feee2016-04-22 10:03:37 -0300347 err = adv7180_set_video_standard(state,
348 ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM);
349 if (err)
350 goto unlock;
351
352 msleep(100);
353 __adv7180_status(state, NULL, std);
354
355 err = v4l2_std_to_adv7180(state->curr_norm);
356 if (err < 0)
357 goto unlock;
358
359 err = adv7180_set_video_standard(state, err);
360
361unlock:
Richard Röjfors42752f72009-09-22 06:07:06 -0300362 mutex_unlock(&state->mutex);
Richard Röjforsc277b602009-09-22 06:06:34 -0300363 return err;
Richard Röjforsd3124292009-09-22 06:05:42 -0300364}
Richard Röjfors6789cb52009-09-18 21:17:20 -0300365
Federico Vagabca7ad12012-04-12 12:39:36 -0300366static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
367 u32 output, u32 config)
368{
369 struct adv7180_state *state = to_state(sd);
370 int ret = mutex_lock_interruptible(&state->mutex);
Federico Vagabca7ad12012-04-12 12:39:36 -0300371
372 if (ret)
373 return ret;
374
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300375 if (input > 31 || !(BIT(input) & state->chip_info->valid_input_mask)) {
376 ret = -EINVAL;
Federico Vagabca7ad12012-04-12 12:39:36 -0300377 goto out;
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300378 }
Federico Vagabca7ad12012-04-12 12:39:36 -0300379
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300380 ret = state->chip_info->select_input(state, input);
Federico Vagabca7ad12012-04-12 12:39:36 -0300381
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300382 if (ret == 0)
383 state->input = input;
Federico Vagabca7ad12012-04-12 12:39:36 -0300384out:
385 mutex_unlock(&state->mutex);
386 return ret;
387}
388
Richard Röjforsd3124292009-09-22 06:05:42 -0300389static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
390{
Richard Röjfors42752f72009-09-22 06:07:06 -0300391 struct adv7180_state *state = to_state(sd);
392 int ret = mutex_lock_interruptible(&state->mutex);
393 if (ret)
394 return ret;
395
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300396 ret = __adv7180_status(state, status, NULL);
Richard Röjfors42752f72009-09-22 06:07:06 -0300397 mutex_unlock(&state->mutex);
398 return ret;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300399}
400
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300401static int adv7180_program_std(struct adv7180_state *state)
402{
403 int ret;
404
Hans Verkuil937feee2016-04-22 10:03:37 -0300405 ret = v4l2_std_to_adv7180(state->curr_norm);
406 if (ret < 0)
407 return ret;
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300408
Hans Verkuil937feee2016-04-22 10:03:37 -0300409 ret = adv7180_set_video_standard(state, ret);
410 if (ret < 0)
411 return ret;
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300412 return 0;
413}
414
Richard Röjforsc277b602009-09-22 06:06:34 -0300415static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
416{
417 struct adv7180_state *state = to_state(sd);
Richard Röjfors42752f72009-09-22 06:07:06 -0300418 int ret = mutex_lock_interruptible(&state->mutex);
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -0300419
Richard Röjfors42752f72009-09-22 06:07:06 -0300420 if (ret)
421 return ret;
Richard Röjforsc277b602009-09-22 06:06:34 -0300422
Hans Verkuil937feee2016-04-22 10:03:37 -0300423 /* Make sure we can support this std */
424 ret = v4l2_std_to_adv7180(std);
425 if (ret < 0)
426 goto out;
Richard Röjforsc277b602009-09-22 06:06:34 -0300427
Hans Verkuil937feee2016-04-22 10:03:37 -0300428 state->curr_norm = std;
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
Niklas Söderlundd0fadc82016-04-02 14:42:18 -0300436static int adv7180_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
437{
438 struct adv7180_state *state = to_state(sd);
439
440 *norm = state->curr_norm;
441
442 return 0;
443}
444
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300445static int adv7180_set_power(struct adv7180_state *state, bool on)
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300446{
447 u8 val;
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300448 int ret;
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300449
450 if (on)
451 val = ADV7180_PWR_MAN_ON;
452 else
453 val = ADV7180_PWR_MAN_OFF;
454
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300455 ret = adv7180_write(state, ADV7180_REG_PWR_MAN, val);
456 if (ret)
457 return ret;
458
459 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
460 if (on) {
461 adv7180_csi_write(state, 0xDE, 0x02);
462 adv7180_csi_write(state, 0xD2, 0xF7);
463 adv7180_csi_write(state, 0xD8, 0x65);
464 adv7180_csi_write(state, 0xE0, 0x09);
465 adv7180_csi_write(state, 0x2C, 0x00);
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300466 if (state->field == V4L2_FIELD_NONE)
467 adv7180_csi_write(state, 0x1D, 0x80);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300468 adv7180_csi_write(state, 0x00, 0x00);
469 } else {
470 adv7180_csi_write(state, 0x00, 0x80);
471 }
472 }
473
474 return 0;
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300475}
476
477static int adv7180_s_power(struct v4l2_subdev *sd, int on)
478{
479 struct adv7180_state *state = to_state(sd);
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300480 int ret;
481
482 ret = mutex_lock_interruptible(&state->mutex);
483 if (ret)
484 return ret;
485
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300486 ret = adv7180_set_power(state, on);
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300487 if (ret == 0)
488 state->powered = on;
489
490 mutex_unlock(&state->mutex);
491 return ret;
492}
493
Federico Vagac9fbedd2012-07-11 11:29:33 -0300494static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
Federico Vagabca7ad12012-04-12 12:39:36 -0300495{
Federico Vagac9fbedd2012-07-11 11:29:33 -0300496 struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
Federico Vagabca7ad12012-04-12 12:39:36 -0300497 struct adv7180_state *state = to_state(sd);
Federico Vagabca7ad12012-04-12 12:39:36 -0300498 int ret = mutex_lock_interruptible(&state->mutex);
Federico Vagac9fbedd2012-07-11 11:29:33 -0300499 int val;
500
Federico Vagabca7ad12012-04-12 12:39:36 -0300501 if (ret)
502 return ret;
Federico Vagac9fbedd2012-07-11 11:29:33 -0300503 val = ctrl->val;
Federico Vagabca7ad12012-04-12 12:39:36 -0300504 switch (ctrl->id) {
505 case V4L2_CID_BRIGHTNESS:
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300506 ret = adv7180_write(state, ADV7180_REG_BRI, val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300507 break;
508 case V4L2_CID_HUE:
Federico Vagabca7ad12012-04-12 12:39:36 -0300509 /*Hue is inverted according to HSL chart */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300510 ret = adv7180_write(state, ADV7180_REG_HUE, -val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300511 break;
512 case V4L2_CID_CONTRAST:
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300513 ret = adv7180_write(state, ADV7180_REG_CON, val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300514 break;
515 case V4L2_CID_SATURATION:
Federico Vagabca7ad12012-04-12 12:39:36 -0300516 /*
517 *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
518 *Let's not confuse the user, everybody understands saturation
519 */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300520 ret = adv7180_write(state, ADV7180_REG_SD_SAT_CB, val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300521 if (ret < 0)
522 break;
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300523 ret = adv7180_write(state, ADV7180_REG_SD_SAT_CR, val);
Federico Vagabca7ad12012-04-12 12:39:36 -0300524 break;
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300525 case V4L2_CID_ADV_FAST_SWITCH:
526 if (ctrl->val) {
527 /* ADI required write */
528 adv7180_write(state, 0x80d9, 0x44);
529 adv7180_write(state, ADV7180_REG_FLCONTROL,
530 ADV7180_FLCONTROL_FL_ENABLE);
531 } else {
532 /* ADI required write */
533 adv7180_write(state, 0x80d9, 0xc4);
534 adv7180_write(state, ADV7180_REG_FLCONTROL, 0x00);
535 }
536 break;
Federico Vagabca7ad12012-04-12 12:39:36 -0300537 default:
538 ret = -EINVAL;
539 }
540
541 mutex_unlock(&state->mutex);
542 return ret;
543}
544
Federico Vagac9fbedd2012-07-11 11:29:33 -0300545static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
546 .s_ctrl = adv7180_s_ctrl,
547};
548
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300549static const struct v4l2_ctrl_config adv7180_ctrl_fast_switch = {
550 .ops = &adv7180_ctrl_ops,
551 .id = V4L2_CID_ADV_FAST_SWITCH,
552 .name = "Fast Switching",
553 .type = V4L2_CTRL_TYPE_BOOLEAN,
554 .min = 0,
555 .max = 1,
556 .step = 1,
557};
558
Federico Vagac9fbedd2012-07-11 11:29:33 -0300559static int adv7180_init_controls(struct adv7180_state *state)
560{
561 v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
562
563 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
564 V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
565 ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
566 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
567 V4L2_CID_CONTRAST, ADV7180_CON_MIN,
568 ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
569 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
570 V4L2_CID_SATURATION, ADV7180_SAT_MIN,
571 ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
572 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
573 V4L2_CID_HUE, ADV7180_HUE_MIN,
574 ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
Lars-Peter Clausen08b717c2015-01-23 12:52:33 -0300575 v4l2_ctrl_new_custom(&state->ctrl_hdl, &adv7180_ctrl_fast_switch, NULL);
576
Federico Vagac9fbedd2012-07-11 11:29:33 -0300577 state->sd.ctrl_handler = &state->ctrl_hdl;
578 if (state->ctrl_hdl.error) {
579 int err = state->ctrl_hdl.error;
580
581 v4l2_ctrl_handler_free(&state->ctrl_hdl);
582 return err;
583 }
584 v4l2_ctrl_handler_setup(&state->ctrl_hdl);
585
586 return 0;
587}
588static void adv7180_exit_controls(struct adv7180_state *state)
589{
590 v4l2_ctrl_handler_free(&state->ctrl_hdl);
591}
592
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300593static int adv7180_enum_mbus_code(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -0800594 struct v4l2_subdev_pad_config *cfg,
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300595 struct v4l2_subdev_mbus_code_enum *code)
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300596{
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300597 if (code->index != 0)
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300598 return -EINVAL;
599
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300600 code->code = MEDIA_BUS_FMT_YUYV8_2X8;
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300601
602 return 0;
603}
604
605static int adv7180_mbus_fmt(struct v4l2_subdev *sd,
606 struct v4l2_mbus_framefmt *fmt)
607{
608 struct adv7180_state *state = to_state(sd);
609
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300610 fmt->code = MEDIA_BUS_FMT_YUYV8_2X8;
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300611 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300612 fmt->width = 720;
613 fmt->height = state->curr_norm & V4L2_STD_525_60 ? 480 : 576;
614
615 return 0;
616}
617
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300618static int adv7180_set_field_mode(struct adv7180_state *state)
619{
620 if (!(state->chip_info->flags & ADV7180_FLAG_I2P))
621 return 0;
622
623 if (state->field == V4L2_FIELD_NONE) {
624 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
625 adv7180_csi_write(state, 0x01, 0x20);
626 adv7180_csi_write(state, 0x02, 0x28);
627 adv7180_csi_write(state, 0x03, 0x38);
628 adv7180_csi_write(state, 0x04, 0x30);
629 adv7180_csi_write(state, 0x05, 0x30);
630 adv7180_csi_write(state, 0x06, 0x80);
631 adv7180_csi_write(state, 0x07, 0x70);
632 adv7180_csi_write(state, 0x08, 0x50);
633 }
634 adv7180_vpp_write(state, 0xa3, 0x00);
635 adv7180_vpp_write(state, 0x5b, 0x00);
636 adv7180_vpp_write(state, 0x55, 0x80);
637 } else {
638 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
639 adv7180_csi_write(state, 0x01, 0x18);
640 adv7180_csi_write(state, 0x02, 0x18);
641 adv7180_csi_write(state, 0x03, 0x30);
642 adv7180_csi_write(state, 0x04, 0x20);
643 adv7180_csi_write(state, 0x05, 0x28);
644 adv7180_csi_write(state, 0x06, 0x40);
645 adv7180_csi_write(state, 0x07, 0x58);
646 adv7180_csi_write(state, 0x08, 0x30);
647 }
648 adv7180_vpp_write(state, 0xa3, 0x70);
649 adv7180_vpp_write(state, 0x5b, 0x80);
650 adv7180_vpp_write(state, 0x55, 0x00);
651 }
652
653 return 0;
654}
655
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300656static int adv7180_get_pad_format(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -0800657 struct v4l2_subdev_pad_config *cfg,
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300658 struct v4l2_subdev_format *format)
659{
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300660 struct adv7180_state *state = to_state(sd);
661
662 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
Hans Verkuilf7234132015-03-04 01:47:54 -0800663 format->format = *v4l2_subdev_get_try_format(sd, cfg, 0);
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300664 } else {
665 adv7180_mbus_fmt(sd, &format->format);
666 format->format.field = state->field;
667 }
668
669 return 0;
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300670}
671
672static int adv7180_set_pad_format(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -0800673 struct v4l2_subdev_pad_config *cfg,
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300674 struct v4l2_subdev_format *format)
675{
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300676 struct adv7180_state *state = to_state(sd);
677 struct v4l2_mbus_framefmt *framefmt;
678
679 switch (format->format.field) {
680 case V4L2_FIELD_NONE:
681 if (!(state->chip_info->flags & ADV7180_FLAG_I2P))
682 format->format.field = V4L2_FIELD_INTERLACED;
683 break;
684 default:
685 format->format.field = V4L2_FIELD_INTERLACED;
686 break;
687 }
688
689 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
690 framefmt = &format->format;
691 if (state->field != format->format.field) {
692 state->field = format->format.field;
693 adv7180_set_power(state, false);
694 adv7180_set_field_mode(state);
695 adv7180_set_power(state, true);
696 }
697 } else {
Hans Verkuilf7234132015-03-04 01:47:54 -0800698 framefmt = v4l2_subdev_get_try_format(sd, cfg, 0);
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300699 *framefmt = format->format;
700 }
701
702 return adv7180_mbus_fmt(sd, framefmt);
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300703}
704
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300705static int adv7180_g_mbus_config(struct v4l2_subdev *sd,
706 struct v4l2_mbus_config *cfg)
707{
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300708 struct adv7180_state *state = to_state(sd);
709
710 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
711 cfg->type = V4L2_MBUS_CSI2;
712 cfg->flags = V4L2_MBUS_CSI2_1_LANE |
713 V4L2_MBUS_CSI2_CHANNEL_0 |
714 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
715 } else {
716 /*
717 * The ADV7180 sensor supports BT.601/656 output modes.
718 * The BT.656 is default and not yet configurable by s/w.
719 */
720 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
721 V4L2_MBUS_DATA_ACTIVE_HIGH;
722 cfg->type = V4L2_MBUS_BT656;
723 }
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300724
725 return 0;
726}
727
Niklas Söderlund64b3df92016-04-02 14:42:19 -0300728static int adv7180_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *cropcap)
729{
730 struct adv7180_state *state = to_state(sd);
731
732 if (state->curr_norm & V4L2_STD_525_60) {
733 cropcap->pixelaspect.numerator = 11;
734 cropcap->pixelaspect.denominator = 10;
735 } else {
736 cropcap->pixelaspect.numerator = 54;
737 cropcap->pixelaspect.denominator = 59;
738 }
739
740 return 0;
741}
742
Niklas Söderlundbae4c752016-04-02 14:42:20 -0300743static int adv7180_g_tvnorms(struct v4l2_subdev *sd, v4l2_std_id *norm)
744{
745 *norm = V4L2_STD_ALL;
746 return 0;
747}
748
Hans Verkuil937feee2016-04-22 10:03:37 -0300749static int adv7180_s_stream(struct v4l2_subdev *sd, int enable)
750{
751 struct adv7180_state *state = to_state(sd);
752 int ret;
753
754 /* It's always safe to stop streaming, no need to take the lock */
755 if (!enable) {
756 state->streaming = enable;
757 return 0;
758 }
759
760 /* Must wait until querystd released the lock */
761 ret = mutex_lock_interruptible(&state->mutex);
762 if (ret)
763 return ret;
764 state->streaming = enable;
765 mutex_unlock(&state->mutex);
766 return 0;
767}
768
769static int adv7180_subscribe_event(struct v4l2_subdev *sd,
770 struct v4l2_fh *fh,
771 struct v4l2_event_subscription *sub)
772{
773 switch (sub->type) {
774 case V4L2_EVENT_SOURCE_CHANGE:
775 return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
776 case V4L2_EVENT_CTRL:
777 return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
778 default:
779 return -EINVAL;
780 }
781}
782
Richard Röjfors6789cb52009-09-18 21:17:20 -0300783static const struct v4l2_subdev_video_ops adv7180_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300784 .s_std = adv7180_s_std,
Niklas Söderlundd0fadc82016-04-02 14:42:18 -0300785 .g_std = adv7180_g_std,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300786 .querystd = adv7180_querystd,
Richard Röjforsd3124292009-09-22 06:05:42 -0300787 .g_input_status = adv7180_g_input_status,
Federico Vagabca7ad12012-04-12 12:39:36 -0300788 .s_routing = adv7180_s_routing,
Vladimir Barinovcccb83f2013-05-29 14:50:57 -0300789 .g_mbus_config = adv7180_g_mbus_config,
Niklas Söderlund64b3df92016-04-02 14:42:19 -0300790 .cropcap = adv7180_cropcap,
Niklas Söderlundbae4c752016-04-02 14:42:20 -0300791 .g_tvnorms = adv7180_g_tvnorms,
Hans Verkuil937feee2016-04-22 10:03:37 -0300792 .s_stream = adv7180_s_stream,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300793};
794
795static const struct v4l2_subdev_core_ops adv7180_core_ops = {
Lars-Peter Clausene246c332014-03-10 14:05:39 -0300796 .s_power = adv7180_s_power,
Hans Verkuil937feee2016-04-22 10:03:37 -0300797 .subscribe_event = adv7180_subscribe_event,
798 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300799};
800
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300801static const struct v4l2_subdev_pad_ops adv7180_pad_ops = {
802 .enum_mbus_code = adv7180_enum_mbus_code,
803 .set_fmt = adv7180_set_pad_format,
804 .get_fmt = adv7180_get_pad_format,
805};
806
Richard Röjfors6789cb52009-09-18 21:17:20 -0300807static const struct v4l2_subdev_ops adv7180_ops = {
808 .core = &adv7180_core_ops,
809 .video = &adv7180_video_ops,
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -0300810 .pad = &adv7180_pad_ops,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300811};
812
Lars-Peter Clausen0c255342014-03-07 13:14:31 -0300813static irqreturn_t adv7180_irq(int irq, void *devid)
Richard Röjfors42752f72009-09-22 06:07:06 -0300814{
Lars-Peter Clausen0c255342014-03-07 13:14:31 -0300815 struct adv7180_state *state = devid;
Richard Röjfors42752f72009-09-22 06:07:06 -0300816 u8 isr3;
817
818 mutex_lock(&state->mutex);
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300819 isr3 = adv7180_read(state, ADV7180_REG_ISR3);
Richard Röjfors42752f72009-09-22 06:07:06 -0300820 /* clear */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -0300821 adv7180_write(state, ADV7180_REG_ICR3, isr3);
Richard Röjfors42752f72009-09-22 06:07:06 -0300822
Hans Verkuil937feee2016-04-22 10:03:37 -0300823 if (isr3 & ADV7180_IRQ3_AD_CHANGE) {
824 static const struct v4l2_event src_ch = {
825 .type = V4L2_EVENT_SOURCE_CHANGE,
826 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
827 };
828
829 v4l2_subdev_notify_event(&state->sd, &src_ch);
830 }
Richard Röjfors42752f72009-09-22 06:07:06 -0300831 mutex_unlock(&state->mutex);
832
Richard Röjfors42752f72009-09-22 06:07:06 -0300833 return IRQ_HANDLED;
834}
835
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -0300836static int adv7180_init(struct adv7180_state *state)
837{
838 int ret;
839
840 /* ITU-R BT.656-4 compatible */
841 ret = adv7180_write(state, ADV7180_REG_EXTENDED_OUTPUT_CONTROL,
842 ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
843 if (ret < 0)
844 return ret;
845
846 /* Manually set V bit end position in NTSC mode */
847 return adv7180_write(state, ADV7180_REG_NTSC_V_BIT_END,
848 ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
849}
850
851static int adv7180_set_std(struct adv7180_state *state, unsigned int std)
852{
853 return adv7180_write(state, ADV7180_REG_INPUT_CONTROL,
854 (std << 4) | state->input);
855}
856
857static int adv7180_select_input(struct adv7180_state *state, unsigned int input)
858{
859 int ret;
860
861 ret = adv7180_read(state, ADV7180_REG_INPUT_CONTROL);
862 if (ret < 0)
863 return ret;
864
865 ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
866 ret |= input;
867 return adv7180_write(state, ADV7180_REG_INPUT_CONTROL, ret);
868}
869
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300870static int adv7182_init(struct adv7180_state *state)
871{
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300872 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2)
873 adv7180_write(state, ADV7180_REG_CSI_SLAVE_ADDR,
874 ADV7180_DEFAULT_CSI_I2C_ADDR << 1);
875
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -0300876 if (state->chip_info->flags & ADV7180_FLAG_I2P)
877 adv7180_write(state, ADV7180_REG_VPP_SLAVE_ADDR,
878 ADV7180_DEFAULT_VPP_I2C_ADDR << 1);
879
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300880 if (state->chip_info->flags & ADV7180_FLAG_V2) {
881 /* ADI recommended writes for improved video quality */
882 adv7180_write(state, 0x0080, 0x51);
883 adv7180_write(state, 0x0081, 0x51);
884 adv7180_write(state, 0x0082, 0x68);
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300885 }
886
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300887 /* ADI required writes */
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -0300888 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
889 adv7180_write(state, 0x0003, 0x4e);
890 adv7180_write(state, 0x0004, 0x57);
891 adv7180_write(state, 0x001d, 0xc0);
892 } else {
893 if (state->chip_info->flags & ADV7180_FLAG_V2)
894 adv7180_write(state, 0x0004, 0x17);
895 else
896 adv7180_write(state, 0x0004, 0x07);
897 adv7180_write(state, 0x0003, 0x0c);
898 adv7180_write(state, 0x001d, 0x40);
899 }
900
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300901 adv7180_write(state, 0x0013, 0x00);
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300902
903 return 0;
904}
905
906static int adv7182_set_std(struct adv7180_state *state, unsigned int std)
907{
908 return adv7180_write(state, ADV7182_REG_INPUT_VIDSEL, std << 4);
909}
910
911enum adv7182_input_type {
912 ADV7182_INPUT_TYPE_CVBS,
913 ADV7182_INPUT_TYPE_DIFF_CVBS,
914 ADV7182_INPUT_TYPE_SVIDEO,
915 ADV7182_INPUT_TYPE_YPBPR,
916};
917
918static enum adv7182_input_type adv7182_get_input_type(unsigned int input)
919{
920 switch (input) {
921 case ADV7182_INPUT_CVBS_AIN1:
922 case ADV7182_INPUT_CVBS_AIN2:
923 case ADV7182_INPUT_CVBS_AIN3:
924 case ADV7182_INPUT_CVBS_AIN4:
925 case ADV7182_INPUT_CVBS_AIN5:
926 case ADV7182_INPUT_CVBS_AIN6:
927 case ADV7182_INPUT_CVBS_AIN7:
928 case ADV7182_INPUT_CVBS_AIN8:
929 return ADV7182_INPUT_TYPE_CVBS;
930 case ADV7182_INPUT_SVIDEO_AIN1_AIN2:
931 case ADV7182_INPUT_SVIDEO_AIN3_AIN4:
932 case ADV7182_INPUT_SVIDEO_AIN5_AIN6:
933 case ADV7182_INPUT_SVIDEO_AIN7_AIN8:
934 return ADV7182_INPUT_TYPE_SVIDEO;
935 case ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3:
936 case ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6:
937 return ADV7182_INPUT_TYPE_YPBPR;
938 case ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2:
939 case ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4:
940 case ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6:
941 case ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8:
942 return ADV7182_INPUT_TYPE_DIFF_CVBS;
943 default: /* Will never happen */
944 return 0;
945 }
946}
947
948/* ADI recommended writes to registers 0x52, 0x53, 0x54 */
949static unsigned int adv7182_lbias_settings[][3] = {
950 [ADV7182_INPUT_TYPE_CVBS] = { 0xCB, 0x4E, 0x80 },
951 [ADV7182_INPUT_TYPE_DIFF_CVBS] = { 0xC0, 0x4E, 0x80 },
952 [ADV7182_INPUT_TYPE_SVIDEO] = { 0x0B, 0xCE, 0x80 },
953 [ADV7182_INPUT_TYPE_YPBPR] = { 0x0B, 0x4E, 0xC0 },
954};
955
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300956static unsigned int adv7280_lbias_settings[][3] = {
957 [ADV7182_INPUT_TYPE_CVBS] = { 0xCD, 0x4E, 0x80 },
958 [ADV7182_INPUT_TYPE_DIFF_CVBS] = { 0xC0, 0x4E, 0x80 },
959 [ADV7182_INPUT_TYPE_SVIDEO] = { 0x0B, 0xCE, 0x80 },
960 [ADV7182_INPUT_TYPE_YPBPR] = { 0x0B, 0x4E, 0xC0 },
961};
962
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300963static int adv7182_select_input(struct adv7180_state *state, unsigned int input)
964{
965 enum adv7182_input_type input_type;
966 unsigned int *lbias;
967 unsigned int i;
968 int ret;
969
970 ret = adv7180_write(state, ADV7180_REG_INPUT_CONTROL, input);
971 if (ret)
972 return ret;
973
974 /* Reset clamp circuitry - ADI recommended writes */
975 adv7180_write(state, 0x809c, 0x00);
976 adv7180_write(state, 0x809c, 0xff);
977
978 input_type = adv7182_get_input_type(input);
979
980 switch (input_type) {
981 case ADV7182_INPUT_TYPE_CVBS:
982 case ADV7182_INPUT_TYPE_DIFF_CVBS:
983 /* ADI recommends to use the SH1 filter */
984 adv7180_write(state, 0x0017, 0x41);
985 break;
986 default:
987 adv7180_write(state, 0x0017, 0x01);
988 break;
989 }
990
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -0300991 if (state->chip_info->flags & ADV7180_FLAG_V2)
992 lbias = adv7280_lbias_settings[input_type];
993 else
994 lbias = adv7182_lbias_settings[input_type];
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -0300995
996 for (i = 0; i < ARRAY_SIZE(adv7182_lbias_settings[0]); i++)
997 adv7180_write(state, 0x0052 + i, lbias[i]);
998
999 if (input_type == ADV7182_INPUT_TYPE_DIFF_CVBS) {
1000 /* ADI required writes to make differential CVBS work */
1001 adv7180_write(state, 0x005f, 0xa8);
1002 adv7180_write(state, 0x005a, 0x90);
1003 adv7180_write(state, 0x0060, 0xb0);
1004 adv7180_write(state, 0x80b6, 0x08);
1005 adv7180_write(state, 0x80c0, 0xa0);
1006 } else {
1007 adv7180_write(state, 0x005f, 0xf0);
1008 adv7180_write(state, 0x005a, 0xd0);
1009 adv7180_write(state, 0x0060, 0x10);
1010 adv7180_write(state, 0x80b6, 0x9c);
1011 adv7180_write(state, 0x80c0, 0x00);
1012 }
1013
1014 return 0;
1015}
1016
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001017static const struct adv7180_chip_info adv7180_info = {
1018 .flags = ADV7180_FLAG_RESET_POWERED,
1019 /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
1020 * all inputs and let the card driver take care of validation
1021 */
1022 .valid_input_mask = BIT(ADV7180_INPUT_CVBS_AIN1) |
1023 BIT(ADV7180_INPUT_CVBS_AIN2) |
1024 BIT(ADV7180_INPUT_CVBS_AIN3) |
1025 BIT(ADV7180_INPUT_CVBS_AIN4) |
1026 BIT(ADV7180_INPUT_CVBS_AIN5) |
1027 BIT(ADV7180_INPUT_CVBS_AIN6) |
1028 BIT(ADV7180_INPUT_SVIDEO_AIN1_AIN2) |
1029 BIT(ADV7180_INPUT_SVIDEO_AIN3_AIN4) |
1030 BIT(ADV7180_INPUT_SVIDEO_AIN5_AIN6) |
1031 BIT(ADV7180_INPUT_YPRPB_AIN1_AIN2_AIN3) |
1032 BIT(ADV7180_INPUT_YPRPB_AIN4_AIN5_AIN6),
1033 .init = adv7180_init,
1034 .set_std = adv7180_set_std,
1035 .select_input = adv7180_select_input,
1036};
1037
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -03001038static const struct adv7180_chip_info adv7182_info = {
1039 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1040 BIT(ADV7182_INPUT_CVBS_AIN2) |
1041 BIT(ADV7182_INPUT_CVBS_AIN3) |
1042 BIT(ADV7182_INPUT_CVBS_AIN4) |
1043 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1044 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1045 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
1046 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1047 BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4),
1048 .init = adv7182_init,
1049 .set_std = adv7182_set_std,
1050 .select_input = adv7182_select_input,
1051};
1052
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001053static const struct adv7180_chip_info adv7280_info = {
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001054 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_I2P,
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001055 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1056 BIT(ADV7182_INPUT_CVBS_AIN2) |
1057 BIT(ADV7182_INPUT_CVBS_AIN3) |
1058 BIT(ADV7182_INPUT_CVBS_AIN4) |
1059 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1060 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1061 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3),
1062 .init = adv7182_init,
1063 .set_std = adv7182_set_std,
1064 .select_input = adv7182_select_input,
1065};
1066
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001067static const struct adv7180_chip_info adv7280_m_info = {
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001068 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2 | ADV7180_FLAG_I2P,
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001069 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1070 BIT(ADV7182_INPUT_CVBS_AIN2) |
1071 BIT(ADV7182_INPUT_CVBS_AIN3) |
1072 BIT(ADV7182_INPUT_CVBS_AIN4) |
1073 BIT(ADV7182_INPUT_CVBS_AIN5) |
1074 BIT(ADV7182_INPUT_CVBS_AIN6) |
1075 BIT(ADV7182_INPUT_CVBS_AIN7) |
1076 BIT(ADV7182_INPUT_CVBS_AIN8) |
1077 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1078 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1079 BIT(ADV7182_INPUT_SVIDEO_AIN5_AIN6) |
1080 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1081 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
1082 BIT(ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6),
1083 .init = adv7182_init,
1084 .set_std = adv7182_set_std,
1085 .select_input = adv7182_select_input,
1086};
1087
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001088static const struct adv7180_chip_info adv7281_info = {
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001089 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2,
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001090 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1091 BIT(ADV7182_INPUT_CVBS_AIN2) |
1092 BIT(ADV7182_INPUT_CVBS_AIN7) |
1093 BIT(ADV7182_INPUT_CVBS_AIN8) |
1094 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1095 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1096 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1097 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1098 .init = adv7182_init,
1099 .set_std = adv7182_set_std,
1100 .select_input = adv7182_select_input,
1101};
1102
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001103static const struct adv7180_chip_info adv7281_m_info = {
1104 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2,
1105 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1106 BIT(ADV7182_INPUT_CVBS_AIN2) |
1107 BIT(ADV7182_INPUT_CVBS_AIN3) |
1108 BIT(ADV7182_INPUT_CVBS_AIN4) |
1109 BIT(ADV7182_INPUT_CVBS_AIN7) |
1110 BIT(ADV7182_INPUT_CVBS_AIN8) |
1111 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1112 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1113 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1114 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
1115 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1116 BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4) |
1117 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1118 .init = adv7182_init,
1119 .set_std = adv7182_set_std,
1120 .select_input = adv7182_select_input,
1121};
1122
1123static const struct adv7180_chip_info adv7281_ma_info = {
1124 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2,
1125 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1126 BIT(ADV7182_INPUT_CVBS_AIN2) |
1127 BIT(ADV7182_INPUT_CVBS_AIN3) |
1128 BIT(ADV7182_INPUT_CVBS_AIN4) |
1129 BIT(ADV7182_INPUT_CVBS_AIN5) |
1130 BIT(ADV7182_INPUT_CVBS_AIN6) |
1131 BIT(ADV7182_INPUT_CVBS_AIN7) |
1132 BIT(ADV7182_INPUT_CVBS_AIN8) |
1133 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1134 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1135 BIT(ADV7182_INPUT_SVIDEO_AIN5_AIN6) |
1136 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1137 BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
1138 BIT(ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6) |
1139 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1140 BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4) |
1141 BIT(ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6) |
1142 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1143 .init = adv7182_init,
1144 .set_std = adv7182_set_std,
1145 .select_input = adv7182_select_input,
1146};
1147
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001148static const struct adv7180_chip_info adv7282_info = {
1149 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_I2P,
1150 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1151 BIT(ADV7182_INPUT_CVBS_AIN2) |
1152 BIT(ADV7182_INPUT_CVBS_AIN7) |
1153 BIT(ADV7182_INPUT_CVBS_AIN8) |
1154 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1155 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1156 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1157 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1158 .init = adv7182_init,
1159 .set_std = adv7182_set_std,
1160 .select_input = adv7182_select_input,
1161};
1162
1163static const struct adv7180_chip_info adv7282_m_info = {
1164 .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2 | ADV7180_FLAG_I2P,
1165 .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
1166 BIT(ADV7182_INPUT_CVBS_AIN2) |
1167 BIT(ADV7182_INPUT_CVBS_AIN3) |
1168 BIT(ADV7182_INPUT_CVBS_AIN4) |
1169 BIT(ADV7182_INPUT_CVBS_AIN7) |
1170 BIT(ADV7182_INPUT_CVBS_AIN8) |
1171 BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
1172 BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
1173 BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
1174 BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
1175 BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4) |
1176 BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
1177 .init = adv7182_init,
1178 .set_std = adv7182_set_std,
1179 .select_input = adv7182_select_input,
1180};
1181
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001182static int init_device(struct adv7180_state *state)
Federico Vagabca7ad12012-04-12 12:39:36 -03001183{
1184 int ret;
1185
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001186 mutex_lock(&state->mutex);
1187
Lars-Peter Clausenc18818e2015-01-23 12:52:25 -03001188 adv7180_write(state, ADV7180_REG_PWR_MAN, ADV7180_PWR_MAN_RES);
Ulrich Hecht16dfe722015-11-10 11:39:00 -02001189 usleep_range(5000, 10000);
Lars-Peter Clausenc18818e2015-01-23 12:52:25 -03001190
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001191 ret = state->chip_info->init(state);
Lars-Peter Clausen3e35e332015-01-23 12:52:27 -03001192 if (ret)
1193 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001194
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001195 ret = adv7180_program_std(state);
1196 if (ret)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001197 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001198
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001199 adv7180_set_field_mode(state);
1200
Federico Vagabca7ad12012-04-12 12:39:36 -03001201 /* register for interrupts */
1202 if (state->irq > 0) {
Federico Vagabca7ad12012-04-12 12:39:36 -03001203 /* config the Interrupt pin to be active low */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001204 ret = adv7180_write(state, ADV7180_REG_ICONF1,
Federico Vagabca7ad12012-04-12 12:39:36 -03001205 ADV7180_ICONF1_ACTIVE_LOW |
1206 ADV7180_ICONF1_PSYNC_ONLY);
1207 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001208 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001209
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001210 ret = adv7180_write(state, ADV7180_REG_IMR1, 0);
Federico Vagabca7ad12012-04-12 12:39:36 -03001211 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001212 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001213
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001214 ret = adv7180_write(state, ADV7180_REG_IMR2, 0);
Federico Vagabca7ad12012-04-12 12:39:36 -03001215 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001216 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001217
1218 /* enable AD change interrupts interrupts */
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001219 ret = adv7180_write(state, ADV7180_REG_IMR3,
Federico Vagabca7ad12012-04-12 12:39:36 -03001220 ADV7180_IRQ3_AD_CHANGE);
1221 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001222 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001223
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001224 ret = adv7180_write(state, ADV7180_REG_IMR4, 0);
Federico Vagabca7ad12012-04-12 12:39:36 -03001225 if (ret < 0)
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001226 goto out_unlock;
Federico Vagabca7ad12012-04-12 12:39:36 -03001227 }
1228
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001229out_unlock:
1230 mutex_unlock(&state->mutex);
Alexey Khoroshilovdf065b32014-03-14 18:04:03 -03001231
Alexey Khoroshilovdf065b32014-03-14 18:04:03 -03001232 return ret;
Federico Vagabca7ad12012-04-12 12:39:36 -03001233}
Richard Röjfors6789cb52009-09-18 21:17:20 -03001234
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001235static int adv7180_probe(struct i2c_client *client,
1236 const struct i2c_device_id *id)
Richard Röjfors6789cb52009-09-18 21:17:20 -03001237{
1238 struct adv7180_state *state;
1239 struct v4l2_subdev *sd;
1240 int ret;
1241
1242 /* Check if the adapter supports the needed features */
1243 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1244 return -EIO;
1245
1246 v4l_info(client, "chip found @ 0x%02x (%s)\n",
Federico Vagabca7ad12012-04-12 12:39:36 -03001247 client->addr, client->adapter->name);
Richard Röjfors6789cb52009-09-18 21:17:20 -03001248
Laurent Pinchartc02b2112013-05-02 08:29:43 -03001249 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Fabio Estevam7657e0642014-12-16 13:49:07 -03001250 if (state == NULL)
1251 return -ENOMEM;
Richard Röjfors42752f72009-09-22 06:07:06 -03001252
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001253 state->client = client;
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001254 state->field = V4L2_FIELD_INTERLACED;
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001255 state->chip_info = (struct adv7180_chip_info *)id->driver_data;
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001256
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001257 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
1258 state->csi_client = i2c_new_dummy(client->adapter,
1259 ADV7180_DEFAULT_CSI_I2C_ADDR);
1260 if (!state->csi_client)
1261 return -ENOMEM;
1262 }
1263
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001264 if (state->chip_info->flags & ADV7180_FLAG_I2P) {
1265 state->vpp_client = i2c_new_dummy(client->adapter,
1266 ADV7180_DEFAULT_VPP_I2C_ADDR);
1267 if (!state->vpp_client) {
1268 ret = -ENOMEM;
1269 goto err_unregister_csi_client;
1270 }
1271 }
1272
Richard Röjfors42752f72009-09-22 06:07:06 -03001273 state->irq = client->irq;
Richard Röjfors42752f72009-09-22 06:07:06 -03001274 mutex_init(&state->mutex);
Hans Verkuil937feee2016-04-22 10:03:37 -03001275 state->curr_norm = V4L2_STD_NTSC;
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001276 if (state->chip_info->flags & ADV7180_FLAG_RESET_POWERED)
1277 state->powered = true;
1278 else
1279 state->powered = false;
Federico Vagabca7ad12012-04-12 12:39:36 -03001280 state->input = 0;
Richard Röjfors6789cb52009-09-18 21:17:20 -03001281 sd = &state->sd;
1282 v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
Hans Verkuil937feee2016-04-22 10:03:37 -03001283 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
Richard Röjfors6789cb52009-09-18 21:17:20 -03001284
Federico Vagac9fbedd2012-07-11 11:29:33 -03001285 ret = adv7180_init_controls(state);
1286 if (ret)
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001287 goto err_unregister_vpp_client;
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001288
1289 state->pad.flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -02001290 sd->entity.flags |= MEDIA_ENT_F_ATV_DECODER;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001291 ret = media_entity_pads_init(&sd->entity, 1, &state->pad);
Federico Vagac9fbedd2012-07-11 11:29:33 -03001292 if (ret)
1293 goto err_free_ctrl;
Lars-Peter Clausenfa5b7942014-03-07 13:14:32 -03001294
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001295 ret = init_device(state);
1296 if (ret)
1297 goto err_media_entity_cleanup;
1298
Lars-Peter Clausenfa5721d2015-01-23 12:52:20 -03001299 if (state->irq) {
1300 ret = request_threaded_irq(client->irq, NULL, adv7180_irq,
Lars-Peter Clausenf3e991d2015-01-23 12:52:21 -03001301 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
1302 KBUILD_MODNAME, state);
Lars-Peter Clausenfa5721d2015-01-23 12:52:20 -03001303 if (ret)
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001304 goto err_media_entity_cleanup;
Lars-Peter Clausenfa5721d2015-01-23 12:52:20 -03001305 }
1306
Lars-Peter Clausenfa5b7942014-03-07 13:14:32 -03001307 ret = v4l2_async_register_subdev(sd);
1308 if (ret)
1309 goto err_free_irq;
1310
Richard Röjfors6789cb52009-09-18 21:17:20 -03001311 return 0;
Richard Röjfors42752f72009-09-22 06:07:06 -03001312
Lars-Peter Clausenfa5b7942014-03-07 13:14:32 -03001313err_free_irq:
1314 if (state->irq > 0)
1315 free_irq(client->irq, state);
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001316err_media_entity_cleanup:
1317 media_entity_cleanup(&sd->entity);
Federico Vagac9fbedd2012-07-11 11:29:33 -03001318err_free_ctrl:
1319 adv7180_exit_controls(state);
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001320err_unregister_vpp_client:
1321 if (state->chip_info->flags & ADV7180_FLAG_I2P)
1322 i2c_unregister_device(state->vpp_client);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001323err_unregister_csi_client:
1324 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2)
1325 i2c_unregister_device(state->csi_client);
Lars-Peter Clausen297a0ae2014-03-07 13:14:27 -03001326 mutex_destroy(&state->mutex);
Richard Röjfors42752f72009-09-22 06:07:06 -03001327 return ret;
Richard Röjfors6789cb52009-09-18 21:17:20 -03001328}
1329
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001330static int adv7180_remove(struct i2c_client *client)
Richard Röjfors6789cb52009-09-18 21:17:20 -03001331{
1332 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Richard Röjfors42752f72009-09-22 06:07:06 -03001333 struct adv7180_state *state = to_state(sd);
Richard Röjfors6789cb52009-09-18 21:17:20 -03001334
Lars-Peter Clausenfa5b7942014-03-07 13:14:32 -03001335 v4l2_async_unregister_subdev(sd);
1336
Lars-Peter Clausen0c255342014-03-07 13:14:31 -03001337 if (state->irq > 0)
Richard Röjfors42752f72009-09-22 06:07:06 -03001338 free_irq(client->irq, state);
Richard Röjfors42752f72009-09-22 06:07:06 -03001339
Lars-Peter Clausend5d51a82015-01-23 12:52:26 -03001340 media_entity_cleanup(&sd->entity);
Lars-Peter Clausenb13f4af2014-03-07 13:14:28 -03001341 adv7180_exit_controls(state);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001342
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001343 if (state->chip_info->flags & ADV7180_FLAG_I2P)
1344 i2c_unregister_device(state->vpp_client);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001345 if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2)
1346 i2c_unregister_device(state->csi_client);
1347
Lars-Peter Clausen297a0ae2014-03-07 13:14:27 -03001348 mutex_destroy(&state->mutex);
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001349
Richard Röjfors6789cb52009-09-18 21:17:20 -03001350 return 0;
1351}
1352
1353static const struct i2c_device_id adv7180_id[] = {
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001354 { "adv7180", (kernel_ulong_t)&adv7180_info },
Lars-Peter Clausenc5ef8f82015-01-23 12:52:29 -03001355 { "adv7182", (kernel_ulong_t)&adv7182_info },
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001356 { "adv7280", (kernel_ulong_t)&adv7280_info },
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001357 { "adv7280-m", (kernel_ulong_t)&adv7280_m_info },
Lars-Peter Clausenbf7dcb82015-01-23 12:52:30 -03001358 { "adv7281", (kernel_ulong_t)&adv7281_info },
Lars-Peter Clausenb37135e2015-01-23 12:52:31 -03001359 { "adv7281-m", (kernel_ulong_t)&adv7281_m_info },
1360 { "adv7281-ma", (kernel_ulong_t)&adv7281_ma_info },
Lars-Peter Clausen851a54e2015-01-23 12:52:32 -03001361 { "adv7282", (kernel_ulong_t)&adv7282_info },
1362 { "adv7282-m", (kernel_ulong_t)&adv7282_m_info },
Richard Röjfors6789cb52009-09-18 21:17:20 -03001363 {},
1364};
Lars-Peter Clausenf5dde492015-01-23 12:52:28 -03001365MODULE_DEVICE_TABLE(i2c, adv7180_id);
Richard Röjfors6789cb52009-09-18 21:17:20 -03001366
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001367#ifdef CONFIG_PM_SLEEP
1368static int adv7180_suspend(struct device *dev)
Federico Vagabca7ad12012-04-12 12:39:36 -03001369{
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001370 struct i2c_client *client = to_i2c_client(dev);
Lars-Peter Clausene246c332014-03-10 14:05:39 -03001371 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1372 struct adv7180_state *state = to_state(sd);
Federico Vagabca7ad12012-04-12 12:39:36 -03001373
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001374 return adv7180_set_power(state, false);
Federico Vagabca7ad12012-04-12 12:39:36 -03001375}
1376
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001377static int adv7180_resume(struct device *dev)
Federico Vagabca7ad12012-04-12 12:39:36 -03001378{
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001379 struct i2c_client *client = to_i2c_client(dev);
Federico Vagabca7ad12012-04-12 12:39:36 -03001380 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1381 struct adv7180_state *state = to_state(sd);
1382 int ret;
1383
Lars-Peter Clausen3999e5d2015-01-23 12:52:24 -03001384 ret = init_device(state);
Federico Vagabca7ad12012-04-12 12:39:36 -03001385 if (ret < 0)
1386 return ret;
Lars-Peter Clausenc18818e2015-01-23 12:52:25 -03001387
1388 ret = adv7180_set_power(state, state->powered);
1389 if (ret)
1390 return ret;
1391
Federico Vagabca7ad12012-04-12 12:39:36 -03001392 return 0;
1393}
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001394
1395static SIMPLE_DEV_PM_OPS(adv7180_pm_ops, adv7180_suspend, adv7180_resume);
1396#define ADV7180_PM_OPS (&adv7180_pm_ops)
1397
1398#else
1399#define ADV7180_PM_OPS NULL
Federico Vagabca7ad12012-04-12 12:39:36 -03001400#endif
1401
Ben Dooks250121d2015-06-03 10:59:50 -03001402#ifdef CONFIG_OF
1403static const struct of_device_id adv7180_of_id[] = {
1404 { .compatible = "adi,adv7180", },
Julian Scheelbf14e742016-02-23 18:11:21 -03001405 { .compatible = "adi,adv7182", },
1406 { .compatible = "adi,adv7280", },
1407 { .compatible = "adi,adv7280-m", },
1408 { .compatible = "adi,adv7281", },
1409 { .compatible = "adi,adv7281-m", },
1410 { .compatible = "adi,adv7281-ma", },
1411 { .compatible = "adi,adv7282", },
1412 { .compatible = "adi,adv7282-m", },
Ben Dooks250121d2015-06-03 10:59:50 -03001413 { },
1414};
1415
1416MODULE_DEVICE_TABLE(of, adv7180_of_id);
1417#endif
1418
Richard Röjfors6789cb52009-09-18 21:17:20 -03001419static struct i2c_driver adv7180_driver = {
1420 .driver = {
Federico Vagac9fbedd2012-07-11 11:29:33 -03001421 .name = KBUILD_MODNAME,
Lars-Peter Clausencc1088d2013-04-13 05:25:59 -03001422 .pm = ADV7180_PM_OPS,
Ben Dooks250121d2015-06-03 10:59:50 -03001423 .of_match_table = of_match_ptr(adv7180_of_id),
Federico Vagabca7ad12012-04-12 12:39:36 -03001424 },
1425 .probe = adv7180_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001426 .remove = adv7180_remove,
Federico Vagabca7ad12012-04-12 12:39:36 -03001427 .id_table = adv7180_id,
Richard Röjfors6789cb52009-09-18 21:17:20 -03001428};
1429
Axel Linc6e8d862012-02-12 06:56:32 -03001430module_i2c_driver(adv7180_driver);
Richard Röjfors6789cb52009-09-18 21:17:20 -03001431
1432MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
1433MODULE_AUTHOR("Mocean Laboratories");
1434MODULE_LICENSE("GPL v2");