adv7180.c 19 KB

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