saa6752hs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 <media/v4l2-common.h>
  30. #include <media/v4l2-chip-ident.h>
  31. #include <media/v4l2-i2c-drv-legacy.h>
  32. #include <linux/init.h>
  33. #include <linux/crc32.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. /* Addresses to scan */
  39. static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
  40. I2C_CLIENT_INSMOD;
  41. MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
  42. MODULE_AUTHOR("Andrew de Quincey");
  43. MODULE_LICENSE("GPL");
  44. enum saa6752hs_videoformat {
  45. SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
  46. SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
  47. SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
  48. SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
  49. SAA6752HS_VF_UNKNOWN,
  50. };
  51. struct saa6752hs_mpeg_params {
  52. /* transport streams */
  53. __u16 ts_pid_pmt;
  54. __u16 ts_pid_audio;
  55. __u16 ts_pid_video;
  56. __u16 ts_pid_pcr;
  57. /* audio */
  58. enum v4l2_mpeg_audio_encoding au_encoding;
  59. enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
  60. enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
  61. /* video */
  62. enum v4l2_mpeg_video_aspect vi_aspect;
  63. enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
  64. __u32 vi_bitrate;
  65. __u32 vi_bitrate_peak;
  66. };
  67. static const struct v4l2_format v4l2_format_table[] =
  68. {
  69. [SAA6752HS_VF_D1] =
  70. { .fmt = { .pix = { .width = 720, .height = 576 }}},
  71. [SAA6752HS_VF_2_3_D1] =
  72. { .fmt = { .pix = { .width = 480, .height = 576 }}},
  73. [SAA6752HS_VF_1_2_D1] =
  74. { .fmt = { .pix = { .width = 352, .height = 576 }}},
  75. [SAA6752HS_VF_SIF] =
  76. { .fmt = { .pix = { .width = 352, .height = 288 }}},
  77. [SAA6752HS_VF_UNKNOWN] =
  78. { .fmt = { .pix = { .width = 0, .height = 0}}},
  79. };
  80. struct saa6752hs_state {
  81. int chip;
  82. u32 revision;
  83. int has_ac3;
  84. struct saa6752hs_mpeg_params params;
  85. enum saa6752hs_videoformat video_format;
  86. v4l2_std_id standard;
  87. };
  88. enum saa6752hs_command {
  89. SAA6752HS_COMMAND_RESET = 0,
  90. SAA6752HS_COMMAND_STOP = 1,
  91. SAA6752HS_COMMAND_START = 2,
  92. SAA6752HS_COMMAND_PAUSE = 3,
  93. SAA6752HS_COMMAND_RECONFIGURE = 4,
  94. SAA6752HS_COMMAND_SLEEP = 5,
  95. SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
  96. SAA6752HS_COMMAND_MAX
  97. };
  98. /* ---------------------------------------------------------------------- */
  99. static u8 PAT[] = {
  100. 0xc2, /* i2c register */
  101. 0x00, /* table number for encoder */
  102. 0x47, /* sync */
  103. 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
  104. 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
  105. 0x00, /* PSI pointer to start of table */
  106. 0x00, /* tid(0) */
  107. 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
  108. 0x00, 0x01, /* transport_stream_id(1) */
  109. 0xc1, /* version_number(0), current_next_indicator(1) */
  110. 0x00, 0x00, /* section_number(0), last_section_number(0) */
  111. 0x00, 0x01, /* program_number(1) */
  112. 0xe0, 0x00, /* PMT PID */
  113. 0x00, 0x00, 0x00, 0x00 /* CRC32 */
  114. };
  115. static u8 PMT[] = {
  116. 0xc2, /* i2c register */
  117. 0x01, /* table number for encoder */
  118. 0x47, /* sync */
  119. 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
  120. 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
  121. 0x00, /* PSI pointer to start of table */
  122. 0x02, /* tid(2) */
  123. 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
  124. 0x00, 0x01, /* program_number(1) */
  125. 0xc1, /* version_number(0), current_next_indicator(1) */
  126. 0x00, 0x00, /* section_number(0), last_section_number(0) */
  127. 0xe0, 0x00, /* PCR_PID */
  128. 0xf0, 0x00, /* program_info_length(0) */
  129. 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
  130. 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
  131. 0x00, 0x00, 0x00, 0x00 /* CRC32 */
  132. };
  133. static u8 PMT_AC3[] = {
  134. 0xc2, /* i2c register */
  135. 0x01, /* table number for encoder(1) */
  136. 0x47, /* sync */
  137. 0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
  138. 0x10, /* PMT PID (0x0010) */
  139. 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
  140. 0x00, /* PSI pointer to start of table */
  141. 0x02, /* TID (2) */
  142. 0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
  143. 0x00, 0x01, /* program_number(1) */
  144. 0xc1, /* version_number(0), current_next_indicator(1) */
  145. 0x00, 0x00, /* section_number(0), last_section_number(0) */
  146. 0xe1, 0x04, /* PCR_PID (0x0104) */
  147. 0xf0, 0x00, /* program_info_length(0) */
  148. 0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
  149. 0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
  150. 0x6a, /* AC3 */
  151. 0x01, /* Descriptor_length(1) */
  152. 0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
  153. 0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
  154. };
  155. static struct saa6752hs_mpeg_params param_defaults =
  156. {
  157. .ts_pid_pmt = 16,
  158. .ts_pid_video = 260,
  159. .ts_pid_audio = 256,
  160. .ts_pid_pcr = 259,
  161. .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
  162. .vi_bitrate = 4000,
  163. .vi_bitrate_peak = 6000,
  164. .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  165. .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
  166. .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
  167. .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
  168. };
  169. /* ---------------------------------------------------------------------- */
  170. static int saa6752hs_chip_command(struct i2c_client* client,
  171. enum saa6752hs_command command)
  172. {
  173. unsigned char buf[3];
  174. unsigned long timeout;
  175. int status = 0;
  176. /* execute the command */
  177. switch(command) {
  178. case SAA6752HS_COMMAND_RESET:
  179. buf[0] = 0x00;
  180. break;
  181. case SAA6752HS_COMMAND_STOP:
  182. buf[0] = 0x03;
  183. break;
  184. case SAA6752HS_COMMAND_START:
  185. buf[0] = 0x02;
  186. break;
  187. case SAA6752HS_COMMAND_PAUSE:
  188. buf[0] = 0x04;
  189. break;
  190. case SAA6752HS_COMMAND_RECONFIGURE:
  191. buf[0] = 0x05;
  192. break;
  193. case SAA6752HS_COMMAND_SLEEP:
  194. buf[0] = 0x06;
  195. break;
  196. case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
  197. buf[0] = 0x07;
  198. break;
  199. default:
  200. return -EINVAL;
  201. }
  202. /* set it and wait for it to be so */
  203. i2c_master_send(client, buf, 1);
  204. timeout = jiffies + HZ * 3;
  205. for (;;) {
  206. /* get the current status */
  207. buf[0] = 0x10;
  208. i2c_master_send(client, buf, 1);
  209. i2c_master_recv(client, buf, 1);
  210. if (!(buf[0] & 0x20))
  211. break;
  212. if (time_after(jiffies,timeout)) {
  213. status = -ETIMEDOUT;
  214. break;
  215. }
  216. msleep(10);
  217. }
  218. /* delay a bit to let encoder settle */
  219. msleep(50);
  220. return status;
  221. }
  222. static int saa6752hs_set_bitrate(struct i2c_client* client,
  223. struct saa6752hs_state *h)
  224. {
  225. struct saa6752hs_mpeg_params *params = &h->params;
  226. u8 buf[3];
  227. int tot_bitrate;
  228. /* set the bitrate mode */
  229. buf[0] = 0x71;
  230. buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1;
  231. i2c_master_send(client, buf, 2);
  232. /* set the video bitrate */
  233. if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
  234. /* set the target bitrate */
  235. buf[0] = 0x80;
  236. buf[1] = params->vi_bitrate >> 8;
  237. buf[2] = params->vi_bitrate & 0xff;
  238. i2c_master_send(client, buf, 3);
  239. /* set the max bitrate */
  240. buf[0] = 0x81;
  241. buf[1] = params->vi_bitrate_peak >> 8;
  242. buf[2] = params->vi_bitrate_peak & 0xff;
  243. i2c_master_send(client, buf, 3);
  244. tot_bitrate = params->vi_bitrate_peak;
  245. } else {
  246. /* set the target bitrate (no max bitrate for CBR) */
  247. buf[0] = 0x81;
  248. buf[1] = params->vi_bitrate >> 8;
  249. buf[2] = params->vi_bitrate & 0xff;
  250. i2c_master_send(client, buf, 3);
  251. tot_bitrate = params->vi_bitrate;
  252. }
  253. /* set the audio encoding */
  254. buf[0] = 0x93;
  255. buf[1] = (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
  256. i2c_master_send(client, buf, 2);
  257. /* set the audio bitrate */
  258. buf[0] = 0x94;
  259. if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
  260. buf[1] = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
  261. else
  262. buf[1] = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
  263. tot_bitrate += buf[1] ? 384 : 256;
  264. i2c_master_send(client, buf, 2);
  265. /* Note: the total max bitrate is determined by adding the video and audio
  266. bitrates together and also adding an extra 768kbit/s to stay on the
  267. safe side. If more control should be required, then an extra MPEG control
  268. should be added. */
  269. tot_bitrate += 768;
  270. if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
  271. tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
  272. /* set the total bitrate */
  273. buf[0] = 0xb1;
  274. buf[1] = tot_bitrate >> 8;
  275. buf[2] = tot_bitrate & 0xff;
  276. i2c_master_send(client, buf, 3);
  277. return 0;
  278. }
  279. static void saa6752hs_set_subsampling(struct i2c_client* client,
  280. struct v4l2_format* f)
  281. {
  282. struct saa6752hs_state *h = i2c_get_clientdata(client);
  283. int dist_352, dist_480, dist_720;
  284. /*
  285. FIXME: translate and round width/height into EMPRESS
  286. subsample type:
  287. type | PAL | NTSC
  288. ---------------------------
  289. SIF | 352x288 | 352x240
  290. 1/2 D1 | 352x576 | 352x480
  291. 2/3 D1 | 480x576 | 480x480
  292. D1 | 720x576 | 720x480
  293. */
  294. dist_352 = abs(f->fmt.pix.width - 352);
  295. dist_480 = abs(f->fmt.pix.width - 480);
  296. dist_720 = abs(f->fmt.pix.width - 720);
  297. if (dist_720 < dist_480) {
  298. f->fmt.pix.width = 720;
  299. f->fmt.pix.height = 576;
  300. h->video_format = SAA6752HS_VF_D1;
  301. }
  302. else if (dist_480 < dist_352) {
  303. f->fmt.pix.width = 480;
  304. f->fmt.pix.height = 576;
  305. h->video_format = SAA6752HS_VF_2_3_D1;
  306. }
  307. else {
  308. f->fmt.pix.width = 352;
  309. if (abs(f->fmt.pix.height - 576) <
  310. abs(f->fmt.pix.height - 288)) {
  311. f->fmt.pix.height = 576;
  312. h->video_format = SAA6752HS_VF_1_2_D1;
  313. }
  314. else {
  315. f->fmt.pix.height = 288;
  316. h->video_format = SAA6752HS_VF_SIF;
  317. }
  318. }
  319. }
  320. static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
  321. struct v4l2_ext_control *ctrl, unsigned int cmd)
  322. {
  323. int old = 0, new;
  324. int set = (cmd == VIDIOC_S_EXT_CTRLS);
  325. new = ctrl->value;
  326. switch (ctrl->id) {
  327. case V4L2_CID_MPEG_STREAM_TYPE:
  328. old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
  329. if (set && new != old)
  330. return -ERANGE;
  331. new = old;
  332. break;
  333. case V4L2_CID_MPEG_STREAM_PID_PMT:
  334. old = params->ts_pid_pmt;
  335. if (set && new > MPEG_PID_MAX)
  336. return -ERANGE;
  337. if (new > MPEG_PID_MAX)
  338. new = MPEG_PID_MAX;
  339. params->ts_pid_pmt = new;
  340. break;
  341. case V4L2_CID_MPEG_STREAM_PID_AUDIO:
  342. old = params->ts_pid_audio;
  343. if (set && new > MPEG_PID_MAX)
  344. return -ERANGE;
  345. if (new > MPEG_PID_MAX)
  346. new = MPEG_PID_MAX;
  347. params->ts_pid_audio = new;
  348. break;
  349. case V4L2_CID_MPEG_STREAM_PID_VIDEO:
  350. old = params->ts_pid_video;
  351. if (set && new > MPEG_PID_MAX)
  352. return -ERANGE;
  353. if (new > MPEG_PID_MAX)
  354. new = MPEG_PID_MAX;
  355. params->ts_pid_video = new;
  356. break;
  357. case V4L2_CID_MPEG_STREAM_PID_PCR:
  358. old = params->ts_pid_pcr;
  359. if (set && new > MPEG_PID_MAX)
  360. return -ERANGE;
  361. if (new > MPEG_PID_MAX)
  362. new = MPEG_PID_MAX;
  363. params->ts_pid_pcr = new;
  364. break;
  365. case V4L2_CID_MPEG_AUDIO_ENCODING:
  366. old = params->au_encoding;
  367. if (set && new != V4L2_MPEG_AUDIO_ENCODING_LAYER_2 &&
  368. (!has_ac3 || new != V4L2_MPEG_AUDIO_ENCODING_AC3))
  369. return -ERANGE;
  370. new = old;
  371. break;
  372. case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
  373. old = params->au_l2_bitrate;
  374. if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
  375. new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
  376. return -ERANGE;
  377. if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
  378. new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
  379. else
  380. new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
  381. params->au_l2_bitrate = new;
  382. break;
  383. case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
  384. if (!has_ac3)
  385. return -EINVAL;
  386. old = params->au_ac3_bitrate;
  387. if (set && new != V4L2_MPEG_AUDIO_AC3_BITRATE_256K &&
  388. new != V4L2_MPEG_AUDIO_AC3_BITRATE_384K)
  389. return -ERANGE;
  390. if (new <= V4L2_MPEG_AUDIO_AC3_BITRATE_256K)
  391. new = V4L2_MPEG_AUDIO_AC3_BITRATE_256K;
  392. else
  393. new = V4L2_MPEG_AUDIO_AC3_BITRATE_384K;
  394. params->au_ac3_bitrate = new;
  395. break;
  396. case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
  397. old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
  398. if (set && new != old)
  399. return -ERANGE;
  400. new = old;
  401. break;
  402. case V4L2_CID_MPEG_VIDEO_ENCODING:
  403. old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
  404. if (set && new != old)
  405. return -ERANGE;
  406. new = old;
  407. break;
  408. case V4L2_CID_MPEG_VIDEO_ASPECT:
  409. old = params->vi_aspect;
  410. if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
  411. new != V4L2_MPEG_VIDEO_ASPECT_4x3)
  412. return -ERANGE;
  413. if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
  414. new = V4L2_MPEG_VIDEO_ASPECT_4x3;
  415. params->vi_aspect = new;
  416. break;
  417. case V4L2_CID_MPEG_VIDEO_BITRATE:
  418. old = params->vi_bitrate * 1000;
  419. new = 1000 * (new / 1000);
  420. if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
  421. return -ERANGE;
  422. if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
  423. new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
  424. params->vi_bitrate = new / 1000;
  425. break;
  426. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  427. old = params->vi_bitrate_peak * 1000;
  428. new = 1000 * (new / 1000);
  429. if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
  430. return -ERANGE;
  431. if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
  432. new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
  433. params->vi_bitrate_peak = new / 1000;
  434. break;
  435. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  436. old = params->vi_bitrate_mode;
  437. params->vi_bitrate_mode = new;
  438. break;
  439. default:
  440. return -EINVAL;
  441. }
  442. if (cmd == VIDIOC_G_EXT_CTRLS)
  443. ctrl->value = old;
  444. else
  445. ctrl->value = new;
  446. return 0;
  447. }
  448. static int saa6752hs_qctrl(struct saa6752hs_state *h,
  449. struct v4l2_queryctrl *qctrl)
  450. {
  451. struct saa6752hs_mpeg_params *params = &h->params;
  452. int err;
  453. switch (qctrl->id) {
  454. case V4L2_CID_MPEG_AUDIO_ENCODING:
  455. return v4l2_ctrl_query_fill(qctrl,
  456. V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
  457. h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
  458. V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
  459. 1, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
  460. case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
  461. return v4l2_ctrl_query_fill(qctrl,
  462. V4L2_MPEG_AUDIO_L2_BITRATE_256K,
  463. V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
  464. V4L2_MPEG_AUDIO_L2_BITRATE_256K);
  465. case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
  466. if (!h->has_ac3)
  467. return -EINVAL;
  468. return v4l2_ctrl_query_fill(qctrl,
  469. V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
  470. V4L2_MPEG_AUDIO_AC3_BITRATE_384K, 1,
  471. V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
  472. case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
  473. return v4l2_ctrl_query_fill(qctrl,
  474. V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
  475. V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000, 1,
  476. V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
  477. case V4L2_CID_MPEG_VIDEO_ENCODING:
  478. return v4l2_ctrl_query_fill(qctrl,
  479. V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
  480. V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1,
  481. V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
  482. case V4L2_CID_MPEG_VIDEO_ASPECT:
  483. return v4l2_ctrl_query_fill(qctrl,
  484. V4L2_MPEG_VIDEO_ASPECT_4x3,
  485. V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
  486. V4L2_MPEG_VIDEO_ASPECT_4x3);
  487. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  488. err = v4l2_ctrl_query_fill_std(qctrl);
  489. if (err == 0 &&
  490. params->vi_bitrate_mode ==
  491. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
  492. qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
  493. return err;
  494. case V4L2_CID_MPEG_STREAM_TYPE:
  495. return v4l2_ctrl_query_fill(qctrl,
  496. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  497. V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 1,
  498. V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
  499. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  500. case V4L2_CID_MPEG_VIDEO_BITRATE:
  501. case V4L2_CID_MPEG_STREAM_PID_PMT:
  502. case V4L2_CID_MPEG_STREAM_PID_AUDIO:
  503. case V4L2_CID_MPEG_STREAM_PID_VIDEO:
  504. case V4L2_CID_MPEG_STREAM_PID_PCR:
  505. return v4l2_ctrl_query_fill_std(qctrl);
  506. default:
  507. break;
  508. }
  509. return -EINVAL;
  510. }
  511. static int saa6752hs_qmenu(struct saa6752hs_state *h,
  512. struct v4l2_querymenu *qmenu)
  513. {
  514. static const u32 mpeg_audio_encoding[] = {
  515. V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
  516. V4L2_CTRL_MENU_IDS_END
  517. };
  518. static const u32 mpeg_audio_ac3_encoding[] = {
  519. V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
  520. V4L2_MPEG_AUDIO_ENCODING_AC3,
  521. V4L2_CTRL_MENU_IDS_END
  522. };
  523. static u32 mpeg_audio_l2_bitrate[] = {
  524. V4L2_MPEG_AUDIO_L2_BITRATE_256K,
  525. V4L2_MPEG_AUDIO_L2_BITRATE_384K,
  526. V4L2_CTRL_MENU_IDS_END
  527. };
  528. static u32 mpeg_audio_ac3_bitrate[] = {
  529. V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
  530. V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
  531. V4L2_CTRL_MENU_IDS_END
  532. };
  533. struct v4l2_queryctrl qctrl;
  534. int err;
  535. qctrl.id = qmenu->id;
  536. err = saa6752hs_qctrl(h, &qctrl);
  537. if (err)
  538. return err;
  539. switch (qmenu->id) {
  540. case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
  541. return v4l2_ctrl_query_menu_valid_items(qmenu,
  542. mpeg_audio_l2_bitrate);
  543. case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
  544. if (!h->has_ac3)
  545. return -EINVAL;
  546. return v4l2_ctrl_query_menu_valid_items(qmenu,
  547. mpeg_audio_ac3_bitrate);
  548. case V4L2_CID_MPEG_AUDIO_ENCODING:
  549. return v4l2_ctrl_query_menu_valid_items(qmenu,
  550. h->has_ac3 ? mpeg_audio_ac3_encoding :
  551. mpeg_audio_encoding);
  552. }
  553. return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL);
  554. }
  555. static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes)
  556. {
  557. unsigned char buf[9], buf2[4];
  558. struct saa6752hs_state *h;
  559. unsigned size;
  560. u32 crc;
  561. unsigned char localPAT[256];
  562. unsigned char localPMT[256];
  563. h = i2c_get_clientdata(client);
  564. /* Set video format - must be done first as it resets other settings */
  565. buf[0] = 0x41;
  566. buf[1] = h->video_format;
  567. i2c_master_send(client, buf, 2);
  568. /* Set number of lines in input signal */
  569. buf[0] = 0x40;
  570. buf[1] = 0x00;
  571. if (h->standard & V4L2_STD_525_60)
  572. buf[1] = 0x01;
  573. i2c_master_send(client, buf, 2);
  574. /* set bitrate */
  575. saa6752hs_set_bitrate(client, h);
  576. /* Set GOP structure {3, 13} */
  577. buf[0] = 0x72;
  578. buf[1] = 0x03;
  579. buf[2] = 0x0D;
  580. i2c_master_send(client,buf,3);
  581. /* Set minimum Q-scale {4} */
  582. buf[0] = 0x82;
  583. buf[1] = 0x04;
  584. i2c_master_send(client,buf,2);
  585. /* Set maximum Q-scale {12} */
  586. buf[0] = 0x83;
  587. buf[1] = 0x0C;
  588. i2c_master_send(client,buf,2);
  589. /* Set Output Protocol */
  590. buf[0] = 0xD0;
  591. buf[1] = 0x81;
  592. i2c_master_send(client,buf,2);
  593. /* Set video output stream format {TS} */
  594. buf[0] = 0xB0;
  595. buf[1] = 0x05;
  596. i2c_master_send(client,buf,2);
  597. /* Set leading null byte for TS */
  598. buf[0] = 0xF6;
  599. buf[1] = (leading_null_bytes >> 8) & 0xff;
  600. buf[2] = leading_null_bytes & 0xff;
  601. i2c_master_send(client, buf, 3);
  602. /* compute PAT */
  603. memcpy(localPAT, PAT, sizeof(PAT));
  604. localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
  605. localPAT[18] = h->params.ts_pid_pmt & 0xff;
  606. crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
  607. localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
  608. localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
  609. localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
  610. localPAT[sizeof(PAT) - 1] = crc & 0xFF;
  611. /* compute PMT */
  612. if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
  613. size = sizeof(PMT_AC3);
  614. memcpy(localPMT, PMT_AC3, size);
  615. } else {
  616. size = sizeof(PMT);
  617. memcpy(localPMT, PMT, size);
  618. }
  619. localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
  620. localPMT[4] = h->params.ts_pid_pmt & 0xff;
  621. localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
  622. localPMT[16] = h->params.ts_pid_pcr & 0xFF;
  623. localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
  624. localPMT[21] = h->params.ts_pid_video & 0xFF;
  625. localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
  626. localPMT[26] = h->params.ts_pid_audio & 0xFF;
  627. crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
  628. localPMT[size - 4] = (crc >> 24) & 0xFF;
  629. localPMT[size - 3] = (crc >> 16) & 0xFF;
  630. localPMT[size - 2] = (crc >> 8) & 0xFF;
  631. localPMT[size - 1] = crc & 0xFF;
  632. /* Set Audio PID */
  633. buf[0] = 0xC1;
  634. buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
  635. buf[2] = h->params.ts_pid_audio & 0xFF;
  636. i2c_master_send(client,buf,3);
  637. /* Set Video PID */
  638. buf[0] = 0xC0;
  639. buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
  640. buf[2] = h->params.ts_pid_video & 0xFF;
  641. i2c_master_send(client,buf,3);
  642. /* Set PCR PID */
  643. buf[0] = 0xC4;
  644. buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
  645. buf[2] = h->params.ts_pid_pcr & 0xFF;
  646. i2c_master_send(client,buf,3);
  647. /* Send SI tables */
  648. i2c_master_send(client, localPAT, sizeof(PAT));
  649. i2c_master_send(client, localPMT, size);
  650. /* mute then unmute audio. This removes buzzing artefacts */
  651. buf[0] = 0xa4;
  652. buf[1] = 1;
  653. i2c_master_send(client, buf, 2);
  654. buf[1] = 0;
  655. i2c_master_send(client, buf, 2);
  656. /* start it going */
  657. saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
  658. /* readout current state */
  659. buf[0] = 0xE1;
  660. buf[1] = 0xA7;
  661. buf[2] = 0xFE;
  662. buf[3] = 0x82;
  663. buf[4] = 0xB0;
  664. i2c_master_send(client, buf, 5);
  665. i2c_master_recv(client, buf2, 4);
  666. /* change aspect ratio */
  667. buf[0] = 0xE0;
  668. buf[1] = 0xA7;
  669. buf[2] = 0xFE;
  670. buf[3] = 0x82;
  671. buf[4] = 0xB0;
  672. buf[5] = buf2[0];
  673. switch(h->params.vi_aspect) {
  674. case V4L2_MPEG_VIDEO_ASPECT_16x9:
  675. buf[6] = buf2[1] | 0x40;
  676. break;
  677. case V4L2_MPEG_VIDEO_ASPECT_4x3:
  678. default:
  679. buf[6] = buf2[1] & 0xBF;
  680. break;
  681. break;
  682. }
  683. buf[7] = buf2[2];
  684. buf[8] = buf2[3];
  685. i2c_master_send(client, buf, 9);
  686. return 0;
  687. }
  688. static int
  689. saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
  690. {
  691. struct saa6752hs_state *h = i2c_get_clientdata(client);
  692. struct v4l2_ext_controls *ctrls = arg;
  693. struct saa6752hs_mpeg_params params;
  694. int err = 0;
  695. int i;
  696. switch (cmd) {
  697. case VIDIOC_INT_INIT:
  698. /* apply settings and start encoder */
  699. saa6752hs_init(client, *(u32 *)arg);
  700. break;
  701. case VIDIOC_S_EXT_CTRLS:
  702. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  703. return -EINVAL;
  704. /* fall through */
  705. case VIDIOC_TRY_EXT_CTRLS:
  706. case VIDIOC_G_EXT_CTRLS:
  707. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  708. return -EINVAL;
  709. params = h->params;
  710. for (i = 0; i < ctrls->count; i++) {
  711. err = handle_ctrl(h->has_ac3, &params, ctrls->controls + i, cmd);
  712. if (err) {
  713. ctrls->error_idx = i;
  714. return err;
  715. }
  716. }
  717. h->params = params;
  718. break;
  719. case VIDIOC_QUERYCTRL:
  720. return saa6752hs_qctrl(h, arg);
  721. case VIDIOC_QUERYMENU:
  722. return saa6752hs_qmenu(h, arg);
  723. case VIDIOC_G_FMT:
  724. {
  725. struct v4l2_format *f = arg;
  726. if (h->video_format == SAA6752HS_VF_UNKNOWN)
  727. h->video_format = SAA6752HS_VF_D1;
  728. f->fmt.pix.width =
  729. v4l2_format_table[h->video_format].fmt.pix.width;
  730. f->fmt.pix.height =
  731. v4l2_format_table[h->video_format].fmt.pix.height;
  732. break ;
  733. }
  734. case VIDIOC_S_FMT:
  735. {
  736. struct v4l2_format *f = arg;
  737. saa6752hs_set_subsampling(client, f);
  738. break;
  739. }
  740. case VIDIOC_S_STD:
  741. h->standard = *((v4l2_std_id *) arg);
  742. break;
  743. case VIDIOC_G_CHIP_IDENT:
  744. return v4l2_chip_ident_i2c_client(client,
  745. arg, h->chip, h->revision);
  746. default:
  747. /* nothing */
  748. break;
  749. }
  750. return err;
  751. }
  752. static int saa6752hs_probe(struct i2c_client *client,
  753. const struct i2c_device_id *id)
  754. {
  755. struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
  756. u8 addr = 0x13;
  757. u8 data[12];
  758. v4l_info(client, "chip found @ 0x%x (%s)\n",
  759. client->addr << 1, client->adapter->name);
  760. if (h == NULL)
  761. return -ENOMEM;
  762. i2c_master_send(client, &addr, 1);
  763. i2c_master_recv(client, data, sizeof(data));
  764. h->chip = V4L2_IDENT_SAA6752HS;
  765. h->revision = (data[8] << 8) | data[9];
  766. h->has_ac3 = 0;
  767. if (h->revision == 0x0206) {
  768. h->chip = V4L2_IDENT_SAA6752HS_AC3;
  769. h->has_ac3 = 1;
  770. v4l_info(client, "support AC-3\n");
  771. }
  772. h->params = param_defaults;
  773. h->standard = 0; /* Assume 625 input lines */
  774. i2c_set_clientdata(client, h);
  775. return 0;
  776. }
  777. static int saa6752hs_remove(struct i2c_client *client)
  778. {
  779. kfree(i2c_get_clientdata(client));
  780. return 0;
  781. }
  782. static const struct i2c_device_id saa6752hs_id[] = {
  783. { "saa6752hs", 0 },
  784. { }
  785. };
  786. MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
  787. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  788. .name = "saa6752hs",
  789. .driverid = I2C_DRIVERID_SAA6752HS,
  790. .command = saa6752hs_command,
  791. .probe = saa6752hs_probe,
  792. .remove = saa6752hs_remove,
  793. .id_table = saa6752hs_id,
  794. };
  795. /*
  796. * Overrides for Emacs so that we follow Linus's tabbing style.
  797. * ---------------------------------------------------------------------------
  798. * Local variables:
  799. * c-basic-offset: 8
  800. * End:
  801. */