cx25840-core.c 27 KB

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