saa6752hs.c 20 KB

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