adv7183.c 18 KB

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