cx25840-core.c 27 KB

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