saa6752hs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. saa6752hs - i2c-driver for the saa6752hs by Philips
  3. Copyright (C) 2004 Andrew de Quincey
  4. AC-3 support:
  5. Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License vs published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mvss Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/string.h>
  21. #include <linux/timer.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/slab.h>
  25. #include <linux/poll.h>
  26. #include <linux/i2c.h>
  27. #include <linux/types.h>
  28. #include <linux/videodev2.h>
  29. #include <linux/init.h>
  30. #include <linux/crc32.h>
  31. #include <media/v4l2-device.h>
  32. #include <media/v4l2-ctrls.h>
  33. #include <media/v4l2-common.h>
  34. #define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
  35. #define MPEG_VIDEO_MAX_BITRATE_MAX 27000
  36. #define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
  37. #define MPEG_PID_MAX ((1 << 14) - 1)
  38. MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
  39. MODULE_AUTHOR("Andrew de Quincey");
  40. MODULE_LICENSE("GPL");
  41. enum saa6752hs_videoformat {
  42. SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
  43. SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
  44. SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
  45. SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
  46. SAA6752HS_VF_UNKNOWN,
  47. };
  48. struct saa6752hs_mpeg_params {
  49. /* transport streams */
  50. __u16 ts_pid_pmt;
  51. __u16 ts_pid_audio;
  52. __u16 ts_pid_video;
  53. __u16 ts_pid_pcr;
  54. /* audio */
  55. enum v4l2_mpeg_audio_encoding au_encoding;
  56. enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
  57. enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
  58. /* video */
  59. enum v4l2_mpeg_video_aspect vi_aspect;
  60. enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
  61. __u32 vi_bitrate;
  62. __u32 vi_bitrate_peak;
  63. };
  64. static const struct v4l2_format v4l2_format_table[] =
  65. {
  66. [SAA6752HS_VF_D1] =
  67. { .fmt = { .pix = { .width = 720, .height = 576 }}},
  68. [SAA6752HS_VF_2_3_D1] =
  69. { .fmt = { .pix = { .width = 480, .height = 576 }}},
  70. [SAA6752HS_VF_1_2_D1] =
  71. { .fmt = { .pix = { .width = 352, .height = 576 }}},
  72. [SAA6752HS_VF_SIF] =
  73. { .fmt = { .pix = { .width = 352, .height = 288 }}},
  74. [SAA6752HS_VF_UNKNOWN] =
  75. { .fmt = { .pix = { .width = 0, .height = 0}}},
  76. };
  77. struct saa6752hs_state {
  78. struct v4l2_subdev sd;
  79. struct v4l2_ctrl_handler hdl;
  80. struct { /* video bitrate mode control cluster */
  81. struct v4l2_ctrl *video_bitrate_mode;
  82. struct v4l2_ctrl *video_bitrate;
  83. struct v4l2_ctrl *video_bitrate_peak;
  84. };
  85. u32 revision;
  86. int has_ac3;
  87. struct saa6752hs_mpeg_params params;
  88. enum saa6752hs_videoformat video_format;
  89. v4l2_std_id standard;
  90. };
  91. enum saa6752hs_command {
  92. SAA6752HS_COMMAND_RESET = 0,
  93. SAA6752HS_COMMAND_STOP = 1,
  94. SAA6752HS_COMMAND_START = 2,
  95. SAA6752HS_COMMAND_PAUSE = 3,
  96. SAA6752HS_COMMAND_RECONFIGURE = 4,
  97. SAA6752HS_COMMAND_SLEEP = 5,
  98. SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
  99. SAA6752HS_COMMAND_MAX
  100. };
  101. static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd)
  102. {
  103. return container_of(sd, struct saa6752hs_state, sd);
  104. }
  105. /* ---------------------------------------------------------------------- */
  106. static const u8 PAT[] = {
  107. 0xc2, /* i2c register */
  108. 0x00, /* table number for encoder */
  109. 0x47, /* sync */
  110. 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
  111. 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
  112. 0x00, /* PSI pointer to start of table */
  113. 0x00, /* tid(0) */
  114. 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
  115. 0x00, 0x01, /* transport_stream_id(1) */
  116. 0xc1, /* version_number(0), current_next_indicator(1) */
  117. 0x00, 0x00, /* section_number(0), last_section_number(0) */
  118. 0x00, 0x01, /* program_number(1) */
  119. 0xe0, 0x00, /* PMT PID */
  120. 0x00, 0x00, 0x00, 0x00 /* CRC32 */
  121. };
  122. static const u8 PMT[] = {
  123. 0xc2, /* i2c register */
  124. 0x01, /* table number for encoder */
  125. 0x47, /* sync */
  126. 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
  127. 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
  128. 0x00, /* PSI pointer to start of table */
  129. 0x02, /* tid(2) */
  130. 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
  131. 0x00, 0x01, /* program_number(1) */
  132. 0xc1, /* version_number(0), current_next_indicator(1) */
  133. 0x00, 0x00, /* section_number(0), last_section_number(0) */
  134. 0xe0, 0x00, /* PCR_PID */
  135. 0xf0, 0x00, /* program_info_length(0) */
  136. 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
  137. 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
  138. 0x00, 0x00, 0x00, 0x00 /* CRC32 */
  139. };
  140. static const u8 PMT_AC3[] = {
  141. 0xc2, /* i2c register */
  142. 0x01, /* table number for encoder(1) */
  143. 0x47, /* sync */
  144. 0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
  145. 0x10, /* PMT PID (0x0010) */
  146. 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
  147. 0x00, /* PSI pointer to start of table */
  148. 0x02, /* TID (2) */
  149. 0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
  150. 0x00, 0x01, /* program_number(1) */
  151. 0xc1, /* version_number(0), current_next_indicator(1) */
  152. 0x00, 0x00, /* section_number(0), last_section_number(0) */
  153. 0xe1, 0x04, /* PCR_PID (0x0104) */
  154. 0xf0, 0x00, /* program_info_length(0) */
  155. 0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
  156. 0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
  157. 0x6a, /* AC3 */
  158. 0x01, /* Descriptor_length(1) */
  159. 0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
  160. 0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
  161. };
  162. static const struct saa6752hs_mpeg_params param_defaults =
  163. {
  164. .ts_pid_pmt = 16,
  165. .ts_pid_video = 260,
  166. .ts_pid_audio = 256,
  167. .ts_pid_pcr = 259,
  168. .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
  169. .vi_bitrate = 4000,
  170. .vi_bitrate_peak = 6000,
  171. .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  172. .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
  173. .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
  174. .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
  175. };
  176. /* ---------------------------------------------------------------------- */
  177. static int saa6752hs_chip_command(struct i2c_client *client,
  178. enum saa6752hs_command command)
  179. {
  180. unsigned char buf[3];
  181. unsigned long timeout;
  182. int status = 0;
  183. /* execute the command */
  184. switch(command) {
  185. case SAA6752HS_COMMAND_RESET:
  186. buf[0] = 0x00;
  187. break;
  188. case SAA6752HS_COMMAND_STOP:
  189. buf[0] = 0x03;
  190. break;
  191. case SAA6752HS_COMMAND_START:
  192. buf[0] = 0x02;
  193. break;
  194. case SAA6752HS_COMMAND_PAUSE:
  195. buf[0] = 0x04;
  196. break;
  197. case SAA6752HS_COMMAND_RECONFIGURE:
  198. buf[0] = 0x05;
  199. break;
  200. case SAA6752HS_COMMAND_SLEEP:
  201. buf[0] = 0x06;
  202. break;
  203. case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
  204. buf[0] = 0x07;
  205. break;
  206. default:
  207. return -EINVAL;
  208. }
  209. /* set it and wait for it to be so */
  210. i2c_master_send(client, buf, 1);
  211. timeout = jiffies + HZ * 3;
  212. for (;;) {
  213. /* get the current status */
  214. buf[0] = 0x10;
  215. i2c_master_send(client, buf, 1);
  216. i2c_master_recv(client, buf, 1);
  217. if (!(buf[0] & 0x20))
  218. break;
  219. if (time_after(jiffies,timeout)) {
  220. status = -ETIMEDOUT;
  221. break;
  222. }
  223. msleep(10);
  224. }
  225. /* delay a bit to let encoder settle */
  226. msleep(50);
  227. return status;
  228. }
  229. static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
  230. {
  231. u8 buf[2];
  232. buf[0] = reg;
  233. buf[1] = val;
  234. i2c_master_send(client, buf, 2);
  235. }
  236. static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
  237. {
  238. u8 buf[3];
  239. buf[0] = reg;
  240. buf[1] = val >> 8;
  241. buf[2] = val & 0xff;
  242. i2c_master_send(client, buf, 3);
  243. }
  244. static int saa6752hs_set_bitrate(struct i2c_client *client,
  245. struct saa6752hs_state *h)
  246. {
  247. struct saa6752hs_mpeg_params *params = &h->params;
  248. int tot_bitrate;
  249. int is_384k;
  250. /* set the bitrate mode */
  251. set_reg8(client, 0x71,
  252. params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
  253. /* set the video bitrate */
  254. if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
  255. /* set the target bitrate */
  256. set_reg16(client, 0x80, params->vi_bitrate);
  257. /* set the max bitrate */
  258. set_reg16(client, 0x81, params->vi_bitrate_peak);
  259. tot_bitrate = params->vi_bitrate_peak;
  260. } else {
  261. /* set the target bitrate (no max bitrate for CBR) */
  262. set_reg16(client, 0x81, params->vi_bitrate);
  263. tot_bitrate = params->vi_bitrate;
  264. }
  265. /* set the audio encoding */
  266. set_reg8(client, 0x93,
  267. params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
  268. /* set the audio bitrate */
  269. if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
  270. is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
  271. else
  272. is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
  273. set_reg8(client, 0x94, is_384k);
  274. tot_bitrate += is_384k ? 384 : 256;
  275. /* Note: the total max bitrate is determined by adding the video and audio
  276. bitrates together and also adding an extra 768kbit/s to stay on the
  277. safe side. If more control should be required, then an extra MPEG control
  278. should be added. */
  279. tot_bitrate += 768;
  280. if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
  281. tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
  282. /* set the total bitrate */
  283. set_reg16(client, 0xb1, tot_bitrate);
  284. return 0;
  285. }
  286. static int saa6752hs_try_ctrl(struct v4l2_ctrl *ctrl)
  287. {
  288. struct saa6752hs_state *h =
  289. container_of(ctrl->handler, struct saa6752hs_state, hdl);
  290. switch (ctrl->id) {
  291. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  292. /* peak bitrate shall be >= normal bitrate */
  293. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  294. h->video_bitrate_peak->val < h->video_bitrate->val)
  295. h->video_bitrate_peak->val = h->video_bitrate->val;
  296. break;
  297. }
  298. return 0;
  299. }
  300. static int saa6752hs_s_ctrl(struct v4l2_ctrl *ctrl)
  301. {
  302. struct saa6752hs_state *h =
  303. container_of(ctrl->handler, struct saa6752hs_state, hdl);
  304. struct saa6752hs_mpeg_params *params = &h->params;
  305. switch (ctrl->id) {
  306. case V4L2_CID_MPEG_STREAM_TYPE:
  307. break;
  308. case V4L2_CID_MPEG_STREAM_PID_PMT:
  309. params->ts_pid_pmt = ctrl->val;
  310. break;
  311. case V4L2_CID_MPEG_STREAM_PID_AUDIO:
  312. params->ts_pid_audio = ctrl->val;
  313. break;
  314. case V4L2_CID_MPEG_STREAM_PID_VIDEO:
  315. params->ts_pid_video = ctrl->val;
  316. break;
  317. case V4L2_CID_MPEG_STREAM_PID_PCR:
  318. params->ts_pid_pcr = ctrl->val;
  319. break;
  320. case V4L2_CID_MPEG_AUDIO_ENCODING:
  321. params->au_encoding = ctrl->val;
  322. break;
  323. case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
  324. params->au_l2_bitrate = ctrl->val;
  325. break;
  326. case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
  327. params->au_ac3_bitrate = ctrl->val;
  328. break;
  329. case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
  330. break;
  331. case V4L2_CID_MPEG_VIDEO_ENCODING:
  332. break;
  333. case V4L2_CID_MPEG_VIDEO_ASPECT:
  334. params->vi_aspect = ctrl->val;
  335. break;
  336. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  337. params->vi_bitrate_mode = ctrl->val;
  338. params->vi_bitrate = h->video_bitrate->val / 1000;
  339. params->vi_bitrate_peak = h->video_bitrate_peak->val / 1000;
  340. v4l2_ctrl_activate(h->video_bitrate_peak,
  341. ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
  342. break;
  343. default:
  344. return -EINVAL;
  345. }
  346. return 0;
  347. }
  348. static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
  349. {
  350. unsigned char buf[9], buf2[4];
  351. struct saa6752hs_state *h = to_state(sd);
  352. struct i2c_client *client = v4l2_get_subdevdata(sd);
  353. unsigned size;
  354. u32 crc;
  355. unsigned char localPAT[256];
  356. unsigned char localPMT[256];
  357. /* Set video format - must be done first as it resets other settings */
  358. set_reg8(client, 0x41, h->video_format);
  359. /* Set number of lines in input signal */
  360. set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0);
  361. /* set bitrate */
  362. saa6752hs_set_bitrate(client, h);
  363. /* Set GOP structure {3, 13} */
  364. set_reg16(client, 0x72, 0x030d);
  365. /* Set minimum Q-scale {4} */
  366. set_reg8(client, 0x82, 0x04);
  367. /* Set maximum Q-scale {12} */
  368. set_reg8(client, 0x83, 0x0c);
  369. /* Set Output Protocol */
  370. set_reg8(client, 0xd0, 0x81);
  371. /* Set video output stream format {TS} */
  372. set_reg8(client, 0xb0, 0x05);
  373. /* Set leading null byte for TS */
  374. set_reg16(client, 0xf6, leading_null_bytes);
  375. /* compute PAT */
  376. memcpy(localPAT, PAT, sizeof(PAT));
  377. localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
  378. localPAT[18] = h->params.ts_pid_pmt & 0xff;
  379. crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
  380. localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
  381. localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
  382. localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
  383. localPAT[sizeof(PAT) - 1] = crc & 0xFF;
  384. /* compute PMT */
  385. if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
  386. size = sizeof(PMT_AC3);
  387. memcpy(localPMT, PMT_AC3, size);
  388. } else {
  389. size = sizeof(PMT);
  390. memcpy(localPMT, PMT, size);
  391. }
  392. localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
  393. localPMT[4] = h->params.ts_pid_pmt & 0xff;
  394. localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
  395. localPMT[16] = h->params.ts_pid_pcr & 0xFF;
  396. localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
  397. localPMT[21] = h->params.ts_pid_video & 0xFF;
  398. localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
  399. localPMT[26] = h->params.ts_pid_audio & 0xFF;
  400. crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
  401. localPMT[size - 4] = (crc >> 24) & 0xFF;
  402. localPMT[size - 3] = (crc >> 16) & 0xFF;
  403. localPMT[size - 2] = (crc >> 8) & 0xFF;
  404. localPMT[size - 1] = crc & 0xFF;
  405. /* Set Audio PID */
  406. set_reg16(client, 0xc1, h->params.ts_pid_audio);
  407. /* Set Video PID */
  408. set_reg16(client, 0xc0, h->params.ts_pid_video);
  409. /* Set PCR PID */
  410. set_reg16(client, 0xc4, h->params.ts_pid_pcr);
  411. /* Send SI tables */
  412. i2c_master_send(client, localPAT, sizeof(PAT));
  413. i2c_master_send(client, localPMT, size);
  414. /* mute then unmute audio. This removes buzzing artefacts */
  415. set_reg8(client, 0xa4, 1);
  416. set_reg8(client, 0xa4, 0);
  417. /* start it going */
  418. saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
  419. /* readout current state */
  420. buf[0] = 0xE1;
  421. buf[1] = 0xA7;
  422. buf[2] = 0xFE;
  423. buf[3] = 0x82;
  424. buf[4] = 0xB0;
  425. i2c_master_send(client, buf, 5);
  426. i2c_master_recv(client, buf2, 4);
  427. /* change aspect ratio */
  428. buf[0] = 0xE0;
  429. buf[1] = 0xA7;
  430. buf[2] = 0xFE;
  431. buf[3] = 0x82;
  432. buf[4] = 0xB0;
  433. buf[5] = buf2[0];
  434. switch (h->params.vi_aspect) {
  435. case V4L2_MPEG_VIDEO_ASPECT_16x9:
  436. buf[6] = buf2[1] | 0x40;
  437. break;
  438. case V4L2_MPEG_VIDEO_ASPECT_4x3:
  439. default:
  440. buf[6] = buf2[1] & 0xBF;
  441. break;
  442. }
  443. buf[7] = buf2[2];
  444. buf[8] = buf2[3];
  445. i2c_master_send(client, buf, 9);
  446. return 0;
  447. }
  448. static int saa6752hs_get_fmt(struct v4l2_subdev *sd,
  449. struct v4l2_subdev_pad_config *cfg,
  450. struct v4l2_subdev_format *format)
  451. {
  452. struct v4l2_mbus_framefmt *f = &format->format;
  453. struct saa6752hs_state *h = to_state(sd);
  454. if (format->pad)
  455. return -EINVAL;
  456. if (h->video_format == SAA6752HS_VF_UNKNOWN)
  457. h->video_format = SAA6752HS_VF_D1;
  458. f->width = v4l2_format_table[h->video_format].fmt.pix.width;
  459. f->height = v4l2_format_table[h->video_format].fmt.pix.height;
  460. f->code = MEDIA_BUS_FMT_FIXED;
  461. f->field = V4L2_FIELD_INTERLACED;
  462. f->colorspace = V4L2_COLORSPACE_SMPTE170M;
  463. return 0;
  464. }
  465. static int saa6752hs_set_fmt(struct v4l2_subdev *sd,
  466. struct v4l2_subdev_pad_config *cfg,
  467. struct v4l2_subdev_format *format)
  468. {
  469. struct v4l2_mbus_framefmt *f = &format->format;
  470. struct saa6752hs_state *h = to_state(sd);
  471. int dist_352, dist_480, dist_720;
  472. if (format->pad)
  473. return -EINVAL;
  474. f->code = MEDIA_BUS_FMT_FIXED;
  475. dist_352 = abs(f->width - 352);
  476. dist_480 = abs(f->width - 480);
  477. dist_720 = abs(f->width - 720);
  478. if (dist_720 < dist_480) {
  479. f->width = 720;
  480. f->height = 576;
  481. } else if (dist_480 < dist_352) {
  482. f->width = 480;
  483. f->height = 576;
  484. } else {
  485. f->width = 352;
  486. if (abs(f->height - 576) < abs(f->height - 288))
  487. f->height = 576;
  488. else
  489. f->height = 288;
  490. }
  491. f->field = V4L2_FIELD_INTERLACED;
  492. f->colorspace = V4L2_COLORSPACE_SMPTE170M;
  493. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  494. cfg->try_fmt = *f;
  495. return 0;
  496. }
  497. /*
  498. FIXME: translate and round width/height into EMPRESS
  499. subsample type:
  500. type | PAL | NTSC
  501. ---------------------------
  502. SIF | 352x288 | 352x240
  503. 1/2 D1 | 352x576 | 352x480
  504. 2/3 D1 | 480x576 | 480x480
  505. D1 | 720x576 | 720x480
  506. */
  507. if (f->code != MEDIA_BUS_FMT_FIXED)
  508. return -EINVAL;
  509. if (f->width == 720)
  510. h->video_format = SAA6752HS_VF_D1;
  511. else if (f->width == 480)
  512. h->video_format = SAA6752HS_VF_2_3_D1;
  513. else if (f->height == 576)
  514. h->video_format = SAA6752HS_VF_1_2_D1;
  515. else
  516. h->video_format = SAA6752HS_VF_SIF;
  517. return 0;
  518. }
  519. static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  520. {
  521. struct saa6752hs_state *h = to_state(sd);
  522. h->standard = std;
  523. return 0;
  524. }
  525. /* ----------------------------------------------------------------------- */
  526. static const struct v4l2_ctrl_ops saa6752hs_ctrl_ops = {
  527. .try_ctrl = saa6752hs_try_ctrl,
  528. .s_ctrl = saa6752hs_s_ctrl,
  529. };
  530. static const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
  531. .init = saa6752hs_init,
  532. };
  533. static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
  534. .s_std = saa6752hs_s_std,
  535. };
  536. static const struct v4l2_subdev_pad_ops saa6752hs_pad_ops = {
  537. .get_fmt = saa6752hs_get_fmt,
  538. .set_fmt = saa6752hs_set_fmt,
  539. };
  540. static const struct v4l2_subdev_ops saa6752hs_ops = {
  541. .core = &saa6752hs_core_ops,
  542. .video = &saa6752hs_video_ops,
  543. .pad = &saa6752hs_pad_ops,
  544. };
  545. static int saa6752hs_probe(struct i2c_client *client,
  546. const struct i2c_device_id *id)
  547. {
  548. struct saa6752hs_state *h;
  549. struct v4l2_subdev *sd;
  550. struct v4l2_ctrl_handler *hdl;
  551. u8 addr = 0x13;
  552. u8 data[12];
  553. v4l_info(client, "chip found @ 0x%x (%s)\n",
  554. client->addr << 1, client->adapter->name);
  555. h = devm_kzalloc(&client->dev, sizeof(*h), GFP_KERNEL);
  556. if (h == NULL)
  557. return -ENOMEM;
  558. sd = &h->sd;
  559. v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
  560. i2c_master_send(client, &addr, 1);
  561. i2c_master_recv(client, data, sizeof(data));
  562. h->revision = (data[8] << 8) | data[9];
  563. h->has_ac3 = 0;
  564. if (h->revision == 0x0206) {
  565. h->has_ac3 = 1;
  566. v4l_info(client, "supports AC-3\n");
  567. }
  568. h->params = param_defaults;
  569. hdl = &h->hdl;
  570. v4l2_ctrl_handler_init(hdl, 14);
  571. v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
  572. V4L2_CID_MPEG_AUDIO_ENCODING,
  573. h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
  574. V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
  575. 0x0d, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
  576. v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
  577. V4L2_CID_MPEG_AUDIO_L2_BITRATE,
  578. V4L2_MPEG_AUDIO_L2_BITRATE_384K,
  579. ~((1 << V4L2_MPEG_AUDIO_L2_BITRATE_256K) |
  580. (1 << V4L2_MPEG_AUDIO_L2_BITRATE_384K)),
  581. V4L2_MPEG_AUDIO_L2_BITRATE_256K);
  582. if (h->has_ac3)
  583. v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
  584. V4L2_CID_MPEG_AUDIO_AC3_BITRATE,
  585. V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
  586. ~((1 << V4L2_MPEG_AUDIO_AC3_BITRATE_256K) |
  587. (1 << V4L2_MPEG_AUDIO_AC3_BITRATE_384K)),
  588. V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
  589. v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
  590. V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
  591. V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
  592. ~(1 << V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000),
  593. V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
  594. v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
  595. V4L2_CID_MPEG_VIDEO_ENCODING,
  596. V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
  597. ~(1 << V4L2_MPEG_VIDEO_ENCODING_MPEG_2),
  598. V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
  599. v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
  600. V4L2_CID_MPEG_VIDEO_ASPECT,
  601. V4L2_MPEG_VIDEO_ASPECT_16x9, 0x01,
  602. V4L2_MPEG_VIDEO_ASPECT_4x3);
  603. h->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
  604. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  605. 1000000, 27000000, 1000, 8000000);
  606. v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
  607. V4L2_CID_MPEG_STREAM_TYPE,
  608. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  609. ~(1 << V4L2_MPEG_STREAM_TYPE_MPEG2_TS),
  610. V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
  611. h->video_bitrate_mode = v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
  612. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  613. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
  614. V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
  615. h->video_bitrate = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
  616. V4L2_CID_MPEG_VIDEO_BITRATE, 1000000, 27000000, 1000, 6000000);
  617. v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
  618. V4L2_CID_MPEG_STREAM_PID_PMT, 0, (1 << 14) - 1, 1, 16);
  619. v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
  620. V4L2_CID_MPEG_STREAM_PID_AUDIO, 0, (1 << 14) - 1, 1, 260);
  621. v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
  622. V4L2_CID_MPEG_STREAM_PID_VIDEO, 0, (1 << 14) - 1, 1, 256);
  623. v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
  624. V4L2_CID_MPEG_STREAM_PID_PCR, 0, (1 << 14) - 1, 1, 259);
  625. sd->ctrl_handler = hdl;
  626. if (hdl->error) {
  627. int err = hdl->error;
  628. v4l2_ctrl_handler_free(hdl);
  629. return err;
  630. }
  631. v4l2_ctrl_cluster(3, &h->video_bitrate_mode);
  632. v4l2_ctrl_handler_setup(hdl);
  633. h->standard = 0; /* Assume 625 input lines */
  634. return 0;
  635. }
  636. static int saa6752hs_remove(struct i2c_client *client)
  637. {
  638. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  639. v4l2_device_unregister_subdev(sd);
  640. v4l2_ctrl_handler_free(&to_state(sd)->hdl);
  641. return 0;
  642. }
  643. static const struct i2c_device_id saa6752hs_id[] = {
  644. { "saa6752hs", 0 },
  645. { }
  646. };
  647. MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
  648. static struct i2c_driver saa6752hs_driver = {
  649. .driver = {
  650. .name = "saa6752hs",
  651. },
  652. .probe = saa6752hs_probe,
  653. .remove = saa6752hs_remove,
  654. .id_table = saa6752hs_id,
  655. };
  656. module_i2c_driver(saa6752hs_driver);