adv7183.c 17 KB

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