cx25840-core.c 26 KB

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