tw9910.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * tw9910 Video Driver
  3. *
  4. * Copyright (C) 2008 Renesas Solutions Corp.
  5. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  6. *
  7. * Based on ov772x driver,
  8. *
  9. * Copyright (C) 2008 Kuninori Morimoto <morimoto.kuninori@renesas.com>
  10. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  11. * Copyright (C) 2008 Magnus Damm
  12. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/i2c.h>
  21. #include <linux/slab.h>
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/videodev2.h>
  25. #include <media/v4l2-chip-ident.h>
  26. #include <media/v4l2-subdev.h>
  27. #include <media/soc_camera.h>
  28. #include <media/tw9910.h>
  29. #define GET_ID(val) ((val & 0xF8) >> 3)
  30. #define GET_REV(val) (val & 0x07)
  31. /*
  32. * register offset
  33. */
  34. #define ID 0x00 /* Product ID Code Register */
  35. #define STATUS1 0x01 /* Chip Status Register I */
  36. #define INFORM 0x02 /* Input Format */
  37. #define OPFORM 0x03 /* Output Format Control Register */
  38. #define DLYCTR 0x04 /* Hysteresis and HSYNC Delay Control */
  39. #define OUTCTR1 0x05 /* Output Control I */
  40. #define ACNTL1 0x06 /* Analog Control Register 1 */
  41. #define CROP_HI 0x07 /* Cropping Register, High */
  42. #define VDELAY_LO 0x08 /* Vertical Delay Register, Low */
  43. #define VACTIVE_LO 0x09 /* Vertical Active Register, Low */
  44. #define HDELAY_LO 0x0A /* Horizontal Delay Register, Low */
  45. #define HACTIVE_LO 0x0B /* Horizontal Active Register, Low */
  46. #define CNTRL1 0x0C /* Control Register I */
  47. #define VSCALE_LO 0x0D /* Vertical Scaling Register, Low */
  48. #define SCALE_HI 0x0E /* Scaling Register, High */
  49. #define HSCALE_LO 0x0F /* Horizontal Scaling Register, Low */
  50. #define BRIGHT 0x10 /* BRIGHTNESS Control Register */
  51. #define CONTRAST 0x11 /* CONTRAST Control Register */
  52. #define SHARPNESS 0x12 /* SHARPNESS Control Register I */
  53. #define SAT_U 0x13 /* Chroma (U) Gain Register */
  54. #define SAT_V 0x14 /* Chroma (V) Gain Register */
  55. #define HUE 0x15 /* Hue Control Register */
  56. #define CORING1 0x17
  57. #define CORING2 0x18 /* Coring and IF compensation */
  58. #define VBICNTL 0x19 /* VBI Control Register */
  59. #define ACNTL2 0x1A /* Analog Control 2 */
  60. #define OUTCTR2 0x1B /* Output Control 2 */
  61. #define SDT 0x1C /* Standard Selection */
  62. #define SDTR 0x1D /* Standard Recognition */
  63. #define TEST 0x1F /* Test Control Register */
  64. #define CLMPG 0x20 /* Clamping Gain */
  65. #define IAGC 0x21 /* Individual AGC Gain */
  66. #define AGCGAIN 0x22 /* AGC Gain */
  67. #define PEAKWT 0x23 /* White Peak Threshold */
  68. #define CLMPL 0x24 /* Clamp level */
  69. #define SYNCT 0x25 /* Sync Amplitude */
  70. #define MISSCNT 0x26 /* Sync Miss Count Register */
  71. #define PCLAMP 0x27 /* Clamp Position Register */
  72. #define VCNTL1 0x28 /* Vertical Control I */
  73. #define VCNTL2 0x29 /* Vertical Control II */
  74. #define CKILL 0x2A /* Color Killer Level Control */
  75. #define COMB 0x2B /* Comb Filter Control */
  76. #define LDLY 0x2C /* Luma Delay and H Filter Control */
  77. #define MISC1 0x2D /* Miscellaneous Control I */
  78. #define LOOP 0x2E /* LOOP Control Register */
  79. #define MISC2 0x2F /* Miscellaneous Control II */
  80. #define MVSN 0x30 /* Macrovision Detection */
  81. #define STATUS2 0x31 /* Chip STATUS II */
  82. #define HFREF 0x32 /* H monitor */
  83. #define CLMD 0x33 /* CLAMP MODE */
  84. #define IDCNTL 0x34 /* ID Detection Control */
  85. #define CLCNTL1 0x35 /* Clamp Control I */
  86. #define ANAPLLCTL 0x4C
  87. #define VBIMIN 0x4D
  88. #define HSLOWCTL 0x4E
  89. #define WSS3 0x4F
  90. #define FILLDATA 0x50
  91. #define SDID 0x51
  92. #define DID 0x52
  93. #define WSS1 0x53
  94. #define WSS2 0x54
  95. #define VVBI 0x55
  96. #define LCTL6 0x56
  97. #define LCTL7 0x57
  98. #define LCTL8 0x58
  99. #define LCTL9 0x59
  100. #define LCTL10 0x5A
  101. #define LCTL11 0x5B
  102. #define LCTL12 0x5C
  103. #define LCTL13 0x5D
  104. #define LCTL14 0x5E
  105. #define LCTL15 0x5F
  106. #define LCTL16 0x60
  107. #define LCTL17 0x61
  108. #define LCTL18 0x62
  109. #define LCTL19 0x63
  110. #define LCTL20 0x64
  111. #define LCTL21 0x65
  112. #define LCTL22 0x66
  113. #define LCTL23 0x67
  114. #define LCTL24 0x68
  115. #define LCTL25 0x69
  116. #define LCTL26 0x6A
  117. #define HSBEGIN 0x6B
  118. #define HSEND 0x6C
  119. #define OVSDLY 0x6D
  120. #define OVSEND 0x6E
  121. #define VBIDELAY 0x6F
  122. /*
  123. * register detail
  124. */
  125. /* INFORM */
  126. #define FC27_ON 0x40 /* 1 : Input crystal clock frequency is 27MHz */
  127. #define FC27_FF 0x00 /* 0 : Square pixel mode. */
  128. /* Must use 24.54MHz for 60Hz field rate */
  129. /* source or 29.5MHz for 50Hz field rate */
  130. #define IFSEL_S 0x10 /* 01 : S-video decoding */
  131. #define IFSEL_C 0x00 /* 00 : Composite video decoding */
  132. /* Y input video selection */
  133. #define YSEL_M0 0x00 /* 00 : Mux0 selected */
  134. #define YSEL_M1 0x04 /* 01 : Mux1 selected */
  135. #define YSEL_M2 0x08 /* 10 : Mux2 selected */
  136. #define YSEL_M3 0x10 /* 11 : Mux3 selected */
  137. /* OPFORM */
  138. #define MODE 0x80 /* 0 : CCIR601 compatible YCrCb 4:2:2 format */
  139. /* 1 : ITU-R-656 compatible data sequence format */
  140. #define LEN 0x40 /* 0 : 8-bit YCrCb 4:2:2 output format */
  141. /* 1 : 16-bit YCrCb 4:2:2 output format.*/
  142. #define LLCMODE 0x20 /* 1 : LLC output mode. */
  143. /* 0 : free-run output mode */
  144. #define AINC 0x10 /* Serial interface auto-indexing control */
  145. /* 0 : auto-increment */
  146. /* 1 : non-auto */
  147. #define VSCTL 0x08 /* 1 : Vertical out ctrl by DVALID */
  148. /* 0 : Vertical out ctrl by HACTIVE and DVALID */
  149. #define OEN_TRI_SEL_MASK 0x07
  150. #define OEN_TRI_SEL_ALL_ON 0x00 /* Enable output for Rev0/Rev1 */
  151. #define OEN_TRI_SEL_ALL_OFF_r0 0x06 /* All tri-stated for Rev0 */
  152. #define OEN_TRI_SEL_ALL_OFF_r1 0x07 /* All tri-stated for Rev1 */
  153. /* OUTCTR1 */
  154. #define VSP_LO 0x00 /* 0 : VS pin output polarity is active low */
  155. #define VSP_HI 0x80 /* 1 : VS pin output polarity is active high. */
  156. /* VS pin output control */
  157. #define VSSL_VSYNC 0x00 /* 0 : VSYNC */
  158. #define VSSL_VACT 0x10 /* 1 : VACT */
  159. #define VSSL_FIELD 0x20 /* 2 : FIELD */
  160. #define VSSL_VVALID 0x30 /* 3 : VVALID */
  161. #define VSSL_ZERO 0x70 /* 7 : 0 */
  162. #define HSP_LOW 0x00 /* 0 : HS pin output polarity is active low */
  163. #define HSP_HI 0x08 /* 1 : HS pin output polarity is active high.*/
  164. /* HS pin output control */
  165. #define HSSL_HACT 0x00 /* 0 : HACT */
  166. #define HSSL_HSYNC 0x01 /* 1 : HSYNC */
  167. #define HSSL_DVALID 0x02 /* 2 : DVALID */
  168. #define HSSL_HLOCK 0x03 /* 3 : HLOCK */
  169. #define HSSL_ASYNCW 0x04 /* 4 : ASYNCW */
  170. #define HSSL_ZERO 0x07 /* 7 : 0 */
  171. /* ACNTL1 */
  172. #define SRESET 0x80 /* resets the device to its default state
  173. * but all register content remain unchanged.
  174. * This bit is self-resetting.
  175. */
  176. #define ACNTL1_PDN_MASK 0x0e
  177. #define CLK_PDN 0x08 /* system clock power down */
  178. #define Y_PDN 0x04 /* Luma ADC power down */
  179. #define C_PDN 0x02 /* Chroma ADC power down */
  180. /* ACNTL2 */
  181. #define ACNTL2_PDN_MASK 0x40
  182. #define PLL_PDN 0x40 /* PLL power down */
  183. /* VBICNTL */
  184. /* RTSEL : control the real time signal output from the MPOUT pin */
  185. #define RTSEL_MASK 0x07
  186. #define RTSEL_VLOSS 0x00 /* 0000 = Video loss */
  187. #define RTSEL_HLOCK 0x01 /* 0001 = H-lock */
  188. #define RTSEL_SLOCK 0x02 /* 0010 = S-lock */
  189. #define RTSEL_VLOCK 0x03 /* 0011 = V-lock */
  190. #define RTSEL_MONO 0x04 /* 0100 = MONO */
  191. #define RTSEL_DET50 0x05 /* 0101 = DET50 */
  192. #define RTSEL_FIELD 0x06 /* 0110 = FIELD */
  193. #define RTSEL_RTCO 0x07 /* 0111 = RTCO ( Real Time Control ) */
  194. /*
  195. * structure
  196. */
  197. struct regval_list {
  198. unsigned char reg_num;
  199. unsigned char value;
  200. };
  201. struct tw9910_scale_ctrl {
  202. char *name;
  203. unsigned short width;
  204. unsigned short height;
  205. u16 hscale;
  206. u16 vscale;
  207. };
  208. struct tw9910_cropping_ctrl {
  209. u16 vdelay;
  210. u16 vactive;
  211. u16 hdelay;
  212. u16 hactive;
  213. };
  214. struct tw9910_hsync_ctrl {
  215. u16 start;
  216. u16 end;
  217. };
  218. struct tw9910_priv {
  219. struct v4l2_subdev subdev;
  220. struct tw9910_video_info *info;
  221. const struct tw9910_scale_ctrl *scale;
  222. u32 revision;
  223. };
  224. static const struct tw9910_scale_ctrl tw9910_ntsc_scales[] = {
  225. {
  226. .name = "NTSC SQ",
  227. .width = 640,
  228. .height = 480,
  229. .hscale = 0x0100,
  230. .vscale = 0x0100,
  231. },
  232. {
  233. .name = "NTSC CCIR601",
  234. .width = 720,
  235. .height = 480,
  236. .hscale = 0x0100,
  237. .vscale = 0x0100,
  238. },
  239. {
  240. .name = "NTSC SQ (CIF)",
  241. .width = 320,
  242. .height = 240,
  243. .hscale = 0x0200,
  244. .vscale = 0x0200,
  245. },
  246. {
  247. .name = "NTSC CCIR601 (CIF)",
  248. .width = 360,
  249. .height = 240,
  250. .hscale = 0x0200,
  251. .vscale = 0x0200,
  252. },
  253. {
  254. .name = "NTSC SQ (QCIF)",
  255. .width = 160,
  256. .height = 120,
  257. .hscale = 0x0400,
  258. .vscale = 0x0400,
  259. },
  260. {
  261. .name = "NTSC CCIR601 (QCIF)",
  262. .width = 180,
  263. .height = 120,
  264. .hscale = 0x0400,
  265. .vscale = 0x0400,
  266. },
  267. };
  268. static const struct tw9910_scale_ctrl tw9910_pal_scales[] = {
  269. {
  270. .name = "PAL SQ",
  271. .width = 768,
  272. .height = 576,
  273. .hscale = 0x0100,
  274. .vscale = 0x0100,
  275. },
  276. {
  277. .name = "PAL CCIR601",
  278. .width = 720,
  279. .height = 576,
  280. .hscale = 0x0100,
  281. .vscale = 0x0100,
  282. },
  283. {
  284. .name = "PAL SQ (CIF)",
  285. .width = 384,
  286. .height = 288,
  287. .hscale = 0x0200,
  288. .vscale = 0x0200,
  289. },
  290. {
  291. .name = "PAL CCIR601 (CIF)",
  292. .width = 360,
  293. .height = 288,
  294. .hscale = 0x0200,
  295. .vscale = 0x0200,
  296. },
  297. {
  298. .name = "PAL SQ (QCIF)",
  299. .width = 192,
  300. .height = 144,
  301. .hscale = 0x0400,
  302. .vscale = 0x0400,
  303. },
  304. {
  305. .name = "PAL CCIR601 (QCIF)",
  306. .width = 180,
  307. .height = 144,
  308. .hscale = 0x0400,
  309. .vscale = 0x0400,
  310. },
  311. };
  312. static const struct tw9910_cropping_ctrl tw9910_cropping_ctrl = {
  313. .vdelay = 0x0012,
  314. .vactive = 0x00F0,
  315. .hdelay = 0x0010,
  316. .hactive = 0x02D0,
  317. };
  318. static const struct tw9910_hsync_ctrl tw9910_hsync_ctrl = {
  319. .start = 0x0260,
  320. .end = 0x0300,
  321. };
  322. /*
  323. * general function
  324. */
  325. static struct tw9910_priv *to_tw9910(const struct i2c_client *client)
  326. {
  327. return container_of(i2c_get_clientdata(client), struct tw9910_priv,
  328. subdev);
  329. }
  330. static int tw9910_mask_set(struct i2c_client *client, u8 command,
  331. u8 mask, u8 set)
  332. {
  333. s32 val = i2c_smbus_read_byte_data(client, command);
  334. if (val < 0)
  335. return val;
  336. val &= ~mask;
  337. val |= set & mask;
  338. return i2c_smbus_write_byte_data(client, command, val);
  339. }
  340. static int tw9910_set_scale(struct i2c_client *client,
  341. const struct tw9910_scale_ctrl *scale)
  342. {
  343. int ret;
  344. ret = i2c_smbus_write_byte_data(client, SCALE_HI,
  345. (scale->vscale & 0x0F00) >> 4 |
  346. (scale->hscale & 0x0F00) >> 8);
  347. if (ret < 0)
  348. return ret;
  349. ret = i2c_smbus_write_byte_data(client, HSCALE_LO,
  350. scale->hscale & 0x00FF);
  351. if (ret < 0)
  352. return ret;
  353. ret = i2c_smbus_write_byte_data(client, VSCALE_LO,
  354. scale->vscale & 0x00FF);
  355. return ret;
  356. }
  357. static int tw9910_set_cropping(struct i2c_client *client,
  358. const struct tw9910_cropping_ctrl *cropping)
  359. {
  360. int ret;
  361. ret = i2c_smbus_write_byte_data(client, CROP_HI,
  362. (cropping->vdelay & 0x0300) >> 2 |
  363. (cropping->vactive & 0x0300) >> 4 |
  364. (cropping->hdelay & 0x0300) >> 6 |
  365. (cropping->hactive & 0x0300) >> 8);
  366. if (ret < 0)
  367. return ret;
  368. ret = i2c_smbus_write_byte_data(client, VDELAY_LO,
  369. cropping->vdelay & 0x00FF);
  370. if (ret < 0)
  371. return ret;
  372. ret = i2c_smbus_write_byte_data(client, VACTIVE_LO,
  373. cropping->vactive & 0x00FF);
  374. if (ret < 0)
  375. return ret;
  376. ret = i2c_smbus_write_byte_data(client, HDELAY_LO,
  377. cropping->hdelay & 0x00FF);
  378. if (ret < 0)
  379. return ret;
  380. ret = i2c_smbus_write_byte_data(client, HACTIVE_LO,
  381. cropping->hactive & 0x00FF);
  382. return ret;
  383. }
  384. static int tw9910_set_hsync(struct i2c_client *client,
  385. const struct tw9910_hsync_ctrl *hsync)
  386. {
  387. struct tw9910_priv *priv = to_tw9910(client);
  388. int ret;
  389. /* bit 10 - 3 */
  390. ret = i2c_smbus_write_byte_data(client, HSBEGIN,
  391. (hsync->start & 0x07F8) >> 3);
  392. if (ret < 0)
  393. return ret;
  394. /* bit 10 - 3 */
  395. ret = i2c_smbus_write_byte_data(client, HSEND,
  396. (hsync->end & 0x07F8) >> 3);
  397. if (ret < 0)
  398. return ret;
  399. /* So far only revisions 0 and 1 have been seen */
  400. /* bit 2 - 0 */
  401. if (1 == priv->revision)
  402. ret = tw9910_mask_set(client, HSLOWCTL, 0x77,
  403. (hsync->start & 0x0007) << 4 |
  404. (hsync->end & 0x0007));
  405. return ret;
  406. }
  407. static void tw9910_reset(struct i2c_client *client)
  408. {
  409. tw9910_mask_set(client, ACNTL1, SRESET, SRESET);
  410. msleep(1);
  411. }
  412. static int tw9910_power(struct i2c_client *client, int enable)
  413. {
  414. int ret;
  415. u8 acntl1;
  416. u8 acntl2;
  417. if (enable) {
  418. acntl1 = 0;
  419. acntl2 = 0;
  420. } else {
  421. acntl1 = CLK_PDN | Y_PDN | C_PDN;
  422. acntl2 = PLL_PDN;
  423. }
  424. ret = tw9910_mask_set(client, ACNTL1, ACNTL1_PDN_MASK, acntl1);
  425. if (ret < 0)
  426. return ret;
  427. return tw9910_mask_set(client, ACNTL2, ACNTL2_PDN_MASK, acntl2);
  428. }
  429. static const struct tw9910_scale_ctrl*
  430. tw9910_select_norm(struct soc_camera_device *icd, u32 width, u32 height)
  431. {
  432. const struct tw9910_scale_ctrl *scale;
  433. const struct tw9910_scale_ctrl *ret = NULL;
  434. v4l2_std_id norm = icd->vdev->current_norm;
  435. __u32 diff = 0xffffffff, tmp;
  436. int size, i;
  437. if (norm & V4L2_STD_NTSC) {
  438. scale = tw9910_ntsc_scales;
  439. size = ARRAY_SIZE(tw9910_ntsc_scales);
  440. } else if (norm & V4L2_STD_PAL) {
  441. scale = tw9910_pal_scales;
  442. size = ARRAY_SIZE(tw9910_pal_scales);
  443. } else {
  444. return NULL;
  445. }
  446. for (i = 0; i < size; i++) {
  447. tmp = abs(width - scale[i].width) +
  448. abs(height - scale[i].height);
  449. if (tmp < diff) {
  450. diff = tmp;
  451. ret = scale + i;
  452. }
  453. }
  454. return ret;
  455. }
  456. /*
  457. * soc_camera_ops function
  458. */
  459. static int tw9910_s_stream(struct v4l2_subdev *sd, int enable)
  460. {
  461. struct i2c_client *client = sd->priv;
  462. struct tw9910_priv *priv = to_tw9910(client);
  463. u8 val;
  464. int ret;
  465. if (!enable) {
  466. switch (priv->revision) {
  467. case 0:
  468. val = OEN_TRI_SEL_ALL_OFF_r0;
  469. break;
  470. case 1:
  471. val = OEN_TRI_SEL_ALL_OFF_r1;
  472. break;
  473. default:
  474. dev_err(&client->dev, "un-supported revision\n");
  475. return -EINVAL;
  476. }
  477. } else {
  478. val = OEN_TRI_SEL_ALL_ON;
  479. if (!priv->scale) {
  480. dev_err(&client->dev, "norm select error\n");
  481. return -EPERM;
  482. }
  483. dev_dbg(&client->dev, "%s %dx%d\n",
  484. priv->scale->name,
  485. priv->scale->width,
  486. priv->scale->height);
  487. }
  488. ret = tw9910_mask_set(client, OPFORM, OEN_TRI_SEL_MASK, val);
  489. if (ret < 0)
  490. return ret;
  491. return tw9910_power(client, enable);
  492. }
  493. static int tw9910_set_bus_param(struct soc_camera_device *icd,
  494. unsigned long flags)
  495. {
  496. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  497. struct i2c_client *client = sd->priv;
  498. /*
  499. * set OUTCTR1
  500. */
  501. return i2c_smbus_write_byte_data(client, OUTCTR1,
  502. VSSL_VVALID | HSSL_DVALID);
  503. }
  504. static unsigned long tw9910_query_bus_param(struct soc_camera_device *icd)
  505. {
  506. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  507. struct tw9910_priv *priv = to_tw9910(client);
  508. struct soc_camera_link *icl = to_soc_camera_link(icd);
  509. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  510. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  511. SOCAM_DATA_ACTIVE_HIGH | priv->info->buswidth;
  512. return soc_camera_apply_sensor_flags(icl, flags);
  513. }
  514. static int tw9910_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  515. {
  516. int ret = -EINVAL;
  517. if (norm & (V4L2_STD_NTSC | V4L2_STD_PAL))
  518. ret = 0;
  519. return ret;
  520. }
  521. static int tw9910_enum_input(struct soc_camera_device *icd,
  522. struct v4l2_input *inp)
  523. {
  524. inp->type = V4L2_INPUT_TYPE_TUNER;
  525. inp->std = V4L2_STD_UNKNOWN;
  526. strcpy(inp->name, "Video");
  527. return 0;
  528. }
  529. static int tw9910_g_chip_ident(struct v4l2_subdev *sd,
  530. struct v4l2_dbg_chip_ident *id)
  531. {
  532. struct i2c_client *client = sd->priv;
  533. struct tw9910_priv *priv = to_tw9910(client);
  534. id->ident = V4L2_IDENT_TW9910;
  535. id->revision = priv->revision;
  536. return 0;
  537. }
  538. #ifdef CONFIG_VIDEO_ADV_DEBUG
  539. static int tw9910_g_register(struct v4l2_subdev *sd,
  540. struct v4l2_dbg_register *reg)
  541. {
  542. struct i2c_client *client = sd->priv;
  543. int ret;
  544. if (reg->reg > 0xff)
  545. return -EINVAL;
  546. ret = i2c_smbus_read_byte_data(client, reg->reg);
  547. if (ret < 0)
  548. return ret;
  549. /*
  550. * ret = int
  551. * reg->val = __u64
  552. */
  553. reg->val = (__u64)ret;
  554. return 0;
  555. }
  556. static int tw9910_s_register(struct v4l2_subdev *sd,
  557. struct v4l2_dbg_register *reg)
  558. {
  559. struct i2c_client *client = sd->priv;
  560. if (reg->reg > 0xff ||
  561. reg->val > 0xff)
  562. return -EINVAL;
  563. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  564. }
  565. #endif
  566. static int tw9910_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  567. {
  568. struct v4l2_rect *rect = &a->c;
  569. struct i2c_client *client = sd->priv;
  570. struct tw9910_priv *priv = to_tw9910(client);
  571. struct soc_camera_device *icd = client->dev.platform_data;
  572. int ret = -EINVAL;
  573. u8 val;
  574. /*
  575. * select suitable norm
  576. */
  577. priv->scale = tw9910_select_norm(icd, rect->width, rect->height);
  578. if (!priv->scale)
  579. goto tw9910_set_fmt_error;
  580. /*
  581. * reset hardware
  582. */
  583. tw9910_reset(client);
  584. /*
  585. * set bus width
  586. */
  587. val = 0x00;
  588. if (SOCAM_DATAWIDTH_16 == priv->info->buswidth)
  589. val = LEN;
  590. ret = tw9910_mask_set(client, OPFORM, LEN, val);
  591. if (ret < 0)
  592. goto tw9910_set_fmt_error;
  593. /*
  594. * select MPOUT behavior
  595. */
  596. switch (priv->info->mpout) {
  597. case TW9910_MPO_VLOSS:
  598. val = RTSEL_VLOSS; break;
  599. case TW9910_MPO_HLOCK:
  600. val = RTSEL_HLOCK; break;
  601. case TW9910_MPO_SLOCK:
  602. val = RTSEL_SLOCK; break;
  603. case TW9910_MPO_VLOCK:
  604. val = RTSEL_VLOCK; break;
  605. case TW9910_MPO_MONO:
  606. val = RTSEL_MONO; break;
  607. case TW9910_MPO_DET50:
  608. val = RTSEL_DET50; break;
  609. case TW9910_MPO_FIELD:
  610. val = RTSEL_FIELD; break;
  611. case TW9910_MPO_RTCO:
  612. val = RTSEL_RTCO; break;
  613. default:
  614. val = 0;
  615. }
  616. ret = tw9910_mask_set(client, VBICNTL, RTSEL_MASK, val);
  617. if (ret < 0)
  618. goto tw9910_set_fmt_error;
  619. /*
  620. * set scale
  621. */
  622. ret = tw9910_set_scale(client, priv->scale);
  623. if (ret < 0)
  624. goto tw9910_set_fmt_error;
  625. /*
  626. * set cropping
  627. */
  628. ret = tw9910_set_cropping(client, &tw9910_cropping_ctrl);
  629. if (ret < 0)
  630. goto tw9910_set_fmt_error;
  631. /*
  632. * set hsync
  633. */
  634. ret = tw9910_set_hsync(client, &tw9910_hsync_ctrl);
  635. if (ret < 0)
  636. goto tw9910_set_fmt_error;
  637. rect->width = priv->scale->width;
  638. rect->height = priv->scale->height;
  639. rect->left = 0;
  640. rect->top = 0;
  641. return ret;
  642. tw9910_set_fmt_error:
  643. tw9910_reset(client);
  644. priv->scale = NULL;
  645. return ret;
  646. }
  647. static int tw9910_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  648. {
  649. struct i2c_client *client = sd->priv;
  650. struct tw9910_priv *priv = to_tw9910(client);
  651. if (!priv->scale) {
  652. int ret;
  653. struct v4l2_crop crop = {
  654. .c = {
  655. .left = 0,
  656. .top = 0,
  657. .width = 640,
  658. .height = 480,
  659. },
  660. };
  661. ret = tw9910_s_crop(sd, &crop);
  662. if (ret < 0)
  663. return ret;
  664. }
  665. a->c.left = 0;
  666. a->c.top = 0;
  667. a->c.width = priv->scale->width;
  668. a->c.height = priv->scale->height;
  669. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  670. return 0;
  671. }
  672. static int tw9910_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  673. {
  674. a->bounds.left = 0;
  675. a->bounds.top = 0;
  676. a->bounds.width = 768;
  677. a->bounds.height = 576;
  678. a->defrect.left = 0;
  679. a->defrect.top = 0;
  680. a->defrect.width = 640;
  681. a->defrect.height = 480;
  682. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  683. a->pixelaspect.numerator = 1;
  684. a->pixelaspect.denominator = 1;
  685. return 0;
  686. }
  687. static int tw9910_g_fmt(struct v4l2_subdev *sd,
  688. struct v4l2_mbus_framefmt *mf)
  689. {
  690. struct i2c_client *client = sd->priv;
  691. struct tw9910_priv *priv = to_tw9910(client);
  692. if (!priv->scale) {
  693. int ret;
  694. struct v4l2_crop crop = {
  695. .c = {
  696. .left = 0,
  697. .top = 0,
  698. .width = 640,
  699. .height = 480,
  700. },
  701. };
  702. ret = tw9910_s_crop(sd, &crop);
  703. if (ret < 0)
  704. return ret;
  705. }
  706. mf->width = priv->scale->width;
  707. mf->height = priv->scale->height;
  708. mf->code = V4L2_MBUS_FMT_YUYV8_2X8_BE;
  709. mf->colorspace = V4L2_COLORSPACE_JPEG;
  710. mf->field = V4L2_FIELD_INTERLACED_BT;
  711. return 0;
  712. }
  713. static int tw9910_s_fmt(struct v4l2_subdev *sd,
  714. struct v4l2_mbus_framefmt *mf)
  715. {
  716. struct i2c_client *client = sd->priv;
  717. struct tw9910_priv *priv = to_tw9910(client);
  718. /* See tw9910_s_crop() - no proper cropping support */
  719. struct v4l2_crop a = {
  720. .c = {
  721. .left = 0,
  722. .top = 0,
  723. .width = mf->width,
  724. .height = mf->height,
  725. },
  726. };
  727. int ret;
  728. WARN_ON(mf->field != V4L2_FIELD_ANY &&
  729. mf->field != V4L2_FIELD_INTERLACED_BT);
  730. /*
  731. * check color format
  732. */
  733. if (mf->code != V4L2_MBUS_FMT_YUYV8_2X8_BE)
  734. return -EINVAL;
  735. mf->colorspace = V4L2_COLORSPACE_JPEG;
  736. ret = tw9910_s_crop(sd, &a);
  737. if (!ret) {
  738. mf->width = priv->scale->width;
  739. mf->height = priv->scale->height;
  740. }
  741. return ret;
  742. }
  743. static int tw9910_try_fmt(struct v4l2_subdev *sd,
  744. struct v4l2_mbus_framefmt *mf)
  745. {
  746. struct i2c_client *client = sd->priv;
  747. struct soc_camera_device *icd = client->dev.platform_data;
  748. const struct tw9910_scale_ctrl *scale;
  749. if (V4L2_FIELD_ANY == mf->field) {
  750. mf->field = V4L2_FIELD_INTERLACED_BT;
  751. } else if (V4L2_FIELD_INTERLACED_BT != mf->field) {
  752. dev_err(&client->dev, "Field type %d invalid.\n", mf->field);
  753. return -EINVAL;
  754. }
  755. mf->code = V4L2_MBUS_FMT_YUYV8_2X8_BE;
  756. mf->colorspace = V4L2_COLORSPACE_JPEG;
  757. /*
  758. * select suitable norm
  759. */
  760. scale = tw9910_select_norm(icd, mf->width, mf->height);
  761. if (!scale)
  762. return -EINVAL;
  763. mf->width = scale->width;
  764. mf->height = scale->height;
  765. return 0;
  766. }
  767. static int tw9910_video_probe(struct soc_camera_device *icd,
  768. struct i2c_client *client)
  769. {
  770. struct tw9910_priv *priv = to_tw9910(client);
  771. s32 id;
  772. /*
  773. * We must have a parent by now. And it cannot be a wrong one.
  774. * So this entire test is completely redundant.
  775. */
  776. if (!icd->dev.parent ||
  777. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  778. return -ENODEV;
  779. /*
  780. * tw9910 only use 8 or 16 bit bus width
  781. */
  782. if (SOCAM_DATAWIDTH_16 != priv->info->buswidth &&
  783. SOCAM_DATAWIDTH_8 != priv->info->buswidth) {
  784. dev_err(&client->dev, "bus width error\n");
  785. return -ENODEV;
  786. }
  787. /*
  788. * check and show Product ID
  789. * So far only revisions 0 and 1 have been seen
  790. */
  791. id = i2c_smbus_read_byte_data(client, ID);
  792. priv->revision = GET_REV(id);
  793. id = GET_ID(id);
  794. if (0x0B != id ||
  795. 0x01 < priv->revision) {
  796. dev_err(&client->dev,
  797. "Product ID error %x:%x\n",
  798. id, priv->revision);
  799. return -ENODEV;
  800. }
  801. dev_info(&client->dev,
  802. "tw9910 Product ID %0x:%0x\n", id, priv->revision);
  803. icd->vdev->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL;
  804. icd->vdev->current_norm = V4L2_STD_NTSC;
  805. return 0;
  806. }
  807. static struct soc_camera_ops tw9910_ops = {
  808. .set_bus_param = tw9910_set_bus_param,
  809. .query_bus_param = tw9910_query_bus_param,
  810. .enum_input = tw9910_enum_input,
  811. };
  812. static struct v4l2_subdev_core_ops tw9910_subdev_core_ops = {
  813. .g_chip_ident = tw9910_g_chip_ident,
  814. .s_std = tw9910_s_std,
  815. #ifdef CONFIG_VIDEO_ADV_DEBUG
  816. .g_register = tw9910_g_register,
  817. .s_register = tw9910_s_register,
  818. #endif
  819. };
  820. static int tw9910_enum_fmt(struct v4l2_subdev *sd, int index,
  821. enum v4l2_mbus_pixelcode *code)
  822. {
  823. if (index)
  824. return -EINVAL;
  825. *code = V4L2_MBUS_FMT_YUYV8_2X8_BE;
  826. return 0;
  827. }
  828. static struct v4l2_subdev_video_ops tw9910_subdev_video_ops = {
  829. .s_stream = tw9910_s_stream,
  830. .g_mbus_fmt = tw9910_g_fmt,
  831. .s_mbus_fmt = tw9910_s_fmt,
  832. .try_mbus_fmt = tw9910_try_fmt,
  833. .cropcap = tw9910_cropcap,
  834. .g_crop = tw9910_g_crop,
  835. .s_crop = tw9910_s_crop,
  836. .enum_mbus_fmt = tw9910_enum_fmt,
  837. };
  838. static struct v4l2_subdev_ops tw9910_subdev_ops = {
  839. .core = &tw9910_subdev_core_ops,
  840. .video = &tw9910_subdev_video_ops,
  841. };
  842. /*
  843. * i2c_driver function
  844. */
  845. static int tw9910_probe(struct i2c_client *client,
  846. const struct i2c_device_id *did)
  847. {
  848. struct tw9910_priv *priv;
  849. struct tw9910_video_info *info;
  850. struct soc_camera_device *icd = client->dev.platform_data;
  851. struct i2c_adapter *adapter =
  852. to_i2c_adapter(client->dev.parent);
  853. struct soc_camera_link *icl;
  854. int ret;
  855. if (!icd) {
  856. dev_err(&client->dev, "TW9910: missing soc-camera data!\n");
  857. return -EINVAL;
  858. }
  859. icl = to_soc_camera_link(icd);
  860. if (!icl || !icl->priv)
  861. return -EINVAL;
  862. info = icl->priv;
  863. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  864. dev_err(&client->dev,
  865. "I2C-Adapter doesn't support "
  866. "I2C_FUNC_SMBUS_BYTE_DATA\n");
  867. return -EIO;
  868. }
  869. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  870. if (!priv)
  871. return -ENOMEM;
  872. priv->info = info;
  873. v4l2_i2c_subdev_init(&priv->subdev, client, &tw9910_subdev_ops);
  874. icd->ops = &tw9910_ops;
  875. icd->iface = icl->bus_id;
  876. ret = tw9910_video_probe(icd, client);
  877. if (ret) {
  878. icd->ops = NULL;
  879. i2c_set_clientdata(client, NULL);
  880. kfree(priv);
  881. }
  882. return ret;
  883. }
  884. static int tw9910_remove(struct i2c_client *client)
  885. {
  886. struct tw9910_priv *priv = to_tw9910(client);
  887. struct soc_camera_device *icd = client->dev.platform_data;
  888. icd->ops = NULL;
  889. i2c_set_clientdata(client, NULL);
  890. kfree(priv);
  891. return 0;
  892. }
  893. static const struct i2c_device_id tw9910_id[] = {
  894. { "tw9910", 0 },
  895. { }
  896. };
  897. MODULE_DEVICE_TABLE(i2c, tw9910_id);
  898. static struct i2c_driver tw9910_i2c_driver = {
  899. .driver = {
  900. .name = "tw9910",
  901. },
  902. .probe = tw9910_probe,
  903. .remove = tw9910_remove,
  904. .id_table = tw9910_id,
  905. };
  906. /*
  907. * module function
  908. */
  909. static int __init tw9910_module_init(void)
  910. {
  911. return i2c_add_driver(&tw9910_i2c_driver);
  912. }
  913. static void __exit tw9910_module_exit(void)
  914. {
  915. i2c_del_driver(&tw9910_i2c_driver);
  916. }
  917. module_init(tw9910_module_init);
  918. module_exit(tw9910_module_exit);
  919. MODULE_DESCRIPTION("SoC Camera driver for tw9910");
  920. MODULE_AUTHOR("Kuninori Morimoto");
  921. MODULE_LICENSE("GPL v2");