cx25840-core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. client->flags = I2C_CLIENT_ALLOW_USE;
  643. snprintf(client->name, sizeof(client->name) - 1, "cx25840");
  644. cx25840_dbg("detecting cx25840 client on address 0x%x\n", address << 1);
  645. device_id = cx25840_read(client, 0x101) << 8;
  646. device_id |= cx25840_read(client, 0x100);
  647. /* The high byte of the device ID should be
  648. * 0x84 if chip is present */
  649. if ((device_id & 0xff00) != 0x8400) {
  650. cx25840_dbg("cx25840 not found\n");
  651. kfree(client);
  652. return 0;
  653. }
  654. cx25840_info("cx25%3x-2%x found @ 0x%x (%s)\n",
  655. (device_id & 0xfff0) >> 4,
  656. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
  657. address << 1, adapter->name);
  658. state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  659. if (state == NULL) {
  660. kfree(client);
  661. return -ENOMEM;
  662. }
  663. i2c_set_clientdata(client, state);
  664. memset(state, 0, sizeof(struct cx25840_state));
  665. state->input = CX25840_TUNER;
  666. state->audclk_freq = V4L2_AUDCLK_48_KHZ;
  667. state->audio_input = AUDIO_TUNER;
  668. state->cardtype = CARDTYPE_PVR150;
  669. cx25840_initialize(client, 1);
  670. i2c_attach_client(client);
  671. return 0;
  672. }
  673. static int cx25840_attach_adapter(struct i2c_adapter *adapter)
  674. {
  675. #ifdef I2C_CLASS_TV_ANALOG
  676. if (adapter->class & I2C_CLASS_TV_ANALOG)
  677. #else
  678. if (adapter->id == I2C_HW_B_BT848)
  679. #endif
  680. return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
  681. return 0;
  682. }
  683. static int cx25840_detach_client(struct i2c_client *client)
  684. {
  685. struct cx25840_state *state = i2c_get_clientdata(client);
  686. int err;
  687. err = i2c_detach_client(client);
  688. if (err) {
  689. return err;
  690. }
  691. kfree(state);
  692. kfree(client);
  693. return 0;
  694. }
  695. /* ----------------------------------------------------------------------- */
  696. static struct i2c_driver i2c_driver_cx25840 = {
  697. .name = "cx25840",
  698. .id = I2C_DRIVERID_CX25840,
  699. .flags = I2C_DF_NOTIFY,
  700. .attach_adapter = cx25840_attach_adapter,
  701. .detach_client = cx25840_detach_client,
  702. .command = cx25840_command,
  703. .owner = THIS_MODULE,
  704. };
  705. static int __init m__init(void)
  706. {
  707. return i2c_add_driver(&i2c_driver_cx25840);
  708. }
  709. static void __exit m__exit(void)
  710. {
  711. i2c_del_driver(&i2c_driver_cx25840);
  712. }
  713. module_init(m__init);
  714. module_exit(m__exit);
  715. /* ----------------------------------------------------------------------- */
  716. static void log_status(struct i2c_client *client)
  717. {
  718. static const char *const fmt_strs[] = {
  719. "0x0",
  720. "NTSC-M", "NTSC-J", "NTSC-4.43",
  721. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  722. "0x9", "0xA", "0xB",
  723. "SECAM",
  724. "0xD", "0xE", "0xF"
  725. };
  726. struct cx25840_state *state = i2c_get_clientdata(client);
  727. u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
  728. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  729. u8 gen_stat1 = cx25840_read(client, 0x40d);
  730. u8 download_ctl = cx25840_read(client, 0x803);
  731. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  732. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  733. u8 audio_config = cx25840_read(client, 0x808);
  734. u8 pref_mode = cx25840_read(client, 0x809);
  735. u8 afc0 = cx25840_read(client, 0x80b);
  736. u8 mute_ctl = cx25840_read(client, 0x8d3);
  737. char *p;
  738. cx25840_info("Video signal: %spresent\n",
  739. (microctrl_vidfmt & 0x10) ? "" : "not ");
  740. cx25840_info("Detected format: %s\n",
  741. fmt_strs[gen_stat1 & 0xf]);
  742. switch (mod_det_stat0) {
  743. case 0x00: p = "mono"; break;
  744. case 0x01: p = "stereo"; break;
  745. case 0x02: p = "dual"; break;
  746. case 0x04: p = "tri"; break;
  747. case 0x10: p = "mono with SAP"; break;
  748. case 0x11: p = "stereo with SAP"; break;
  749. case 0x12: p = "dual with SAP"; break;
  750. case 0x14: p = "tri with SAP"; break;
  751. case 0xfe: p = "forced mode"; break;
  752. default: p = "not defined";
  753. }
  754. cx25840_info("Detected audio mode: %s\n", p);
  755. switch (mod_det_stat1) {
  756. case 0x00: p = "not defined"; break;
  757. case 0x01: p = "EIAJ"; break;
  758. case 0x02: p = "A2-M"; break;
  759. case 0x03: p = "A2-BG"; break;
  760. case 0x04: p = "A2-DK1"; break;
  761. case 0x05: p = "A2-DK2"; break;
  762. case 0x06: p = "A2-DK3"; break;
  763. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  764. case 0x08: p = "AM-L"; break;
  765. case 0x09: p = "NICAM-BG"; break;
  766. case 0x0a: p = "NICAM-DK"; break;
  767. case 0x0b: p = "NICAM-I"; break;
  768. case 0x0c: p = "NICAM-L"; break;
  769. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  770. case 0x0e: p = "IF FM Radio"; break;
  771. case 0x0f: p = "BTSC"; break;
  772. case 0x10: p = "high-deviation FM"; break;
  773. case 0x11: p = "very high-deviation FM"; break;
  774. case 0xfd: p = "unknown audio standard"; break;
  775. case 0xfe: p = "forced audio standard"; break;
  776. case 0xff: p = "no detected audio standard"; break;
  777. default: p = "not defined";
  778. }
  779. cx25840_info("Detected audio standard: %s\n", p);
  780. cx25840_info("Audio muted: %s\n",
  781. (mute_ctl & 0x2) ? "yes" : "no");
  782. cx25840_info("Audio microcontroller: %s\n",
  783. (download_ctl & 0x10) ? "running" : "stopped");
  784. switch (audio_config >> 4) {
  785. case 0x00: p = "undefined"; break;
  786. case 0x01: p = "BTSC"; break;
  787. case 0x02: p = "EIAJ"; break;
  788. case 0x03: p = "A2-M"; break;
  789. case 0x04: p = "A2-BG"; break;
  790. case 0x05: p = "A2-DK1"; break;
  791. case 0x06: p = "A2-DK2"; break;
  792. case 0x07: p = "A2-DK3"; break;
  793. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  794. case 0x09: p = "AM-L"; break;
  795. case 0x0a: p = "NICAM-BG"; break;
  796. case 0x0b: p = "NICAM-DK"; break;
  797. case 0x0c: p = "NICAM-I"; break;
  798. case 0x0d: p = "NICAM-L"; break;
  799. case 0x0e: p = "FM radio"; break;
  800. case 0x0f: p = "automatic detection"; break;
  801. default: p = "undefined";
  802. }
  803. cx25840_info("Configured audio standard: %s\n", p);
  804. if ((audio_config >> 4) < 0xF) {
  805. switch (audio_config & 0xF) {
  806. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  807. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  808. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  809. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  810. case 0x04: p = "STEREO"; break;
  811. case 0x05: p = "DUAL1 (AB)"; break;
  812. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  813. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  814. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  815. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  816. case 0x0a: p = "SAP"; break;
  817. default: p = "undefined";
  818. }
  819. cx25840_info("Configured audio mode: %s\n", p);
  820. } else {
  821. switch (audio_config & 0xF) {
  822. case 0x00: p = "BG"; break;
  823. case 0x01: p = "DK1"; break;
  824. case 0x02: p = "DK2"; break;
  825. case 0x03: p = "DK3"; break;
  826. case 0x04: p = "I"; break;
  827. case 0x05: p = "L"; break;
  828. case 0x06: p = "BTSC"; break;
  829. case 0x07: p = "EIAJ"; break;
  830. case 0x08: p = "A2-M"; break;
  831. case 0x09: p = "FM Radio"; break;
  832. case 0x0f: p = "automatic standard and mode detection"; break;
  833. default: p = "undefined";
  834. }
  835. cx25840_info("Configured audio system: %s\n", p);
  836. }
  837. cx25840_info("Specified standard: %s\n",
  838. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  839. switch (state->input) {
  840. case CX25840_COMPOSITE0: p = "Composite 0"; break;
  841. case CX25840_COMPOSITE1: p = "Composite 1"; break;
  842. case CX25840_SVIDEO0: p = "S-Video 0"; break;
  843. case CX25840_SVIDEO1: p = "S-Video 1"; break;
  844. case CX25840_TUNER: p = "Tuner"; break;
  845. }
  846. cx25840_info("Specified input: %s\n", p);
  847. cx25840_info("Specified audio input: %s\n",
  848. state->audio_input == 0 ? "Tuner" : "External");
  849. switch (state->audclk_freq) {
  850. case V4L2_AUDCLK_441_KHZ: p = "44.1 kHz"; break;
  851. case V4L2_AUDCLK_48_KHZ: p = "48 kHz"; break;
  852. case V4L2_AUDCLK_32_KHZ: p = "32 kHz"; break;
  853. default: p = "undefined";
  854. }
  855. cx25840_info("Specified audioclock freq: %s\n", p);
  856. switch (pref_mode & 0xf) {
  857. case 0: p = "mono/language A"; break;
  858. case 1: p = "language B"; break;
  859. case 2: p = "language C"; break;
  860. case 3: p = "analog fallback"; break;
  861. case 4: p = "stereo"; break;
  862. case 5: p = "language AC"; break;
  863. case 6: p = "language BC"; break;
  864. case 7: p = "language AB"; break;
  865. default: p = "undefined";
  866. }
  867. cx25840_info("Preferred audio mode: %s\n", p);
  868. if ((audio_config & 0xf) == 0xf) {
  869. switch ((afc0 >> 3) & 0x3) {
  870. case 0: p = "system DK"; break;
  871. case 1: p = "system L"; break;
  872. case 2: p = "autodetect"; break;
  873. default: p = "undefined";
  874. }
  875. cx25840_info("Selected 65 MHz format: %s\n", p);
  876. switch (afc0 & 0x7) {
  877. case 0: p = "chroma"; break;
  878. case 1: p = "BTSC"; break;
  879. case 2: p = "EIAJ"; break;
  880. case 3: p = "A2-M"; break;
  881. case 4: p = "autodetect"; break;
  882. default: p = "undefined";
  883. }
  884. cx25840_info("Selected 45 MHz format: %s\n", p);
  885. }
  886. }