cx25840-core.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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/audiochip.h>
  33. #include <media/v4l2-common.h>
  34. #include "cx25840.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. int cx25840_debug = 0;
  40. module_param(cx25840_debug, bool, 0644);
  41. MODULE_PARM_DESC(cx25840_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 *, enum cx25840_input);
  95. static void input_change(struct i2c_client *);
  96. static void log_status(struct i2c_client *client);
  97. /* ----------------------------------------------------------------------- */
  98. static inline 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 inline 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_write(client, 0x401, 0x18);
  151. cx25840_write(client, 0x4a2, 0x10);
  152. cx25840_write(client, 0x402, 0x04);
  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 video input */
  166. set_input(client, state->input);
  167. /* (re)set audio input */
  168. cx25840_audio(client, AUDC_SET_INPUT, &state->audio_input);
  169. /* start microcontroller */
  170. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  171. }
  172. /* ----------------------------------------------------------------------- */
  173. static void input_change(struct i2c_client *client)
  174. {
  175. v4l2_std_id std = cx25840_get_v4lstd(client);
  176. if (std & V4L2_STD_PAL) {
  177. /* Follow tuner change procedure for PAL */
  178. cx25840_write(client, 0x808, 0xff);
  179. cx25840_write(client, 0x80b, 0x10);
  180. } else if (std & V4L2_STD_SECAM) {
  181. /* Select autodetect for SECAM */
  182. cx25840_write(client, 0x808, 0xff);
  183. cx25840_write(client, 0x80b, 0x10);
  184. } else if (std & V4L2_STD_NTSC) {
  185. /* NTSC */
  186. cx25840_write(client, 0x808, 0xf6);
  187. cx25840_write(client, 0x80b, 0x00);
  188. }
  189. if (cx25840_read(client, 0x803) & 0x10) {
  190. /* restart audio decoder microcontroller */
  191. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  192. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  193. }
  194. }
  195. static int set_input(struct i2c_client *client, enum cx25840_input input)
  196. {
  197. struct cx25840_state *state = i2c_get_clientdata(client);
  198. cx25840_dbg("decoder set input (%d)\n", input);
  199. switch (input) {
  200. case CX25840_TUNER:
  201. cx25840_dbg("now setting Tuner input\n");
  202. if (state->cardtype == CARDTYPE_PVR150) {
  203. /* CH_SEL_ADC2=1 */
  204. cx25840_and_or(client, 0x102, ~0x2, 0x02);
  205. }
  206. /* Video Input Control */
  207. if (state->cardtype == CARDTYPE_PG600) {
  208. cx25840_write(client, 0x103, 0x11);
  209. } else {
  210. cx25840_write(client, 0x103, 0x46);
  211. }
  212. /* INPUT_MODE=0 */
  213. cx25840_and_or(client, 0x401, ~0x6, 0x00);
  214. break;
  215. case CX25840_COMPOSITE0:
  216. case CX25840_COMPOSITE1:
  217. cx25840_dbg("now setting Composite input\n");
  218. /* Video Input Control */
  219. if (state->cardtype == CARDTYPE_PG600) {
  220. cx25840_write(client, 0x103, 0x00);
  221. } else {
  222. cx25840_write(client, 0x103, 0x02);
  223. }
  224. /* INPUT_MODE=0 */
  225. cx25840_and_or(client, 0x401, ~0x6, 0x00);
  226. break;
  227. case CX25840_SVIDEO0:
  228. case CX25840_SVIDEO1:
  229. cx25840_dbg("now setting S-Video input\n");
  230. /* CH_SEL_ADC2=0 */
  231. cx25840_and_or(client, 0x102, ~0x2, 0x00);
  232. /* Video Input Control */
  233. if (state->cardtype == CARDTYPE_PG600) {
  234. cx25840_write(client, 0x103, 0x02);
  235. } else {
  236. cx25840_write(client, 0x103, 0x10);
  237. }
  238. /* INPUT_MODE=1 */
  239. cx25840_and_or(client, 0x401, ~0x6, 0x02);
  240. break;
  241. default:
  242. cx25840_err("%d is not a valid input!\n", input);
  243. return -EINVAL;
  244. }
  245. state->input = input;
  246. input_change(client);
  247. return 0;
  248. }
  249. /* ----------------------------------------------------------------------- */
  250. static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
  251. {
  252. u8 fmt;
  253. switch (std) {
  254. /* zero is autodetect */
  255. case 0: fmt = 0x0; break;
  256. /* default ntsc to ntsc-m */
  257. case V4L2_STD_NTSC:
  258. case V4L2_STD_NTSC_M: fmt = 0x1; break;
  259. case V4L2_STD_NTSC_M_JP: fmt = 0x2; break;
  260. case V4L2_STD_NTSC_443: fmt = 0x3; break;
  261. case V4L2_STD_PAL: fmt = 0x4; break;
  262. case V4L2_STD_PAL_M: fmt = 0x5; break;
  263. case V4L2_STD_PAL_N: fmt = 0x6; break;
  264. case V4L2_STD_PAL_Nc: fmt = 0x7; break;
  265. case V4L2_STD_PAL_60: fmt = 0x8; break;
  266. case V4L2_STD_SECAM: fmt = 0xc; break;
  267. default:
  268. return -ERANGE;
  269. }
  270. cx25840_and_or(client, 0x400, ~0xf, fmt);
  271. cx25840_vbi_setup(client);
  272. return 0;
  273. }
  274. v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
  275. {
  276. /* check VID_FMT_SEL first */
  277. u8 fmt = cx25840_read(client, 0x400) & 0xf;
  278. if (!fmt) {
  279. /* check AFD_FMT_STAT if set to autodetect */
  280. fmt = cx25840_read(client, 0x40d) & 0xf;
  281. }
  282. switch (fmt) {
  283. case 0x1: return V4L2_STD_NTSC_M;
  284. case 0x2: return V4L2_STD_NTSC_M_JP;
  285. case 0x3: return V4L2_STD_NTSC_443;
  286. case 0x4: return V4L2_STD_PAL;
  287. case 0x5: return V4L2_STD_PAL_M;
  288. case 0x6: return V4L2_STD_PAL_N;
  289. case 0x7: return V4L2_STD_PAL_Nc;
  290. case 0x8: return V4L2_STD_PAL_60;
  291. case 0xc: return V4L2_STD_SECAM;
  292. default: return V4L2_STD_UNKNOWN;
  293. }
  294. }
  295. /* ----------------------------------------------------------------------- */
  296. static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  297. {
  298. struct cx25840_state *state = i2c_get_clientdata(client);
  299. switch (ctrl->id) {
  300. case CX25840_CID_CARDTYPE:
  301. switch (ctrl->value) {
  302. case CARDTYPE_PVR150:
  303. case CARDTYPE_PG600:
  304. state->cardtype = ctrl->value;
  305. break;
  306. default:
  307. return -ERANGE;
  308. }
  309. set_input(client, state->input);
  310. break;
  311. case V4L2_CID_BRIGHTNESS:
  312. if (ctrl->value < 0 || ctrl->value > 255) {
  313. cx25840_err("invalid brightness setting %d\n",
  314. ctrl->value);
  315. return -ERANGE;
  316. }
  317. cx25840_write(client, 0x414, ctrl->value - 128);
  318. break;
  319. case V4L2_CID_CONTRAST:
  320. if (ctrl->value < 0 || ctrl->value > 127) {
  321. cx25840_err("invalid contrast setting %d\n",
  322. ctrl->value);
  323. return -ERANGE;
  324. }
  325. cx25840_write(client, 0x415, ctrl->value << 1);
  326. break;
  327. case V4L2_CID_SATURATION:
  328. if (ctrl->value < 0 || ctrl->value > 127) {
  329. cx25840_err("invalid saturation setting %d\n",
  330. ctrl->value);
  331. return -ERANGE;
  332. }
  333. cx25840_write(client, 0x420, ctrl->value << 1);
  334. cx25840_write(client, 0x421, ctrl->value << 1);
  335. break;
  336. case V4L2_CID_HUE:
  337. if (ctrl->value < -127 || ctrl->value > 127) {
  338. cx25840_err("invalid hue setting %d\n", ctrl->value);
  339. return -ERANGE;
  340. }
  341. cx25840_write(client, 0x422, ctrl->value);
  342. break;
  343. case V4L2_CID_AUDIO_VOLUME:
  344. case V4L2_CID_AUDIO_BASS:
  345. case V4L2_CID_AUDIO_TREBLE:
  346. case V4L2_CID_AUDIO_BALANCE:
  347. case V4L2_CID_AUDIO_MUTE:
  348. return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
  349. }
  350. return 0;
  351. }
  352. static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  353. {
  354. struct cx25840_state *state = i2c_get_clientdata(client);
  355. switch (ctrl->id) {
  356. case CX25840_CID_CARDTYPE:
  357. ctrl->value = state->cardtype;
  358. break;
  359. case V4L2_CID_BRIGHTNESS:
  360. ctrl->value = cx25840_read(client, 0x414) + 128;
  361. break;
  362. case V4L2_CID_CONTRAST:
  363. ctrl->value = cx25840_read(client, 0x415) >> 1;
  364. break;
  365. case V4L2_CID_SATURATION:
  366. ctrl->value = cx25840_read(client, 0x420) >> 1;
  367. break;
  368. case V4L2_CID_HUE:
  369. ctrl->value = cx25840_read(client, 0x422);
  370. break;
  371. case V4L2_CID_AUDIO_VOLUME:
  372. case V4L2_CID_AUDIO_BASS:
  373. case V4L2_CID_AUDIO_TREBLE:
  374. case V4L2_CID_AUDIO_BALANCE:
  375. case V4L2_CID_AUDIO_MUTE:
  376. return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
  377. default:
  378. return -EINVAL;
  379. }
  380. return 0;
  381. }
  382. /* ----------------------------------------------------------------------- */
  383. static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  384. {
  385. switch (fmt->type) {
  386. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  387. return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
  388. default:
  389. return -EINVAL;
  390. }
  391. return 0;
  392. }
  393. static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  394. {
  395. struct v4l2_pix_format *pix;
  396. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  397. int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
  398. switch (fmt->type) {
  399. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  400. pix = &(fmt->fmt.pix);
  401. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  402. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  403. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  404. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  405. Vlines = pix->height + (is_pal ? 4 : 7);
  406. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  407. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  408. cx25840_err("%dx%d is not a valid size!\n",
  409. pix->width, pix->height);
  410. return -ERANGE;
  411. }
  412. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  413. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  414. VSC &= 0x1fff;
  415. if (pix->width >= 385)
  416. filter = 0;
  417. else if (pix->width > 192)
  418. filter = 1;
  419. else if (pix->width > 96)
  420. filter = 2;
  421. else
  422. filter = 3;
  423. cx25840_dbg("decoder set size %dx%d -> scale %ux%u\n",
  424. pix->width, pix->height, HSC, VSC);
  425. /* HSCALE=HSC */
  426. cx25840_write(client, 0x418, HSC & 0xff);
  427. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  428. cx25840_write(client, 0x41a, HSC >> 16);
  429. /* VSCALE=VSC */
  430. cx25840_write(client, 0x41c, VSC & 0xff);
  431. cx25840_write(client, 0x41d, VSC >> 8);
  432. /* VS_INTRLACE=1 VFILT=filter */
  433. cx25840_write(client, 0x41e, 0x8 | filter);
  434. break;
  435. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  436. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  437. case V4L2_BUF_TYPE_VBI_CAPTURE:
  438. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  439. default:
  440. return -EINVAL;
  441. }
  442. return 0;
  443. }
  444. /* ----------------------------------------------------------------------- */
  445. static int cx25840_command(struct i2c_client *client, unsigned int cmd,
  446. void *arg)
  447. {
  448. struct cx25840_state *state = i2c_get_clientdata(client);
  449. struct v4l2_tuner *vt = arg;
  450. int result = 0;
  451. switch (cmd) {
  452. case 0:
  453. break;
  454. #ifdef CONFIG_VIDEO_ADV_DEBUG
  455. /* ioctls to allow direct access to the
  456. * cx25840 registers for testing */
  457. case VIDIOC_INT_G_REGISTER:
  458. {
  459. struct v4l2_register *reg = arg;
  460. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  461. return -EINVAL;
  462. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  463. break;
  464. }
  465. case VIDIOC_INT_S_REGISTER:
  466. {
  467. struct v4l2_register *reg = arg;
  468. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  469. return -EINVAL;
  470. if (!capable(CAP_SYS_ADMIN))
  471. return -EPERM;
  472. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  473. break;
  474. }
  475. #endif
  476. case VIDIOC_INT_DECODE_VBI_LINE:
  477. return cx25840_vbi(client, cmd, arg);
  478. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  479. case AUDC_SET_INPUT:
  480. result = cx25840_audio(client, cmd, arg);
  481. break;
  482. case VIDIOC_STREAMON:
  483. cx25840_dbg("enable output\n");
  484. cx25840_write(client, 0x115, 0x8c);
  485. cx25840_write(client, 0x116, 0x07);
  486. break;
  487. case VIDIOC_STREAMOFF:
  488. cx25840_dbg("disable output\n");
  489. cx25840_write(client, 0x115, 0x00);
  490. cx25840_write(client, 0x116, 0x00);
  491. break;
  492. case VIDIOC_LOG_STATUS:
  493. log_status(client);
  494. break;
  495. case VIDIOC_G_CTRL:
  496. result = get_v4lctrl(client, (struct v4l2_control *)arg);
  497. break;
  498. case VIDIOC_S_CTRL:
  499. result = set_v4lctrl(client, (struct v4l2_control *)arg);
  500. break;
  501. case VIDIOC_G_STD:
  502. *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
  503. break;
  504. case VIDIOC_S_STD:
  505. result = set_v4lstd(client, *(v4l2_std_id *)arg);
  506. break;
  507. case VIDIOC_G_INPUT:
  508. *(int *)arg = state->input;
  509. break;
  510. case VIDIOC_S_INPUT:
  511. result = set_input(client, *(int *)arg);
  512. break;
  513. case VIDIOC_S_FREQUENCY:
  514. input_change(client);
  515. break;
  516. case VIDIOC_G_TUNER:
  517. {
  518. u8 mode = cx25840_read(client, 0x804);
  519. u8 pref = cx25840_read(client, 0x809) & 0xf;
  520. u8 vpres = cx25840_read(client, 0x80a) & 0x10;
  521. int val = 0;
  522. vt->capability |=
  523. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  524. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  525. vt->signal = vpres ? 0xffff : 0x0;
  526. /* get rxsubchans and audmode */
  527. if ((mode & 0xf) == 1)
  528. val |= V4L2_TUNER_SUB_STEREO;
  529. else
  530. val |= V4L2_TUNER_SUB_MONO;
  531. if (mode == 2 || mode == 4)
  532. val |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  533. if (mode & 0x10)
  534. val |= V4L2_TUNER_SUB_SAP;
  535. vt->rxsubchans = val;
  536. switch (pref) {
  537. case 0:
  538. vt->audmode = V4L2_TUNER_MODE_MONO;
  539. break;
  540. case 1:
  541. case 2:
  542. vt->audmode = V4L2_TUNER_MODE_LANG2;
  543. break;
  544. case 4:
  545. default:
  546. vt->audmode = V4L2_TUNER_MODE_STEREO;
  547. }
  548. break;
  549. }
  550. case VIDIOC_S_TUNER:
  551. switch (vt->audmode) {
  552. case V4L2_TUNER_MODE_MONO:
  553. case V4L2_TUNER_MODE_LANG1:
  554. /* Force PREF_MODE to MONO */
  555. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  556. break;
  557. case V4L2_TUNER_MODE_STEREO:
  558. /* Force PREF_MODE to STEREO */
  559. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  560. break;
  561. case V4L2_TUNER_MODE_LANG2:
  562. /* Force PREF_MODE to LANG2 */
  563. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  564. break;
  565. }
  566. break;
  567. case VIDIOC_G_FMT:
  568. result = get_v4lfmt(client, (struct v4l2_format *)arg);
  569. break;
  570. case VIDIOC_S_FMT:
  571. result = set_v4lfmt(client, (struct v4l2_format *)arg);
  572. break;
  573. case VIDIOC_INT_RESET:
  574. cx25840_initialize(client, 0);
  575. break;
  576. case VIDIOC_INT_G_CHIP_IDENT:
  577. *(enum v4l2_chip_ident *)arg =
  578. V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
  579. break;
  580. default:
  581. cx25840_err("invalid ioctl %x\n", cmd);
  582. return -EINVAL;
  583. }
  584. return result;
  585. }
  586. /* ----------------------------------------------------------------------- */
  587. static struct i2c_driver i2c_driver_cx25840;
  588. static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
  589. int kind)
  590. {
  591. struct i2c_client *client;
  592. struct cx25840_state *state;
  593. u16 device_id;
  594. /* Check if the adapter supports the needed features
  595. * Not until kernel version 2.6.11 did the bit-algo
  596. * correctly report that it would do an I2C-level xfer */
  597. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  598. return 0;
  599. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  600. if (client == 0)
  601. return -ENOMEM;
  602. memset(client, 0, sizeof(struct i2c_client));
  603. client->addr = address;
  604. client->adapter = adapter;
  605. client->driver = &i2c_driver_cx25840;
  606. client->flags = I2C_CLIENT_ALLOW_USE;
  607. snprintf(client->name, sizeof(client->name) - 1, "cx25840");
  608. cx25840_dbg("detecting cx25840 client on address 0x%x\n", address << 1);
  609. device_id = cx25840_read(client, 0x101) << 8;
  610. device_id |= cx25840_read(client, 0x100);
  611. /* The high byte of the device ID should be
  612. * 0x84 if chip is present */
  613. if ((device_id & 0xff00) != 0x8400) {
  614. cx25840_dbg("cx25840 not found\n");
  615. kfree(client);
  616. return 0;
  617. }
  618. cx25840_info("cx25%3x-2%x found @ 0x%x (%s)\n",
  619. (device_id & 0xfff0) >> 4,
  620. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
  621. address << 1, adapter->name);
  622. state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  623. if (state == NULL) {
  624. kfree(client);
  625. return -ENOMEM;
  626. }
  627. i2c_set_clientdata(client, state);
  628. memset(state, 0, sizeof(struct cx25840_state));
  629. state->input = CX25840_TUNER;
  630. state->audclk_freq = V4L2_AUDCLK_48_KHZ;
  631. state->audio_input = AUDIO_TUNER;
  632. state->cardtype = CARDTYPE_PVR150;
  633. cx25840_initialize(client, 1);
  634. i2c_attach_client(client);
  635. return 0;
  636. }
  637. static int cx25840_attach_adapter(struct i2c_adapter *adapter)
  638. {
  639. #ifdef I2C_CLASS_TV_ANALOG
  640. if (adapter->class & I2C_CLASS_TV_ANALOG)
  641. #else
  642. if (adapter->id == I2C_HW_B_BT848)
  643. #endif
  644. return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
  645. return 0;
  646. }
  647. static int cx25840_detach_client(struct i2c_client *client)
  648. {
  649. struct cx25840_state *state = i2c_get_clientdata(client);
  650. int err;
  651. err = i2c_detach_client(client);
  652. if (err) {
  653. return err;
  654. }
  655. kfree(state);
  656. kfree(client);
  657. return 0;
  658. }
  659. /* ----------------------------------------------------------------------- */
  660. static struct i2c_driver i2c_driver_cx25840 = {
  661. .name = "cx25840",
  662. .id = I2C_DRIVERID_CX25840,
  663. .flags = I2C_DF_NOTIFY,
  664. .attach_adapter = cx25840_attach_adapter,
  665. .detach_client = cx25840_detach_client,
  666. .command = cx25840_command,
  667. .owner = THIS_MODULE,
  668. };
  669. static int __init m__init(void)
  670. {
  671. return i2c_add_driver(&i2c_driver_cx25840);
  672. }
  673. static void __exit m__exit(void)
  674. {
  675. i2c_del_driver(&i2c_driver_cx25840);
  676. }
  677. module_init(m__init);
  678. module_exit(m__exit);
  679. /* ----------------------------------------------------------------------- */
  680. static void log_status(struct i2c_client *client)
  681. {
  682. static const char *const fmt_strs[] = {
  683. "0x0",
  684. "NTSC-M", "NTSC-J", "NTSC-4.43",
  685. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  686. "0x9", "0xA", "0xB",
  687. "SECAM",
  688. "0xD", "0xE", "0xF"
  689. };
  690. struct cx25840_state *state = i2c_get_clientdata(client);
  691. u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
  692. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  693. u8 gen_stat1 = cx25840_read(client, 0x40d);
  694. u8 download_ctl = cx25840_read(client, 0x803);
  695. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  696. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  697. u8 audio_config = cx25840_read(client, 0x808);
  698. u8 pref_mode = cx25840_read(client, 0x809);
  699. u8 afc0 = cx25840_read(client, 0x80b);
  700. u8 mute_ctl = cx25840_read(client, 0x8d3);
  701. char *p;
  702. cx25840_info("Video signal: %spresent\n",
  703. (microctrl_vidfmt & 0x10) ? "" : "not ");
  704. cx25840_info("Detected format: %s\n",
  705. fmt_strs[gen_stat1 & 0xf]);
  706. switch (mod_det_stat0) {
  707. case 0x00: p = "mono"; break;
  708. case 0x01: p = "stereo"; break;
  709. case 0x02: p = "dual"; break;
  710. case 0x04: p = "tri"; break;
  711. case 0x10: p = "mono with SAP"; break;
  712. case 0x11: p = "stereo with SAP"; break;
  713. case 0x12: p = "dual with SAP"; break;
  714. case 0x14: p = "tri with SAP"; break;
  715. case 0xfe: p = "forced mode"; break;
  716. default: p = "not defined";
  717. }
  718. cx25840_info("Detected audio mode: %s\n", p);
  719. switch (mod_det_stat1) {
  720. case 0x00: p = "not defined"; break;
  721. case 0x01: p = "EIAJ"; break;
  722. case 0x02: p = "A2-M"; break;
  723. case 0x03: p = "A2-BG"; break;
  724. case 0x04: p = "A2-DK1"; break;
  725. case 0x05: p = "A2-DK2"; break;
  726. case 0x06: p = "A2-DK3"; break;
  727. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  728. case 0x08: p = "AM-L"; break;
  729. case 0x09: p = "NICAM-BG"; break;
  730. case 0x0a: p = "NICAM-DK"; break;
  731. case 0x0b: p = "NICAM-I"; break;
  732. case 0x0c: p = "NICAM-L"; break;
  733. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  734. case 0x0e: p = "IF FM Radio"; break;
  735. case 0x0f: p = "BTSC"; break;
  736. case 0x10: p = "high-deviation FM"; break;
  737. case 0x11: p = "very high-deviation FM"; break;
  738. case 0xfd: p = "unknown audio standard"; break;
  739. case 0xfe: p = "forced audio standard"; break;
  740. case 0xff: p = "no detected audio standard"; break;
  741. default: p = "not defined";
  742. }
  743. cx25840_info("Detected audio standard: %s\n", p);
  744. cx25840_info("Audio muted: %s\n",
  745. (mute_ctl & 0x2) ? "yes" : "no");
  746. cx25840_info("Audio microcontroller: %s\n",
  747. (download_ctl & 0x10) ? "running" : "stopped");
  748. switch (audio_config >> 4) {
  749. case 0x00: p = "undefined"; break;
  750. case 0x01: p = "BTSC"; break;
  751. case 0x02: p = "EIAJ"; break;
  752. case 0x03: p = "A2-M"; break;
  753. case 0x04: p = "A2-BG"; break;
  754. case 0x05: p = "A2-DK1"; break;
  755. case 0x06: p = "A2-DK2"; break;
  756. case 0x07: p = "A2-DK3"; break;
  757. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  758. case 0x09: p = "AM-L"; break;
  759. case 0x0a: p = "NICAM-BG"; break;
  760. case 0x0b: p = "NICAM-DK"; break;
  761. case 0x0c: p = "NICAM-I"; break;
  762. case 0x0d: p = "NICAM-L"; break;
  763. case 0x0e: p = "FM radio"; break;
  764. case 0x0f: p = "automatic detection"; break;
  765. default: p = "undefined";
  766. }
  767. cx25840_info("Configured audio standard: %s\n", p);
  768. if ((audio_config >> 4) < 0xF) {
  769. switch (audio_config & 0xF) {
  770. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  771. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  772. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  773. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  774. case 0x04: p = "STEREO"; break;
  775. case 0x05: p = "DUAL1 (AB)"; break;
  776. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  777. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  778. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  779. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  780. case 0x0a: p = "SAP"; break;
  781. default: p = "undefined";
  782. }
  783. cx25840_info("Configured audio mode: %s\n", p);
  784. } else {
  785. switch (audio_config & 0xF) {
  786. case 0x00: p = "BG"; break;
  787. case 0x01: p = "DK1"; break;
  788. case 0x02: p = "DK2"; break;
  789. case 0x03: p = "DK3"; break;
  790. case 0x04: p = "I"; break;
  791. case 0x05: p = "L"; break;
  792. case 0x06: p = "BTSC"; break;
  793. case 0x07: p = "EIAJ"; break;
  794. case 0x08: p = "A2-M"; break;
  795. case 0x09: p = "FM Radio"; break;
  796. case 0x0f: p = "automatic standard and mode detection"; break;
  797. default: p = "undefined";
  798. }
  799. cx25840_info("Configured audio system: %s\n", p);
  800. }
  801. cx25840_info("Specified standard: %s\n",
  802. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  803. switch (state->input) {
  804. case CX25840_COMPOSITE0: p = "Composite 0"; break;
  805. case CX25840_COMPOSITE1: p = "Composite 1"; break;
  806. case CX25840_SVIDEO0: p = "S-Video 0"; break;
  807. case CX25840_SVIDEO1: p = "S-Video 1"; break;
  808. case CX25840_TUNER: p = "Tuner"; break;
  809. }
  810. cx25840_info("Specified input: %s\n", p);
  811. cx25840_info("Specified audio input: %s\n",
  812. state->audio_input == 0 ? "Tuner" : "External");
  813. switch (state->audclk_freq) {
  814. case V4L2_AUDCLK_441_KHZ: p = "44.1 kHz"; break;
  815. case V4L2_AUDCLK_48_KHZ: p = "48 kHz"; break;
  816. case V4L2_AUDCLK_32_KHZ: p = "32 kHz"; break;
  817. default: p = "undefined";
  818. }
  819. cx25840_info("Specified audioclock freq: %s\n", p);
  820. switch (pref_mode & 0xf) {
  821. case 0: p = "mono/language A"; break;
  822. case 1: p = "language B"; break;
  823. case 2: p = "language C"; break;
  824. case 3: p = "analog fallback"; break;
  825. case 4: p = "stereo"; break;
  826. case 5: p = "language AC"; break;
  827. case 6: p = "language BC"; break;
  828. case 7: p = "language AB"; break;
  829. default: p = "undefined";
  830. }
  831. cx25840_info("Preferred audio mode: %s\n", p);
  832. if ((audio_config & 0xf) == 0xf) {
  833. switch ((afc0 >> 3) & 0x3) {
  834. case 0: p = "system DK"; break;
  835. case 1: p = "system L"; break;
  836. case 2: p = "autodetect"; break;
  837. default: p = "undefined";
  838. }
  839. cx25840_info("Selected 65 MHz format: %s\n", p);
  840. switch (afc0 & 0x7) {
  841. case 0: p = "chroma"; break;
  842. case 1: p = "BTSC"; break;
  843. case 2: p = "EIAJ"; break;
  844. case 3: p = "A2-M"; break;
  845. case 4: p = "autodetect"; break;
  846. default: p = "undefined";
  847. }
  848. cx25840_info("Selected 45 MHz format: %s\n", p);
  849. }
  850. }