Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 1 | /* |
| 2 | * adv7180.c Analog Devices ADV7180 video decoder driver |
| 3 | * Copyright (c) 2009 Intel Corporation |
Vladimir Barinov | cccb83f | 2013-05-29 14:50:57 -0300 | [diff] [blame] | 4 | * Copyright (C) 2013 Cogent Embedded, Inc. |
| 5 | * Copyright (C) 2013 Renesas Solutions Corp. |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 6 | * |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 28 | #include <media/v4l2-ioctl.h> |
| 29 | #include <linux/videodev2.h> |
| 30 | #include <media/v4l2-device.h> |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 31 | #include <media/v4l2-ctrls.h> |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 33 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 34 | #define ADV7180_REG_INPUT_CONTROL 0x00 |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 35 | #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00 |
| 36 | #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10 |
| 37 | #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20 |
| 38 | #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30 |
| 39 | #define ADV7180_INPUT_CONTROL_NTSC_J 0x40 |
| 40 | #define ADV7180_INPUT_CONTROL_NTSC_M 0x50 |
| 41 | #define ADV7180_INPUT_CONTROL_PAL60 0x60 |
| 42 | #define ADV7180_INPUT_CONTROL_NTSC_443 0x70 |
| 43 | #define ADV7180_INPUT_CONTROL_PAL_BG 0x80 |
| 44 | #define ADV7180_INPUT_CONTROL_PAL_N 0x90 |
| 45 | #define ADV7180_INPUT_CONTROL_PAL_M 0xa0 |
| 46 | #define ADV7180_INPUT_CONTROL_PAL_M_PED 0xb0 |
| 47 | #define ADV7180_INPUT_CONTROL_PAL_COMB_N 0xc0 |
| 48 | #define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED 0xd0 |
| 49 | #define ADV7180_INPUT_CONTROL_PAL_SECAM 0xe0 |
| 50 | #define ADV7180_INPUT_CONTROL_PAL_SECAM_PED 0xf0 |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 51 | #define ADV7180_INPUT_CONTROL_INSEL_MASK 0x0f |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 52 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 53 | #define ADV7180_REG_EXTENDED_OUTPUT_CONTROL 0x04 |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 54 | #define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5 |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 55 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 56 | #define ADV7180_REG_AUTODETECT_ENABLE 0x07 |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 57 | #define ADV7180_AUTODETECT_DEFAULT 0x7f |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 58 | /* Contrast */ |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 59 | #define ADV7180_REG_CON 0x08 /*Unsigned */ |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 60 | #define ADV7180_CON_MIN 0 |
| 61 | #define ADV7180_CON_DEF 128 |
| 62 | #define ADV7180_CON_MAX 255 |
| 63 | /* Brightness*/ |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 64 | #define ADV7180_REG_BRI 0x0a /*Signed */ |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 65 | #define ADV7180_BRI_MIN -128 |
| 66 | #define ADV7180_BRI_DEF 0 |
| 67 | #define ADV7180_BRI_MAX 127 |
| 68 | /* Hue */ |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 69 | #define ADV7180_REG_HUE 0x0b /*Signed, inverted */ |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 70 | #define ADV7180_HUE_MIN -127 |
| 71 | #define ADV7180_HUE_DEF 0 |
| 72 | #define ADV7180_HUE_MAX 128 |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 73 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 74 | #define ADV7180_REG_CTRL 0x0e |
| 75 | #define ADV7180_CTRL_IRQ_SPACE 0x20 |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 76 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 77 | #define ADV7180_REG_PWR_MAN 0x0f |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 78 | #define ADV7180_PWR_MAN_ON 0x04 |
| 79 | #define ADV7180_PWR_MAN_OFF 0x24 |
| 80 | #define ADV7180_PWR_MAN_RES 0x80 |
| 81 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 82 | #define ADV7180_REG_STATUS1 0x10 |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 83 | #define ADV7180_STATUS1_IN_LOCK 0x01 |
| 84 | #define ADV7180_STATUS1_AUTOD_MASK 0x70 |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 85 | #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00 |
| 86 | #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10 |
| 87 | #define ADV7180_STATUS1_AUTOD_PAL_M 0x20 |
| 88 | #define ADV7180_STATUS1_AUTOD_PAL_60 0x30 |
| 89 | #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40 |
| 90 | #define ADV7180_STATUS1_AUTOD_SECAM 0x50 |
| 91 | #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60 |
| 92 | #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70 |
| 93 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 94 | #define ADV7180_REG_IDENT 0x11 |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 95 | #define ADV7180_ID_7180 0x18 |
| 96 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 97 | #define ADV7180_REG_ICONF1 0x40 |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 98 | #define ADV7180_ICONF1_ACTIVE_LOW 0x01 |
| 99 | #define ADV7180_ICONF1_PSYNC_ONLY 0x10 |
| 100 | #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0 |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 101 | /* Saturation */ |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 102 | #define ADV7180_REG_SD_SAT_CB 0xe3 /*Unsigned */ |
| 103 | #define ADV7180_REG_SD_SAT_CR 0xe4 /*Unsigned */ |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 104 | #define ADV7180_SAT_MIN 0 |
| 105 | #define ADV7180_SAT_DEF 128 |
| 106 | #define ADV7180_SAT_MAX 255 |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 107 | |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 108 | #define ADV7180_IRQ1_LOCK 0x01 |
| 109 | #define ADV7180_IRQ1_UNLOCK 0x02 |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 110 | #define ADV7180_REG_ISR1 0x42 |
| 111 | #define ADV7180_REG_ICR1 0x43 |
| 112 | #define ADV7180_REG_IMR1 0x44 |
| 113 | #define ADV7180_REG_IMR2 0x48 |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 114 | #define ADV7180_IRQ3_AD_CHANGE 0x08 |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 115 | #define ADV7180_REG_ISR3 0x4A |
| 116 | #define ADV7180_REG_ICR3 0x4B |
| 117 | #define ADV7180_REG_IMR3 0x4C |
| 118 | #define ADV7180_REG_IMR4 0x50 |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 119 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 120 | #define ADV7180_REG_NTSC_V_BIT_END 0xE6 |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 121 | #define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F |
| 122 | |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 123 | struct adv7180_state { |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 124 | struct v4l2_ctrl_handler ctrl_hdl; |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 125 | struct v4l2_subdev sd; |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 126 | struct mutex mutex; /* mutual excl. when accessing chip */ |
| 127 | int irq; |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 128 | v4l2_std_id curr_norm; |
| 129 | bool autodetect; |
Lars-Peter Clausen | e246c33 | 2014-03-10 14:05:39 -0300 | [diff] [blame] | 130 | bool powered; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 131 | u8 input; |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 132 | }; |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 133 | #define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler, \ |
| 134 | struct adv7180_state, \ |
| 135 | ctrl_hdl)->sd) |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 136 | |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 137 | static v4l2_std_id adv7180_std_to_v4l2(u8 status1) |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 138 | { |
Vladimir Barinov | b294a19 | 2013-04-11 18:06:46 -0300 | [diff] [blame] | 139 | /* in case V4L2_IN_ST_NO_SIGNAL */ |
| 140 | if (!(status1 & ADV7180_STATUS1_IN_LOCK)) |
| 141 | return V4L2_STD_UNKNOWN; |
| 142 | |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 143 | switch (status1 & ADV7180_STATUS1_AUTOD_MASK) { |
| 144 | case ADV7180_STATUS1_AUTOD_NTSM_M_J: |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 145 | return V4L2_STD_NTSC; |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 146 | case ADV7180_STATUS1_AUTOD_NTSC_4_43: |
| 147 | return V4L2_STD_NTSC_443; |
| 148 | case ADV7180_STATUS1_AUTOD_PAL_M: |
| 149 | return V4L2_STD_PAL_M; |
| 150 | case ADV7180_STATUS1_AUTOD_PAL_60: |
| 151 | return V4L2_STD_PAL_60; |
| 152 | case ADV7180_STATUS1_AUTOD_PAL_B_G: |
| 153 | return V4L2_STD_PAL; |
| 154 | case ADV7180_STATUS1_AUTOD_SECAM: |
| 155 | return V4L2_STD_SECAM; |
| 156 | case ADV7180_STATUS1_AUTOD_PAL_COMB: |
| 157 | return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N; |
| 158 | case ADV7180_STATUS1_AUTOD_SECAM_525: |
| 159 | return V4L2_STD_SECAM; |
| 160 | default: |
| 161 | return V4L2_STD_UNKNOWN; |
| 162 | } |
| 163 | } |
| 164 | |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 165 | static int v4l2_std_to_adv7180(v4l2_std_id std) |
| 166 | { |
| 167 | if (std == V4L2_STD_PAL_60) |
| 168 | return ADV7180_INPUT_CONTROL_PAL60; |
| 169 | if (std == V4L2_STD_NTSC_443) |
| 170 | return ADV7180_INPUT_CONTROL_NTSC_443; |
| 171 | if (std == V4L2_STD_PAL_N) |
| 172 | return ADV7180_INPUT_CONTROL_PAL_N; |
| 173 | if (std == V4L2_STD_PAL_M) |
| 174 | return ADV7180_INPUT_CONTROL_PAL_M; |
| 175 | if (std == V4L2_STD_PAL_Nc) |
| 176 | return ADV7180_INPUT_CONTROL_PAL_COMB_N; |
| 177 | |
| 178 | if (std & V4L2_STD_PAL) |
| 179 | return ADV7180_INPUT_CONTROL_PAL_BG; |
| 180 | if (std & V4L2_STD_NTSC) |
| 181 | return ADV7180_INPUT_CONTROL_NTSC_M; |
| 182 | if (std & V4L2_STD_SECAM) |
| 183 | return ADV7180_INPUT_CONTROL_PAL_SECAM; |
| 184 | |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 188 | static u32 adv7180_status_to_v4l2(u8 status1) |
| 189 | { |
| 190 | if (!(status1 & ADV7180_STATUS1_IN_LOCK)) |
| 191 | return V4L2_IN_ST_NO_SIGNAL; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int __adv7180_status(struct i2c_client *client, u32 *status, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 197 | v4l2_std_id *std) |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 198 | { |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 199 | int status1 = i2c_smbus_read_byte_data(client, ADV7180_REG_STATUS1); |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 200 | |
| 201 | if (status1 < 0) |
| 202 | return status1; |
| 203 | |
| 204 | if (status) |
| 205 | *status = adv7180_status_to_v4l2(status1); |
| 206 | if (std) |
| 207 | *std = adv7180_std_to_v4l2(status1); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 212 | static inline struct adv7180_state *to_state(struct v4l2_subdev *sd) |
| 213 | { |
| 214 | return container_of(sd, struct adv7180_state, sd); |
| 215 | } |
| 216 | |
| 217 | static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std) |
| 218 | { |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 219 | struct adv7180_state *state = to_state(sd); |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 220 | int err = mutex_lock_interruptible(&state->mutex); |
| 221 | if (err) |
| 222 | return err; |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 223 | |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 224 | /* when we are interrupt driven we know the state */ |
| 225 | if (!state->autodetect || state->irq > 0) |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 226 | *std = state->curr_norm; |
| 227 | else |
| 228 | err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std); |
| 229 | |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 230 | mutex_unlock(&state->mutex); |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 231 | return err; |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 232 | } |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 233 | |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 234 | static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input, |
| 235 | u32 output, u32 config) |
| 236 | { |
| 237 | struct adv7180_state *state = to_state(sd); |
| 238 | int ret = mutex_lock_interruptible(&state->mutex); |
| 239 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 240 | |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 244 | /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 245 | * all inputs and let the card driver take care of validation |
| 246 | */ |
| 247 | if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input) |
| 248 | goto out; |
| 249 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 250 | ret = i2c_smbus_read_byte_data(client, ADV7180_REG_INPUT_CONTROL); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 251 | if (ret < 0) |
| 252 | goto out; |
| 253 | |
| 254 | ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK; |
| 255 | ret = i2c_smbus_write_byte_data(client, |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 256 | ADV7180_REG_INPUT_CONTROL, ret | input); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 257 | state->input = input; |
| 258 | out: |
| 259 | mutex_unlock(&state->mutex); |
| 260 | return ret; |
| 261 | } |
| 262 | |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 263 | static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status) |
| 264 | { |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 265 | struct adv7180_state *state = to_state(sd); |
| 266 | int ret = mutex_lock_interruptible(&state->mutex); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | |
| 270 | ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL); |
| 271 | mutex_unlock(&state->mutex); |
| 272 | return ret; |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 273 | } |
| 274 | |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 275 | static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std) |
| 276 | { |
| 277 | struct adv7180_state *state = to_state(sd); |
| 278 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 279 | int ret = mutex_lock_interruptible(&state->mutex); |
| 280 | if (ret) |
| 281 | return ret; |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 282 | |
| 283 | /* all standards -> autodetect */ |
| 284 | if (std == V4L2_STD_ALL) { |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 285 | ret = |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 286 | i2c_smbus_write_byte_data(client, ADV7180_REG_INPUT_CONTROL, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 287 | ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM |
| 288 | | state->input); |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 289 | if (ret < 0) |
| 290 | goto out; |
| 291 | |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 292 | __adv7180_status(client, NULL, &state->curr_norm); |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 293 | state->autodetect = true; |
| 294 | } else { |
| 295 | ret = v4l2_std_to_adv7180(std); |
| 296 | if (ret < 0) |
| 297 | goto out; |
| 298 | |
| 299 | ret = i2c_smbus_write_byte_data(client, |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 300 | ADV7180_REG_INPUT_CONTROL, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 301 | ret | state->input); |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 302 | if (ret < 0) |
| 303 | goto out; |
| 304 | |
| 305 | state->curr_norm = std; |
| 306 | state->autodetect = false; |
| 307 | } |
| 308 | ret = 0; |
| 309 | out: |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 310 | mutex_unlock(&state->mutex); |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 311 | return ret; |
| 312 | } |
| 313 | |
Lars-Peter Clausen | e246c33 | 2014-03-10 14:05:39 -0300 | [diff] [blame] | 314 | static int adv7180_set_power(struct adv7180_state *state, |
| 315 | struct i2c_client *client, bool on) |
| 316 | { |
| 317 | u8 val; |
| 318 | |
| 319 | if (on) |
| 320 | val = ADV7180_PWR_MAN_ON; |
| 321 | else |
| 322 | val = ADV7180_PWR_MAN_OFF; |
| 323 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 324 | return i2c_smbus_write_byte_data(client, ADV7180_REG_PWR_MAN, val); |
Lars-Peter Clausen | e246c33 | 2014-03-10 14:05:39 -0300 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static int adv7180_s_power(struct v4l2_subdev *sd, int on) |
| 328 | { |
| 329 | struct adv7180_state *state = to_state(sd); |
| 330 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 331 | int ret; |
| 332 | |
| 333 | ret = mutex_lock_interruptible(&state->mutex); |
| 334 | if (ret) |
| 335 | return ret; |
| 336 | |
| 337 | ret = adv7180_set_power(state, client, on); |
| 338 | if (ret == 0) |
| 339 | state->powered = on; |
| 340 | |
| 341 | mutex_unlock(&state->mutex); |
| 342 | return ret; |
| 343 | } |
| 344 | |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 345 | static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl) |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 346 | { |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 347 | struct v4l2_subdev *sd = to_adv7180_sd(ctrl); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 348 | struct adv7180_state *state = to_state(sd); |
| 349 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 350 | int ret = mutex_lock_interruptible(&state->mutex); |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 351 | int val; |
| 352 | |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 353 | if (ret) |
| 354 | return ret; |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 355 | val = ctrl->val; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 356 | switch (ctrl->id) { |
| 357 | case V4L2_CID_BRIGHTNESS: |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 358 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_BRI, val); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 359 | break; |
| 360 | case V4L2_CID_HUE: |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 361 | /*Hue is inverted according to HSL chart */ |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 362 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_HUE, -val); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 363 | break; |
| 364 | case V4L2_CID_CONTRAST: |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 365 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_CON, val); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 366 | break; |
| 367 | case V4L2_CID_SATURATION: |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 368 | /* |
| 369 | *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE |
| 370 | *Let's not confuse the user, everybody understands saturation |
| 371 | */ |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 372 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_SD_SAT_CB, |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 373 | val); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 374 | if (ret < 0) |
| 375 | break; |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 376 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_SD_SAT_CR, |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 377 | val); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 378 | break; |
| 379 | default: |
| 380 | ret = -EINVAL; |
| 381 | } |
| 382 | |
| 383 | mutex_unlock(&state->mutex); |
| 384 | return ret; |
| 385 | } |
| 386 | |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 387 | static const struct v4l2_ctrl_ops adv7180_ctrl_ops = { |
| 388 | .s_ctrl = adv7180_s_ctrl, |
| 389 | }; |
| 390 | |
| 391 | static int adv7180_init_controls(struct adv7180_state *state) |
| 392 | { |
| 393 | v4l2_ctrl_handler_init(&state->ctrl_hdl, 4); |
| 394 | |
| 395 | v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops, |
| 396 | V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN, |
| 397 | ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF); |
| 398 | v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops, |
| 399 | V4L2_CID_CONTRAST, ADV7180_CON_MIN, |
| 400 | ADV7180_CON_MAX, 1, ADV7180_CON_DEF); |
| 401 | v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops, |
| 402 | V4L2_CID_SATURATION, ADV7180_SAT_MIN, |
| 403 | ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF); |
| 404 | v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops, |
| 405 | V4L2_CID_HUE, ADV7180_HUE_MIN, |
| 406 | ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF); |
| 407 | state->sd.ctrl_handler = &state->ctrl_hdl; |
| 408 | if (state->ctrl_hdl.error) { |
| 409 | int err = state->ctrl_hdl.error; |
| 410 | |
| 411 | v4l2_ctrl_handler_free(&state->ctrl_hdl); |
| 412 | return err; |
| 413 | } |
| 414 | v4l2_ctrl_handler_setup(&state->ctrl_hdl); |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | static void adv7180_exit_controls(struct adv7180_state *state) |
| 419 | { |
| 420 | v4l2_ctrl_handler_free(&state->ctrl_hdl); |
| 421 | } |
| 422 | |
Vladimir Barinov | cccb83f | 2013-05-29 14:50:57 -0300 | [diff] [blame] | 423 | static int adv7180_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index, |
Boris BREZILLON | f5fe58f | 2014-11-10 14:28:29 -0300 | [diff] [blame] | 424 | u32 *code) |
Vladimir Barinov | cccb83f | 2013-05-29 14:50:57 -0300 | [diff] [blame] | 425 | { |
| 426 | if (index > 0) |
| 427 | return -EINVAL; |
| 428 | |
Boris BREZILLON | f5fe58f | 2014-11-10 14:28:29 -0300 | [diff] [blame] | 429 | *code = MEDIA_BUS_FMT_YUYV8_2X8; |
Vladimir Barinov | cccb83f | 2013-05-29 14:50:57 -0300 | [diff] [blame] | 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | static int adv7180_mbus_fmt(struct v4l2_subdev *sd, |
| 435 | struct v4l2_mbus_framefmt *fmt) |
| 436 | { |
| 437 | struct adv7180_state *state = to_state(sd); |
| 438 | |
Boris BREZILLON | f5fe58f | 2014-11-10 14:28:29 -0300 | [diff] [blame] | 439 | fmt->code = MEDIA_BUS_FMT_YUYV8_2X8; |
Vladimir Barinov | cccb83f | 2013-05-29 14:50:57 -0300 | [diff] [blame] | 440 | fmt->colorspace = V4L2_COLORSPACE_SMPTE170M; |
| 441 | fmt->field = V4L2_FIELD_INTERLACED; |
| 442 | fmt->width = 720; |
| 443 | fmt->height = state->curr_norm & V4L2_STD_525_60 ? 480 : 576; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static int adv7180_g_mbus_config(struct v4l2_subdev *sd, |
| 449 | struct v4l2_mbus_config *cfg) |
| 450 | { |
| 451 | /* |
| 452 | * The ADV7180 sensor supports BT.601/656 output modes. |
| 453 | * The BT.656 is default and not yet configurable by s/w. |
| 454 | */ |
| 455 | cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING | |
| 456 | V4L2_MBUS_DATA_ACTIVE_HIGH; |
| 457 | cfg->type = V4L2_MBUS_BT656; |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 462 | static const struct v4l2_subdev_video_ops adv7180_video_ops = { |
Laurent Pinchart | 8774bed | 2014-04-28 16:53:01 -0300 | [diff] [blame] | 463 | .s_std = adv7180_s_std, |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 464 | .querystd = adv7180_querystd, |
Richard Röjfors | d312429 | 2009-09-22 06:05:42 -0300 | [diff] [blame] | 465 | .g_input_status = adv7180_g_input_status, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 466 | .s_routing = adv7180_s_routing, |
Vladimir Barinov | cccb83f | 2013-05-29 14:50:57 -0300 | [diff] [blame] | 467 | .enum_mbus_fmt = adv7180_enum_mbus_fmt, |
| 468 | .try_mbus_fmt = adv7180_mbus_fmt, |
| 469 | .g_mbus_fmt = adv7180_mbus_fmt, |
| 470 | .s_mbus_fmt = adv7180_mbus_fmt, |
| 471 | .g_mbus_config = adv7180_g_mbus_config, |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | static const struct v4l2_subdev_core_ops adv7180_core_ops = { |
Lars-Peter Clausen | e246c33 | 2014-03-10 14:05:39 -0300 | [diff] [blame] | 475 | .s_power = adv7180_s_power, |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | static const struct v4l2_subdev_ops adv7180_ops = { |
| 479 | .core = &adv7180_core_ops, |
| 480 | .video = &adv7180_video_ops, |
| 481 | }; |
| 482 | |
Lars-Peter Clausen | 0c25534 | 2014-03-07 13:14:31 -0300 | [diff] [blame] | 483 | static irqreturn_t adv7180_irq(int irq, void *devid) |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 484 | { |
Lars-Peter Clausen | 0c25534 | 2014-03-07 13:14:31 -0300 | [diff] [blame] | 485 | struct adv7180_state *state = devid; |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 486 | struct i2c_client *client = v4l2_get_subdevdata(&state->sd); |
| 487 | u8 isr3; |
| 488 | |
| 489 | mutex_lock(&state->mutex); |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 490 | i2c_smbus_write_byte_data(client, ADV7180_REG_CTRL, |
| 491 | ADV7180_CTRL_IRQ_SPACE); |
| 492 | isr3 = i2c_smbus_read_byte_data(client, ADV7180_REG_ISR3); |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 493 | /* clear */ |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 494 | i2c_smbus_write_byte_data(client, ADV7180_REG_ICR3, isr3); |
| 495 | i2c_smbus_write_byte_data(client, ADV7180_REG_CTRL, 0); |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 496 | |
| 497 | if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect) |
| 498 | __adv7180_status(client, NULL, &state->curr_norm); |
| 499 | mutex_unlock(&state->mutex); |
| 500 | |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 501 | return IRQ_HANDLED; |
| 502 | } |
| 503 | |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 504 | static int init_device(struct i2c_client *client, struct adv7180_state *state) |
| 505 | { |
| 506 | int ret; |
| 507 | |
| 508 | /* Initialize adv7180 */ |
| 509 | /* Enable autodetection */ |
| 510 | if (state->autodetect) { |
| 511 | ret = |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 512 | i2c_smbus_write_byte_data(client, ADV7180_REG_INPUT_CONTROL, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 513 | ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM |
| 514 | | state->input); |
| 515 | if (ret < 0) |
| 516 | return ret; |
| 517 | |
| 518 | ret = |
| 519 | i2c_smbus_write_byte_data(client, |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 520 | ADV7180_REG_AUTODETECT_ENABLE, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 521 | ADV7180_AUTODETECT_DEFAULT); |
| 522 | if (ret < 0) |
| 523 | return ret; |
| 524 | } else { |
| 525 | ret = v4l2_std_to_adv7180(state->curr_norm); |
| 526 | if (ret < 0) |
| 527 | return ret; |
| 528 | |
| 529 | ret = |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 530 | i2c_smbus_write_byte_data(client, ADV7180_REG_INPUT_CONTROL, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 531 | ret | state->input); |
| 532 | if (ret < 0) |
| 533 | return ret; |
| 534 | |
| 535 | } |
| 536 | /* ITU-R BT.656-4 compatible */ |
| 537 | ret = i2c_smbus_write_byte_data(client, |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 538 | ADV7180_REG_EXTENDED_OUTPUT_CONTROL, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 539 | ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS); |
| 540 | if (ret < 0) |
| 541 | return ret; |
| 542 | |
| 543 | /* Manually set V bit end position in NTSC mode */ |
| 544 | ret = i2c_smbus_write_byte_data(client, |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 545 | ADV7180_REG_NTSC_V_BIT_END, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 546 | ADV7180_NTSC_V_BIT_END_MANUAL_NVEND); |
| 547 | if (ret < 0) |
| 548 | return ret; |
| 549 | |
| 550 | /* read current norm */ |
| 551 | __adv7180_status(client, NULL, &state->curr_norm); |
| 552 | |
| 553 | /* register for interrupts */ |
| 554 | if (state->irq > 0) { |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 555 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_CTRL, |
| 556 | ADV7180_CTRL_IRQ_SPACE); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 557 | if (ret < 0) |
Alexey Khoroshilov | df065b3 | 2014-03-14 18:04:03 -0300 | [diff] [blame] | 558 | goto err; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 559 | |
| 560 | /* config the Interrupt pin to be active low */ |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 561 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_ICONF1, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 562 | ADV7180_ICONF1_ACTIVE_LOW | |
| 563 | ADV7180_ICONF1_PSYNC_ONLY); |
| 564 | if (ret < 0) |
Alexey Khoroshilov | df065b3 | 2014-03-14 18:04:03 -0300 | [diff] [blame] | 565 | goto err; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 566 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 567 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_IMR1, 0); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 568 | if (ret < 0) |
Alexey Khoroshilov | df065b3 | 2014-03-14 18:04:03 -0300 | [diff] [blame] | 569 | goto err; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 570 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 571 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_IMR2, 0); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 572 | if (ret < 0) |
Alexey Khoroshilov | df065b3 | 2014-03-14 18:04:03 -0300 | [diff] [blame] | 573 | goto err; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 574 | |
| 575 | /* enable AD change interrupts interrupts */ |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 576 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_IMR3, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 577 | ADV7180_IRQ3_AD_CHANGE); |
| 578 | if (ret < 0) |
Alexey Khoroshilov | df065b3 | 2014-03-14 18:04:03 -0300 | [diff] [blame] | 579 | goto err; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 580 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 581 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_IMR4, 0); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 582 | if (ret < 0) |
Alexey Khoroshilov | df065b3 | 2014-03-14 18:04:03 -0300 | [diff] [blame] | 583 | goto err; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 584 | |
Lars-Peter Clausen | 029d617 | 2015-01-23 12:52:23 -0300 | [diff] [blame^] | 585 | ret = i2c_smbus_write_byte_data(client, ADV7180_REG_CTRL, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 586 | 0); |
| 587 | if (ret < 0) |
Alexey Khoroshilov | df065b3 | 2014-03-14 18:04:03 -0300 | [diff] [blame] | 588 | goto err; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 589 | } |
| 590 | |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 591 | return 0; |
Alexey Khoroshilov | df065b3 | 2014-03-14 18:04:03 -0300 | [diff] [blame] | 592 | |
| 593 | err: |
Alexey Khoroshilov | df065b3 | 2014-03-14 18:04:03 -0300 | [diff] [blame] | 594 | return ret; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 595 | } |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 596 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 597 | static int adv7180_probe(struct i2c_client *client, |
| 598 | const struct i2c_device_id *id) |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 599 | { |
| 600 | struct adv7180_state *state; |
| 601 | struct v4l2_subdev *sd; |
| 602 | int ret; |
| 603 | |
| 604 | /* Check if the adapter supports the needed features */ |
| 605 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 606 | return -EIO; |
| 607 | |
| 608 | v4l_info(client, "chip found @ 0x%02x (%s)\n", |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 609 | client->addr, client->adapter->name); |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 610 | |
Laurent Pinchart | c02b211 | 2013-05-02 08:29:43 -0300 | [diff] [blame] | 611 | state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL); |
Fabio Estevam | 7657e06 | 2014-12-16 13:49:07 -0300 | [diff] [blame] | 612 | if (state == NULL) |
| 613 | return -ENOMEM; |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 614 | |
| 615 | state->irq = client->irq; |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 616 | mutex_init(&state->mutex); |
Richard Röjfors | c277b60 | 2009-09-22 06:06:34 -0300 | [diff] [blame] | 617 | state->autodetect = true; |
Lars-Peter Clausen | e246c33 | 2014-03-10 14:05:39 -0300 | [diff] [blame] | 618 | state->powered = true; |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 619 | state->input = 0; |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 620 | sd = &state->sd; |
| 621 | v4l2_i2c_subdev_init(sd, client, &adv7180_ops); |
| 622 | |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 623 | ret = adv7180_init_controls(state); |
| 624 | if (ret) |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 625 | goto err_unreg_subdev; |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 626 | ret = init_device(client, state); |
| 627 | if (ret) |
| 628 | goto err_free_ctrl; |
Lars-Peter Clausen | fa5b794 | 2014-03-07 13:14:32 -0300 | [diff] [blame] | 629 | |
Lars-Peter Clausen | fa5721d | 2015-01-23 12:52:20 -0300 | [diff] [blame] | 630 | if (state->irq) { |
| 631 | ret = request_threaded_irq(client->irq, NULL, adv7180_irq, |
Lars-Peter Clausen | f3e991d | 2015-01-23 12:52:21 -0300 | [diff] [blame] | 632 | IRQF_ONESHOT | IRQF_TRIGGER_FALLING, |
| 633 | KBUILD_MODNAME, state); |
Lars-Peter Clausen | fa5721d | 2015-01-23 12:52:20 -0300 | [diff] [blame] | 634 | if (ret) |
| 635 | goto err_free_ctrl; |
| 636 | } |
| 637 | |
Lars-Peter Clausen | fa5b794 | 2014-03-07 13:14:32 -0300 | [diff] [blame] | 638 | ret = v4l2_async_register_subdev(sd); |
| 639 | if (ret) |
| 640 | goto err_free_irq; |
| 641 | |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 642 | return 0; |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 643 | |
Lars-Peter Clausen | fa5b794 | 2014-03-07 13:14:32 -0300 | [diff] [blame] | 644 | err_free_irq: |
| 645 | if (state->irq > 0) |
| 646 | free_irq(client->irq, state); |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 647 | err_free_ctrl: |
| 648 | adv7180_exit_controls(state); |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 649 | err_unreg_subdev: |
Lars-Peter Clausen | 297a0ae | 2014-03-07 13:14:27 -0300 | [diff] [blame] | 650 | mutex_destroy(&state->mutex); |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 651 | return ret; |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 652 | } |
| 653 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 654 | static int adv7180_remove(struct i2c_client *client) |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 655 | { |
| 656 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 657 | struct adv7180_state *state = to_state(sd); |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 658 | |
Lars-Peter Clausen | fa5b794 | 2014-03-07 13:14:32 -0300 | [diff] [blame] | 659 | v4l2_async_unregister_subdev(sd); |
| 660 | |
Lars-Peter Clausen | 0c25534 | 2014-03-07 13:14:31 -0300 | [diff] [blame] | 661 | if (state->irq > 0) |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 662 | free_irq(client->irq, state); |
Richard Röjfors | 42752f7 | 2009-09-22 06:07:06 -0300 | [diff] [blame] | 663 | |
Lars-Peter Clausen | b13f4af | 2014-03-07 13:14:28 -0300 | [diff] [blame] | 664 | adv7180_exit_controls(state); |
Lars-Peter Clausen | 297a0ae | 2014-03-07 13:14:27 -0300 | [diff] [blame] | 665 | mutex_destroy(&state->mutex); |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static const struct i2c_device_id adv7180_id[] = { |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 670 | {KBUILD_MODNAME, 0}, |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 671 | {}, |
| 672 | }; |
| 673 | |
Lars-Peter Clausen | cc1088d | 2013-04-13 05:25:59 -0300 | [diff] [blame] | 674 | #ifdef CONFIG_PM_SLEEP |
| 675 | static int adv7180_suspend(struct device *dev) |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 676 | { |
Lars-Peter Clausen | cc1088d | 2013-04-13 05:25:59 -0300 | [diff] [blame] | 677 | struct i2c_client *client = to_i2c_client(dev); |
Lars-Peter Clausen | e246c33 | 2014-03-10 14:05:39 -0300 | [diff] [blame] | 678 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 679 | struct adv7180_state *state = to_state(sd); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 680 | |
Lars-Peter Clausen | e246c33 | 2014-03-10 14:05:39 -0300 | [diff] [blame] | 681 | return adv7180_set_power(state, client, false); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 682 | } |
| 683 | |
Lars-Peter Clausen | cc1088d | 2013-04-13 05:25:59 -0300 | [diff] [blame] | 684 | static int adv7180_resume(struct device *dev) |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 685 | { |
Lars-Peter Clausen | cc1088d | 2013-04-13 05:25:59 -0300 | [diff] [blame] | 686 | struct i2c_client *client = to_i2c_client(dev); |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 687 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 688 | struct adv7180_state *state = to_state(sd); |
| 689 | int ret; |
| 690 | |
Lars-Peter Clausen | e246c33 | 2014-03-10 14:05:39 -0300 | [diff] [blame] | 691 | if (state->powered) { |
| 692 | ret = adv7180_set_power(state, client, true); |
| 693 | if (ret) |
| 694 | return ret; |
| 695 | } |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 696 | ret = init_device(client, state); |
| 697 | if (ret < 0) |
| 698 | return ret; |
| 699 | return 0; |
| 700 | } |
Lars-Peter Clausen | cc1088d | 2013-04-13 05:25:59 -0300 | [diff] [blame] | 701 | |
| 702 | static SIMPLE_DEV_PM_OPS(adv7180_pm_ops, adv7180_suspend, adv7180_resume); |
| 703 | #define ADV7180_PM_OPS (&adv7180_pm_ops) |
| 704 | |
| 705 | #else |
| 706 | #define ADV7180_PM_OPS NULL |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 707 | #endif |
| 708 | |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 709 | MODULE_DEVICE_TABLE(i2c, adv7180_id); |
| 710 | |
| 711 | static struct i2c_driver adv7180_driver = { |
| 712 | .driver = { |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 713 | .owner = THIS_MODULE, |
Federico Vaga | c9fbedd | 2012-07-11 11:29:33 -0300 | [diff] [blame] | 714 | .name = KBUILD_MODNAME, |
Lars-Peter Clausen | cc1088d | 2013-04-13 05:25:59 -0300 | [diff] [blame] | 715 | .pm = ADV7180_PM_OPS, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 716 | }, |
| 717 | .probe = adv7180_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 718 | .remove = adv7180_remove, |
Federico Vaga | bca7ad1 | 2012-04-12 12:39:36 -0300 | [diff] [blame] | 719 | .id_table = adv7180_id, |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 720 | }; |
| 721 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 722 | module_i2c_driver(adv7180_driver); |
Richard Röjfors | 6789cb5 | 2009-09-18 21:17:20 -0300 | [diff] [blame] | 723 | |
| 724 | MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver"); |
| 725 | MODULE_AUTHOR("Mocean Laboratories"); |
| 726 | MODULE_LICENSE("GPL v2"); |