cx25840-core.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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. * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca>
  14. * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>.
  15. *
  16. * CX23885 support by Steven Toth <stoth@hauppauge.com>.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version 2
  21. * of the License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <linux/videodev2.h>
  36. #include <linux/i2c.h>
  37. #include <linux/delay.h>
  38. #include <media/v4l2-common.h>
  39. #include <media/v4l2-chip-ident.h>
  40. #include <media/v4l2-i2c-drv-legacy.h>
  41. #include <media/cx25840.h>
  42. #include "cx25840-core.h"
  43. MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
  44. MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
  45. MODULE_LICENSE("GPL");
  46. static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
  47. int cx25840_debug;
  48. module_param_named(debug,cx25840_debug, int, 0644);
  49. MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
  50. I2C_CLIENT_INSMOD;
  51. /* ----------------------------------------------------------------------- */
  52. int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
  53. {
  54. u8 buffer[3];
  55. buffer[0] = addr >> 8;
  56. buffer[1] = addr & 0xff;
  57. buffer[2] = value;
  58. return i2c_master_send(client, buffer, 3);
  59. }
  60. int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
  61. {
  62. u8 buffer[6];
  63. buffer[0] = addr >> 8;
  64. buffer[1] = addr & 0xff;
  65. buffer[2] = value & 0xff;
  66. buffer[3] = (value >> 8) & 0xff;
  67. buffer[4] = (value >> 16) & 0xff;
  68. buffer[5] = value >> 24;
  69. return i2c_master_send(client, buffer, 6);
  70. }
  71. u8 cx25840_read(struct i2c_client * client, u16 addr)
  72. {
  73. u8 buffer[2];
  74. buffer[0] = addr >> 8;
  75. buffer[1] = addr & 0xff;
  76. if (i2c_master_send(client, buffer, 2) < 2)
  77. return 0;
  78. if (i2c_master_recv(client, buffer, 1) < 1)
  79. return 0;
  80. return buffer[0];
  81. }
  82. u32 cx25840_read4(struct i2c_client * client, u16 addr)
  83. {
  84. u8 buffer[4];
  85. buffer[0] = addr >> 8;
  86. buffer[1] = addr & 0xff;
  87. if (i2c_master_send(client, buffer, 2) < 2)
  88. return 0;
  89. if (i2c_master_recv(client, buffer, 4) < 4)
  90. return 0;
  91. return (buffer[3] << 24) | (buffer[2] << 16) |
  92. (buffer[1] << 8) | buffer[0];
  93. }
  94. int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
  95. u8 or_value)
  96. {
  97. return cx25840_write(client, addr,
  98. (cx25840_read(client, addr) & and_mask) |
  99. or_value);
  100. }
  101. /* ----------------------------------------------------------------------- */
  102. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  103. enum cx25840_audio_input aud_input);
  104. /* ----------------------------------------------------------------------- */
  105. static void init_dll1(struct i2c_client *client)
  106. {
  107. /* This is the Hauppauge sequence used to
  108. * initialize the Delay Lock Loop 1 (ADC DLL). */
  109. cx25840_write(client, 0x159, 0x23);
  110. cx25840_write(client, 0x15a, 0x87);
  111. cx25840_write(client, 0x15b, 0x06);
  112. udelay(10);
  113. cx25840_write(client, 0x159, 0xe1);
  114. udelay(10);
  115. cx25840_write(client, 0x15a, 0x86);
  116. cx25840_write(client, 0x159, 0xe0);
  117. cx25840_write(client, 0x159, 0xe1);
  118. cx25840_write(client, 0x15b, 0x10);
  119. }
  120. static void init_dll2(struct i2c_client *client)
  121. {
  122. /* This is the Hauppauge sequence used to
  123. * initialize the Delay Lock Loop 2 (ADC DLL). */
  124. cx25840_write(client, 0x15d, 0xe3);
  125. cx25840_write(client, 0x15e, 0x86);
  126. cx25840_write(client, 0x15f, 0x06);
  127. udelay(10);
  128. cx25840_write(client, 0x15d, 0xe1);
  129. cx25840_write(client, 0x15d, 0xe0);
  130. cx25840_write(client, 0x15d, 0xe1);
  131. }
  132. static void cx25836_initialize(struct i2c_client *client)
  133. {
  134. /* reset configuration is described on page 3-77 of the CX25836 datasheet */
  135. /* 2. */
  136. cx25840_and_or(client, 0x000, ~0x01, 0x01);
  137. cx25840_and_or(client, 0x000, ~0x01, 0x00);
  138. /* 3a. */
  139. cx25840_and_or(client, 0x15a, ~0x70, 0x00);
  140. /* 3b. */
  141. cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
  142. /* 3c. */
  143. cx25840_and_or(client, 0x159, ~0x02, 0x02);
  144. /* 3d. */
  145. udelay(10);
  146. /* 3e. */
  147. cx25840_and_or(client, 0x159, ~0x02, 0x00);
  148. /* 3f. */
  149. cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
  150. /* 3g. */
  151. cx25840_and_or(client, 0x159, ~0x01, 0x00);
  152. cx25840_and_or(client, 0x159, ~0x01, 0x01);
  153. /* 3h. */
  154. cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
  155. }
  156. static void cx25840_work_handler(struct work_struct *work)
  157. {
  158. struct cx25840_state *state = container_of(work, struct cx25840_state, fw_work);
  159. cx25840_loadfw(state->c);
  160. wake_up(&state->fw_wait);
  161. }
  162. static void cx25840_initialize(struct i2c_client *client)
  163. {
  164. DEFINE_WAIT(wait);
  165. struct cx25840_state *state = i2c_get_clientdata(client);
  166. struct workqueue_struct *q;
  167. /* datasheet startup in numbered steps, refer to page 3-77 */
  168. /* 2. */
  169. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  170. /* The default of this register should be 4, but I get 0 instead.
  171. * Set this register to 4 manually. */
  172. cx25840_write(client, 0x000, 0x04);
  173. /* 3. */
  174. init_dll1(client);
  175. init_dll2(client);
  176. cx25840_write(client, 0x136, 0x0a);
  177. /* 4. */
  178. cx25840_write(client, 0x13c, 0x01);
  179. cx25840_write(client, 0x13c, 0x00);
  180. /* 5. */
  181. /* Do the firmware load in a work handler to prevent.
  182. Otherwise the kernel is blocked waiting for the
  183. bit-banging i2c interface to finish uploading the
  184. firmware. */
  185. INIT_WORK(&state->fw_work, cx25840_work_handler);
  186. init_waitqueue_head(&state->fw_wait);
  187. q = create_singlethread_workqueue("cx25840_fw");
  188. prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
  189. queue_work(q, &state->fw_work);
  190. schedule();
  191. finish_wait(&state->fw_wait, &wait);
  192. destroy_workqueue(q);
  193. /* 6. */
  194. cx25840_write(client, 0x115, 0x8c);
  195. cx25840_write(client, 0x116, 0x07);
  196. cx25840_write(client, 0x118, 0x02);
  197. /* 7. */
  198. cx25840_write(client, 0x4a5, 0x80);
  199. cx25840_write(client, 0x4a5, 0x00);
  200. cx25840_write(client, 0x402, 0x00);
  201. /* 8. */
  202. cx25840_and_or(client, 0x401, ~0x18, 0);
  203. cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
  204. /* steps 8c and 8d are done in change_input() */
  205. /* 10. */
  206. cx25840_write(client, 0x8d3, 0x1f);
  207. cx25840_write(client, 0x8e3, 0x03);
  208. cx25840_vbi_setup(client);
  209. /* trial and error says these are needed to get audio */
  210. cx25840_write(client, 0x914, 0xa0);
  211. cx25840_write(client, 0x918, 0xa0);
  212. cx25840_write(client, 0x919, 0x01);
  213. /* stereo prefered */
  214. cx25840_write(client, 0x809, 0x04);
  215. /* AC97 shift */
  216. cx25840_write(client, 0x8cf, 0x0f);
  217. /* (re)set input */
  218. set_input(client, state->vid_input, state->aud_input);
  219. /* start microcontroller */
  220. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  221. }
  222. static void cx23885_initialize(struct i2c_client *client)
  223. {
  224. DEFINE_WAIT(wait);
  225. struct cx25840_state *state = i2c_get_clientdata(client);
  226. struct workqueue_struct *q;
  227. /* Internal Reset */
  228. cx25840_and_or(client, 0x102, ~0x01, 0x01);
  229. cx25840_and_or(client, 0x102, ~0x01, 0x00);
  230. /* Stop microcontroller */
  231. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  232. /* DIF in reset? */
  233. cx25840_write(client, 0x398, 0);
  234. /* Trust the default xtal, no division */
  235. /* This changes for the cx23888 products */
  236. cx25840_write(client, 0x2, 0x76);
  237. /* Bring down the regulator for AUX clk */
  238. cx25840_write(client, 0x1, 0x40);
  239. /* Sys PLL frac */
  240. cx25840_write4(client, 0x11c, 0x01d1744c);
  241. /* Sys PLL int */
  242. cx25840_write4(client, 0x118, 0x00000416);
  243. /* Disable DIF bypass */
  244. cx25840_write4(client, 0x33c, 0x00000001);
  245. /* DIF Src phase inc */
  246. cx25840_write4(client, 0x340, 0x0df7df83);
  247. /* Vid PLL frac */
  248. cx25840_write4(client, 0x10c, 0x01b6db7b);
  249. /* Vid PLL int */
  250. cx25840_write4(client, 0x108, 0x00000512);
  251. /* Luma */
  252. cx25840_write4(client, 0x414, 0x00107d12);
  253. /* Chroma */
  254. cx25840_write4(client, 0x420, 0x3d008282);
  255. /* Aux PLL frac */
  256. cx25840_write4(client, 0x114, 0x017dbf48);
  257. /* Aux PLL int */
  258. cx25840_write4(client, 0x110, 0x000a030e);
  259. /* ADC2 input select */
  260. cx25840_write(client, 0x102, 0x10);
  261. /* VIN1 & VIN5 */
  262. cx25840_write(client, 0x103, 0x11);
  263. /* Enable format auto detect */
  264. cx25840_write(client, 0x400, 0);
  265. /* Fast subchroma lock */
  266. /* White crush, Chroma AGC & Chroma Killer enabled */
  267. cx25840_write(client, 0x401, 0xe8);
  268. /* Select AFE clock pad output source */
  269. cx25840_write(client, 0x144, 0x05);
  270. /* Do the firmware load in a work handler to prevent.
  271. Otherwise the kernel is blocked waiting for the
  272. bit-banging i2c interface to finish uploading the
  273. firmware. */
  274. INIT_WORK(&state->fw_work, cx25840_work_handler);
  275. init_waitqueue_head(&state->fw_wait);
  276. q = create_singlethread_workqueue("cx25840_fw");
  277. prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
  278. queue_work(q, &state->fw_work);
  279. schedule();
  280. finish_wait(&state->fw_wait, &wait);
  281. destroy_workqueue(q);
  282. cx25840_vbi_setup(client);
  283. /* (re)set input */
  284. set_input(client, state->vid_input, state->aud_input);
  285. /* start microcontroller */
  286. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  287. }
  288. /* ----------------------------------------------------------------------- */
  289. static void input_change(struct i2c_client *client)
  290. {
  291. struct cx25840_state *state = i2c_get_clientdata(client);
  292. v4l2_std_id std = state->std;
  293. /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
  294. if (std & V4L2_STD_SECAM) {
  295. cx25840_write(client, 0x402, 0);
  296. }
  297. else {
  298. cx25840_write(client, 0x402, 0x04);
  299. cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  300. }
  301. cx25840_and_or(client, 0x401, ~0x60, 0);
  302. cx25840_and_or(client, 0x401, ~0x60, 0x60);
  303. cx25840_and_or(client, 0x810, ~0x01, 1);
  304. if (state->radio) {
  305. cx25840_write(client, 0x808, 0xf9);
  306. cx25840_write(client, 0x80b, 0x00);
  307. }
  308. else if (std & V4L2_STD_525_60) {
  309. /* Certain Hauppauge PVR150 models have a hardware bug
  310. that causes audio to drop out. For these models the
  311. audio standard must be set explicitly.
  312. To be precise: it affects cards with tuner models
  313. 85, 99 and 112 (model numbers from tveeprom). */
  314. int hw_fix = state->pvr150_workaround;
  315. if (std == V4L2_STD_NTSC_M_JP) {
  316. /* Japan uses EIAJ audio standard */
  317. cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
  318. } else if (std == V4L2_STD_NTSC_M_KR) {
  319. /* South Korea uses A2 audio standard */
  320. cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
  321. } else {
  322. /* Others use the BTSC audio standard */
  323. cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
  324. }
  325. cx25840_write(client, 0x80b, 0x00);
  326. } else if (std & V4L2_STD_PAL) {
  327. /* Follow tuner change procedure for PAL */
  328. cx25840_write(client, 0x808, 0xff);
  329. cx25840_write(client, 0x80b, 0x10);
  330. } else if (std & V4L2_STD_SECAM) {
  331. /* Select autodetect for SECAM */
  332. cx25840_write(client, 0x808, 0xff);
  333. cx25840_write(client, 0x80b, 0x10);
  334. }
  335. cx25840_and_or(client, 0x810, ~0x01, 0);
  336. }
  337. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  338. enum cx25840_audio_input aud_input)
  339. {
  340. struct cx25840_state *state = i2c_get_clientdata(client);
  341. u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
  342. vid_input <= CX25840_COMPOSITE8);
  343. u8 reg;
  344. v4l_dbg(1, cx25840_debug, client,
  345. "decoder set video input %d, audio input %d\n",
  346. vid_input, aud_input);
  347. if (vid_input >= CX25840_VIN1_CH1) {
  348. v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
  349. vid_input);
  350. reg = vid_input & 0xff;
  351. if ((vid_input & CX25840_SVIDEO_ON) == CX25840_SVIDEO_ON)
  352. is_composite = 0;
  353. else
  354. is_composite = 1;
  355. v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
  356. reg, is_composite);
  357. } else
  358. if (is_composite) {
  359. reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
  360. } else {
  361. int luma = vid_input & 0xf0;
  362. int chroma = vid_input & 0xf00;
  363. if ((vid_input & ~0xff0) ||
  364. luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
  365. chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
  366. v4l_err(client, "0x%04x is not a valid video input!\n",
  367. vid_input);
  368. return -EINVAL;
  369. }
  370. reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
  371. if (chroma >= CX25840_SVIDEO_CHROMA7) {
  372. reg &= 0x3f;
  373. reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
  374. } else {
  375. reg &= 0xcf;
  376. reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
  377. }
  378. }
  379. /* The caller has previously prepared the correct routing
  380. * configuration in reg (for the cx23885) so we have no
  381. * need to attempt to flip bits for earlier av decoders.
  382. */
  383. if (!state->is_cx23885) {
  384. switch (aud_input) {
  385. case CX25840_AUDIO_SERIAL:
  386. /* do nothing, use serial audio input */
  387. break;
  388. case CX25840_AUDIO4: reg &= ~0x30; break;
  389. case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  390. case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  391. case CX25840_AUDIO7: reg &= ~0xc0; break;
  392. case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  393. default:
  394. v4l_err(client, "0x%04x is not a valid audio input!\n",
  395. aud_input);
  396. return -EINVAL;
  397. }
  398. }
  399. cx25840_write(client, 0x103, reg);
  400. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  401. cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
  402. if (!state->is_cx23885) {
  403. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  404. cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
  405. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
  406. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  407. cx25840_and_or(client, 0x102, ~0x4, 4);
  408. else
  409. cx25840_and_or(client, 0x102, ~0x4, 0);
  410. } else {
  411. if (is_composite)
  412. /* ADC2 input select channel 2 */
  413. cx25840_and_or(client, 0x102, ~0x2, 0);
  414. else
  415. /* ADC2 input select channel 3 */
  416. cx25840_and_or(client, 0x102, ~0x2, 2);
  417. }
  418. state->vid_input = vid_input;
  419. state->aud_input = aud_input;
  420. if (!state->is_cx25836) {
  421. cx25840_audio_set_path(client);
  422. input_change(client);
  423. }
  424. if (state->is_cx23885) {
  425. /* Audio channel 1 src : Parallel 1 */
  426. cx25840_write(client, 0x124, 0x03);
  427. /* Select AFE clock pad output source */
  428. cx25840_write(client, 0x144, 0x05);
  429. /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
  430. cx25840_write(client, 0x914, 0xa0);
  431. /* I2S_OUT_CTL:
  432. * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
  433. * I2S_OUT_MASTER_MODE = Master
  434. */
  435. cx25840_write(client, 0x918, 0xa0);
  436. cx25840_write(client, 0x919, 0x01);
  437. }
  438. return 0;
  439. }
  440. /* ----------------------------------------------------------------------- */
  441. static int set_v4lstd(struct i2c_client *client)
  442. {
  443. struct cx25840_state *state = i2c_get_clientdata(client);
  444. u8 fmt = 0; /* zero is autodetect */
  445. u8 pal_m = 0;
  446. /* First tests should be against specific std */
  447. if (state->std == V4L2_STD_NTSC_M_JP) {
  448. fmt = 0x2;
  449. } else if (state->std == V4L2_STD_NTSC_443) {
  450. fmt = 0x3;
  451. } else if (state->std == V4L2_STD_PAL_M) {
  452. pal_m = 1;
  453. fmt = 0x5;
  454. } else if (state->std == V4L2_STD_PAL_N) {
  455. fmt = 0x6;
  456. } else if (state->std == V4L2_STD_PAL_Nc) {
  457. fmt = 0x7;
  458. } else if (state->std == V4L2_STD_PAL_60) {
  459. fmt = 0x8;
  460. } else {
  461. /* Then, test against generic ones */
  462. if (state->std & V4L2_STD_NTSC)
  463. fmt = 0x1;
  464. else if (state->std & V4L2_STD_PAL)
  465. fmt = 0x4;
  466. else if (state->std & V4L2_STD_SECAM)
  467. fmt = 0xc;
  468. }
  469. v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
  470. /* Follow step 9 of section 3.16 in the cx25840 datasheet.
  471. Without this PAL may display a vertical ghosting effect.
  472. This happens for example with the Yuan MPC622. */
  473. if (fmt >= 4 && fmt < 8) {
  474. /* Set format to NTSC-M */
  475. cx25840_and_or(client, 0x400, ~0xf, 1);
  476. /* Turn off LCOMB */
  477. cx25840_and_or(client, 0x47b, ~6, 0);
  478. }
  479. cx25840_and_or(client, 0x400, ~0xf, fmt);
  480. cx25840_and_or(client, 0x403, ~0x3, pal_m);
  481. cx25840_vbi_setup(client);
  482. if (!state->is_cx25836)
  483. input_change(client);
  484. return 0;
  485. }
  486. /* ----------------------------------------------------------------------- */
  487. static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  488. {
  489. struct cx25840_state *state = i2c_get_clientdata(client);
  490. switch (ctrl->id) {
  491. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  492. state->pvr150_workaround = ctrl->value;
  493. set_input(client, state->vid_input, state->aud_input);
  494. break;
  495. case V4L2_CID_BRIGHTNESS:
  496. if (ctrl->value < 0 || ctrl->value > 255) {
  497. v4l_err(client, "invalid brightness setting %d\n",
  498. ctrl->value);
  499. return -ERANGE;
  500. }
  501. cx25840_write(client, 0x414, ctrl->value - 128);
  502. break;
  503. case V4L2_CID_CONTRAST:
  504. if (ctrl->value < 0 || ctrl->value > 127) {
  505. v4l_err(client, "invalid contrast setting %d\n",
  506. ctrl->value);
  507. return -ERANGE;
  508. }
  509. cx25840_write(client, 0x415, ctrl->value << 1);
  510. break;
  511. case V4L2_CID_SATURATION:
  512. if (ctrl->value < 0 || ctrl->value > 127) {
  513. v4l_err(client, "invalid saturation setting %d\n",
  514. ctrl->value);
  515. return -ERANGE;
  516. }
  517. cx25840_write(client, 0x420, ctrl->value << 1);
  518. cx25840_write(client, 0x421, ctrl->value << 1);
  519. break;
  520. case V4L2_CID_HUE:
  521. if (ctrl->value < -127 || ctrl->value > 127) {
  522. v4l_err(client, "invalid hue setting %d\n", ctrl->value);
  523. return -ERANGE;
  524. }
  525. cx25840_write(client, 0x422, ctrl->value);
  526. break;
  527. case V4L2_CID_AUDIO_VOLUME:
  528. case V4L2_CID_AUDIO_BASS:
  529. case V4L2_CID_AUDIO_TREBLE:
  530. case V4L2_CID_AUDIO_BALANCE:
  531. case V4L2_CID_AUDIO_MUTE:
  532. if (state->is_cx25836)
  533. return -EINVAL;
  534. return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
  535. default:
  536. return -EINVAL;
  537. }
  538. return 0;
  539. }
  540. static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  541. {
  542. struct cx25840_state *state = i2c_get_clientdata(client);
  543. switch (ctrl->id) {
  544. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  545. ctrl->value = state->pvr150_workaround;
  546. break;
  547. case V4L2_CID_BRIGHTNESS:
  548. ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
  549. break;
  550. case V4L2_CID_CONTRAST:
  551. ctrl->value = cx25840_read(client, 0x415) >> 1;
  552. break;
  553. case V4L2_CID_SATURATION:
  554. ctrl->value = cx25840_read(client, 0x420) >> 1;
  555. break;
  556. case V4L2_CID_HUE:
  557. ctrl->value = (s8)cx25840_read(client, 0x422);
  558. break;
  559. case V4L2_CID_AUDIO_VOLUME:
  560. case V4L2_CID_AUDIO_BASS:
  561. case V4L2_CID_AUDIO_TREBLE:
  562. case V4L2_CID_AUDIO_BALANCE:
  563. case V4L2_CID_AUDIO_MUTE:
  564. if (state->is_cx25836)
  565. return -EINVAL;
  566. return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
  567. default:
  568. return -EINVAL;
  569. }
  570. return 0;
  571. }
  572. /* ----------------------------------------------------------------------- */
  573. static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  574. {
  575. switch (fmt->type) {
  576. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  577. return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
  578. default:
  579. return -EINVAL;
  580. }
  581. return 0;
  582. }
  583. static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  584. {
  585. struct cx25840_state *state = i2c_get_clientdata(client);
  586. struct v4l2_pix_format *pix;
  587. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  588. int is_50Hz = !(state->std & V4L2_STD_525_60);
  589. switch (fmt->type) {
  590. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  591. pix = &(fmt->fmt.pix);
  592. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  593. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  594. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  595. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  596. Vlines = pix->height + (is_50Hz ? 4 : 7);
  597. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  598. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  599. v4l_err(client, "%dx%d is not a valid size!\n",
  600. pix->width, pix->height);
  601. return -ERANGE;
  602. }
  603. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  604. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  605. VSC &= 0x1fff;
  606. if (pix->width >= 385)
  607. filter = 0;
  608. else if (pix->width > 192)
  609. filter = 1;
  610. else if (pix->width > 96)
  611. filter = 2;
  612. else
  613. filter = 3;
  614. v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
  615. pix->width, pix->height, HSC, VSC);
  616. /* HSCALE=HSC */
  617. cx25840_write(client, 0x418, HSC & 0xff);
  618. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  619. cx25840_write(client, 0x41a, HSC >> 16);
  620. /* VSCALE=VSC */
  621. cx25840_write(client, 0x41c, VSC & 0xff);
  622. cx25840_write(client, 0x41d, VSC >> 8);
  623. /* VS_INTRLACE=1 VFILT=filter */
  624. cx25840_write(client, 0x41e, 0x8 | filter);
  625. break;
  626. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  627. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  628. case V4L2_BUF_TYPE_VBI_CAPTURE:
  629. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  630. default:
  631. return -EINVAL;
  632. }
  633. return 0;
  634. }
  635. /* ----------------------------------------------------------------------- */
  636. static void log_video_status(struct i2c_client *client)
  637. {
  638. static const char *const fmt_strs[] = {
  639. "0x0",
  640. "NTSC-M", "NTSC-J", "NTSC-4.43",
  641. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  642. "0x9", "0xA", "0xB",
  643. "SECAM",
  644. "0xD", "0xE", "0xF"
  645. };
  646. struct cx25840_state *state = i2c_get_clientdata(client);
  647. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  648. u8 gen_stat1 = cx25840_read(client, 0x40d);
  649. u8 gen_stat2 = cx25840_read(client, 0x40e);
  650. int vid_input = state->vid_input;
  651. v4l_info(client, "Video signal: %spresent\n",
  652. (gen_stat2 & 0x20) ? "" : "not ");
  653. v4l_info(client, "Detected format: %s\n",
  654. fmt_strs[gen_stat1 & 0xf]);
  655. v4l_info(client, "Specified standard: %s\n",
  656. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  657. if (vid_input >= CX25840_COMPOSITE1 &&
  658. vid_input <= CX25840_COMPOSITE8) {
  659. v4l_info(client, "Specified video input: Composite %d\n",
  660. vid_input - CX25840_COMPOSITE1 + 1);
  661. } else {
  662. v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  663. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  664. }
  665. v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
  666. }
  667. /* ----------------------------------------------------------------------- */
  668. static void log_audio_status(struct i2c_client *client)
  669. {
  670. struct cx25840_state *state = i2c_get_clientdata(client);
  671. u8 download_ctl = cx25840_read(client, 0x803);
  672. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  673. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  674. u8 audio_config = cx25840_read(client, 0x808);
  675. u8 pref_mode = cx25840_read(client, 0x809);
  676. u8 afc0 = cx25840_read(client, 0x80b);
  677. u8 mute_ctl = cx25840_read(client, 0x8d3);
  678. int aud_input = state->aud_input;
  679. char *p;
  680. switch (mod_det_stat0) {
  681. case 0x00: p = "mono"; break;
  682. case 0x01: p = "stereo"; break;
  683. case 0x02: p = "dual"; break;
  684. case 0x04: p = "tri"; break;
  685. case 0x10: p = "mono with SAP"; break;
  686. case 0x11: p = "stereo with SAP"; break;
  687. case 0x12: p = "dual with SAP"; break;
  688. case 0x14: p = "tri with SAP"; break;
  689. case 0xfe: p = "forced mode"; break;
  690. default: p = "not defined";
  691. }
  692. v4l_info(client, "Detected audio mode: %s\n", p);
  693. switch (mod_det_stat1) {
  694. case 0x00: p = "not defined"; break;
  695. case 0x01: p = "EIAJ"; break;
  696. case 0x02: p = "A2-M"; break;
  697. case 0x03: p = "A2-BG"; break;
  698. case 0x04: p = "A2-DK1"; break;
  699. case 0x05: p = "A2-DK2"; break;
  700. case 0x06: p = "A2-DK3"; break;
  701. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  702. case 0x08: p = "AM-L"; break;
  703. case 0x09: p = "NICAM-BG"; break;
  704. case 0x0a: p = "NICAM-DK"; break;
  705. case 0x0b: p = "NICAM-I"; break;
  706. case 0x0c: p = "NICAM-L"; break;
  707. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  708. case 0x0e: p = "IF FM Radio"; break;
  709. case 0x0f: p = "BTSC"; break;
  710. case 0x10: p = "high-deviation FM"; break;
  711. case 0x11: p = "very high-deviation FM"; break;
  712. case 0xfd: p = "unknown audio standard"; break;
  713. case 0xfe: p = "forced audio standard"; break;
  714. case 0xff: p = "no detected audio standard"; break;
  715. default: p = "not defined";
  716. }
  717. v4l_info(client, "Detected audio standard: %s\n", p);
  718. v4l_info(client, "Audio muted: %s\n",
  719. (state->unmute_volume >= 0) ? "yes" : "no");
  720. v4l_info(client, "Audio microcontroller: %s\n",
  721. (download_ctl & 0x10) ?
  722. ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
  723. switch (audio_config >> 4) {
  724. case 0x00: p = "undefined"; break;
  725. case 0x01: p = "BTSC"; break;
  726. case 0x02: p = "EIAJ"; break;
  727. case 0x03: p = "A2-M"; break;
  728. case 0x04: p = "A2-BG"; break;
  729. case 0x05: p = "A2-DK1"; break;
  730. case 0x06: p = "A2-DK2"; break;
  731. case 0x07: p = "A2-DK3"; break;
  732. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  733. case 0x09: p = "AM-L"; break;
  734. case 0x0a: p = "NICAM-BG"; break;
  735. case 0x0b: p = "NICAM-DK"; break;
  736. case 0x0c: p = "NICAM-I"; break;
  737. case 0x0d: p = "NICAM-L"; break;
  738. case 0x0e: p = "FM radio"; break;
  739. case 0x0f: p = "automatic detection"; break;
  740. default: p = "undefined";
  741. }
  742. v4l_info(client, "Configured audio standard: %s\n", p);
  743. if ((audio_config >> 4) < 0xF) {
  744. switch (audio_config & 0xF) {
  745. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  746. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  747. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  748. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  749. case 0x04: p = "STEREO"; break;
  750. case 0x05: p = "DUAL1 (AB)"; break;
  751. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  752. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  753. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  754. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  755. case 0x0a: p = "SAP"; break;
  756. default: p = "undefined";
  757. }
  758. v4l_info(client, "Configured audio mode: %s\n", p);
  759. } else {
  760. switch (audio_config & 0xF) {
  761. case 0x00: p = "BG"; break;
  762. case 0x01: p = "DK1"; break;
  763. case 0x02: p = "DK2"; break;
  764. case 0x03: p = "DK3"; break;
  765. case 0x04: p = "I"; break;
  766. case 0x05: p = "L"; break;
  767. case 0x06: p = "BTSC"; break;
  768. case 0x07: p = "EIAJ"; break;
  769. case 0x08: p = "A2-M"; break;
  770. case 0x09: p = "FM Radio"; break;
  771. case 0x0f: p = "automatic standard and mode detection"; break;
  772. default: p = "undefined";
  773. }
  774. v4l_info(client, "Configured audio system: %s\n", p);
  775. }
  776. if (aud_input) {
  777. v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
  778. } else {
  779. v4l_info(client, "Specified audio input: External\n");
  780. }
  781. switch (pref_mode & 0xf) {
  782. case 0: p = "mono/language A"; break;
  783. case 1: p = "language B"; break;
  784. case 2: p = "language C"; break;
  785. case 3: p = "analog fallback"; break;
  786. case 4: p = "stereo"; break;
  787. case 5: p = "language AC"; break;
  788. case 6: p = "language BC"; break;
  789. case 7: p = "language AB"; break;
  790. default: p = "undefined";
  791. }
  792. v4l_info(client, "Preferred audio mode: %s\n", p);
  793. if ((audio_config & 0xf) == 0xf) {
  794. switch ((afc0 >> 3) & 0x3) {
  795. case 0: p = "system DK"; break;
  796. case 1: p = "system L"; break;
  797. case 2: p = "autodetect"; break;
  798. default: p = "undefined";
  799. }
  800. v4l_info(client, "Selected 65 MHz format: %s\n", p);
  801. switch (afc0 & 0x7) {
  802. case 0: p = "chroma"; break;
  803. case 1: p = "BTSC"; break;
  804. case 2: p = "EIAJ"; break;
  805. case 3: p = "A2-M"; break;
  806. case 4: p = "autodetect"; break;
  807. default: p = "undefined";
  808. }
  809. v4l_info(client, "Selected 45 MHz format: %s\n", p);
  810. }
  811. }
  812. /* ----------------------------------------------------------------------- */
  813. static int cx25840_command(struct i2c_client *client, unsigned int cmd,
  814. void *arg)
  815. {
  816. struct cx25840_state *state = i2c_get_clientdata(client);
  817. struct v4l2_tuner *vt = arg;
  818. struct v4l2_routing *route = arg;
  819. /* ignore these commands */
  820. switch (cmd) {
  821. case TUNER_SET_TYPE_ADDR:
  822. return 0;
  823. }
  824. if (!state->is_initialized) {
  825. v4l_dbg(1, cx25840_debug, client, "cmd %08x triggered fw load\n", cmd);
  826. /* initialize on first use */
  827. state->is_initialized = 1;
  828. if (state->is_cx25836)
  829. cx25836_initialize(client);
  830. else if (state->is_cx23885)
  831. cx23885_initialize(client);
  832. else
  833. cx25840_initialize(client);
  834. }
  835. switch (cmd) {
  836. #ifdef CONFIG_VIDEO_ADV_DEBUG
  837. /* ioctls to allow direct access to the
  838. * cx25840 registers for testing */
  839. case VIDIOC_DBG_G_REGISTER:
  840. case VIDIOC_DBG_S_REGISTER:
  841. {
  842. struct v4l2_register *reg = arg;
  843. if (!v4l2_chip_match_i2c_client(client, reg->match_type, reg->match_chip))
  844. return -EINVAL;
  845. if (!capable(CAP_SYS_ADMIN))
  846. return -EPERM;
  847. if (cmd == VIDIOC_DBG_G_REGISTER)
  848. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  849. else
  850. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  851. break;
  852. }
  853. #endif
  854. case VIDIOC_INT_DECODE_VBI_LINE:
  855. return cx25840_vbi(client, cmd, arg);
  856. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  857. return cx25840_audio(client, cmd, arg);
  858. case VIDIOC_STREAMON:
  859. v4l_dbg(1, cx25840_debug, client, "enable output\n");
  860. if (state->is_cx23885) {
  861. u8 v = (cx25840_read(client, 0x421) | 0x0b);
  862. cx25840_write(client, 0x421, v);
  863. } else {
  864. cx25840_write(client, 0x115,
  865. state->is_cx25836 ? 0x0c : 0x8c);
  866. cx25840_write(client, 0x116,
  867. state->is_cx25836 ? 0x04 : 0x07);
  868. }
  869. break;
  870. case VIDIOC_STREAMOFF:
  871. v4l_dbg(1, cx25840_debug, client, "disable output\n");
  872. if (state->is_cx23885) {
  873. u8 v = cx25840_read(client, 0x421) & ~(0x0b);
  874. cx25840_write(client, 0x421, v);
  875. } else {
  876. cx25840_write(client, 0x115, 0x00);
  877. cx25840_write(client, 0x116, 0x00);
  878. }
  879. break;
  880. case VIDIOC_LOG_STATUS:
  881. log_video_status(client);
  882. if (!state->is_cx25836)
  883. log_audio_status(client);
  884. break;
  885. case VIDIOC_G_CTRL:
  886. return get_v4lctrl(client, (struct v4l2_control *)arg);
  887. case VIDIOC_S_CTRL:
  888. return set_v4lctrl(client, (struct v4l2_control *)arg);
  889. case VIDIOC_QUERYCTRL:
  890. {
  891. struct v4l2_queryctrl *qc = arg;
  892. switch (qc->id) {
  893. case V4L2_CID_BRIGHTNESS:
  894. case V4L2_CID_CONTRAST:
  895. case V4L2_CID_SATURATION:
  896. case V4L2_CID_HUE:
  897. return v4l2_ctrl_query_fill_std(qc);
  898. default:
  899. break;
  900. }
  901. if (state->is_cx25836)
  902. return -EINVAL;
  903. switch (qc->id) {
  904. case V4L2_CID_AUDIO_VOLUME:
  905. case V4L2_CID_AUDIO_MUTE:
  906. case V4L2_CID_AUDIO_BALANCE:
  907. case V4L2_CID_AUDIO_BASS:
  908. case V4L2_CID_AUDIO_TREBLE:
  909. return v4l2_ctrl_query_fill_std(qc);
  910. default:
  911. return -EINVAL;
  912. }
  913. return -EINVAL;
  914. }
  915. case VIDIOC_G_STD:
  916. *(v4l2_std_id *)arg = state->std;
  917. break;
  918. case VIDIOC_S_STD:
  919. if (state->radio == 0 && state->std == *(v4l2_std_id *)arg)
  920. return 0;
  921. state->radio = 0;
  922. state->std = *(v4l2_std_id *)arg;
  923. return set_v4lstd(client);
  924. case AUDC_SET_RADIO:
  925. state->radio = 1;
  926. break;
  927. case VIDIOC_INT_G_VIDEO_ROUTING:
  928. route->input = state->vid_input;
  929. route->output = 0;
  930. break;
  931. case VIDIOC_INT_S_VIDEO_ROUTING:
  932. return set_input(client, route->input, state->aud_input);
  933. case VIDIOC_INT_G_AUDIO_ROUTING:
  934. if (state->is_cx25836)
  935. return -EINVAL;
  936. route->input = state->aud_input;
  937. route->output = 0;
  938. break;
  939. case VIDIOC_INT_S_AUDIO_ROUTING:
  940. if (state->is_cx25836)
  941. return -EINVAL;
  942. return set_input(client, state->vid_input, route->input);
  943. case VIDIOC_S_FREQUENCY:
  944. if (!state->is_cx25836) {
  945. input_change(client);
  946. }
  947. break;
  948. case VIDIOC_G_TUNER:
  949. {
  950. u8 vpres = cx25840_read(client, 0x40e) & 0x20;
  951. u8 mode;
  952. int val = 0;
  953. if (state->radio)
  954. break;
  955. vt->signal = vpres ? 0xffff : 0x0;
  956. if (state->is_cx25836)
  957. break;
  958. vt->capability |=
  959. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  960. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  961. mode = cx25840_read(client, 0x804);
  962. /* get rxsubchans and audmode */
  963. if ((mode & 0xf) == 1)
  964. val |= V4L2_TUNER_SUB_STEREO;
  965. else
  966. val |= V4L2_TUNER_SUB_MONO;
  967. if (mode == 2 || mode == 4)
  968. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  969. if (mode & 0x10)
  970. val |= V4L2_TUNER_SUB_SAP;
  971. vt->rxsubchans = val;
  972. vt->audmode = state->audmode;
  973. break;
  974. }
  975. case VIDIOC_S_TUNER:
  976. if (state->radio || state->is_cx25836)
  977. break;
  978. switch (vt->audmode) {
  979. case V4L2_TUNER_MODE_MONO:
  980. /* mono -> mono
  981. stereo -> mono
  982. bilingual -> lang1 */
  983. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  984. break;
  985. case V4L2_TUNER_MODE_STEREO:
  986. case V4L2_TUNER_MODE_LANG1:
  987. /* mono -> mono
  988. stereo -> stereo
  989. bilingual -> lang1 */
  990. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  991. break;
  992. case V4L2_TUNER_MODE_LANG1_LANG2:
  993. /* mono -> mono
  994. stereo -> stereo
  995. bilingual -> lang1/lang2 */
  996. cx25840_and_or(client, 0x809, ~0xf, 0x07);
  997. break;
  998. case V4L2_TUNER_MODE_LANG2:
  999. /* mono -> mono
  1000. stereo -> stereo
  1001. bilingual -> lang2 */
  1002. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  1003. break;
  1004. default:
  1005. return -EINVAL;
  1006. }
  1007. state->audmode = vt->audmode;
  1008. break;
  1009. case VIDIOC_G_FMT:
  1010. return get_v4lfmt(client, (struct v4l2_format *)arg);
  1011. case VIDIOC_S_FMT:
  1012. return set_v4lfmt(client, (struct v4l2_format *)arg);
  1013. case VIDIOC_INT_RESET:
  1014. if (state->is_cx25836)
  1015. cx25836_initialize(client);
  1016. else if (state->is_cx23885)
  1017. cx23885_initialize(client);
  1018. else
  1019. cx25840_initialize(client);
  1020. break;
  1021. case VIDIOC_G_CHIP_IDENT:
  1022. return v4l2_chip_ident_i2c_client(client, arg, state->id, state->rev);
  1023. default:
  1024. return -EINVAL;
  1025. }
  1026. return 0;
  1027. }
  1028. /* ----------------------------------------------------------------------- */
  1029. static int cx25840_probe(struct i2c_client *client,
  1030. const struct i2c_device_id *did)
  1031. {
  1032. struct cx25840_state *state;
  1033. u32 id;
  1034. u16 device_id;
  1035. /* Check if the adapter supports the needed features */
  1036. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1037. return -EIO;
  1038. v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
  1039. device_id = cx25840_read(client, 0x101) << 8;
  1040. device_id |= cx25840_read(client, 0x100);
  1041. v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
  1042. /* The high byte of the device ID should be
  1043. * 0x83 for the cx2583x and 0x84 for the cx2584x */
  1044. if ((device_id & 0xff00) == 0x8300) {
  1045. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1046. }
  1047. else if ((device_id & 0xff00) == 0x8400) {
  1048. id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
  1049. } else if (device_id == 0x0000) {
  1050. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1051. } else if (device_id == 0x1313) {
  1052. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1053. }
  1054. else {
  1055. v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
  1056. return -ENODEV;
  1057. }
  1058. state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  1059. if (state == NULL) {
  1060. return -ENOMEM;
  1061. }
  1062. /* Note: revision '(device_id & 0x0f) == 2' was never built. The
  1063. marking skips from 0x1 == 22 to 0x3 == 23. */
  1064. v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
  1065. (device_id & 0xfff0) >> 4,
  1066. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : (device_id & 0x0f),
  1067. client->addr << 1, client->adapter->name);
  1068. i2c_set_clientdata(client, state);
  1069. state->c = client;
  1070. state->is_cx25836 = ((device_id & 0xff00) == 0x8300);
  1071. state->is_cx23885 = (device_id == 0x0000) || (device_id == 0x1313);
  1072. state->vid_input = CX25840_COMPOSITE7;
  1073. state->aud_input = CX25840_AUDIO8;
  1074. state->audclk_freq = 48000;
  1075. state->pvr150_workaround = 0;
  1076. state->audmode = V4L2_TUNER_MODE_LANG1;
  1077. state->unmute_volume = -1;
  1078. state->vbi_line_offset = 8;
  1079. state->id = id;
  1080. state->rev = device_id;
  1081. if (state->is_cx23885) {
  1082. /* Drive GPIO2 direction and values */
  1083. cx25840_write(client, 0x160, 0x1d);
  1084. cx25840_write(client, 0x164, 0x00);
  1085. }
  1086. return 0;
  1087. }
  1088. static int cx25840_remove(struct i2c_client *client)
  1089. {
  1090. kfree(i2c_get_clientdata(client));
  1091. return 0;
  1092. }
  1093. static const struct i2c_device_id cx25840_id[] = {
  1094. { "cx25840", 0 },
  1095. { }
  1096. };
  1097. MODULE_DEVICE_TABLE(i2c, cx25840_id);
  1098. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  1099. .name = "cx25840",
  1100. .driverid = I2C_DRIVERID_CX25840,
  1101. .command = cx25840_command,
  1102. .probe = cx25840_probe,
  1103. .remove = cx25840_remove,
  1104. .id_table = cx25840_id,
  1105. };