adv7183.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * adv7183.c Analog Devices ADV7183 video decoder driver
  4. *
  5. * Copyright (c) 2011 Analog Devices Inc.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/i2c.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. #include <linux/videodev2.h>
  16. #include <media/i2c/adv7183.h>
  17. #include <media/v4l2-ctrls.h>
  18. #include <media/v4l2-device.h>
  19. #include "adv7183_regs.h"
  20. struct adv7183 {
  21. struct v4l2_subdev sd;
  22. struct v4l2_ctrl_handler hdl;
  23. v4l2_std_id std; /* Current set standard */
  24. u32 input;
  25. u32 output;
  26. struct gpio_desc *reset_pin;
  27. struct gpio_desc *oe_pin;
  28. struct v4l2_mbus_framefmt fmt;
  29. };
  30. /* EXAMPLES USING 27 MHz CLOCK
  31. * Mode 1 CVBS Input (Composite Video on AIN5)
  32. * All standards are supported through autodetect, 8-bit, 4:2:2, ITU-R BT.656 output on P15 to P8.
  33. */
  34. static const unsigned char adv7183_init_regs[] = {
  35. ADV7183_IN_CTRL, 0x04, /* CVBS input on AIN5 */
  36. ADV7183_DIGI_CLAMP_CTRL_1, 0x00, /* Slow down digital clamps */
  37. ADV7183_SHAP_FILT_CTRL, 0x41, /* Set CSFM to SH1 */
  38. ADV7183_ADC_CTRL, 0x16, /* Power down ADC 1 and ADC 2 */
  39. ADV7183_CTI_DNR_CTRL_4, 0x04, /* Set DNR threshold to 4 for flat response */
  40. /* ADI recommended programming sequence */
  41. ADV7183_ADI_CTRL, 0x80,
  42. ADV7183_CTI_DNR_CTRL_4, 0x20,
  43. 0x52, 0x18,
  44. 0x58, 0xED,
  45. 0x77, 0xC5,
  46. 0x7C, 0x93,
  47. 0x7D, 0x00,
  48. 0xD0, 0x48,
  49. 0xD5, 0xA0,
  50. 0xD7, 0xEA,
  51. ADV7183_SD_SATURATION_CR, 0x3E,
  52. ADV7183_PAL_V_END, 0x3E,
  53. ADV7183_PAL_F_TOGGLE, 0x0F,
  54. ADV7183_ADI_CTRL, 0x00,
  55. };
  56. static inline struct adv7183 *to_adv7183(struct v4l2_subdev *sd)
  57. {
  58. return container_of(sd, struct adv7183, sd);
  59. }
  60. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  61. {
  62. return &container_of(ctrl->handler, struct adv7183, hdl)->sd;
  63. }
  64. static inline int adv7183_read(struct v4l2_subdev *sd, unsigned char reg)
  65. {
  66. struct i2c_client *client = v4l2_get_subdevdata(sd);
  67. return i2c_smbus_read_byte_data(client, reg);
  68. }
  69. static inline int adv7183_write(struct v4l2_subdev *sd, unsigned char reg,
  70. unsigned char value)
  71. {
  72. struct i2c_client *client = v4l2_get_subdevdata(sd);
  73. return i2c_smbus_write_byte_data(client, reg, value);
  74. }
  75. static int adv7183_writeregs(struct v4l2_subdev *sd,
  76. const unsigned char *regs, unsigned int num)
  77. {
  78. unsigned char reg, data;
  79. unsigned int cnt = 0;
  80. if (num & 0x1) {
  81. v4l2_err(sd, "invalid regs array\n");
  82. return -1;
  83. }
  84. while (cnt < num) {
  85. reg = *regs++;
  86. data = *regs++;
  87. cnt += 2;
  88. adv7183_write(sd, reg, data);
  89. }
  90. return 0;
  91. }
  92. static int adv7183_log_status(struct v4l2_subdev *sd)
  93. {
  94. struct adv7183 *decoder = to_adv7183(sd);
  95. v4l2_info(sd, "adv7183: Input control = 0x%02x\n",
  96. adv7183_read(sd, ADV7183_IN_CTRL));
  97. v4l2_info(sd, "adv7183: Video selection = 0x%02x\n",
  98. adv7183_read(sd, ADV7183_VD_SEL));
  99. v4l2_info(sd, "adv7183: Output control = 0x%02x\n",
  100. adv7183_read(sd, ADV7183_OUT_CTRL));
  101. v4l2_info(sd, "adv7183: Extended output control = 0x%02x\n",
  102. adv7183_read(sd, ADV7183_EXT_OUT_CTRL));
  103. v4l2_info(sd, "adv7183: Autodetect enable = 0x%02x\n",
  104. adv7183_read(sd, ADV7183_AUTO_DET_EN));
  105. v4l2_info(sd, "adv7183: Contrast = 0x%02x\n",
  106. adv7183_read(sd, ADV7183_CONTRAST));
  107. v4l2_info(sd, "adv7183: Brightness = 0x%02x\n",
  108. adv7183_read(sd, ADV7183_BRIGHTNESS));
  109. v4l2_info(sd, "adv7183: Hue = 0x%02x\n",
  110. adv7183_read(sd, ADV7183_HUE));
  111. v4l2_info(sd, "adv7183: Default value Y = 0x%02x\n",
  112. adv7183_read(sd, ADV7183_DEF_Y));
  113. v4l2_info(sd, "adv7183: Default value C = 0x%02x\n",
  114. adv7183_read(sd, ADV7183_DEF_C));
  115. v4l2_info(sd, "adv7183: ADI control = 0x%02x\n",
  116. adv7183_read(sd, ADV7183_ADI_CTRL));
  117. v4l2_info(sd, "adv7183: Power Management = 0x%02x\n",
  118. adv7183_read(sd, ADV7183_POW_MANAGE));
  119. v4l2_info(sd, "adv7183: Status 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
  120. adv7183_read(sd, ADV7183_STATUS_1),
  121. adv7183_read(sd, ADV7183_STATUS_2),
  122. adv7183_read(sd, ADV7183_STATUS_3));
  123. v4l2_info(sd, "adv7183: Ident = 0x%02x\n",
  124. adv7183_read(sd, ADV7183_IDENT));
  125. v4l2_info(sd, "adv7183: Analog clamp control = 0x%02x\n",
  126. adv7183_read(sd, ADV7183_ANAL_CLAMP_CTRL));
  127. v4l2_info(sd, "adv7183: Digital clamp control 1 = 0x%02x\n",
  128. adv7183_read(sd, ADV7183_DIGI_CLAMP_CTRL_1));
  129. v4l2_info(sd, "adv7183: Shaping filter control 1 and 2 = 0x%02x 0x%02x\n",
  130. adv7183_read(sd, ADV7183_SHAP_FILT_CTRL),
  131. adv7183_read(sd, ADV7183_SHAP_FILT_CTRL_2));
  132. v4l2_info(sd, "adv7183: Comb filter control = 0x%02x\n",
  133. adv7183_read(sd, ADV7183_COMB_FILT_CTRL));
  134. v4l2_info(sd, "adv7183: ADI control 2 = 0x%02x\n",
  135. adv7183_read(sd, ADV7183_ADI_CTRL_2));
  136. v4l2_info(sd, "adv7183: Pixel delay control = 0x%02x\n",
  137. adv7183_read(sd, ADV7183_PIX_DELAY_CTRL));
  138. v4l2_info(sd, "adv7183: Misc gain control = 0x%02x\n",
  139. adv7183_read(sd, ADV7183_MISC_GAIN_CTRL));
  140. v4l2_info(sd, "adv7183: AGC mode control = 0x%02x\n",
  141. adv7183_read(sd, ADV7183_AGC_MODE_CTRL));
  142. v4l2_info(sd, "adv7183: Chroma gain control 1 and 2 = 0x%02x 0x%02x\n",
  143. adv7183_read(sd, ADV7183_CHRO_GAIN_CTRL_1),
  144. adv7183_read(sd, ADV7183_CHRO_GAIN_CTRL_2));
  145. v4l2_info(sd, "adv7183: Luma gain control 1 and 2 = 0x%02x 0x%02x\n",
  146. adv7183_read(sd, ADV7183_LUMA_GAIN_CTRL_1),
  147. adv7183_read(sd, ADV7183_LUMA_GAIN_CTRL_2));
  148. v4l2_info(sd, "adv7183: Vsync field control 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
  149. adv7183_read(sd, ADV7183_VS_FIELD_CTRL_1),
  150. adv7183_read(sd, ADV7183_VS_FIELD_CTRL_2),
  151. adv7183_read(sd, ADV7183_VS_FIELD_CTRL_3));
  152. v4l2_info(sd, "adv7183: Hsync position control 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
  153. adv7183_read(sd, ADV7183_HS_POS_CTRL_1),
  154. adv7183_read(sd, ADV7183_HS_POS_CTRL_2),
  155. adv7183_read(sd, ADV7183_HS_POS_CTRL_3));
  156. v4l2_info(sd, "adv7183: Polarity = 0x%02x\n",
  157. adv7183_read(sd, ADV7183_POLARITY));
  158. v4l2_info(sd, "adv7183: ADC control = 0x%02x\n",
  159. adv7183_read(sd, ADV7183_ADC_CTRL));
  160. v4l2_info(sd, "adv7183: SD offset Cb and Cr = 0x%02x 0x%02x\n",
  161. adv7183_read(sd, ADV7183_SD_OFFSET_CB),
  162. adv7183_read(sd, ADV7183_SD_OFFSET_CR));
  163. v4l2_info(sd, "adv7183: SD saturation Cb and Cr = 0x%02x 0x%02x\n",
  164. adv7183_read(sd, ADV7183_SD_SATURATION_CB),
  165. adv7183_read(sd, ADV7183_SD_SATURATION_CR));
  166. v4l2_info(sd, "adv7183: Drive strength = 0x%02x\n",
  167. adv7183_read(sd, ADV7183_DRIVE_STR));
  168. v4l2_ctrl_handler_log_status(&decoder->hdl, sd->name);
  169. return 0;
  170. }
  171. static int adv7183_g_std(struct v4l2_subdev *sd, v4l2_std_id *std)
  172. {
  173. struct adv7183 *decoder = to_adv7183(sd);
  174. *std = decoder->std;
  175. return 0;
  176. }
  177. static int adv7183_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  178. {
  179. struct adv7183 *decoder = to_adv7183(sd);
  180. int reg;
  181. reg = adv7183_read(sd, ADV7183_IN_CTRL) & 0xF;
  182. if (std == V4L2_STD_PAL_60)
  183. reg |= 0x60;
  184. else if (std == V4L2_STD_NTSC_443)
  185. reg |= 0x70;
  186. else if (std == V4L2_STD_PAL_N)
  187. reg |= 0x90;
  188. else if (std == V4L2_STD_PAL_M)
  189. reg |= 0xA0;
  190. else if (std == V4L2_STD_PAL_Nc)
  191. reg |= 0xC0;
  192. else if (std & V4L2_STD_PAL)
  193. reg |= 0x80;
  194. else if (std & V4L2_STD_NTSC)
  195. reg |= 0x50;
  196. else if (std & V4L2_STD_SECAM)
  197. reg |= 0xE0;
  198. else
  199. return -EINVAL;
  200. adv7183_write(sd, ADV7183_IN_CTRL, reg);
  201. decoder->std = std;
  202. return 0;
  203. }
  204. static int adv7183_reset(struct v4l2_subdev *sd, u32 val)
  205. {
  206. int reg;
  207. reg = adv7183_read(sd, ADV7183_POW_MANAGE) | 0x80;
  208. adv7183_write(sd, ADV7183_POW_MANAGE, reg);
  209. /* wait 5ms before any further i2c writes are performed */
  210. usleep_range(5000, 10000);
  211. return 0;
  212. }
  213. static int adv7183_s_routing(struct v4l2_subdev *sd,
  214. u32 input, u32 output, u32 config)
  215. {
  216. struct adv7183 *decoder = to_adv7183(sd);
  217. int reg;
  218. if ((input > ADV7183_COMPONENT1) || (output > ADV7183_16BIT_OUT))
  219. return -EINVAL;
  220. if (input != decoder->input) {
  221. decoder->input = input;
  222. reg = adv7183_read(sd, ADV7183_IN_CTRL) & 0xF0;
  223. switch (input) {
  224. case ADV7183_COMPOSITE1:
  225. reg |= 0x1;
  226. break;
  227. case ADV7183_COMPOSITE2:
  228. reg |= 0x2;
  229. break;
  230. case ADV7183_COMPOSITE3:
  231. reg |= 0x3;
  232. break;
  233. case ADV7183_COMPOSITE4:
  234. reg |= 0x4;
  235. break;
  236. case ADV7183_COMPOSITE5:
  237. reg |= 0x5;
  238. break;
  239. case ADV7183_COMPOSITE6:
  240. reg |= 0xB;
  241. break;
  242. case ADV7183_COMPOSITE7:
  243. reg |= 0xC;
  244. break;
  245. case ADV7183_COMPOSITE8:
  246. reg |= 0xD;
  247. break;
  248. case ADV7183_COMPOSITE9:
  249. reg |= 0xE;
  250. break;
  251. case ADV7183_COMPOSITE10:
  252. reg |= 0xF;
  253. break;
  254. case ADV7183_SVIDEO0:
  255. reg |= 0x6;
  256. break;
  257. case ADV7183_SVIDEO1:
  258. reg |= 0x7;
  259. break;
  260. case ADV7183_SVIDEO2:
  261. reg |= 0x8;
  262. break;
  263. case ADV7183_COMPONENT0:
  264. reg |= 0x9;
  265. break;
  266. case ADV7183_COMPONENT1:
  267. reg |= 0xA;
  268. break;
  269. default:
  270. break;
  271. }
  272. adv7183_write(sd, ADV7183_IN_CTRL, reg);
  273. }
  274. if (output != decoder->output) {
  275. decoder->output = output;
  276. reg = adv7183_read(sd, ADV7183_OUT_CTRL) & 0xC0;
  277. switch (output) {
  278. case ADV7183_16BIT_OUT:
  279. reg |= 0x9;
  280. break;
  281. default:
  282. reg |= 0xC;
  283. break;
  284. }
  285. adv7183_write(sd, ADV7183_OUT_CTRL, reg);
  286. }
  287. return 0;
  288. }
  289. static int adv7183_s_ctrl(struct v4l2_ctrl *ctrl)
  290. {
  291. struct v4l2_subdev *sd = to_sd(ctrl);
  292. int val = ctrl->val;
  293. switch (ctrl->id) {
  294. case V4L2_CID_BRIGHTNESS:
  295. if (val < 0)
  296. val = 127 - val;
  297. adv7183_write(sd, ADV7183_BRIGHTNESS, val);
  298. break;
  299. case V4L2_CID_CONTRAST:
  300. adv7183_write(sd, ADV7183_CONTRAST, val);
  301. break;
  302. case V4L2_CID_SATURATION:
  303. adv7183_write(sd, ADV7183_SD_SATURATION_CB, val >> 8);
  304. adv7183_write(sd, ADV7183_SD_SATURATION_CR, (val & 0xFF));
  305. break;
  306. case V4L2_CID_HUE:
  307. adv7183_write(sd, ADV7183_SD_OFFSET_CB, val >> 8);
  308. adv7183_write(sd, ADV7183_SD_OFFSET_CR, (val & 0xFF));
  309. break;
  310. default:
  311. return -EINVAL;
  312. }
  313. return 0;
  314. }
  315. static int adv7183_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  316. {
  317. struct adv7183 *decoder = to_adv7183(sd);
  318. int reg;
  319. /* enable autodetection block */
  320. reg = adv7183_read(sd, ADV7183_IN_CTRL) & 0xF;
  321. adv7183_write(sd, ADV7183_IN_CTRL, reg);
  322. /* wait autodetection switch */
  323. mdelay(10);
  324. /* get autodetection result */
  325. reg = adv7183_read(sd, ADV7183_STATUS_1);
  326. switch ((reg >> 0x4) & 0x7) {
  327. case 0:
  328. *std &= V4L2_STD_NTSC;
  329. break;
  330. case 1:
  331. *std &= V4L2_STD_NTSC_443;
  332. break;
  333. case 2:
  334. *std &= V4L2_STD_PAL_M;
  335. break;
  336. case 3:
  337. *std &= V4L2_STD_PAL_60;
  338. break;
  339. case 4:
  340. *std &= V4L2_STD_PAL;
  341. break;
  342. case 5:
  343. *std &= V4L2_STD_SECAM;
  344. break;
  345. case 6:
  346. *std &= V4L2_STD_PAL_Nc;
  347. break;
  348. case 7:
  349. *std &= V4L2_STD_SECAM;
  350. break;
  351. default:
  352. *std = V4L2_STD_UNKNOWN;
  353. break;
  354. }
  355. /* after std detection, write back user set std */
  356. adv7183_s_std(sd, decoder->std);
  357. return 0;
  358. }
  359. static int adv7183_g_input_status(struct v4l2_subdev *sd, u32 *status)
  360. {
  361. int reg;
  362. *status = V4L2_IN_ST_NO_SIGNAL;
  363. reg = adv7183_read(sd, ADV7183_STATUS_1);
  364. if (reg < 0)
  365. return reg;
  366. if (reg & 0x1)
  367. *status = 0;
  368. return 0;
  369. }
  370. static int adv7183_enum_mbus_code(struct v4l2_subdev *sd,
  371. struct v4l2_subdev_state *sd_state,
  372. struct v4l2_subdev_mbus_code_enum *code)
  373. {
  374. if (code->pad || code->index > 0)
  375. return -EINVAL;
  376. code->code = MEDIA_BUS_FMT_UYVY8_2X8;
  377. return 0;
  378. }
  379. static int adv7183_set_fmt(struct v4l2_subdev *sd,
  380. struct v4l2_subdev_state *sd_state,
  381. struct v4l2_subdev_format *format)
  382. {
  383. struct adv7183 *decoder = to_adv7183(sd);
  384. struct v4l2_mbus_framefmt *fmt = &format->format;
  385. if (format->pad)
  386. return -EINVAL;
  387. fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
  388. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  389. if (decoder->std & V4L2_STD_525_60) {
  390. fmt->field = V4L2_FIELD_SEQ_TB;
  391. fmt->width = 720;
  392. fmt->height = 480;
  393. } else {
  394. fmt->field = V4L2_FIELD_SEQ_BT;
  395. fmt->width = 720;
  396. fmt->height = 576;
  397. }
  398. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  399. decoder->fmt = *fmt;
  400. else
  401. sd_state->pads->try_fmt = *fmt;
  402. return 0;
  403. }
  404. static int adv7183_get_fmt(struct v4l2_subdev *sd,
  405. struct v4l2_subdev_state *sd_state,
  406. struct v4l2_subdev_format *format)
  407. {
  408. struct adv7183 *decoder = to_adv7183(sd);
  409. if (format->pad)
  410. return -EINVAL;
  411. format->format = decoder->fmt;
  412. return 0;
  413. }
  414. static int adv7183_s_stream(struct v4l2_subdev *sd, int enable)
  415. {
  416. struct adv7183 *decoder = to_adv7183(sd);
  417. if (enable)
  418. gpiod_set_value(decoder->oe_pin, 1);
  419. else
  420. gpiod_set_value(decoder->oe_pin, 0);
  421. udelay(1);
  422. return 0;
  423. }
  424. #ifdef CONFIG_VIDEO_ADV_DEBUG
  425. static int adv7183_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  426. {
  427. reg->val = adv7183_read(sd, reg->reg & 0xff);
  428. reg->size = 1;
  429. return 0;
  430. }
  431. static int adv7183_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  432. {
  433. adv7183_write(sd, reg->reg & 0xff, reg->val & 0xff);
  434. return 0;
  435. }
  436. #endif
  437. static const struct v4l2_ctrl_ops adv7183_ctrl_ops = {
  438. .s_ctrl = adv7183_s_ctrl,
  439. };
  440. static const struct v4l2_subdev_core_ops adv7183_core_ops = {
  441. .log_status = adv7183_log_status,
  442. .reset = adv7183_reset,
  443. #ifdef CONFIG_VIDEO_ADV_DEBUG
  444. .g_register = adv7183_g_register,
  445. .s_register = adv7183_s_register,
  446. #endif
  447. };
  448. static const struct v4l2_subdev_video_ops adv7183_video_ops = {
  449. .g_std = adv7183_g_std,
  450. .s_std = adv7183_s_std,
  451. .s_routing = adv7183_s_routing,
  452. .querystd = adv7183_querystd,
  453. .g_input_status = adv7183_g_input_status,
  454. .s_stream = adv7183_s_stream,
  455. };
  456. static const struct v4l2_subdev_pad_ops adv7183_pad_ops = {
  457. .enum_mbus_code = adv7183_enum_mbus_code,
  458. .get_fmt = adv7183_get_fmt,
  459. .set_fmt = adv7183_set_fmt,
  460. };
  461. static const struct v4l2_subdev_ops adv7183_ops = {
  462. .core = &adv7183_core_ops,
  463. .video = &adv7183_video_ops,
  464. .pad = &adv7183_pad_ops,
  465. };
  466. static int adv7183_probe(struct i2c_client *client,
  467. const struct i2c_device_id *id)
  468. {
  469. struct adv7183 *decoder;
  470. struct v4l2_subdev *sd;
  471. struct v4l2_ctrl_handler *hdl;
  472. int ret;
  473. struct v4l2_subdev_format fmt = {
  474. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  475. };
  476. /* Check if the adapter supports the needed features */
  477. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  478. return -EIO;
  479. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  480. client->addr << 1, client->adapter->name);
  481. decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
  482. if (decoder == NULL)
  483. return -ENOMEM;
  484. /*
  485. * Requesting high will assert reset, the line should be
  486. * flagged as active low in descriptor table or machine description.
  487. */
  488. decoder->reset_pin = devm_gpiod_get(&client->dev, "reset",
  489. GPIOD_OUT_HIGH);
  490. if (IS_ERR(decoder->reset_pin))
  491. return PTR_ERR(decoder->reset_pin);
  492. gpiod_set_consumer_name(decoder->reset_pin, "ADV7183 Reset");
  493. /*
  494. * Requesting low will start with output disabled, the line should be
  495. * flagged as active low in descriptor table or machine description.
  496. */
  497. decoder->oe_pin = devm_gpiod_get(&client->dev, "oe",
  498. GPIOD_OUT_LOW);
  499. if (IS_ERR(decoder->oe_pin))
  500. return PTR_ERR(decoder->oe_pin);
  501. gpiod_set_consumer_name(decoder->reset_pin, "ADV7183 Output Enable");
  502. sd = &decoder->sd;
  503. v4l2_i2c_subdev_init(sd, client, &adv7183_ops);
  504. hdl = &decoder->hdl;
  505. v4l2_ctrl_handler_init(hdl, 4);
  506. v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
  507. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  508. v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
  509. V4L2_CID_CONTRAST, 0, 0xFF, 1, 0x80);
  510. v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
  511. V4L2_CID_SATURATION, 0, 0xFFFF, 1, 0x8080);
  512. v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
  513. V4L2_CID_HUE, 0, 0xFFFF, 1, 0x8080);
  514. /* hook the control handler into the driver */
  515. sd->ctrl_handler = hdl;
  516. if (hdl->error) {
  517. ret = hdl->error;
  518. v4l2_ctrl_handler_free(hdl);
  519. return ret;
  520. }
  521. /* v4l2 doesn't support an autodetect standard, pick PAL as default */
  522. decoder->std = V4L2_STD_PAL;
  523. decoder->input = ADV7183_COMPOSITE4;
  524. decoder->output = ADV7183_8BIT_OUT;
  525. /* reset chip */
  526. /* reset pulse width at least 5ms */
  527. mdelay(10);
  528. /* De-assert reset line (descriptor tagged active low) */
  529. gpiod_set_value(decoder->reset_pin, 0);
  530. /* wait 5ms before any further i2c writes are performed */
  531. mdelay(5);
  532. adv7183_writeregs(sd, adv7183_init_regs, ARRAY_SIZE(adv7183_init_regs));
  533. adv7183_s_std(sd, decoder->std);
  534. fmt.format.width = 720;
  535. fmt.format.height = 576;
  536. adv7183_set_fmt(sd, NULL, &fmt);
  537. /* initialize the hardware to the default control values */
  538. ret = v4l2_ctrl_handler_setup(hdl);
  539. if (ret) {
  540. v4l2_ctrl_handler_free(hdl);
  541. return ret;
  542. }
  543. return 0;
  544. }
  545. static void adv7183_remove(struct i2c_client *client)
  546. {
  547. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  548. v4l2_device_unregister_subdev(sd);
  549. v4l2_ctrl_handler_free(sd->ctrl_handler);
  550. }
  551. static const struct i2c_device_id adv7183_id[] = {
  552. {"adv7183", 0},
  553. {},
  554. };
  555. MODULE_DEVICE_TABLE(i2c, adv7183_id);
  556. static struct i2c_driver adv7183_driver = {
  557. .driver = {
  558. .name = "adv7183",
  559. },
  560. .probe = adv7183_probe,
  561. .remove = adv7183_remove,
  562. .id_table = adv7183_id,
  563. };
  564. module_i2c_driver(adv7183_driver);
  565. MODULE_DESCRIPTION("Analog Devices ADV7183 video decoder driver");
  566. MODULE_AUTHOR("Scott Jiang <[email protected]>");
  567. MODULE_LICENSE("GPL v2");