cx25840-core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /* cx25840 - Conexant CX25840 audio/video decoder driver
  2. *
  3. * Copyright (C) 2004 Ulf Eklund
  4. *
  5. * Based on the saa7115 driver and on the first verison of Chris Kennedy's
  6. * cx25840 driver.
  7. *
  8. * Changes by Tyler Trafford <tatrafford@comcast.net>
  9. * - cleanup/rewrite for V4L2 API (2005)
  10. *
  11. * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/videodev2.h>
  31. #include <linux/i2c.h>
  32. #include <media/v4l2-common.h>
  33. #include <media/cx25840.h>
  34. #include "cx25840-core.h"
  35. MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
  36. MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
  37. MODULE_LICENSE("GPL");
  38. static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
  39. static int cx25840_debug;
  40. module_param_named(debug,cx25840_debug, int, 0644);
  41. MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
  42. I2C_CLIENT_INSMOD;
  43. /* ----------------------------------------------------------------------- */
  44. int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
  45. {
  46. u8 buffer[3];
  47. buffer[0] = addr >> 8;
  48. buffer[1] = addr & 0xff;
  49. buffer[2] = value;
  50. return i2c_master_send(client, buffer, 3);
  51. }
  52. int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
  53. {
  54. u8 buffer[6];
  55. buffer[0] = addr >> 8;
  56. buffer[1] = addr & 0xff;
  57. buffer[2] = value >> 24;
  58. buffer[3] = (value >> 16) & 0xff;
  59. buffer[4] = (value >> 8) & 0xff;
  60. buffer[5] = value & 0xff;
  61. return i2c_master_send(client, buffer, 6);
  62. }
  63. u8 cx25840_read(struct i2c_client * client, u16 addr)
  64. {
  65. u8 buffer[2];
  66. buffer[0] = addr >> 8;
  67. buffer[1] = addr & 0xff;
  68. if (i2c_master_send(client, buffer, 2) < 2)
  69. return 0;
  70. if (i2c_master_recv(client, buffer, 1) < 1)
  71. return 0;
  72. return buffer[0];
  73. }
  74. u32 cx25840_read4(struct i2c_client * client, u16 addr)
  75. {
  76. u8 buffer[4];
  77. buffer[0] = addr >> 8;
  78. buffer[1] = addr & 0xff;
  79. if (i2c_master_send(client, buffer, 2) < 2)
  80. return 0;
  81. if (i2c_master_recv(client, buffer, 4) < 4)
  82. return 0;
  83. return (buffer[0] << 24) | (buffer[1] << 16) |
  84. (buffer[2] << 8) | buffer[3];
  85. }
  86. int cx25840_and_or(struct i2c_client *client, u16 addr, u8 and_mask,
  87. u8 or_value)
  88. {
  89. return cx25840_write(client, addr,
  90. (cx25840_read(client, addr) & and_mask) |
  91. or_value);
  92. }
  93. /* ----------------------------------------------------------------------- */
  94. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  95. enum cx25840_audio_input aud_input);
  96. static void log_status(struct i2c_client *client);
  97. /* ----------------------------------------------------------------------- */
  98. static void init_dll1(struct i2c_client *client)
  99. {
  100. /* This is the Hauppauge sequence used to
  101. * initialize the Delay Lock Loop 1 (ADC DLL). */
  102. cx25840_write(client, 0x159, 0x23);
  103. cx25840_write(client, 0x15a, 0x87);
  104. cx25840_write(client, 0x15b, 0x06);
  105. cx25840_write(client, 0x159, 0xe1);
  106. cx25840_write(client, 0x15a, 0x86);
  107. cx25840_write(client, 0x159, 0xe0);
  108. cx25840_write(client, 0x159, 0xe1);
  109. cx25840_write(client, 0x15b, 0x10);
  110. }
  111. static void init_dll2(struct i2c_client *client)
  112. {
  113. /* This is the Hauppauge sequence used to
  114. * initialize the Delay Lock Loop 2 (ADC DLL). */
  115. cx25840_write(client, 0x15d, 0xe3);
  116. cx25840_write(client, 0x15e, 0x86);
  117. cx25840_write(client, 0x15f, 0x06);
  118. cx25840_write(client, 0x15d, 0xe1);
  119. cx25840_write(client, 0x15d, 0xe0);
  120. cx25840_write(client, 0x15d, 0xe1);
  121. }
  122. static void cx25840_initialize(struct i2c_client *client, int loadfw)
  123. {
  124. struct cx25840_state *state = i2c_get_clientdata(client);
  125. /* datasheet startup in numbered steps, refer to page 3-77 */
  126. /* 2. */
  127. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  128. /* The default of this register should be 4, but I get 0 instead.
  129. * Set this register to 4 manually. */
  130. cx25840_write(client, 0x000, 0x04);
  131. /* 3. */
  132. init_dll1(client);
  133. init_dll2(client);
  134. cx25840_write(client, 0x136, 0x0a);
  135. /* 4. */
  136. cx25840_write(client, 0x13c, 0x01);
  137. cx25840_write(client, 0x13c, 0x00);
  138. /* 5. */
  139. if (loadfw)
  140. cx25840_loadfw(client);
  141. /* 6. */
  142. cx25840_write(client, 0x115, 0x8c);
  143. cx25840_write(client, 0x116, 0x07);
  144. cx25840_write(client, 0x118, 0x02);
  145. /* 7. */
  146. cx25840_write(client, 0x4a5, 0x80);
  147. cx25840_write(client, 0x4a5, 0x00);
  148. cx25840_write(client, 0x402, 0x00);
  149. /* 8. */
  150. cx25840_and_or(client, 0x401, ~0x18, 0);
  151. cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
  152. /* steps 8c and 8d are done in change_input() */
  153. /* 10. */
  154. cx25840_write(client, 0x8d3, 0x1f);
  155. cx25840_write(client, 0x8e3, 0x03);
  156. cx25840_vbi_setup(client);
  157. /* trial and error says these are needed to get audio */
  158. cx25840_write(client, 0x914, 0xa0);
  159. cx25840_write(client, 0x918, 0xa0);
  160. cx25840_write(client, 0x919, 0x01);
  161. /* stereo prefered */
  162. cx25840_write(client, 0x809, 0x04);
  163. /* AC97 shift */
  164. cx25840_write(client, 0x8cf, 0x0f);
  165. /* (re)set input */
  166. set_input(client, state->vid_input, state->aud_input);
  167. /* start microcontroller */
  168. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  169. }
  170. /* ----------------------------------------------------------------------- */
  171. static void input_change(struct i2c_client *client)
  172. {
  173. struct cx25840_state *state = i2c_get_clientdata(client);
  174. v4l2_std_id std = cx25840_get_v4lstd(client);
  175. /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
  176. if (std & V4L2_STD_SECAM) {
  177. cx25840_write(client, 0x402, 0);
  178. }
  179. else {
  180. cx25840_write(client, 0x402, 0x04);
  181. cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  182. }
  183. cx25840_and_or(client, 0x401, ~0x60, 0);
  184. cx25840_and_or(client, 0x401, ~0x60, 0x60);
  185. /* Note: perhaps V4L2_STD_PAL_M should be handled as V4L2_STD_NTSC
  186. instead of V4L2_STD_PAL. Someone needs to test this. */
  187. if (std & V4L2_STD_PAL) {
  188. /* Follow tuner change procedure for PAL */
  189. cx25840_write(client, 0x808, 0xff);
  190. cx25840_write(client, 0x80b, 0x10);
  191. } else if (std & V4L2_STD_SECAM) {
  192. /* Select autodetect for SECAM */
  193. cx25840_write(client, 0x808, 0xff);
  194. cx25840_write(client, 0x80b, 0x10);
  195. } else if (std & V4L2_STD_NTSC) {
  196. /* Certain Hauppauge PVR150 models have a hardware bug
  197. that causes audio to drop out. For these models the
  198. audio standard must be set explicitly.
  199. To be precise: it affects cards with tuner models
  200. 85, 99 and 112 (model numbers from tveeprom). */
  201. int hw_fix = state->pvr150_workaround;
  202. if (std == V4L2_STD_NTSC_M_JP) {
  203. /* Japan uses EIAJ audio standard */
  204. cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
  205. } else if (std == V4L2_STD_NTSC_M_KR) {
  206. /* South Korea uses A2 audio standard */
  207. cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
  208. } else {
  209. /* Others use the BTSC audio standard */
  210. cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
  211. }
  212. cx25840_write(client, 0x80b, 0x00);
  213. }
  214. if (cx25840_read(client, 0x803) & 0x10) {
  215. /* restart audio decoder microcontroller */
  216. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  217. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  218. }
  219. }
  220. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  221. enum cx25840_audio_input aud_input)
  222. {
  223. struct cx25840_state *state = i2c_get_clientdata(client);
  224. u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
  225. vid_input <= CX25840_COMPOSITE8);
  226. u8 reg;
  227. v4l_dbg(1, cx25840_debug, client, "decoder set video input %d, audio input %d\n",
  228. vid_input, aud_input);
  229. if (is_composite) {
  230. reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
  231. } else {
  232. int luma = vid_input & 0xf0;
  233. int chroma = vid_input & 0xf00;
  234. if ((vid_input & ~0xff0) ||
  235. luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
  236. chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
  237. v4l_err(client, "0x%04x is not a valid video input!\n", vid_input);
  238. return -EINVAL;
  239. }
  240. reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
  241. if (chroma >= CX25840_SVIDEO_CHROMA7) {
  242. reg &= 0x3f;
  243. reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
  244. } else {
  245. reg &= 0xcf;
  246. reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
  247. }
  248. }
  249. switch (aud_input) {
  250. case CX25840_AUDIO_SERIAL:
  251. /* do nothing, use serial audio input */
  252. break;
  253. case CX25840_AUDIO4: reg &= ~0x30; break;
  254. case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  255. case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  256. case CX25840_AUDIO7: reg &= ~0xc0; break;
  257. case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  258. default:
  259. v4l_err(client, "0x%04x is not a valid audio input!\n", aud_input);
  260. return -EINVAL;
  261. }
  262. cx25840_write(client, 0x103, reg);
  263. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  264. cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
  265. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  266. cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
  267. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  268. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  269. cx25840_and_or(client, 0x102, ~0x4, 4);
  270. else
  271. cx25840_and_or(client, 0x102, ~0x4, 0);
  272. state->vid_input = vid_input;
  273. state->aud_input = aud_input;
  274. cx25840_audio_set_path(client);
  275. input_change(client);
  276. return 0;
  277. }
  278. /* ----------------------------------------------------------------------- */
  279. static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
  280. {
  281. u8 fmt=0; /* zero is autodetect */
  282. /* First tests should be against specific std */
  283. if (std == V4L2_STD_NTSC_M_JP) {
  284. fmt=0x2;
  285. } else if (std == V4L2_STD_NTSC_443) {
  286. fmt=0x3;
  287. } else if (std == V4L2_STD_PAL_M) {
  288. fmt=0x5;
  289. } else if (std == V4L2_STD_PAL_N) {
  290. fmt=0x6;
  291. } else if (std == V4L2_STD_PAL_Nc) {
  292. fmt=0x7;
  293. } else if (std == V4L2_STD_PAL_60) {
  294. fmt=0x8;
  295. } else {
  296. /* Then, test against generic ones */
  297. if (std & V4L2_STD_NTSC) {
  298. fmt=0x1;
  299. } else if (std & V4L2_STD_PAL) {
  300. fmt=0x4;
  301. } else if (std & V4L2_STD_SECAM) {
  302. fmt=0xc;
  303. }
  304. }
  305. /* Follow step 9 of section 3.16 in the cx25840 datasheet.
  306. Without this PAL may display a vertical ghosting effect.
  307. This happens for example with the Yuan MPC622. */
  308. if (fmt >= 4 && fmt < 8) {
  309. /* Set format to NTSC-M */
  310. cx25840_and_or(client, 0x400, ~0xf, 1);
  311. /* Turn off LCOMB */
  312. cx25840_and_or(client, 0x47b, ~6, 0);
  313. }
  314. cx25840_and_or(client, 0x400, ~0xf, fmt);
  315. cx25840_vbi_setup(client);
  316. return 0;
  317. }
  318. v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
  319. {
  320. /* check VID_FMT_SEL first */
  321. u8 fmt = cx25840_read(client, 0x400) & 0xf;
  322. if (!fmt) {
  323. /* check AFD_FMT_STAT if set to autodetect */
  324. fmt = cx25840_read(client, 0x40d) & 0xf;
  325. }
  326. switch (fmt) {
  327. case 0x1:
  328. {
  329. /* if the audio std is A2-M, then this is the South Korean
  330. NTSC standard */
  331. if (cx25840_read(client, 0x805) == 2)
  332. return V4L2_STD_NTSC_M_KR;
  333. return V4L2_STD_NTSC_M;
  334. }
  335. case 0x2: return V4L2_STD_NTSC_M_JP;
  336. case 0x3: return V4L2_STD_NTSC_443;
  337. case 0x4: return V4L2_STD_PAL;
  338. case 0x5: return V4L2_STD_PAL_M;
  339. case 0x6: return V4L2_STD_PAL_N;
  340. case 0x7: return V4L2_STD_PAL_Nc;
  341. case 0x8: return V4L2_STD_PAL_60;
  342. case 0xc: return V4L2_STD_SECAM;
  343. default: return V4L2_STD_UNKNOWN;
  344. }
  345. }
  346. /* ----------------------------------------------------------------------- */
  347. static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  348. {
  349. struct cx25840_state *state = i2c_get_clientdata(client);
  350. switch (ctrl->id) {
  351. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  352. state->pvr150_workaround = ctrl->value;
  353. set_input(client, state->vid_input, state->aud_input);
  354. break;
  355. case V4L2_CID_BRIGHTNESS:
  356. if (ctrl->value < 0 || ctrl->value > 255) {
  357. v4l_err(client, "invalid brightness setting %d\n",
  358. ctrl->value);
  359. return -ERANGE;
  360. }
  361. cx25840_write(client, 0x414, ctrl->value - 128);
  362. break;
  363. case V4L2_CID_CONTRAST:
  364. if (ctrl->value < 0 || ctrl->value > 127) {
  365. v4l_err(client, "invalid contrast setting %d\n",
  366. ctrl->value);
  367. return -ERANGE;
  368. }
  369. cx25840_write(client, 0x415, ctrl->value << 1);
  370. break;
  371. case V4L2_CID_SATURATION:
  372. if (ctrl->value < 0 || ctrl->value > 127) {
  373. v4l_err(client, "invalid saturation setting %d\n",
  374. ctrl->value);
  375. return -ERANGE;
  376. }
  377. cx25840_write(client, 0x420, ctrl->value << 1);
  378. cx25840_write(client, 0x421, ctrl->value << 1);
  379. break;
  380. case V4L2_CID_HUE:
  381. if (ctrl->value < -127 || ctrl->value > 127) {
  382. v4l_err(client, "invalid hue setting %d\n", ctrl->value);
  383. return -ERANGE;
  384. }
  385. cx25840_write(client, 0x422, ctrl->value);
  386. break;
  387. case V4L2_CID_AUDIO_VOLUME:
  388. case V4L2_CID_AUDIO_BASS:
  389. case V4L2_CID_AUDIO_TREBLE:
  390. case V4L2_CID_AUDIO_BALANCE:
  391. case V4L2_CID_AUDIO_MUTE:
  392. return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
  393. default:
  394. return -EINVAL;
  395. }
  396. return 0;
  397. }
  398. static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  399. {
  400. struct cx25840_state *state = i2c_get_clientdata(client);
  401. switch (ctrl->id) {
  402. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  403. ctrl->value = state->pvr150_workaround;
  404. break;
  405. case V4L2_CID_BRIGHTNESS:
  406. ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
  407. break;
  408. case V4L2_CID_CONTRAST:
  409. ctrl->value = cx25840_read(client, 0x415) >> 1;
  410. break;
  411. case V4L2_CID_SATURATION:
  412. ctrl->value = cx25840_read(client, 0x420) >> 1;
  413. break;
  414. case V4L2_CID_HUE:
  415. ctrl->value = (s8)cx25840_read(client, 0x422);
  416. break;
  417. case V4L2_CID_AUDIO_VOLUME:
  418. case V4L2_CID_AUDIO_BASS:
  419. case V4L2_CID_AUDIO_TREBLE:
  420. case V4L2_CID_AUDIO_BALANCE:
  421. case V4L2_CID_AUDIO_MUTE:
  422. return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
  423. default:
  424. return -EINVAL;
  425. }
  426. return 0;
  427. }
  428. /* ----------------------------------------------------------------------- */
  429. static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  430. {
  431. switch (fmt->type) {
  432. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  433. return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
  434. default:
  435. return -EINVAL;
  436. }
  437. return 0;
  438. }
  439. static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  440. {
  441. struct v4l2_pix_format *pix;
  442. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  443. int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
  444. switch (fmt->type) {
  445. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  446. pix = &(fmt->fmt.pix);
  447. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  448. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  449. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  450. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  451. Vlines = pix->height + (is_pal ? 4 : 7);
  452. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  453. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  454. v4l_err(client, "%dx%d is not a valid size!\n",
  455. pix->width, pix->height);
  456. return -ERANGE;
  457. }
  458. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  459. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  460. VSC &= 0x1fff;
  461. if (pix->width >= 385)
  462. filter = 0;
  463. else if (pix->width > 192)
  464. filter = 1;
  465. else if (pix->width > 96)
  466. filter = 2;
  467. else
  468. filter = 3;
  469. v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
  470. pix->width, pix->height, HSC, VSC);
  471. /* HSCALE=HSC */
  472. cx25840_write(client, 0x418, HSC & 0xff);
  473. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  474. cx25840_write(client, 0x41a, HSC >> 16);
  475. /* VSCALE=VSC */
  476. cx25840_write(client, 0x41c, VSC & 0xff);
  477. cx25840_write(client, 0x41d, VSC >> 8);
  478. /* VS_INTRLACE=1 VFILT=filter */
  479. cx25840_write(client, 0x41e, 0x8 | filter);
  480. break;
  481. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  482. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  483. case V4L2_BUF_TYPE_VBI_CAPTURE:
  484. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  485. default:
  486. return -EINVAL;
  487. }
  488. return 0;
  489. }
  490. /* ----------------------------------------------------------------------- */
  491. static struct v4l2_queryctrl cx25840_qctrl[] = {
  492. {
  493. .id = V4L2_CID_BRIGHTNESS,
  494. .type = V4L2_CTRL_TYPE_INTEGER,
  495. .name = "Brightness",
  496. .minimum = 0,
  497. .maximum = 255,
  498. .step = 1,
  499. .default_value = 128,
  500. .flags = 0,
  501. }, {
  502. .id = V4L2_CID_CONTRAST,
  503. .type = V4L2_CTRL_TYPE_INTEGER,
  504. .name = "Contrast",
  505. .minimum = 0,
  506. .maximum = 127,
  507. .step = 1,
  508. .default_value = 64,
  509. .flags = 0,
  510. }, {
  511. .id = V4L2_CID_SATURATION,
  512. .type = V4L2_CTRL_TYPE_INTEGER,
  513. .name = "Saturation",
  514. .minimum = 0,
  515. .maximum = 127,
  516. .step = 1,
  517. .default_value = 64,
  518. .flags = 0,
  519. }, {
  520. .id = V4L2_CID_HUE,
  521. .type = V4L2_CTRL_TYPE_INTEGER,
  522. .name = "Hue",
  523. .minimum = -128,
  524. .maximum = 127,
  525. .step = 1,
  526. .default_value = 0,
  527. .flags = 0,
  528. }, {
  529. .id = V4L2_CID_AUDIO_VOLUME,
  530. .type = V4L2_CTRL_TYPE_INTEGER,
  531. .name = "Volume",
  532. .minimum = 0,
  533. .maximum = 65535,
  534. .step = 65535/100,
  535. .default_value = 58880,
  536. .flags = 0,
  537. }, {
  538. .id = V4L2_CID_AUDIO_BALANCE,
  539. .type = V4L2_CTRL_TYPE_INTEGER,
  540. .name = "Balance",
  541. .minimum = 0,
  542. .maximum = 65535,
  543. .step = 65535/100,
  544. .default_value = 32768,
  545. .flags = 0,
  546. }, {
  547. .id = V4L2_CID_AUDIO_MUTE,
  548. .type = V4L2_CTRL_TYPE_BOOLEAN,
  549. .name = "Mute",
  550. .minimum = 0,
  551. .maximum = 1,
  552. .step = 1,
  553. .default_value = 1,
  554. .flags = 0,
  555. }, {
  556. .id = V4L2_CID_AUDIO_BASS,
  557. .type = V4L2_CTRL_TYPE_INTEGER,
  558. .name = "Bass",
  559. .minimum = 0,
  560. .maximum = 65535,
  561. .step = 65535/100,
  562. .default_value = 32768,
  563. }, {
  564. .id = V4L2_CID_AUDIO_TREBLE,
  565. .type = V4L2_CTRL_TYPE_INTEGER,
  566. .name = "Treble",
  567. .minimum = 0,
  568. .maximum = 65535,
  569. .step = 65535/100,
  570. .default_value = 32768,
  571. },
  572. };
  573. /* ----------------------------------------------------------------------- */
  574. static int cx25840_command(struct i2c_client *client, unsigned int cmd,
  575. void *arg)
  576. {
  577. struct cx25840_state *state = i2c_get_clientdata(client);
  578. struct v4l2_tuner *vt = arg;
  579. struct v4l2_routing *route = arg;
  580. switch (cmd) {
  581. #ifdef CONFIG_VIDEO_ADV_DEBUG
  582. /* ioctls to allow direct access to the
  583. * cx25840 registers for testing */
  584. case VIDIOC_INT_G_REGISTER:
  585. {
  586. struct v4l2_register *reg = arg;
  587. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  588. return -EINVAL;
  589. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  590. break;
  591. }
  592. case VIDIOC_INT_S_REGISTER:
  593. {
  594. struct v4l2_register *reg = arg;
  595. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  596. return -EINVAL;
  597. if (!capable(CAP_SYS_ADMIN))
  598. return -EPERM;
  599. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  600. break;
  601. }
  602. #endif
  603. case VIDIOC_INT_DECODE_VBI_LINE:
  604. return cx25840_vbi(client, cmd, arg);
  605. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  606. return cx25840_audio(client, cmd, arg);
  607. case VIDIOC_STREAMON:
  608. v4l_dbg(1, cx25840_debug, client, "enable output\n");
  609. cx25840_write(client, 0x115, 0x8c);
  610. cx25840_write(client, 0x116, 0x07);
  611. break;
  612. case VIDIOC_STREAMOFF:
  613. v4l_dbg(1, cx25840_debug, client, "disable output\n");
  614. cx25840_write(client, 0x115, 0x00);
  615. cx25840_write(client, 0x116, 0x00);
  616. break;
  617. case VIDIOC_LOG_STATUS:
  618. log_status(client);
  619. break;
  620. case VIDIOC_G_CTRL:
  621. return get_v4lctrl(client, (struct v4l2_control *)arg);
  622. case VIDIOC_S_CTRL:
  623. return set_v4lctrl(client, (struct v4l2_control *)arg);
  624. case VIDIOC_QUERYCTRL:
  625. {
  626. struct v4l2_queryctrl *qc = arg;
  627. int i;
  628. for (i = 0; i < ARRAY_SIZE(cx25840_qctrl); i++)
  629. if (qc->id && qc->id == cx25840_qctrl[i].id) {
  630. memcpy(qc, &cx25840_qctrl[i], sizeof(*qc));
  631. return 0;
  632. }
  633. return -EINVAL;
  634. }
  635. case VIDIOC_G_STD:
  636. *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
  637. break;
  638. case VIDIOC_S_STD:
  639. state->radio = 0;
  640. return set_v4lstd(client, *(v4l2_std_id *)arg);
  641. case AUDC_SET_RADIO:
  642. state->radio = 1;
  643. break;
  644. case VIDIOC_INT_G_VIDEO_ROUTING:
  645. route->input = state->vid_input;
  646. route->output = 0;
  647. break;
  648. case VIDIOC_INT_S_VIDEO_ROUTING:
  649. return set_input(client, route->input, state->aud_input);
  650. case VIDIOC_INT_G_AUDIO_ROUTING:
  651. route->input = state->aud_input;
  652. route->output = 0;
  653. break;
  654. case VIDIOC_INT_S_AUDIO_ROUTING:
  655. return set_input(client, state->vid_input, route->input);
  656. case VIDIOC_S_FREQUENCY:
  657. input_change(client);
  658. break;
  659. case VIDIOC_G_TUNER:
  660. {
  661. u8 mode = cx25840_read(client, 0x804);
  662. u8 vpres = cx25840_read(client, 0x80a) & 0x10;
  663. int val = 0;
  664. if (state->radio)
  665. break;
  666. vt->capability |=
  667. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  668. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  669. vt->signal = vpres ? 0xffff : 0x0;
  670. /* get rxsubchans and audmode */
  671. if ((mode & 0xf) == 1)
  672. val |= V4L2_TUNER_SUB_STEREO;
  673. else
  674. val |= V4L2_TUNER_SUB_MONO;
  675. if (mode == 2 || mode == 4)
  676. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  677. if (mode & 0x10)
  678. val |= V4L2_TUNER_SUB_SAP;
  679. vt->rxsubchans = val;
  680. vt->audmode = state->audmode;
  681. break;
  682. }
  683. case VIDIOC_S_TUNER:
  684. if (state->radio)
  685. break;
  686. switch (vt->audmode) {
  687. case V4L2_TUNER_MODE_MONO:
  688. /* mono -> mono
  689. stereo -> mono
  690. bilingual -> lang1 */
  691. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  692. break;
  693. case V4L2_TUNER_MODE_STEREO:
  694. case V4L2_TUNER_MODE_LANG1:
  695. /* mono -> mono
  696. stereo -> stereo
  697. bilingual -> lang1 */
  698. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  699. break;
  700. case V4L2_TUNER_MODE_LANG1_LANG2:
  701. /* mono -> mono
  702. stereo -> stereo
  703. bilingual -> lang1/lang2 */
  704. cx25840_and_or(client, 0x809, ~0xf, 0x07);
  705. break;
  706. case V4L2_TUNER_MODE_LANG2:
  707. /* mono -> mono
  708. stereo -> stereo
  709. bilingual -> lang2 */
  710. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  711. break;
  712. default:
  713. return -EINVAL;
  714. }
  715. state->audmode = vt->audmode;
  716. break;
  717. case VIDIOC_G_FMT:
  718. return get_v4lfmt(client, (struct v4l2_format *)arg);
  719. case VIDIOC_S_FMT:
  720. return set_v4lfmt(client, (struct v4l2_format *)arg);
  721. case VIDIOC_INT_RESET:
  722. cx25840_initialize(client, 0);
  723. break;
  724. case VIDIOC_INT_G_CHIP_IDENT:
  725. *(enum v4l2_chip_ident *)arg =
  726. V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
  727. break;
  728. default:
  729. return -EINVAL;
  730. }
  731. return 0;
  732. }
  733. /* ----------------------------------------------------------------------- */
  734. static struct i2c_driver i2c_driver_cx25840;
  735. static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
  736. int kind)
  737. {
  738. struct i2c_client *client;
  739. struct cx25840_state *state;
  740. u16 device_id;
  741. /* Check if the adapter supports the needed features
  742. * Not until kernel version 2.6.11 did the bit-algo
  743. * correctly report that it would do an I2C-level xfer */
  744. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  745. return 0;
  746. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  747. if (client == 0)
  748. return -ENOMEM;
  749. client->addr = address;
  750. client->adapter = adapter;
  751. client->driver = &i2c_driver_cx25840;
  752. snprintf(client->name, sizeof(client->name) - 1, "cx25840");
  753. v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", address << 1);
  754. device_id = cx25840_read(client, 0x101) << 8;
  755. device_id |= cx25840_read(client, 0x100);
  756. /* The high byte of the device ID should be
  757. * 0x84 if chip is present */
  758. if ((device_id & 0xff00) != 0x8400) {
  759. v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
  760. kfree(client);
  761. return 0;
  762. }
  763. v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
  764. (device_id & 0xfff0) >> 4,
  765. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
  766. address << 1, adapter->name);
  767. state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  768. if (state == NULL) {
  769. kfree(client);
  770. return -ENOMEM;
  771. }
  772. i2c_set_clientdata(client, state);
  773. memset(state, 0, sizeof(struct cx25840_state));
  774. state->vid_input = CX25840_COMPOSITE7;
  775. state->aud_input = CX25840_AUDIO8;
  776. state->audclk_freq = 48000;
  777. state->pvr150_workaround = 0;
  778. state->audmode = V4L2_TUNER_MODE_LANG1;
  779. cx25840_initialize(client, 1);
  780. i2c_attach_client(client);
  781. return 0;
  782. }
  783. static int cx25840_attach_adapter(struct i2c_adapter *adapter)
  784. {
  785. if (adapter->class & I2C_CLASS_TV_ANALOG)
  786. return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
  787. return 0;
  788. }
  789. static int cx25840_detach_client(struct i2c_client *client)
  790. {
  791. struct cx25840_state *state = i2c_get_clientdata(client);
  792. int err;
  793. err = i2c_detach_client(client);
  794. if (err) {
  795. return err;
  796. }
  797. kfree(state);
  798. kfree(client);
  799. return 0;
  800. }
  801. /* ----------------------------------------------------------------------- */
  802. static struct i2c_driver i2c_driver_cx25840 = {
  803. .driver = {
  804. .name = "cx25840",
  805. },
  806. .id = I2C_DRIVERID_CX25840,
  807. .attach_adapter = cx25840_attach_adapter,
  808. .detach_client = cx25840_detach_client,
  809. .command = cx25840_command,
  810. };
  811. static int __init m__init(void)
  812. {
  813. return i2c_add_driver(&i2c_driver_cx25840);
  814. }
  815. static void __exit m__exit(void)
  816. {
  817. i2c_del_driver(&i2c_driver_cx25840);
  818. }
  819. module_init(m__init);
  820. module_exit(m__exit);
  821. /* ----------------------------------------------------------------------- */
  822. static void log_status(struct i2c_client *client)
  823. {
  824. static const char *const fmt_strs[] = {
  825. "0x0",
  826. "NTSC-M", "NTSC-J", "NTSC-4.43",
  827. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  828. "0x9", "0xA", "0xB",
  829. "SECAM",
  830. "0xD", "0xE", "0xF"
  831. };
  832. struct cx25840_state *state = i2c_get_clientdata(client);
  833. u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
  834. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  835. u8 gen_stat1 = cx25840_read(client, 0x40d);
  836. u8 download_ctl = cx25840_read(client, 0x803);
  837. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  838. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  839. u8 audio_config = cx25840_read(client, 0x808);
  840. u8 pref_mode = cx25840_read(client, 0x809);
  841. u8 afc0 = cx25840_read(client, 0x80b);
  842. u8 mute_ctl = cx25840_read(client, 0x8d3);
  843. int vid_input = state->vid_input;
  844. int aud_input = state->aud_input;
  845. char *p;
  846. v4l_info(client, "Video signal: %spresent\n",
  847. (microctrl_vidfmt & 0x10) ? "" : "not ");
  848. v4l_info(client, "Detected format: %s\n",
  849. fmt_strs[gen_stat1 & 0xf]);
  850. switch (mod_det_stat0) {
  851. case 0x00: p = "mono"; break;
  852. case 0x01: p = "stereo"; break;
  853. case 0x02: p = "dual"; break;
  854. case 0x04: p = "tri"; break;
  855. case 0x10: p = "mono with SAP"; break;
  856. case 0x11: p = "stereo with SAP"; break;
  857. case 0x12: p = "dual with SAP"; break;
  858. case 0x14: p = "tri with SAP"; break;
  859. case 0xfe: p = "forced mode"; break;
  860. default: p = "not defined";
  861. }
  862. v4l_info(client, "Detected audio mode: %s\n", p);
  863. switch (mod_det_stat1) {
  864. case 0x00: p = "not defined"; break;
  865. case 0x01: p = "EIAJ"; break;
  866. case 0x02: p = "A2-M"; break;
  867. case 0x03: p = "A2-BG"; break;
  868. case 0x04: p = "A2-DK1"; break;
  869. case 0x05: p = "A2-DK2"; break;
  870. case 0x06: p = "A2-DK3"; break;
  871. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  872. case 0x08: p = "AM-L"; break;
  873. case 0x09: p = "NICAM-BG"; break;
  874. case 0x0a: p = "NICAM-DK"; break;
  875. case 0x0b: p = "NICAM-I"; break;
  876. case 0x0c: p = "NICAM-L"; break;
  877. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  878. case 0x0e: p = "IF FM Radio"; break;
  879. case 0x0f: p = "BTSC"; break;
  880. case 0x10: p = "high-deviation FM"; break;
  881. case 0x11: p = "very high-deviation FM"; break;
  882. case 0xfd: p = "unknown audio standard"; break;
  883. case 0xfe: p = "forced audio standard"; break;
  884. case 0xff: p = "no detected audio standard"; break;
  885. default: p = "not defined";
  886. }
  887. v4l_info(client, "Detected audio standard: %s\n", p);
  888. v4l_info(client, "Audio muted: %s\n",
  889. (mute_ctl & 0x2) ? "yes" : "no");
  890. v4l_info(client, "Audio microcontroller: %s\n",
  891. (download_ctl & 0x10) ? "running" : "stopped");
  892. switch (audio_config >> 4) {
  893. case 0x00: p = "undefined"; break;
  894. case 0x01: p = "BTSC"; break;
  895. case 0x02: p = "EIAJ"; break;
  896. case 0x03: p = "A2-M"; break;
  897. case 0x04: p = "A2-BG"; break;
  898. case 0x05: p = "A2-DK1"; break;
  899. case 0x06: p = "A2-DK2"; break;
  900. case 0x07: p = "A2-DK3"; break;
  901. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  902. case 0x09: p = "AM-L"; break;
  903. case 0x0a: p = "NICAM-BG"; break;
  904. case 0x0b: p = "NICAM-DK"; break;
  905. case 0x0c: p = "NICAM-I"; break;
  906. case 0x0d: p = "NICAM-L"; break;
  907. case 0x0e: p = "FM radio"; break;
  908. case 0x0f: p = "automatic detection"; break;
  909. default: p = "undefined";
  910. }
  911. v4l_info(client, "Configured audio standard: %s\n", p);
  912. if ((audio_config >> 4) < 0xF) {
  913. switch (audio_config & 0xF) {
  914. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  915. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  916. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  917. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  918. case 0x04: p = "STEREO"; break;
  919. case 0x05: p = "DUAL1 (AB)"; break;
  920. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  921. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  922. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  923. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  924. case 0x0a: p = "SAP"; break;
  925. default: p = "undefined";
  926. }
  927. v4l_info(client, "Configured audio mode: %s\n", p);
  928. } else {
  929. switch (audio_config & 0xF) {
  930. case 0x00: p = "BG"; break;
  931. case 0x01: p = "DK1"; break;
  932. case 0x02: p = "DK2"; break;
  933. case 0x03: p = "DK3"; break;
  934. case 0x04: p = "I"; break;
  935. case 0x05: p = "L"; break;
  936. case 0x06: p = "BTSC"; break;
  937. case 0x07: p = "EIAJ"; break;
  938. case 0x08: p = "A2-M"; break;
  939. case 0x09: p = "FM Radio"; break;
  940. case 0x0f: p = "automatic standard and mode detection"; break;
  941. default: p = "undefined";
  942. }
  943. v4l_info(client, "Configured audio system: %s\n", p);
  944. }
  945. v4l_info(client, "Specified standard: %s\n",
  946. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  947. if (vid_input >= CX25840_COMPOSITE1 &&
  948. vid_input <= CX25840_COMPOSITE8) {
  949. v4l_info(client, "Specified video input: Composite %d\n",
  950. vid_input - CX25840_COMPOSITE1 + 1);
  951. } else {
  952. v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  953. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  954. }
  955. if (aud_input) {
  956. v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
  957. } else {
  958. v4l_info(client, "Specified audio input: External\n");
  959. }
  960. v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
  961. switch (pref_mode & 0xf) {
  962. case 0: p = "mono/language A"; break;
  963. case 1: p = "language B"; break;
  964. case 2: p = "language C"; break;
  965. case 3: p = "analog fallback"; break;
  966. case 4: p = "stereo"; break;
  967. case 5: p = "language AC"; break;
  968. case 6: p = "language BC"; break;
  969. case 7: p = "language AB"; break;
  970. default: p = "undefined";
  971. }
  972. v4l_info(client, "Preferred audio mode: %s\n", p);
  973. if ((audio_config & 0xf) == 0xf) {
  974. switch ((afc0 >> 3) & 0x3) {
  975. case 0: p = "system DK"; break;
  976. case 1: p = "system L"; break;
  977. case 2: p = "autodetect"; break;
  978. default: p = "undefined";
  979. }
  980. v4l_info(client, "Selected 65 MHz format: %s\n", p);
  981. switch (afc0 & 0x7) {
  982. case 0: p = "chroma"; break;
  983. case 1: p = "BTSC"; break;
  984. case 2: p = "EIAJ"; break;
  985. case 3: p = "A2-M"; break;
  986. case 4: p = "autodetect"; break;
  987. default: p = "undefined";
  988. }
  989. v4l_info(client, "Selected 45 MHz format: %s\n", p);
  990. }
  991. }