saa6752hs.c 23 KB

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