saa6752hs.c 22 KB

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