tw9910.c 22 KB

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