cx25840-core.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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@linuxtv.org>.
  17. *
  18. * CX2388[578] IRQ handling, IO Pin mux configuration and other small fixes are
  19. * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
  20. *
  21. * This program is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU General Public License
  23. * as published by the Free Software Foundation; either version 2
  24. * of the License, or (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/slab.h>
  38. #include <linux/videodev2.h>
  39. #include <linux/i2c.h>
  40. #include <linux/delay.h>
  41. #include <media/v4l2-common.h>
  42. #include <media/v4l2-chip-ident.h>
  43. #include <media/cx25840.h>
  44. #include "cx25840-core.h"
  45. MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
  46. MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
  47. MODULE_LICENSE("GPL");
  48. #define CX25840_VID_INT_STAT_REG 0x410
  49. #define CX25840_VID_INT_STAT_BITS 0x0000ffff
  50. #define CX25840_VID_INT_MASK_BITS 0xffff0000
  51. #define CX25840_VID_INT_MASK_SHFT 16
  52. #define CX25840_VID_INT_MASK_REG 0x412
  53. #define CX23885_AUD_MC_INT_MASK_REG 0x80c
  54. #define CX23885_AUD_MC_INT_STAT_BITS 0xffff0000
  55. #define CX23885_AUD_MC_INT_CTRL_BITS 0x0000ffff
  56. #define CX23885_AUD_MC_INT_STAT_SHFT 16
  57. #define CX25840_AUD_INT_CTRL_REG 0x812
  58. #define CX25840_AUD_INT_STAT_REG 0x813
  59. #define CX23885_PIN_CTRL_IRQ_REG 0x123
  60. #define CX23885_PIN_CTRL_IRQ_IR_STAT 0x40
  61. #define CX23885_PIN_CTRL_IRQ_AUD_STAT 0x20
  62. #define CX23885_PIN_CTRL_IRQ_VID_STAT 0x10
  63. #define CX25840_IR_STATS_REG 0x210
  64. #define CX25840_IR_IRQEN_REG 0x214
  65. static int cx25840_debug;
  66. module_param_named(debug,cx25840_debug, int, 0644);
  67. MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
  68. /* ----------------------------------------------------------------------- */
  69. int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
  70. {
  71. u8 buffer[3];
  72. buffer[0] = addr >> 8;
  73. buffer[1] = addr & 0xff;
  74. buffer[2] = value;
  75. return i2c_master_send(client, buffer, 3);
  76. }
  77. int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
  78. {
  79. u8 buffer[6];
  80. buffer[0] = addr >> 8;
  81. buffer[1] = addr & 0xff;
  82. buffer[2] = value & 0xff;
  83. buffer[3] = (value >> 8) & 0xff;
  84. buffer[4] = (value >> 16) & 0xff;
  85. buffer[5] = value >> 24;
  86. return i2c_master_send(client, buffer, 6);
  87. }
  88. u8 cx25840_read(struct i2c_client * client, u16 addr)
  89. {
  90. struct i2c_msg msgs[2];
  91. u8 tx_buf[2], rx_buf[1];
  92. /* Write register address */
  93. tx_buf[0] = addr >> 8;
  94. tx_buf[1] = addr & 0xff;
  95. msgs[0].addr = client->addr;
  96. msgs[0].flags = 0;
  97. msgs[0].len = 2;
  98. msgs[0].buf = (char *) tx_buf;
  99. /* Read data from register */
  100. msgs[1].addr = client->addr;
  101. msgs[1].flags = I2C_M_RD;
  102. msgs[1].len = 1;
  103. msgs[1].buf = (char *) rx_buf;
  104. if (i2c_transfer(client->adapter, msgs, 2) < 2)
  105. return 0;
  106. return rx_buf[0];
  107. }
  108. u32 cx25840_read4(struct i2c_client * client, u16 addr)
  109. {
  110. struct i2c_msg msgs[2];
  111. u8 tx_buf[2], rx_buf[4];
  112. /* Write register address */
  113. tx_buf[0] = addr >> 8;
  114. tx_buf[1] = addr & 0xff;
  115. msgs[0].addr = client->addr;
  116. msgs[0].flags = 0;
  117. msgs[0].len = 2;
  118. msgs[0].buf = (char *) tx_buf;
  119. /* Read data from registers */
  120. msgs[1].addr = client->addr;
  121. msgs[1].flags = I2C_M_RD;
  122. msgs[1].len = 4;
  123. msgs[1].buf = (char *) rx_buf;
  124. if (i2c_transfer(client->adapter, msgs, 2) < 2)
  125. return 0;
  126. return (rx_buf[3] << 24) | (rx_buf[2] << 16) | (rx_buf[1] << 8) |
  127. rx_buf[0];
  128. }
  129. int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
  130. u8 or_value)
  131. {
  132. return cx25840_write(client, addr,
  133. (cx25840_read(client, addr) & and_mask) |
  134. or_value);
  135. }
  136. int cx25840_and_or4(struct i2c_client *client, u16 addr, u32 and_mask,
  137. u32 or_value)
  138. {
  139. return cx25840_write4(client, addr,
  140. (cx25840_read4(client, addr) & and_mask) |
  141. or_value);
  142. }
  143. /* ----------------------------------------------------------------------- */
  144. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  145. enum cx25840_audio_input aud_input);
  146. /* ----------------------------------------------------------------------- */
  147. static int cx23885_s_io_pin_config(struct v4l2_subdev *sd, size_t n,
  148. struct v4l2_subdev_io_pin_config *p)
  149. {
  150. struct i2c_client *client = v4l2_get_subdevdata(sd);
  151. int i;
  152. u32 pin_ctrl;
  153. u8 gpio_oe, gpio_data, strength;
  154. pin_ctrl = cx25840_read4(client, 0x120);
  155. gpio_oe = cx25840_read(client, 0x160);
  156. gpio_data = cx25840_read(client, 0x164);
  157. for (i = 0; i < n; i++) {
  158. strength = p[i].strength;
  159. if (strength > CX25840_PIN_DRIVE_FAST)
  160. strength = CX25840_PIN_DRIVE_FAST;
  161. switch (p[i].pin) {
  162. case CX23885_PIN_IRQ_N_GPIO16:
  163. if (p[i].function != CX23885_PAD_IRQ_N) {
  164. /* GPIO16 */
  165. pin_ctrl &= ~(0x1 << 25);
  166. } else {
  167. /* IRQ_N */
  168. if (p[i].flags &
  169. (V4L2_SUBDEV_IO_PIN_DISABLE |
  170. V4L2_SUBDEV_IO_PIN_INPUT)) {
  171. pin_ctrl &= ~(0x1 << 25);
  172. } else {
  173. pin_ctrl |= (0x1 << 25);
  174. }
  175. if (p[i].flags &
  176. V4L2_SUBDEV_IO_PIN_ACTIVE_LOW) {
  177. pin_ctrl &= ~(0x1 << 24);
  178. } else {
  179. pin_ctrl |= (0x1 << 24);
  180. }
  181. }
  182. break;
  183. case CX23885_PIN_IR_RX_GPIO19:
  184. if (p[i].function != CX23885_PAD_GPIO19) {
  185. /* IR_RX */
  186. gpio_oe |= (0x1 << 0);
  187. pin_ctrl &= ~(0x3 << 18);
  188. pin_ctrl |= (strength << 18);
  189. } else {
  190. /* GPIO19 */
  191. gpio_oe &= ~(0x1 << 0);
  192. if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
  193. gpio_data &= ~(0x1 << 0);
  194. gpio_data |= ((p[i].value & 0x1) << 0);
  195. }
  196. pin_ctrl &= ~(0x3 << 12);
  197. pin_ctrl |= (strength << 12);
  198. }
  199. break;
  200. case CX23885_PIN_IR_TX_GPIO20:
  201. if (p[i].function != CX23885_PAD_GPIO20) {
  202. /* IR_TX */
  203. gpio_oe |= (0x1 << 1);
  204. if (p[i].flags & V4L2_SUBDEV_IO_PIN_DISABLE)
  205. pin_ctrl &= ~(0x1 << 10);
  206. else
  207. pin_ctrl |= (0x1 << 10);
  208. pin_ctrl &= ~(0x3 << 18);
  209. pin_ctrl |= (strength << 18);
  210. } else {
  211. /* GPIO20 */
  212. gpio_oe &= ~(0x1 << 1);
  213. if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
  214. gpio_data &= ~(0x1 << 1);
  215. gpio_data |= ((p[i].value & 0x1) << 1);
  216. }
  217. pin_ctrl &= ~(0x3 << 12);
  218. pin_ctrl |= (strength << 12);
  219. }
  220. break;
  221. case CX23885_PIN_I2S_SDAT_GPIO21:
  222. if (p[i].function != CX23885_PAD_GPIO21) {
  223. /* I2S_SDAT */
  224. /* TODO: Input or Output config */
  225. gpio_oe |= (0x1 << 2);
  226. pin_ctrl &= ~(0x3 << 22);
  227. pin_ctrl |= (strength << 22);
  228. } else {
  229. /* GPIO21 */
  230. gpio_oe &= ~(0x1 << 2);
  231. if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
  232. gpio_data &= ~(0x1 << 2);
  233. gpio_data |= ((p[i].value & 0x1) << 2);
  234. }
  235. pin_ctrl &= ~(0x3 << 12);
  236. pin_ctrl |= (strength << 12);
  237. }
  238. break;
  239. case CX23885_PIN_I2S_WCLK_GPIO22:
  240. if (p[i].function != CX23885_PAD_GPIO22) {
  241. /* I2S_WCLK */
  242. /* TODO: Input or Output config */
  243. gpio_oe |= (0x1 << 3);
  244. pin_ctrl &= ~(0x3 << 22);
  245. pin_ctrl |= (strength << 22);
  246. } else {
  247. /* GPIO22 */
  248. gpio_oe &= ~(0x1 << 3);
  249. if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
  250. gpio_data &= ~(0x1 << 3);
  251. gpio_data |= ((p[i].value & 0x1) << 3);
  252. }
  253. pin_ctrl &= ~(0x3 << 12);
  254. pin_ctrl |= (strength << 12);
  255. }
  256. break;
  257. case CX23885_PIN_I2S_BCLK_GPIO23:
  258. if (p[i].function != CX23885_PAD_GPIO23) {
  259. /* I2S_BCLK */
  260. /* TODO: Input or Output config */
  261. gpio_oe |= (0x1 << 4);
  262. pin_ctrl &= ~(0x3 << 22);
  263. pin_ctrl |= (strength << 22);
  264. } else {
  265. /* GPIO23 */
  266. gpio_oe &= ~(0x1 << 4);
  267. if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
  268. gpio_data &= ~(0x1 << 4);
  269. gpio_data |= ((p[i].value & 0x1) << 4);
  270. }
  271. pin_ctrl &= ~(0x3 << 12);
  272. pin_ctrl |= (strength << 12);
  273. }
  274. break;
  275. }
  276. }
  277. cx25840_write(client, 0x164, gpio_data);
  278. cx25840_write(client, 0x160, gpio_oe);
  279. cx25840_write4(client, 0x120, pin_ctrl);
  280. return 0;
  281. }
  282. static int common_s_io_pin_config(struct v4l2_subdev *sd, size_t n,
  283. struct v4l2_subdev_io_pin_config *pincfg)
  284. {
  285. struct cx25840_state *state = to_state(sd);
  286. if (is_cx2388x(state))
  287. return cx23885_s_io_pin_config(sd, n, pincfg);
  288. return 0;
  289. }
  290. /* ----------------------------------------------------------------------- */
  291. static void init_dll1(struct i2c_client *client)
  292. {
  293. /* This is the Hauppauge sequence used to
  294. * initialize the Delay Lock Loop 1 (ADC DLL). */
  295. cx25840_write(client, 0x159, 0x23);
  296. cx25840_write(client, 0x15a, 0x87);
  297. cx25840_write(client, 0x15b, 0x06);
  298. udelay(10);
  299. cx25840_write(client, 0x159, 0xe1);
  300. udelay(10);
  301. cx25840_write(client, 0x15a, 0x86);
  302. cx25840_write(client, 0x159, 0xe0);
  303. cx25840_write(client, 0x159, 0xe1);
  304. cx25840_write(client, 0x15b, 0x10);
  305. }
  306. static void init_dll2(struct i2c_client *client)
  307. {
  308. /* This is the Hauppauge sequence used to
  309. * initialize the Delay Lock Loop 2 (ADC DLL). */
  310. cx25840_write(client, 0x15d, 0xe3);
  311. cx25840_write(client, 0x15e, 0x86);
  312. cx25840_write(client, 0x15f, 0x06);
  313. udelay(10);
  314. cx25840_write(client, 0x15d, 0xe1);
  315. cx25840_write(client, 0x15d, 0xe0);
  316. cx25840_write(client, 0x15d, 0xe1);
  317. }
  318. static void cx25836_initialize(struct i2c_client *client)
  319. {
  320. /* reset configuration is described on page 3-77 of the CX25836 datasheet */
  321. /* 2. */
  322. cx25840_and_or(client, 0x000, ~0x01, 0x01);
  323. cx25840_and_or(client, 0x000, ~0x01, 0x00);
  324. /* 3a. */
  325. cx25840_and_or(client, 0x15a, ~0x70, 0x00);
  326. /* 3b. */
  327. cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
  328. /* 3c. */
  329. cx25840_and_or(client, 0x159, ~0x02, 0x02);
  330. /* 3d. */
  331. udelay(10);
  332. /* 3e. */
  333. cx25840_and_or(client, 0x159, ~0x02, 0x00);
  334. /* 3f. */
  335. cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
  336. /* 3g. */
  337. cx25840_and_or(client, 0x159, ~0x01, 0x00);
  338. cx25840_and_or(client, 0x159, ~0x01, 0x01);
  339. /* 3h. */
  340. cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
  341. }
  342. static void cx25840_work_handler(struct work_struct *work)
  343. {
  344. struct cx25840_state *state = container_of(work, struct cx25840_state, fw_work);
  345. cx25840_loadfw(state->c);
  346. wake_up(&state->fw_wait);
  347. }
  348. static void cx25840_initialize(struct i2c_client *client)
  349. {
  350. DEFINE_WAIT(wait);
  351. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  352. struct workqueue_struct *q;
  353. /* datasheet startup in numbered steps, refer to page 3-77 */
  354. /* 2. */
  355. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  356. /* The default of this register should be 4, but I get 0 instead.
  357. * Set this register to 4 manually. */
  358. cx25840_write(client, 0x000, 0x04);
  359. /* 3. */
  360. init_dll1(client);
  361. init_dll2(client);
  362. cx25840_write(client, 0x136, 0x0a);
  363. /* 4. */
  364. cx25840_write(client, 0x13c, 0x01);
  365. cx25840_write(client, 0x13c, 0x00);
  366. /* 5. */
  367. /* Do the firmware load in a work handler to prevent.
  368. Otherwise the kernel is blocked waiting for the
  369. bit-banging i2c interface to finish uploading the
  370. firmware. */
  371. INIT_WORK(&state->fw_work, cx25840_work_handler);
  372. init_waitqueue_head(&state->fw_wait);
  373. q = create_singlethread_workqueue("cx25840_fw");
  374. prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
  375. queue_work(q, &state->fw_work);
  376. schedule();
  377. finish_wait(&state->fw_wait, &wait);
  378. destroy_workqueue(q);
  379. /* 6. */
  380. cx25840_write(client, 0x115, 0x8c);
  381. cx25840_write(client, 0x116, 0x07);
  382. cx25840_write(client, 0x118, 0x02);
  383. /* 7. */
  384. cx25840_write(client, 0x4a5, 0x80);
  385. cx25840_write(client, 0x4a5, 0x00);
  386. cx25840_write(client, 0x402, 0x00);
  387. /* 8. */
  388. cx25840_and_or(client, 0x401, ~0x18, 0);
  389. cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
  390. /* steps 8c and 8d are done in change_input() */
  391. /* 10. */
  392. cx25840_write(client, 0x8d3, 0x1f);
  393. cx25840_write(client, 0x8e3, 0x03);
  394. cx25840_std_setup(client);
  395. /* trial and error says these are needed to get audio */
  396. cx25840_write(client, 0x914, 0xa0);
  397. cx25840_write(client, 0x918, 0xa0);
  398. cx25840_write(client, 0x919, 0x01);
  399. /* stereo prefered */
  400. cx25840_write(client, 0x809, 0x04);
  401. /* AC97 shift */
  402. cx25840_write(client, 0x8cf, 0x0f);
  403. /* (re)set input */
  404. set_input(client, state->vid_input, state->aud_input);
  405. /* start microcontroller */
  406. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  407. }
  408. static void cx23885_initialize(struct i2c_client *client)
  409. {
  410. DEFINE_WAIT(wait);
  411. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  412. struct workqueue_struct *q;
  413. /*
  414. * Come out of digital power down
  415. * The CX23888, at least, needs this, otherwise registers aside from
  416. * 0x0-0x2 can't be read or written.
  417. */
  418. cx25840_write(client, 0x000, 0);
  419. /* Internal Reset */
  420. cx25840_and_or(client, 0x102, ~0x01, 0x01);
  421. cx25840_and_or(client, 0x102, ~0x01, 0x00);
  422. /* Stop microcontroller */
  423. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  424. /* DIF in reset? */
  425. cx25840_write(client, 0x398, 0);
  426. /*
  427. * Trust the default xtal, no division
  428. * '885: 28.636363... MHz
  429. * '887: 25.000000 MHz
  430. * '888: 50.000000 MHz
  431. */
  432. cx25840_write(client, 0x2, 0x76);
  433. /* Power up all the PLL's and DLL */
  434. cx25840_write(client, 0x1, 0x40);
  435. /* Sys PLL */
  436. switch (state->id) {
  437. case V4L2_IDENT_CX23888_AV:
  438. /*
  439. * 50.0 MHz * (0xb + 0xe8ba26/0x2000000)/4 = 5 * 28.636363 MHz
  440. * 572.73 MHz before post divide
  441. */
  442. cx25840_write4(client, 0x11c, 0x00e8ba26);
  443. cx25840_write4(client, 0x118, 0x0000040b);
  444. break;
  445. case V4L2_IDENT_CX23887_AV:
  446. /*
  447. * 25.0 MHz * (0x16 + 0x1d1744c/0x2000000)/4 = 5 * 28.636363 MHz
  448. * 572.73 MHz before post divide
  449. */
  450. cx25840_write4(client, 0x11c, 0x01d1744c);
  451. cx25840_write4(client, 0x118, 0x00000416);
  452. break;
  453. case V4L2_IDENT_CX23885_AV:
  454. default:
  455. /*
  456. * 28.636363 MHz * (0x14 + 0x0/0x2000000)/4 = 5 * 28.636363 MHz
  457. * 572.73 MHz before post divide
  458. */
  459. cx25840_write4(client, 0x11c, 0x00000000);
  460. cx25840_write4(client, 0x118, 0x00000414);
  461. break;
  462. }
  463. /* Disable DIF bypass */
  464. cx25840_write4(client, 0x33c, 0x00000001);
  465. /* DIF Src phase inc */
  466. cx25840_write4(client, 0x340, 0x0df7df83);
  467. /*
  468. * Vid PLL
  469. * Setup for a BT.656 pixel clock of 13.5 Mpixels/second
  470. *
  471. * 28.636363 MHz * (0xf + 0x02be2c9/0x2000000)/4 = 8 * 13.5 MHz
  472. * 432.0 MHz before post divide
  473. */
  474. cx25840_write4(client, 0x10c, 0x002be2c9);
  475. cx25840_write4(client, 0x108, 0x0000040f);
  476. /* Luma */
  477. cx25840_write4(client, 0x414, 0x00107d12);
  478. /* Chroma */
  479. cx25840_write4(client, 0x420, 0x3d008282);
  480. /*
  481. * Aux PLL
  482. * Initial setup for audio sample clock:
  483. * 48 ksps, 16 bits/sample, x160 multiplier = 122.88 MHz
  484. * Intial I2S output/master clock(?):
  485. * 48 ksps, 16 bits/sample, x16 multiplier = 12.288 MHz
  486. */
  487. switch (state->id) {
  488. case V4L2_IDENT_CX23888_AV:
  489. /*
  490. * 50.0 MHz * (0x7 + 0x0bedfa4/0x2000000)/3 = 122.88 MHz
  491. * 368.64 MHz before post divide
  492. * 122.88 MHz / 0xa = 12.288 MHz
  493. */
  494. cx25840_write4(client, 0x114, 0x00bedfa4);
  495. cx25840_write4(client, 0x110, 0x000a0307);
  496. break;
  497. case V4L2_IDENT_CX23887_AV:
  498. /*
  499. * 25.0 MHz * (0xe + 0x17dbf48/0x2000000)/3 = 122.88 MHz
  500. * 368.64 MHz before post divide
  501. * 122.88 MHz / 0xa = 12.288 MHz
  502. */
  503. cx25840_write4(client, 0x114, 0x017dbf48);
  504. cx25840_write4(client, 0x110, 0x000a030e);
  505. break;
  506. case V4L2_IDENT_CX23885_AV:
  507. default:
  508. /*
  509. * 28.636363 MHz * (0xc + 0x1bf0c9e/0x2000000)/3 = 122.88 MHz
  510. * 368.64 MHz before post divide
  511. * 122.88 MHz / 0xa = 12.288 MHz
  512. */
  513. cx25840_write4(client, 0x114, 0x01bf0c9e);
  514. cx25840_write4(client, 0x110, 0x000a030c);
  515. break;
  516. };
  517. /* ADC2 input select */
  518. cx25840_write(client, 0x102, 0x10);
  519. /* VIN1 & VIN5 */
  520. cx25840_write(client, 0x103, 0x11);
  521. /* Enable format auto detect */
  522. cx25840_write(client, 0x400, 0);
  523. /* Fast subchroma lock */
  524. /* White crush, Chroma AGC & Chroma Killer enabled */
  525. cx25840_write(client, 0x401, 0xe8);
  526. /* Select AFE clock pad output source */
  527. cx25840_write(client, 0x144, 0x05);
  528. /* Drive GPIO2 direction and values for HVR1700
  529. * where an onboard mux selects the output of demodulator
  530. * vs the 417. Failure to set this results in no DTV.
  531. * It's safe to set this across all Hauppauge boards
  532. * currently, regardless of the board type.
  533. */
  534. cx25840_write(client, 0x160, 0x1d);
  535. cx25840_write(client, 0x164, 0x00);
  536. /* Do the firmware load in a work handler to prevent.
  537. Otherwise the kernel is blocked waiting for the
  538. bit-banging i2c interface to finish uploading the
  539. firmware. */
  540. INIT_WORK(&state->fw_work, cx25840_work_handler);
  541. init_waitqueue_head(&state->fw_wait);
  542. q = create_singlethread_workqueue("cx25840_fw");
  543. prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
  544. queue_work(q, &state->fw_work);
  545. schedule();
  546. finish_wait(&state->fw_wait, &wait);
  547. destroy_workqueue(q);
  548. cx25840_std_setup(client);
  549. /* (re)set input */
  550. set_input(client, state->vid_input, state->aud_input);
  551. /* start microcontroller */
  552. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  553. /* Disable and clear video interrupts - we don't use them */
  554. cx25840_write4(client, CX25840_VID_INT_STAT_REG, 0xffffffff);
  555. /* Disable and clear audio interrupts - we don't use them */
  556. cx25840_write(client, CX25840_AUD_INT_CTRL_REG, 0xff);
  557. cx25840_write(client, CX25840_AUD_INT_STAT_REG, 0xff);
  558. }
  559. /* ----------------------------------------------------------------------- */
  560. static void cx231xx_initialize(struct i2c_client *client)
  561. {
  562. DEFINE_WAIT(wait);
  563. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  564. struct workqueue_struct *q;
  565. /* Internal Reset */
  566. cx25840_and_or(client, 0x102, ~0x01, 0x01);
  567. cx25840_and_or(client, 0x102, ~0x01, 0x00);
  568. /* Stop microcontroller */
  569. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  570. /* DIF in reset? */
  571. cx25840_write(client, 0x398, 0);
  572. /* Trust the default xtal, no division */
  573. /* This changes for the cx23888 products */
  574. cx25840_write(client, 0x2, 0x76);
  575. /* Bring down the regulator for AUX clk */
  576. cx25840_write(client, 0x1, 0x40);
  577. /* Disable DIF bypass */
  578. cx25840_write4(client, 0x33c, 0x00000001);
  579. /* DIF Src phase inc */
  580. cx25840_write4(client, 0x340, 0x0df7df83);
  581. /* Luma */
  582. cx25840_write4(client, 0x414, 0x00107d12);
  583. /* Chroma */
  584. cx25840_write4(client, 0x420, 0x3d008282);
  585. /* ADC2 input select */
  586. cx25840_write(client, 0x102, 0x10);
  587. /* VIN1 & VIN5 */
  588. cx25840_write(client, 0x103, 0x11);
  589. /* Enable format auto detect */
  590. cx25840_write(client, 0x400, 0);
  591. /* Fast subchroma lock */
  592. /* White crush, Chroma AGC & Chroma Killer enabled */
  593. cx25840_write(client, 0x401, 0xe8);
  594. /* Do the firmware load in a work handler to prevent.
  595. Otherwise the kernel is blocked waiting for the
  596. bit-banging i2c interface to finish uploading the
  597. firmware. */
  598. INIT_WORK(&state->fw_work, cx25840_work_handler);
  599. init_waitqueue_head(&state->fw_wait);
  600. q = create_singlethread_workqueue("cx25840_fw");
  601. prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
  602. queue_work(q, &state->fw_work);
  603. schedule();
  604. finish_wait(&state->fw_wait, &wait);
  605. destroy_workqueue(q);
  606. cx25840_std_setup(client);
  607. /* (re)set input */
  608. set_input(client, state->vid_input, state->aud_input);
  609. /* start microcontroller */
  610. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  611. }
  612. /* ----------------------------------------------------------------------- */
  613. void cx25840_std_setup(struct i2c_client *client)
  614. {
  615. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  616. v4l2_std_id std = state->std;
  617. int hblank, hactive, burst, vblank, vactive, sc;
  618. int vblank656, src_decimation;
  619. int luma_lpf, uv_lpf, comb;
  620. u32 pll_int, pll_frac, pll_post;
  621. /* datasheet startup, step 8d */
  622. if (std & ~V4L2_STD_NTSC)
  623. cx25840_write(client, 0x49f, 0x11);
  624. else
  625. cx25840_write(client, 0x49f, 0x14);
  626. if (std & V4L2_STD_625_50) {
  627. hblank = 132;
  628. hactive = 720;
  629. burst = 93;
  630. vblank = 36;
  631. vactive = 580;
  632. vblank656 = 40;
  633. src_decimation = 0x21f;
  634. luma_lpf = 2;
  635. if (std & V4L2_STD_SECAM) {
  636. uv_lpf = 0;
  637. comb = 0;
  638. sc = 0x0a425f;
  639. } else if (std == V4L2_STD_PAL_Nc) {
  640. uv_lpf = 1;
  641. comb = 0x20;
  642. sc = 556453;
  643. } else {
  644. uv_lpf = 1;
  645. comb = 0x20;
  646. sc = 688739;
  647. }
  648. } else {
  649. hactive = 720;
  650. hblank = 122;
  651. vactive = 487;
  652. luma_lpf = 1;
  653. uv_lpf = 1;
  654. src_decimation = 0x21f;
  655. if (std == V4L2_STD_PAL_60) {
  656. vblank = 26;
  657. vblank656 = 26;
  658. burst = 0x5b;
  659. luma_lpf = 2;
  660. comb = 0x20;
  661. sc = 688739;
  662. } else if (std == V4L2_STD_PAL_M) {
  663. vblank = 20;
  664. vblank656 = 24;
  665. burst = 0x61;
  666. comb = 0x20;
  667. sc = 555452;
  668. } else {
  669. vblank = 26;
  670. vblank656 = 26;
  671. burst = 0x5b;
  672. comb = 0x66;
  673. sc = 556063;
  674. }
  675. }
  676. /* DEBUG: Displays configured PLL frequency */
  677. if (!is_cx231xx(state)) {
  678. pll_int = cx25840_read(client, 0x108);
  679. pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;
  680. pll_post = cx25840_read(client, 0x109);
  681. v4l_dbg(1, cx25840_debug, client,
  682. "PLL regs = int: %u, frac: %u, post: %u\n",
  683. pll_int, pll_frac, pll_post);
  684. if (pll_post) {
  685. int fin, fsc;
  686. int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;
  687. pll /= pll_post;
  688. v4l_dbg(1, cx25840_debug, client, "PLL = %d.%06d MHz\n",
  689. pll / 1000000, pll % 1000000);
  690. v4l_dbg(1, cx25840_debug, client, "PLL/8 = %d.%06d MHz\n",
  691. pll / 8000000, (pll / 8) % 1000000);
  692. fin = ((u64)src_decimation * pll) >> 12;
  693. v4l_dbg(1, cx25840_debug, client,
  694. "ADC Sampling freq = %d.%06d MHz\n",
  695. fin / 1000000, fin % 1000000);
  696. fsc = (((u64)sc) * pll) >> 24L;
  697. v4l_dbg(1, cx25840_debug, client,
  698. "Chroma sub-carrier freq = %d.%06d MHz\n",
  699. fsc / 1000000, fsc % 1000000);
  700. v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "
  701. "vblank %i, vactive %i, vblank656 %i, src_dec %i, "
  702. "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, "
  703. "sc 0x%06x\n",
  704. hblank, hactive, vblank, vactive, vblank656,
  705. src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
  706. }
  707. }
  708. /* Sets horizontal blanking delay and active lines */
  709. cx25840_write(client, 0x470, hblank);
  710. cx25840_write(client, 0x471,
  711. 0xff & (((hblank >> 8) & 0x3) | (hactive << 4)));
  712. cx25840_write(client, 0x472, hactive >> 4);
  713. /* Sets burst gate delay */
  714. cx25840_write(client, 0x473, burst);
  715. /* Sets vertical blanking delay and active duration */
  716. cx25840_write(client, 0x474, vblank);
  717. cx25840_write(client, 0x475,
  718. 0xff & (((vblank >> 8) & 0x3) | (vactive << 4)));
  719. cx25840_write(client, 0x476, vactive >> 4);
  720. cx25840_write(client, 0x477, vblank656);
  721. /* Sets src decimation rate */
  722. cx25840_write(client, 0x478, 0xff & src_decimation);
  723. cx25840_write(client, 0x479, 0xff & (src_decimation >> 8));
  724. /* Sets Luma and UV Low pass filters */
  725. cx25840_write(client, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
  726. /* Enables comb filters */
  727. cx25840_write(client, 0x47b, comb);
  728. /* Sets SC Step*/
  729. cx25840_write(client, 0x47c, sc);
  730. cx25840_write(client, 0x47d, 0xff & sc >> 8);
  731. cx25840_write(client, 0x47e, 0xff & sc >> 16);
  732. /* Sets VBI parameters */
  733. if (std & V4L2_STD_625_50) {
  734. cx25840_write(client, 0x47f, 0x01);
  735. state->vbi_line_offset = 5;
  736. } else {
  737. cx25840_write(client, 0x47f, 0x00);
  738. state->vbi_line_offset = 8;
  739. }
  740. }
  741. /* ----------------------------------------------------------------------- */
  742. static void input_change(struct i2c_client *client)
  743. {
  744. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  745. v4l2_std_id std = state->std;
  746. /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
  747. if (std & V4L2_STD_SECAM) {
  748. cx25840_write(client, 0x402, 0);
  749. }
  750. else {
  751. cx25840_write(client, 0x402, 0x04);
  752. cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  753. }
  754. cx25840_and_or(client, 0x401, ~0x60, 0);
  755. cx25840_and_or(client, 0x401, ~0x60, 0x60);
  756. /* Don't write into audio registers on cx2583x chips */
  757. if (is_cx2583x(state))
  758. return;
  759. cx25840_and_or(client, 0x810, ~0x01, 1);
  760. if (state->radio) {
  761. cx25840_write(client, 0x808, 0xf9);
  762. cx25840_write(client, 0x80b, 0x00);
  763. }
  764. else if (std & V4L2_STD_525_60) {
  765. /* Certain Hauppauge PVR150 models have a hardware bug
  766. that causes audio to drop out. For these models the
  767. audio standard must be set explicitly.
  768. To be precise: it affects cards with tuner models
  769. 85, 99 and 112 (model numbers from tveeprom). */
  770. int hw_fix = state->pvr150_workaround;
  771. if (std == V4L2_STD_NTSC_M_JP) {
  772. /* Japan uses EIAJ audio standard */
  773. cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
  774. } else if (std == V4L2_STD_NTSC_M_KR) {
  775. /* South Korea uses A2 audio standard */
  776. cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
  777. } else {
  778. /* Others use the BTSC audio standard */
  779. cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
  780. }
  781. cx25840_write(client, 0x80b, 0x00);
  782. } else if (std & V4L2_STD_PAL) {
  783. /* Autodetect audio standard and audio system */
  784. cx25840_write(client, 0x808, 0xff);
  785. /* Since system PAL-L is pretty much non-existant and
  786. not used by any public broadcast network, force
  787. 6.5 MHz carrier to be interpreted as System DK,
  788. this avoids DK audio detection instability */
  789. cx25840_write(client, 0x80b, 0x00);
  790. } else if (std & V4L2_STD_SECAM) {
  791. /* Autodetect audio standard and audio system */
  792. cx25840_write(client, 0x808, 0xff);
  793. /* If only one of SECAM-DK / SECAM-L is required, then force
  794. 6.5MHz carrier, else autodetect it */
  795. if ((std & V4L2_STD_SECAM_DK) &&
  796. !(std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
  797. /* 6.5 MHz carrier to be interpreted as System DK */
  798. cx25840_write(client, 0x80b, 0x00);
  799. } else if (!(std & V4L2_STD_SECAM_DK) &&
  800. (std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
  801. /* 6.5 MHz carrier to be interpreted as System L */
  802. cx25840_write(client, 0x80b, 0x08);
  803. } else {
  804. /* 6.5 MHz carrier to be autodetected */
  805. cx25840_write(client, 0x80b, 0x10);
  806. }
  807. }
  808. cx25840_and_or(client, 0x810, ~0x01, 0);
  809. }
  810. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  811. enum cx25840_audio_input aud_input)
  812. {
  813. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  814. u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
  815. vid_input <= CX25840_COMPOSITE8);
  816. u8 is_component = (vid_input & CX25840_COMPONENT_ON) ==
  817. CX25840_COMPONENT_ON;
  818. int luma = vid_input & 0xf0;
  819. int chroma = vid_input & 0xf00;
  820. u8 reg;
  821. v4l_dbg(1, cx25840_debug, client,
  822. "decoder set video input %d, audio input %d\n",
  823. vid_input, aud_input);
  824. if (vid_input >= CX25840_VIN1_CH1) {
  825. v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
  826. vid_input);
  827. reg = vid_input & 0xff;
  828. is_composite = !is_component &&
  829. ((vid_input & CX25840_SVIDEO_ON) != CX25840_SVIDEO_ON);
  830. v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
  831. reg, is_composite);
  832. } else if (is_composite) {
  833. reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
  834. } else {
  835. if ((vid_input & ~0xff0) ||
  836. luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA8 ||
  837. chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
  838. v4l_err(client, "0x%04x is not a valid video input!\n",
  839. vid_input);
  840. return -EINVAL;
  841. }
  842. reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
  843. if (chroma >= CX25840_SVIDEO_CHROMA7) {
  844. reg &= 0x3f;
  845. reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
  846. } else {
  847. reg &= 0xcf;
  848. reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
  849. }
  850. }
  851. /* The caller has previously prepared the correct routing
  852. * configuration in reg (for the cx23885) so we have no
  853. * need to attempt to flip bits for earlier av decoders.
  854. */
  855. if (!is_cx2388x(state) && !is_cx231xx(state)) {
  856. switch (aud_input) {
  857. case CX25840_AUDIO_SERIAL:
  858. /* do nothing, use serial audio input */
  859. break;
  860. case CX25840_AUDIO4: reg &= ~0x30; break;
  861. case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  862. case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  863. case CX25840_AUDIO7: reg &= ~0xc0; break;
  864. case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  865. default:
  866. v4l_err(client, "0x%04x is not a valid audio input!\n",
  867. aud_input);
  868. return -EINVAL;
  869. }
  870. }
  871. cx25840_write(client, 0x103, reg);
  872. /* Set INPUT_MODE to Composite, S-Video or Component */
  873. if (is_component)
  874. cx25840_and_or(client, 0x401, ~0x6, 0x6);
  875. else
  876. cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
  877. if (!is_cx2388x(state) && !is_cx231xx(state)) {
  878. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  879. cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
  880. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
  881. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  882. cx25840_and_or(client, 0x102, ~0x4, 4);
  883. else
  884. cx25840_and_or(client, 0x102, ~0x4, 0);
  885. } else {
  886. /* Set DUAL_MODE_ADC2 to 1 if component*/
  887. cx25840_and_or(client, 0x102, ~0x4, is_component ? 0x4 : 0x0);
  888. if (is_composite) {
  889. /* ADC2 input select channel 2 */
  890. cx25840_and_or(client, 0x102, ~0x2, 0);
  891. } else if (!is_component) {
  892. /* S-Video */
  893. if (chroma >= CX25840_SVIDEO_CHROMA7) {
  894. /* ADC2 input select channel 3 */
  895. cx25840_and_or(client, 0x102, ~0x2, 2);
  896. } else {
  897. /* ADC2 input select channel 2 */
  898. cx25840_and_or(client, 0x102, ~0x2, 0);
  899. }
  900. }
  901. }
  902. state->vid_input = vid_input;
  903. state->aud_input = aud_input;
  904. cx25840_audio_set_path(client);
  905. input_change(client);
  906. if (is_cx2388x(state)) {
  907. /* Audio channel 1 src : Parallel 1 */
  908. cx25840_write(client, 0x124, 0x03);
  909. /* Select AFE clock pad output source */
  910. cx25840_write(client, 0x144, 0x05);
  911. /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
  912. cx25840_write(client, 0x914, 0xa0);
  913. /* I2S_OUT_CTL:
  914. * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
  915. * I2S_OUT_MASTER_MODE = Master
  916. */
  917. cx25840_write(client, 0x918, 0xa0);
  918. cx25840_write(client, 0x919, 0x01);
  919. } else if (is_cx231xx(state)) {
  920. /* Audio channel 1 src : Parallel 1 */
  921. cx25840_write(client, 0x124, 0x03);
  922. /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
  923. cx25840_write(client, 0x914, 0xa0);
  924. /* I2S_OUT_CTL:
  925. * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
  926. * I2S_OUT_MASTER_MODE = Master
  927. */
  928. cx25840_write(client, 0x918, 0xa0);
  929. cx25840_write(client, 0x919, 0x01);
  930. }
  931. return 0;
  932. }
  933. /* ----------------------------------------------------------------------- */
  934. static int set_v4lstd(struct i2c_client *client)
  935. {
  936. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  937. u8 fmt = 0; /* zero is autodetect */
  938. u8 pal_m = 0;
  939. /* First tests should be against specific std */
  940. if (state->std == V4L2_STD_NTSC_M_JP) {
  941. fmt = 0x2;
  942. } else if (state->std == V4L2_STD_NTSC_443) {
  943. fmt = 0x3;
  944. } else if (state->std == V4L2_STD_PAL_M) {
  945. pal_m = 1;
  946. fmt = 0x5;
  947. } else if (state->std == V4L2_STD_PAL_N) {
  948. fmt = 0x6;
  949. } else if (state->std == V4L2_STD_PAL_Nc) {
  950. fmt = 0x7;
  951. } else if (state->std == V4L2_STD_PAL_60) {
  952. fmt = 0x8;
  953. } else {
  954. /* Then, test against generic ones */
  955. if (state->std & V4L2_STD_NTSC)
  956. fmt = 0x1;
  957. else if (state->std & V4L2_STD_PAL)
  958. fmt = 0x4;
  959. else if (state->std & V4L2_STD_SECAM)
  960. fmt = 0xc;
  961. }
  962. v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
  963. /* Follow step 9 of section 3.16 in the cx25840 datasheet.
  964. Without this PAL may display a vertical ghosting effect.
  965. This happens for example with the Yuan MPC622. */
  966. if (fmt >= 4 && fmt < 8) {
  967. /* Set format to NTSC-M */
  968. cx25840_and_or(client, 0x400, ~0xf, 1);
  969. /* Turn off LCOMB */
  970. cx25840_and_or(client, 0x47b, ~6, 0);
  971. }
  972. cx25840_and_or(client, 0x400, ~0xf, fmt);
  973. cx25840_and_or(client, 0x403, ~0x3, pal_m);
  974. cx25840_std_setup(client);
  975. if (!is_cx2583x(state))
  976. input_change(client);
  977. return 0;
  978. }
  979. /* ----------------------------------------------------------------------- */
  980. static int cx25840_s_ctrl(struct v4l2_ctrl *ctrl)
  981. {
  982. struct v4l2_subdev *sd = to_sd(ctrl);
  983. struct i2c_client *client = v4l2_get_subdevdata(sd);
  984. switch (ctrl->id) {
  985. case V4L2_CID_BRIGHTNESS:
  986. cx25840_write(client, 0x414, ctrl->val - 128);
  987. break;
  988. case V4L2_CID_CONTRAST:
  989. cx25840_write(client, 0x415, ctrl->val << 1);
  990. break;
  991. case V4L2_CID_SATURATION:
  992. cx25840_write(client, 0x420, ctrl->val << 1);
  993. cx25840_write(client, 0x421, ctrl->val << 1);
  994. break;
  995. case V4L2_CID_HUE:
  996. cx25840_write(client, 0x422, ctrl->val);
  997. break;
  998. default:
  999. return -EINVAL;
  1000. }
  1001. return 0;
  1002. }
  1003. /* ----------------------------------------------------------------------- */
  1004. static int cx25840_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)
  1005. {
  1006. struct cx25840_state *state = to_state(sd);
  1007. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1008. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  1009. int is_50Hz = !(state->std & V4L2_STD_525_60);
  1010. if (fmt->code != V4L2_MBUS_FMT_FIXED)
  1011. return -EINVAL;
  1012. fmt->field = V4L2_FIELD_INTERLACED;
  1013. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1014. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  1015. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  1016. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  1017. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  1018. Vlines = fmt->height + (is_50Hz ? 4 : 7);
  1019. if ((fmt->width * 16 < Hsrc) || (Hsrc < fmt->width) ||
  1020. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  1021. v4l_err(client, "%dx%d is not a valid size!\n",
  1022. fmt->width, fmt->height);
  1023. return -ERANGE;
  1024. }
  1025. HSC = (Hsrc * (1 << 20)) / fmt->width - (1 << 20);
  1026. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  1027. VSC &= 0x1fff;
  1028. if (fmt->width >= 385)
  1029. filter = 0;
  1030. else if (fmt->width > 192)
  1031. filter = 1;
  1032. else if (fmt->width > 96)
  1033. filter = 2;
  1034. else
  1035. filter = 3;
  1036. v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
  1037. fmt->width, fmt->height, HSC, VSC);
  1038. /* HSCALE=HSC */
  1039. cx25840_write(client, 0x418, HSC & 0xff);
  1040. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  1041. cx25840_write(client, 0x41a, HSC >> 16);
  1042. /* VSCALE=VSC */
  1043. cx25840_write(client, 0x41c, VSC & 0xff);
  1044. cx25840_write(client, 0x41d, VSC >> 8);
  1045. /* VS_INTRLACE=1 VFILT=filter */
  1046. cx25840_write(client, 0x41e, 0x8 | filter);
  1047. return 0;
  1048. }
  1049. /* ----------------------------------------------------------------------- */
  1050. static void log_video_status(struct i2c_client *client)
  1051. {
  1052. static const char *const fmt_strs[] = {
  1053. "0x0",
  1054. "NTSC-M", "NTSC-J", "NTSC-4.43",
  1055. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  1056. "0x9", "0xA", "0xB",
  1057. "SECAM",
  1058. "0xD", "0xE", "0xF"
  1059. };
  1060. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  1061. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  1062. u8 gen_stat1 = cx25840_read(client, 0x40d);
  1063. u8 gen_stat2 = cx25840_read(client, 0x40e);
  1064. int vid_input = state->vid_input;
  1065. v4l_info(client, "Video signal: %spresent\n",
  1066. (gen_stat2 & 0x20) ? "" : "not ");
  1067. v4l_info(client, "Detected format: %s\n",
  1068. fmt_strs[gen_stat1 & 0xf]);
  1069. v4l_info(client, "Specified standard: %s\n",
  1070. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  1071. if (vid_input >= CX25840_COMPOSITE1 &&
  1072. vid_input <= CX25840_COMPOSITE8) {
  1073. v4l_info(client, "Specified video input: Composite %d\n",
  1074. vid_input - CX25840_COMPOSITE1 + 1);
  1075. } else {
  1076. v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  1077. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  1078. }
  1079. v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
  1080. }
  1081. /* ----------------------------------------------------------------------- */
  1082. static void log_audio_status(struct i2c_client *client)
  1083. {
  1084. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  1085. u8 download_ctl = cx25840_read(client, 0x803);
  1086. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  1087. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  1088. u8 audio_config = cx25840_read(client, 0x808);
  1089. u8 pref_mode = cx25840_read(client, 0x809);
  1090. u8 afc0 = cx25840_read(client, 0x80b);
  1091. u8 mute_ctl = cx25840_read(client, 0x8d3);
  1092. int aud_input = state->aud_input;
  1093. char *p;
  1094. switch (mod_det_stat0) {
  1095. case 0x00: p = "mono"; break;
  1096. case 0x01: p = "stereo"; break;
  1097. case 0x02: p = "dual"; break;
  1098. case 0x04: p = "tri"; break;
  1099. case 0x10: p = "mono with SAP"; break;
  1100. case 0x11: p = "stereo with SAP"; break;
  1101. case 0x12: p = "dual with SAP"; break;
  1102. case 0x14: p = "tri with SAP"; break;
  1103. case 0xfe: p = "forced mode"; break;
  1104. default: p = "not defined";
  1105. }
  1106. v4l_info(client, "Detected audio mode: %s\n", p);
  1107. switch (mod_det_stat1) {
  1108. case 0x00: p = "not defined"; break;
  1109. case 0x01: p = "EIAJ"; break;
  1110. case 0x02: p = "A2-M"; break;
  1111. case 0x03: p = "A2-BG"; break;
  1112. case 0x04: p = "A2-DK1"; break;
  1113. case 0x05: p = "A2-DK2"; break;
  1114. case 0x06: p = "A2-DK3"; break;
  1115. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  1116. case 0x08: p = "AM-L"; break;
  1117. case 0x09: p = "NICAM-BG"; break;
  1118. case 0x0a: p = "NICAM-DK"; break;
  1119. case 0x0b: p = "NICAM-I"; break;
  1120. case 0x0c: p = "NICAM-L"; break;
  1121. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  1122. case 0x0e: p = "IF FM Radio"; break;
  1123. case 0x0f: p = "BTSC"; break;
  1124. case 0x10: p = "high-deviation FM"; break;
  1125. case 0x11: p = "very high-deviation FM"; break;
  1126. case 0xfd: p = "unknown audio standard"; break;
  1127. case 0xfe: p = "forced audio standard"; break;
  1128. case 0xff: p = "no detected audio standard"; break;
  1129. default: p = "not defined";
  1130. }
  1131. v4l_info(client, "Detected audio standard: %s\n", p);
  1132. v4l_info(client, "Audio microcontroller: %s\n",
  1133. (download_ctl & 0x10) ?
  1134. ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
  1135. switch (audio_config >> 4) {
  1136. case 0x00: p = "undefined"; break;
  1137. case 0x01: p = "BTSC"; break;
  1138. case 0x02: p = "EIAJ"; break;
  1139. case 0x03: p = "A2-M"; break;
  1140. case 0x04: p = "A2-BG"; break;
  1141. case 0x05: p = "A2-DK1"; break;
  1142. case 0x06: p = "A2-DK2"; break;
  1143. case 0x07: p = "A2-DK3"; break;
  1144. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  1145. case 0x09: p = "AM-L"; break;
  1146. case 0x0a: p = "NICAM-BG"; break;
  1147. case 0x0b: p = "NICAM-DK"; break;
  1148. case 0x0c: p = "NICAM-I"; break;
  1149. case 0x0d: p = "NICAM-L"; break;
  1150. case 0x0e: p = "FM radio"; break;
  1151. case 0x0f: p = "automatic detection"; break;
  1152. default: p = "undefined";
  1153. }
  1154. v4l_info(client, "Configured audio standard: %s\n", p);
  1155. if ((audio_config >> 4) < 0xF) {
  1156. switch (audio_config & 0xF) {
  1157. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  1158. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  1159. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  1160. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  1161. case 0x04: p = "STEREO"; break;
  1162. case 0x05: p = "DUAL1 (AB)"; break;
  1163. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  1164. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  1165. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  1166. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  1167. case 0x0a: p = "SAP"; break;
  1168. default: p = "undefined";
  1169. }
  1170. v4l_info(client, "Configured audio mode: %s\n", p);
  1171. } else {
  1172. switch (audio_config & 0xF) {
  1173. case 0x00: p = "BG"; break;
  1174. case 0x01: p = "DK1"; break;
  1175. case 0x02: p = "DK2"; break;
  1176. case 0x03: p = "DK3"; break;
  1177. case 0x04: p = "I"; break;
  1178. case 0x05: p = "L"; break;
  1179. case 0x06: p = "BTSC"; break;
  1180. case 0x07: p = "EIAJ"; break;
  1181. case 0x08: p = "A2-M"; break;
  1182. case 0x09: p = "FM Radio"; break;
  1183. case 0x0f: p = "automatic standard and mode detection"; break;
  1184. default: p = "undefined";
  1185. }
  1186. v4l_info(client, "Configured audio system: %s\n", p);
  1187. }
  1188. if (aud_input) {
  1189. v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
  1190. } else {
  1191. v4l_info(client, "Specified audio input: External\n");
  1192. }
  1193. switch (pref_mode & 0xf) {
  1194. case 0: p = "mono/language A"; break;
  1195. case 1: p = "language B"; break;
  1196. case 2: p = "language C"; break;
  1197. case 3: p = "analog fallback"; break;
  1198. case 4: p = "stereo"; break;
  1199. case 5: p = "language AC"; break;
  1200. case 6: p = "language BC"; break;
  1201. case 7: p = "language AB"; break;
  1202. default: p = "undefined";
  1203. }
  1204. v4l_info(client, "Preferred audio mode: %s\n", p);
  1205. if ((audio_config & 0xf) == 0xf) {
  1206. switch ((afc0 >> 3) & 0x3) {
  1207. case 0: p = "system DK"; break;
  1208. case 1: p = "system L"; break;
  1209. case 2: p = "autodetect"; break;
  1210. default: p = "undefined";
  1211. }
  1212. v4l_info(client, "Selected 65 MHz format: %s\n", p);
  1213. switch (afc0 & 0x7) {
  1214. case 0: p = "chroma"; break;
  1215. case 1: p = "BTSC"; break;
  1216. case 2: p = "EIAJ"; break;
  1217. case 3: p = "A2-M"; break;
  1218. case 4: p = "autodetect"; break;
  1219. default: p = "undefined";
  1220. }
  1221. v4l_info(client, "Selected 45 MHz format: %s\n", p);
  1222. }
  1223. }
  1224. /* ----------------------------------------------------------------------- */
  1225. /* This load_fw operation must be called to load the driver's firmware.
  1226. Without this the audio standard detection will fail and you will
  1227. only get mono.
  1228. Since loading the firmware is often problematic when the driver is
  1229. compiled into the kernel I recommend postponing calling this function
  1230. until the first open of the video device. Another reason for
  1231. postponing it is that loading this firmware takes a long time (seconds)
  1232. due to the slow i2c bus speed. So it will speed up the boot process if
  1233. you can avoid loading the fw as long as the video device isn't used. */
  1234. static int cx25840_load_fw(struct v4l2_subdev *sd)
  1235. {
  1236. struct cx25840_state *state = to_state(sd);
  1237. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1238. if (!state->is_initialized) {
  1239. /* initialize and load firmware */
  1240. state->is_initialized = 1;
  1241. if (is_cx2583x(state))
  1242. cx25836_initialize(client);
  1243. else if (is_cx2388x(state))
  1244. cx23885_initialize(client);
  1245. else if (is_cx231xx(state))
  1246. cx231xx_initialize(client);
  1247. else
  1248. cx25840_initialize(client);
  1249. }
  1250. return 0;
  1251. }
  1252. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1253. static int cx25840_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  1254. {
  1255. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1256. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  1257. return -EINVAL;
  1258. if (!capable(CAP_SYS_ADMIN))
  1259. return -EPERM;
  1260. reg->size = 1;
  1261. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  1262. return 0;
  1263. }
  1264. static int cx25840_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  1265. {
  1266. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1267. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  1268. return -EINVAL;
  1269. if (!capable(CAP_SYS_ADMIN))
  1270. return -EPERM;
  1271. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  1272. return 0;
  1273. }
  1274. #endif
  1275. static int cx25840_s_audio_stream(struct v4l2_subdev *sd, int enable)
  1276. {
  1277. struct cx25840_state *state = to_state(sd);
  1278. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1279. u8 v;
  1280. if (is_cx2583x(state) || is_cx2388x(state) || is_cx231xx(state))
  1281. return 0;
  1282. v4l_dbg(1, cx25840_debug, client, "%s audio output\n",
  1283. enable ? "enable" : "disable");
  1284. if (enable) {
  1285. v = cx25840_read(client, 0x115) | 0x80;
  1286. cx25840_write(client, 0x115, v);
  1287. v = cx25840_read(client, 0x116) | 0x03;
  1288. cx25840_write(client, 0x116, v);
  1289. } else {
  1290. v = cx25840_read(client, 0x115) & ~(0x80);
  1291. cx25840_write(client, 0x115, v);
  1292. v = cx25840_read(client, 0x116) & ~(0x03);
  1293. cx25840_write(client, 0x116, v);
  1294. }
  1295. return 0;
  1296. }
  1297. static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)
  1298. {
  1299. struct cx25840_state *state = to_state(sd);
  1300. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1301. u8 v;
  1302. v4l_dbg(1, cx25840_debug, client, "%s video output\n",
  1303. enable ? "enable" : "disable");
  1304. if (enable) {
  1305. if (is_cx2388x(state) || is_cx231xx(state)) {
  1306. v = cx25840_read(client, 0x421) | 0x0b;
  1307. cx25840_write(client, 0x421, v);
  1308. } else {
  1309. v = cx25840_read(client, 0x115) | 0x0c;
  1310. cx25840_write(client, 0x115, v);
  1311. v = cx25840_read(client, 0x116) | 0x04;
  1312. cx25840_write(client, 0x116, v);
  1313. }
  1314. } else {
  1315. if (is_cx2388x(state) || is_cx231xx(state)) {
  1316. v = cx25840_read(client, 0x421) & ~(0x0b);
  1317. cx25840_write(client, 0x421, v);
  1318. } else {
  1319. v = cx25840_read(client, 0x115) & ~(0x0c);
  1320. cx25840_write(client, 0x115, v);
  1321. v = cx25840_read(client, 0x116) & ~(0x04);
  1322. cx25840_write(client, 0x116, v);
  1323. }
  1324. }
  1325. return 0;
  1326. }
  1327. static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  1328. {
  1329. struct cx25840_state *state = to_state(sd);
  1330. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1331. if (state->radio == 0 && state->std == std)
  1332. return 0;
  1333. state->radio = 0;
  1334. state->std = std;
  1335. return set_v4lstd(client);
  1336. }
  1337. static int cx25840_s_radio(struct v4l2_subdev *sd)
  1338. {
  1339. struct cx25840_state *state = to_state(sd);
  1340. state->radio = 1;
  1341. return 0;
  1342. }
  1343. static int cx25840_s_video_routing(struct v4l2_subdev *sd,
  1344. u32 input, u32 output, u32 config)
  1345. {
  1346. struct cx25840_state *state = to_state(sd);
  1347. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1348. return set_input(client, input, state->aud_input);
  1349. }
  1350. static int cx25840_s_audio_routing(struct v4l2_subdev *sd,
  1351. u32 input, u32 output, u32 config)
  1352. {
  1353. struct cx25840_state *state = to_state(sd);
  1354. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1355. return set_input(client, state->vid_input, input);
  1356. }
  1357. static int cx25840_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
  1358. {
  1359. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1360. input_change(client);
  1361. return 0;
  1362. }
  1363. static int cx25840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  1364. {
  1365. struct cx25840_state *state = to_state(sd);
  1366. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1367. u8 vpres = cx25840_read(client, 0x40e) & 0x20;
  1368. u8 mode;
  1369. int val = 0;
  1370. if (state->radio)
  1371. return 0;
  1372. vt->signal = vpres ? 0xffff : 0x0;
  1373. if (is_cx2583x(state))
  1374. return 0;
  1375. vt->capability |=
  1376. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  1377. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  1378. mode = cx25840_read(client, 0x804);
  1379. /* get rxsubchans and audmode */
  1380. if ((mode & 0xf) == 1)
  1381. val |= V4L2_TUNER_SUB_STEREO;
  1382. else
  1383. val |= V4L2_TUNER_SUB_MONO;
  1384. if (mode == 2 || mode == 4)
  1385. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  1386. if (mode & 0x10)
  1387. val |= V4L2_TUNER_SUB_SAP;
  1388. vt->rxsubchans = val;
  1389. vt->audmode = state->audmode;
  1390. return 0;
  1391. }
  1392. static int cx25840_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  1393. {
  1394. struct cx25840_state *state = to_state(sd);
  1395. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1396. if (state->radio || is_cx2583x(state))
  1397. return 0;
  1398. switch (vt->audmode) {
  1399. case V4L2_TUNER_MODE_MONO:
  1400. /* mono -> mono
  1401. stereo -> mono
  1402. bilingual -> lang1 */
  1403. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  1404. break;
  1405. case V4L2_TUNER_MODE_STEREO:
  1406. case V4L2_TUNER_MODE_LANG1:
  1407. /* mono -> mono
  1408. stereo -> stereo
  1409. bilingual -> lang1 */
  1410. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  1411. break;
  1412. case V4L2_TUNER_MODE_LANG1_LANG2:
  1413. /* mono -> mono
  1414. stereo -> stereo
  1415. bilingual -> lang1/lang2 */
  1416. cx25840_and_or(client, 0x809, ~0xf, 0x07);
  1417. break;
  1418. case V4L2_TUNER_MODE_LANG2:
  1419. /* mono -> mono
  1420. stereo -> stereo
  1421. bilingual -> lang2 */
  1422. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  1423. break;
  1424. default:
  1425. return -EINVAL;
  1426. }
  1427. state->audmode = vt->audmode;
  1428. return 0;
  1429. }
  1430. static int cx25840_reset(struct v4l2_subdev *sd, u32 val)
  1431. {
  1432. struct cx25840_state *state = to_state(sd);
  1433. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1434. if (is_cx2583x(state))
  1435. cx25836_initialize(client);
  1436. else if (is_cx2388x(state))
  1437. cx23885_initialize(client);
  1438. else if (is_cx231xx(state))
  1439. cx231xx_initialize(client);
  1440. else
  1441. cx25840_initialize(client);
  1442. return 0;
  1443. }
  1444. static int cx25840_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  1445. {
  1446. struct cx25840_state *state = to_state(sd);
  1447. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1448. return v4l2_chip_ident_i2c_client(client, chip, state->id, state->rev);
  1449. }
  1450. static int cx25840_log_status(struct v4l2_subdev *sd)
  1451. {
  1452. struct cx25840_state *state = to_state(sd);
  1453. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1454. log_video_status(client);
  1455. if (!is_cx2583x(state))
  1456. log_audio_status(client);
  1457. cx25840_ir_log_status(sd);
  1458. v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
  1459. return 0;
  1460. }
  1461. static int cx25840_s_config(struct v4l2_subdev *sd, int irq, void *platform_data)
  1462. {
  1463. struct cx25840_state *state = to_state(sd);
  1464. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1465. if (platform_data) {
  1466. struct cx25840_platform_data *pdata = platform_data;
  1467. state->pvr150_workaround = pdata->pvr150_workaround;
  1468. set_input(client, state->vid_input, state->aud_input);
  1469. }
  1470. return 0;
  1471. }
  1472. static int cx23885_irq_handler(struct v4l2_subdev *sd, u32 status,
  1473. bool *handled)
  1474. {
  1475. struct cx25840_state *state = to_state(sd);
  1476. struct i2c_client *c = v4l2_get_subdevdata(sd);
  1477. u8 irq_stat, aud_stat, aud_en, ir_stat, ir_en;
  1478. u32 vid_stat, aud_mc_stat;
  1479. bool block_handled;
  1480. int ret = 0;
  1481. irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);
  1482. v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (entry): %s %s %s\n",
  1483. irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : " ",
  1484. irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : " ",
  1485. irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : " ");
  1486. if ((is_cx23885(state) || is_cx23887(state))) {
  1487. ir_stat = cx25840_read(c, CX25840_IR_STATS_REG);
  1488. ir_en = cx25840_read(c, CX25840_IR_IRQEN_REG);
  1489. v4l_dbg(2, cx25840_debug, c,
  1490. "AV Core ir IRQ status: %#04x disables: %#04x\n",
  1491. ir_stat, ir_en);
  1492. if (irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT) {
  1493. block_handled = false;
  1494. ret = cx25840_ir_irq_handler(sd,
  1495. status, &block_handled);
  1496. if (block_handled)
  1497. *handled = true;
  1498. }
  1499. }
  1500. aud_stat = cx25840_read(c, CX25840_AUD_INT_STAT_REG);
  1501. aud_en = cx25840_read(c, CX25840_AUD_INT_CTRL_REG);
  1502. v4l_dbg(2, cx25840_debug, c,
  1503. "AV Core audio IRQ status: %#04x disables: %#04x\n",
  1504. aud_stat, aud_en);
  1505. aud_mc_stat = cx25840_read4(c, CX23885_AUD_MC_INT_MASK_REG);
  1506. v4l_dbg(2, cx25840_debug, c,
  1507. "AV Core audio MC IRQ status: %#06x enables: %#06x\n",
  1508. aud_mc_stat >> CX23885_AUD_MC_INT_STAT_SHFT,
  1509. aud_mc_stat & CX23885_AUD_MC_INT_CTRL_BITS);
  1510. if (irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT) {
  1511. if (aud_stat) {
  1512. cx25840_write(c, CX25840_AUD_INT_STAT_REG, aud_stat);
  1513. *handled = true;
  1514. }
  1515. }
  1516. vid_stat = cx25840_read4(c, CX25840_VID_INT_STAT_REG);
  1517. v4l_dbg(2, cx25840_debug, c,
  1518. "AV Core video IRQ status: %#06x disables: %#06x\n",
  1519. vid_stat & CX25840_VID_INT_STAT_BITS,
  1520. vid_stat >> CX25840_VID_INT_MASK_SHFT);
  1521. if (irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT) {
  1522. if (vid_stat & CX25840_VID_INT_STAT_BITS) {
  1523. cx25840_write4(c, CX25840_VID_INT_STAT_REG, vid_stat);
  1524. *handled = true;
  1525. }
  1526. }
  1527. irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);
  1528. v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (exit): %s %s %s\n",
  1529. irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : " ",
  1530. irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : " ",
  1531. irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : " ");
  1532. return ret;
  1533. }
  1534. static int cx25840_irq_handler(struct v4l2_subdev *sd, u32 status,
  1535. bool *handled)
  1536. {
  1537. struct cx25840_state *state = to_state(sd);
  1538. *handled = false;
  1539. /* Only support the CX2388[578] AV Core for now */
  1540. if (is_cx2388x(state))
  1541. return cx23885_irq_handler(sd, status, handled);
  1542. return -ENODEV;
  1543. }
  1544. /* ----------------------------------------------------------------------- */
  1545. static const struct v4l2_ctrl_ops cx25840_ctrl_ops = {
  1546. .s_ctrl = cx25840_s_ctrl,
  1547. };
  1548. static const struct v4l2_subdev_core_ops cx25840_core_ops = {
  1549. .log_status = cx25840_log_status,
  1550. .s_config = cx25840_s_config,
  1551. .g_chip_ident = cx25840_g_chip_ident,
  1552. .g_ctrl = v4l2_subdev_g_ctrl,
  1553. .s_ctrl = v4l2_subdev_s_ctrl,
  1554. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  1555. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  1556. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  1557. .queryctrl = v4l2_subdev_queryctrl,
  1558. .querymenu = v4l2_subdev_querymenu,
  1559. .s_std = cx25840_s_std,
  1560. .reset = cx25840_reset,
  1561. .load_fw = cx25840_load_fw,
  1562. .s_io_pin_config = common_s_io_pin_config,
  1563. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1564. .g_register = cx25840_g_register,
  1565. .s_register = cx25840_s_register,
  1566. #endif
  1567. .interrupt_service_routine = cx25840_irq_handler,
  1568. };
  1569. static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {
  1570. .s_frequency = cx25840_s_frequency,
  1571. .s_radio = cx25840_s_radio,
  1572. .g_tuner = cx25840_g_tuner,
  1573. .s_tuner = cx25840_s_tuner,
  1574. };
  1575. static const struct v4l2_subdev_audio_ops cx25840_audio_ops = {
  1576. .s_clock_freq = cx25840_s_clock_freq,
  1577. .s_routing = cx25840_s_audio_routing,
  1578. .s_stream = cx25840_s_audio_stream,
  1579. };
  1580. static const struct v4l2_subdev_video_ops cx25840_video_ops = {
  1581. .s_routing = cx25840_s_video_routing,
  1582. .s_mbus_fmt = cx25840_s_mbus_fmt,
  1583. .s_stream = cx25840_s_stream,
  1584. };
  1585. static const struct v4l2_subdev_vbi_ops cx25840_vbi_ops = {
  1586. .decode_vbi_line = cx25840_decode_vbi_line,
  1587. .s_raw_fmt = cx25840_s_raw_fmt,
  1588. .s_sliced_fmt = cx25840_s_sliced_fmt,
  1589. .g_sliced_fmt = cx25840_g_sliced_fmt,
  1590. };
  1591. static const struct v4l2_subdev_ops cx25840_ops = {
  1592. .core = &cx25840_core_ops,
  1593. .tuner = &cx25840_tuner_ops,
  1594. .audio = &cx25840_audio_ops,
  1595. .video = &cx25840_video_ops,
  1596. .vbi = &cx25840_vbi_ops,
  1597. .ir = &cx25840_ir_ops,
  1598. };
  1599. /* ----------------------------------------------------------------------- */
  1600. static u32 get_cx2388x_ident(struct i2c_client *client)
  1601. {
  1602. u32 ret;
  1603. /* Come out of digital power down */
  1604. cx25840_write(client, 0x000, 0);
  1605. /* Detecting whether the part is cx23885/7/8 is more
  1606. * difficult than it needs to be. No ID register. Instead we
  1607. * probe certain registers indicated in the datasheets to look
  1608. * for specific defaults that differ between the silicon designs. */
  1609. /* It's either 885/7 if the IR Tx Clk Divider register exists */
  1610. if (cx25840_read4(client, 0x204) & 0xffff) {
  1611. /* CX23885 returns bogus repetitive byte values for the DIF,
  1612. * which doesn't exist for it. (Ex. 8a8a8a8a or 31313131) */
  1613. ret = cx25840_read4(client, 0x300);
  1614. if (((ret & 0xffff0000) >> 16) == (ret & 0xffff)) {
  1615. /* No DIF */
  1616. ret = V4L2_IDENT_CX23885_AV;
  1617. } else {
  1618. /* CX23887 has a broken DIF, but the registers
  1619. * appear valid (but unsed), good enough to detect. */
  1620. ret = V4L2_IDENT_CX23887_AV;
  1621. }
  1622. } else if (cx25840_read4(client, 0x300) & 0x0fffffff) {
  1623. /* DIF PLL Freq Word reg exists; chip must be a CX23888 */
  1624. ret = V4L2_IDENT_CX23888_AV;
  1625. } else {
  1626. v4l_err(client, "Unable to detect h/w, assuming cx23887\n");
  1627. ret = V4L2_IDENT_CX23887_AV;
  1628. }
  1629. /* Back into digital power down */
  1630. cx25840_write(client, 0x000, 2);
  1631. return ret;
  1632. }
  1633. static int cx25840_probe(struct i2c_client *client,
  1634. const struct i2c_device_id *did)
  1635. {
  1636. struct cx25840_state *state;
  1637. struct v4l2_subdev *sd;
  1638. int default_volume;
  1639. u32 id = V4L2_IDENT_NONE;
  1640. u16 device_id;
  1641. /* Check if the adapter supports the needed features */
  1642. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1643. return -EIO;
  1644. v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
  1645. device_id = cx25840_read(client, 0x101) << 8;
  1646. device_id |= cx25840_read(client, 0x100);
  1647. v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
  1648. /* The high byte of the device ID should be
  1649. * 0x83 for the cx2583x and 0x84 for the cx2584x */
  1650. if ((device_id & 0xff00) == 0x8300) {
  1651. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1652. } else if ((device_id & 0xff00) == 0x8400) {
  1653. id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
  1654. } else if (device_id == 0x0000) {
  1655. id = get_cx2388x_ident(client);
  1656. } else if ((device_id & 0xfff0) == 0x5A30) {
  1657. /* The CX23100 (0x5A3C = 23100) doesn't have an A/V decoder */
  1658. id = V4L2_IDENT_CX2310X_AV;
  1659. } else if ((device_id & 0xff) == (device_id >> 8)) {
  1660. v4l_err(client,
  1661. "likely a confused/unresponsive cx2388[578] A/V decoder"
  1662. " found @ 0x%x (%s)\n",
  1663. client->addr << 1, client->adapter->name);
  1664. v4l_err(client, "A method to reset it from the cx25840 driver"
  1665. " software is not known at this time\n");
  1666. return -ENODEV;
  1667. } else {
  1668. v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
  1669. return -ENODEV;
  1670. }
  1671. state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  1672. if (state == NULL)
  1673. return -ENOMEM;
  1674. sd = &state->sd;
  1675. v4l2_i2c_subdev_init(sd, client, &cx25840_ops);
  1676. switch (id) {
  1677. case V4L2_IDENT_CX23885_AV:
  1678. v4l_info(client, "cx23885 A/V decoder found @ 0x%x (%s)\n",
  1679. client->addr << 1, client->adapter->name);
  1680. break;
  1681. case V4L2_IDENT_CX23887_AV:
  1682. v4l_info(client, "cx23887 A/V decoder found @ 0x%x (%s)\n",
  1683. client->addr << 1, client->adapter->name);
  1684. break;
  1685. case V4L2_IDENT_CX23888_AV:
  1686. v4l_info(client, "cx23888 A/V decoder found @ 0x%x (%s)\n",
  1687. client->addr << 1, client->adapter->name);
  1688. break;
  1689. case V4L2_IDENT_CX2310X_AV:
  1690. v4l_info(client, "cx%d A/V decoder found @ 0x%x (%s)\n",
  1691. device_id, client->addr << 1, client->adapter->name);
  1692. break;
  1693. case V4L2_IDENT_CX25840:
  1694. case V4L2_IDENT_CX25841:
  1695. case V4L2_IDENT_CX25842:
  1696. case V4L2_IDENT_CX25843:
  1697. /* Note: revision '(device_id & 0x0f) == 2' was never built. The
  1698. marking skips from 0x1 == 22 to 0x3 == 23. */
  1699. v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
  1700. (device_id & 0xfff0) >> 4,
  1701. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1
  1702. : (device_id & 0x0f),
  1703. client->addr << 1, client->adapter->name);
  1704. break;
  1705. case V4L2_IDENT_CX25836:
  1706. case V4L2_IDENT_CX25837:
  1707. default:
  1708. v4l_info(client, "cx25%3x-%x found @ 0x%x (%s)\n",
  1709. (device_id & 0xfff0) >> 4, device_id & 0x0f,
  1710. client->addr << 1, client->adapter->name);
  1711. break;
  1712. }
  1713. state->c = client;
  1714. state->vid_input = CX25840_COMPOSITE7;
  1715. state->aud_input = CX25840_AUDIO8;
  1716. state->audclk_freq = 48000;
  1717. state->pvr150_workaround = 0;
  1718. state->audmode = V4L2_TUNER_MODE_LANG1;
  1719. state->vbi_line_offset = 8;
  1720. state->id = id;
  1721. state->rev = device_id;
  1722. v4l2_ctrl_handler_init(&state->hdl, 9);
  1723. v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
  1724. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1725. v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
  1726. V4L2_CID_CONTRAST, 0, 127, 1, 64);
  1727. v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
  1728. V4L2_CID_SATURATION, 0, 127, 1, 64);
  1729. v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
  1730. V4L2_CID_HUE, -128, 127, 1, 0);
  1731. if (!is_cx2583x(state)) {
  1732. default_volume = cx25840_read(client, 0x8d4);
  1733. /*
  1734. * Enforce the legacy PVR-350/MSP3400 to PVR-150/CX25843 volume
  1735. * scale mapping limits to avoid -ERANGE errors when
  1736. * initializing the volume control
  1737. */
  1738. if (default_volume > 228) {
  1739. /* Bottom out at -96 dB, v4l2 vol range 0x2e00-0x2fff */
  1740. default_volume = 228;
  1741. cx25840_write(client, 0x8d4, 228);
  1742. }
  1743. else if (default_volume < 20) {
  1744. /* Top out at + 8 dB, v4l2 vol range 0xfe00-0xffff */
  1745. default_volume = 20;
  1746. cx25840_write(client, 0x8d4, 20);
  1747. }
  1748. default_volume = (((228 - default_volume) >> 1) + 23) << 9;
  1749. state->volume = v4l2_ctrl_new_std(&state->hdl,
  1750. &cx25840_audio_ctrl_ops, V4L2_CID_AUDIO_VOLUME,
  1751. 0, 65535, 65535 / 100, default_volume);
  1752. state->mute = v4l2_ctrl_new_std(&state->hdl,
  1753. &cx25840_audio_ctrl_ops, V4L2_CID_AUDIO_MUTE,
  1754. 0, 1, 1, 0);
  1755. v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,
  1756. V4L2_CID_AUDIO_BALANCE,
  1757. 0, 65535, 65535 / 100, 32768);
  1758. v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,
  1759. V4L2_CID_AUDIO_BASS,
  1760. 0, 65535, 65535 / 100, 32768);
  1761. v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,
  1762. V4L2_CID_AUDIO_TREBLE,
  1763. 0, 65535, 65535 / 100, 32768);
  1764. }
  1765. sd->ctrl_handler = &state->hdl;
  1766. if (state->hdl.error) {
  1767. int err = state->hdl.error;
  1768. v4l2_ctrl_handler_free(&state->hdl);
  1769. kfree(state);
  1770. return err;
  1771. }
  1772. v4l2_ctrl_cluster(2, &state->volume);
  1773. v4l2_ctrl_handler_setup(&state->hdl);
  1774. cx25840_ir_probe(sd);
  1775. return 0;
  1776. }
  1777. static int cx25840_remove(struct i2c_client *client)
  1778. {
  1779. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1780. struct cx25840_state *state = to_state(sd);
  1781. cx25840_ir_remove(sd);
  1782. v4l2_device_unregister_subdev(sd);
  1783. v4l2_ctrl_handler_free(&state->hdl);
  1784. kfree(state);
  1785. return 0;
  1786. }
  1787. static const struct i2c_device_id cx25840_id[] = {
  1788. { "cx25840", 0 },
  1789. { }
  1790. };
  1791. MODULE_DEVICE_TABLE(i2c, cx25840_id);
  1792. static struct i2c_driver cx25840_driver = {
  1793. .driver = {
  1794. .owner = THIS_MODULE,
  1795. .name = "cx25840",
  1796. },
  1797. .probe = cx25840_probe,
  1798. .remove = cx25840_remove,
  1799. .id_table = cx25840_id,
  1800. };
  1801. static __init int init_cx25840(void)
  1802. {
  1803. return i2c_add_driver(&cx25840_driver);
  1804. }
  1805. static __exit void exit_cx25840(void)
  1806. {
  1807. i2c_del_driver(&cx25840_driver);
  1808. }
  1809. module_init(init_cx25840);
  1810. module_exit(exit_cx25840);