saa6752hs.c 22 KB

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