tw9910.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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-common.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 HSGEGIN 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 0x04 /* Output Enable together with TRI_SEL. */
  150. /* OUTCTR1 */
  151. #define VSP_LO 0x00 /* 0 : VS pin output polarity is active low */
  152. #define VSP_HI 0x80 /* 1 : VS pin output polarity is active high. */
  153. /* VS pin output control */
  154. #define VSSL_VSYNC 0x00 /* 0 : VSYNC */
  155. #define VSSL_VACT 0x10 /* 1 : VACT */
  156. #define VSSL_FIELD 0x20 /* 2 : FIELD */
  157. #define VSSL_VVALID 0x30 /* 3 : VVALID */
  158. #define VSSL_ZERO 0x70 /* 7 : 0 */
  159. #define HSP_LOW 0x00 /* 0 : HS pin output polarity is active low */
  160. #define HSP_HI 0x08 /* 1 : HS pin output polarity is active high.*/
  161. /* HS pin output control */
  162. #define HSSL_HACT 0x00 /* 0 : HACT */
  163. #define HSSL_HSYNC 0x01 /* 1 : HSYNC */
  164. #define HSSL_DVALID 0x02 /* 2 : DVALID */
  165. #define HSSL_HLOCK 0x03 /* 3 : HLOCK */
  166. #define HSSL_ASYNCW 0x04 /* 4 : ASYNCW */
  167. #define HSSL_ZERO 0x07 /* 7 : 0 */
  168. /* ACNTL1 */
  169. #define SRESET 0x80 /* resets the device to its default state
  170. * but all register content remain unchanged.
  171. * This bit is self-resetting.
  172. */
  173. /* VBICNTL */
  174. /* RTSEL : control the real time signal
  175. * output from the MPOUT pin
  176. */
  177. #define RTSEL_MASK 0x07
  178. #define RTSEL_VLOSS 0x00 /* 0000 = Video loss */
  179. #define RTSEL_HLOCK 0x01 /* 0001 = H-lock */
  180. #define RTSEL_SLOCK 0x02 /* 0010 = S-lock */
  181. #define RTSEL_VLOCK 0x03 /* 0011 = V-lock */
  182. #define RTSEL_MONO 0x04 /* 0100 = MONO */
  183. #define RTSEL_DET50 0x05 /* 0101 = DET50 */
  184. #define RTSEL_FIELD 0x06 /* 0110 = FIELD */
  185. #define RTSEL_RTCO 0x07 /* 0111 = RTCO ( Real Time Control ) */
  186. /*
  187. * structure
  188. */
  189. struct regval_list {
  190. unsigned char reg_num;
  191. unsigned char value;
  192. };
  193. struct tw9910_scale_ctrl {
  194. char *name;
  195. unsigned short width;
  196. unsigned short height;
  197. u16 hscale;
  198. u16 vscale;
  199. };
  200. struct tw9910_cropping_ctrl {
  201. u16 vdelay;
  202. u16 vactive;
  203. u16 hdelay;
  204. u16 hactive;
  205. };
  206. struct tw9910_hsync_ctrl {
  207. u16 start;
  208. u16 end;
  209. };
  210. struct tw9910_priv {
  211. struct tw9910_video_info *info;
  212. const struct tw9910_scale_ctrl *scale;
  213. };
  214. /*
  215. * register settings
  216. */
  217. #define ENDMARKER { 0xff, 0xff }
  218. static const struct regval_list tw9910_default_regs[] =
  219. {
  220. { OPFORM, 0x00 },
  221. { OUTCTR1, VSP_LO | VSSL_VVALID | HSP_HI | HSSL_HSYNC },
  222. ENDMARKER,
  223. };
  224. static const struct soc_camera_data_format tw9910_color_fmt[] = {
  225. {
  226. .name = "VYUY",
  227. .fourcc = V4L2_PIX_FMT_VYUY,
  228. .depth = 16,
  229. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  230. }
  231. };
  232. static const struct tw9910_scale_ctrl tw9910_ntsc_scales[] = {
  233. {
  234. .name = "NTSC SQ",
  235. .width = 640,
  236. .height = 480,
  237. .hscale = 0x0100,
  238. .vscale = 0x0100,
  239. },
  240. {
  241. .name = "NTSC CCIR601",
  242. .width = 720,
  243. .height = 480,
  244. .hscale = 0x0100,
  245. .vscale = 0x0100,
  246. },
  247. {
  248. .name = "NTSC SQ (CIF)",
  249. .width = 320,
  250. .height = 240,
  251. .hscale = 0x0200,
  252. .vscale = 0x0200,
  253. },
  254. {
  255. .name = "NTSC CCIR601 (CIF)",
  256. .width = 360,
  257. .height = 240,
  258. .hscale = 0x0200,
  259. .vscale = 0x0200,
  260. },
  261. {
  262. .name = "NTSC SQ (QCIF)",
  263. .width = 160,
  264. .height = 120,
  265. .hscale = 0x0400,
  266. .vscale = 0x0400,
  267. },
  268. {
  269. .name = "NTSC CCIR601 (QCIF)",
  270. .width = 180,
  271. .height = 120,
  272. .hscale = 0x0400,
  273. .vscale = 0x0400,
  274. },
  275. };
  276. static const struct tw9910_scale_ctrl tw9910_pal_scales[] = {
  277. {
  278. .name = "PAL SQ",
  279. .width = 768,
  280. .height = 576,
  281. .hscale = 0x0100,
  282. .vscale = 0x0100,
  283. },
  284. {
  285. .name = "PAL CCIR601",
  286. .width = 720,
  287. .height = 576,
  288. .hscale = 0x0100,
  289. .vscale = 0x0100,
  290. },
  291. {
  292. .name = "PAL SQ (CIF)",
  293. .width = 384,
  294. .height = 288,
  295. .hscale = 0x0200,
  296. .vscale = 0x0200,
  297. },
  298. {
  299. .name = "PAL CCIR601 (CIF)",
  300. .width = 360,
  301. .height = 288,
  302. .hscale = 0x0200,
  303. .vscale = 0x0200,
  304. },
  305. {
  306. .name = "PAL SQ (QCIF)",
  307. .width = 192,
  308. .height = 144,
  309. .hscale = 0x0400,
  310. .vscale = 0x0400,
  311. },
  312. {
  313. .name = "PAL CCIR601 (QCIF)",
  314. .width = 180,
  315. .height = 144,
  316. .hscale = 0x0400,
  317. .vscale = 0x0400,
  318. },
  319. };
  320. static const struct tw9910_cropping_ctrl tw9910_cropping_ctrl = {
  321. .vdelay = 0x0012,
  322. .vactive = 0x00F0,
  323. .hdelay = 0x0010,
  324. .hactive = 0x02D0,
  325. };
  326. static const struct tw9910_hsync_ctrl tw9910_hsync_ctrl = {
  327. .start = 0x0260,
  328. .end = 0x0300,
  329. };
  330. /*
  331. * general function
  332. */
  333. static int tw9910_set_scale(struct i2c_client *client,
  334. const struct tw9910_scale_ctrl *scale)
  335. {
  336. int ret;
  337. ret = i2c_smbus_write_byte_data(client, SCALE_HI,
  338. (scale->vscale & 0x0F00) >> 4 |
  339. (scale->hscale & 0x0F00) >> 8);
  340. if (ret < 0)
  341. return ret;
  342. ret = i2c_smbus_write_byte_data(client, HSCALE_LO,
  343. scale->hscale & 0x00FF);
  344. if (ret < 0)
  345. return ret;
  346. ret = i2c_smbus_write_byte_data(client, VSCALE_LO,
  347. scale->vscale & 0x00FF);
  348. return ret;
  349. }
  350. static int tw9910_set_cropping(struct i2c_client *client,
  351. const struct tw9910_cropping_ctrl *cropping)
  352. {
  353. int ret;
  354. ret = i2c_smbus_write_byte_data(client, CROP_HI,
  355. (cropping->vdelay & 0x0300) >> 2 |
  356. (cropping->vactive & 0x0300) >> 4 |
  357. (cropping->hdelay & 0x0300) >> 6 |
  358. (cropping->hactive & 0x0300) >> 8);
  359. if (ret < 0)
  360. return ret;
  361. ret = i2c_smbus_write_byte_data(client, VDELAY_LO,
  362. cropping->vdelay & 0x00FF);
  363. if (ret < 0)
  364. return ret;
  365. ret = i2c_smbus_write_byte_data(client, VACTIVE_LO,
  366. cropping->vactive & 0x00FF);
  367. if (ret < 0)
  368. return ret;
  369. ret = i2c_smbus_write_byte_data(client, HDELAY_LO,
  370. cropping->hdelay & 0x00FF);
  371. if (ret < 0)
  372. return ret;
  373. ret = i2c_smbus_write_byte_data(client, HACTIVE_LO,
  374. cropping->hactive & 0x00FF);
  375. return ret;
  376. }
  377. static int tw9910_set_hsync(struct i2c_client *client,
  378. const struct tw9910_hsync_ctrl *hsync)
  379. {
  380. int ret;
  381. /* bit 10 - 3 */
  382. ret = i2c_smbus_write_byte_data(client, HSGEGIN,
  383. (hsync->start & 0x07F8) >> 3);
  384. if (ret < 0)
  385. return ret;
  386. /* bit 10 - 3 */
  387. ret = i2c_smbus_write_byte_data(client, HSEND,
  388. (hsync->end & 0x07F8) >> 3);
  389. if (ret < 0)
  390. return ret;
  391. /* bit 2 - 0 */
  392. ret = i2c_smbus_read_byte_data(client, HSLOWCTL);
  393. if (ret < 0)
  394. return ret;
  395. ret = i2c_smbus_write_byte_data(client, HSLOWCTL,
  396. (ret & 0x88) |
  397. (hsync->start & 0x0007) << 4 |
  398. (hsync->end & 0x0007));
  399. return ret;
  400. }
  401. static int tw9910_write_array(struct i2c_client *client,
  402. const struct regval_list *vals)
  403. {
  404. while (vals->reg_num != 0xff) {
  405. int ret = i2c_smbus_write_byte_data(client,
  406. vals->reg_num,
  407. vals->value);
  408. if (ret < 0)
  409. return ret;
  410. vals++;
  411. }
  412. return 0;
  413. }
  414. static int tw9910_mask_set(struct i2c_client *client, u8 command,
  415. u8 mask, u8 set)
  416. {
  417. s32 val = i2c_smbus_read_byte_data(client, command);
  418. if (val < 0)
  419. return val;
  420. val &= ~mask;
  421. val |= set & mask;
  422. return i2c_smbus_write_byte_data(client, command, val);
  423. }
  424. static void tw9910_reset(struct i2c_client *client)
  425. {
  426. i2c_smbus_write_byte_data(client, ACNTL1, SRESET);
  427. msleep(1);
  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_init(struct soc_camera_device *icd)
  460. {
  461. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  462. struct soc_camera_link *icl = to_soc_camera_link(icd);
  463. int ret = 0;
  464. if (icl->power) {
  465. ret = icl->power(&client->dev, 1);
  466. if (ret < 0)
  467. return ret;
  468. }
  469. if (icl->reset)
  470. ret = icl->reset(&client->dev);
  471. return ret;
  472. }
  473. static int tw9910_release(struct soc_camera_device *icd)
  474. {
  475. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  476. struct soc_camera_link *icl = to_soc_camera_link(icd);
  477. int ret = 0;
  478. if (icl->power)
  479. ret = icl->power(&client->dev, 0);
  480. return ret;
  481. }
  482. static int tw9910_start_capture(struct soc_camera_device *icd)
  483. {
  484. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  485. struct tw9910_priv *priv = i2c_get_clientdata(client);
  486. if (!priv->scale) {
  487. dev_err(&icd->dev, "norm select error\n");
  488. return -EPERM;
  489. }
  490. dev_dbg(&icd->dev, "%s %dx%d\n",
  491. priv->scale->name,
  492. priv->scale->width,
  493. priv->scale->height);
  494. return 0;
  495. }
  496. static int tw9910_stop_capture(struct soc_camera_device *icd)
  497. {
  498. return 0;
  499. }
  500. static int tw9910_set_bus_param(struct soc_camera_device *icd,
  501. unsigned long flags)
  502. {
  503. return 0;
  504. }
  505. static unsigned long tw9910_query_bus_param(struct soc_camera_device *icd)
  506. {
  507. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  508. struct tw9910_priv *priv = i2c_get_clientdata(client);
  509. struct soc_camera_link *icl = to_soc_camera_link(icd);
  510. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  511. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  512. SOCAM_DATA_ACTIVE_HIGH | priv->info->buswidth;
  513. return soc_camera_apply_sensor_flags(icl, flags);
  514. }
  515. static int tw9910_get_chip_id(struct soc_camera_device *icd,
  516. struct v4l2_dbg_chip_ident *id)
  517. {
  518. id->ident = V4L2_IDENT_TW9910;
  519. id->revision = 0;
  520. return 0;
  521. }
  522. static int tw9910_set_std(struct soc_camera_device *icd,
  523. v4l2_std_id *a)
  524. {
  525. int ret = -EINVAL;
  526. if (*a & (V4L2_STD_NTSC | V4L2_STD_PAL))
  527. ret = 0;
  528. return ret;
  529. }
  530. static int tw9910_enum_input(struct soc_camera_device *icd,
  531. struct v4l2_input *inp)
  532. {
  533. inp->type = V4L2_INPUT_TYPE_TUNER;
  534. inp->std = V4L2_STD_UNKNOWN;
  535. strcpy(inp->name, "Video");
  536. return 0;
  537. }
  538. #ifdef CONFIG_VIDEO_ADV_DEBUG
  539. static int tw9910_get_register(struct soc_camera_device *icd,
  540. struct v4l2_dbg_register *reg)
  541. {
  542. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  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. /* ret = int
  550. * reg->val = __u64
  551. */
  552. reg->val = (__u64)ret;
  553. return 0;
  554. }
  555. static int tw9910_set_register(struct soc_camera_device *icd,
  556. struct v4l2_dbg_register *reg)
  557. {
  558. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  559. if (reg->reg > 0xff ||
  560. reg->val > 0xff)
  561. return -EINVAL;
  562. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  563. }
  564. #endif
  565. static int tw9910_set_crop(struct soc_camera_device *icd,
  566. struct v4l2_rect *rect)
  567. {
  568. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  569. struct tw9910_priv *priv = i2c_get_clientdata(client);
  570. int ret = -EINVAL;
  571. u8 val;
  572. /*
  573. * select suitable norm
  574. */
  575. priv->scale = tw9910_select_norm(icd, rect->width, rect->height);
  576. if (!priv->scale)
  577. goto tw9910_set_fmt_error;
  578. /*
  579. * reset hardware
  580. */
  581. tw9910_reset(client);
  582. ret = tw9910_write_array(client, tw9910_default_regs);
  583. if (ret < 0)
  584. goto tw9910_set_fmt_error;
  585. /*
  586. * set bus width
  587. */
  588. val = 0x00;
  589. if (SOCAM_DATAWIDTH_16 == priv->info->buswidth)
  590. val = LEN;
  591. ret = tw9910_mask_set(client, OPFORM, LEN, val);
  592. if (ret < 0)
  593. goto tw9910_set_fmt_error;
  594. /*
  595. * select MPOUT behavior
  596. */
  597. switch (priv->info->mpout) {
  598. case TW9910_MPO_VLOSS:
  599. val = RTSEL_VLOSS; break;
  600. case TW9910_MPO_HLOCK:
  601. val = RTSEL_HLOCK; break;
  602. case TW9910_MPO_SLOCK:
  603. val = RTSEL_SLOCK; break;
  604. case TW9910_MPO_VLOCK:
  605. val = RTSEL_VLOCK; break;
  606. case TW9910_MPO_MONO:
  607. val = RTSEL_MONO; break;
  608. case TW9910_MPO_DET50:
  609. val = RTSEL_DET50; break;
  610. case TW9910_MPO_FIELD:
  611. val = RTSEL_FIELD; break;
  612. case TW9910_MPO_RTCO:
  613. val = RTSEL_RTCO; break;
  614. default:
  615. val = 0;
  616. }
  617. ret = tw9910_mask_set(client, VBICNTL, RTSEL_MASK, val);
  618. if (ret < 0)
  619. goto tw9910_set_fmt_error;
  620. /*
  621. * set scale
  622. */
  623. ret = tw9910_set_scale(client, priv->scale);
  624. if (ret < 0)
  625. goto tw9910_set_fmt_error;
  626. /*
  627. * set cropping
  628. */
  629. ret = tw9910_set_cropping(client, &tw9910_cropping_ctrl);
  630. if (ret < 0)
  631. goto tw9910_set_fmt_error;
  632. /*
  633. * set hsync
  634. */
  635. ret = tw9910_set_hsync(client, &tw9910_hsync_ctrl);
  636. if (ret < 0)
  637. goto tw9910_set_fmt_error;
  638. return ret;
  639. tw9910_set_fmt_error:
  640. tw9910_reset(client);
  641. priv->scale = NULL;
  642. return ret;
  643. }
  644. static int tw9910_set_fmt(struct soc_camera_device *icd,
  645. struct v4l2_format *f)
  646. {
  647. struct v4l2_pix_format *pix = &f->fmt.pix;
  648. struct v4l2_rect rect = {
  649. .left = icd->x_current,
  650. .top = icd->y_current,
  651. .width = pix->width,
  652. .height = pix->height,
  653. };
  654. int i;
  655. /*
  656. * check color format
  657. */
  658. for (i = 0; i < ARRAY_SIZE(tw9910_color_fmt); i++)
  659. if (pix->pixelformat == tw9910_color_fmt[i].fourcc)
  660. break;
  661. if (i == ARRAY_SIZE(tw9910_color_fmt))
  662. return -EINVAL;
  663. return tw9910_set_crop(icd, &rect);
  664. }
  665. static int tw9910_try_fmt(struct soc_camera_device *icd,
  666. struct v4l2_format *f)
  667. {
  668. struct v4l2_pix_format *pix = &f->fmt.pix;
  669. const struct tw9910_scale_ctrl *scale;
  670. if (V4L2_FIELD_ANY == pix->field) {
  671. pix->field = V4L2_FIELD_INTERLACED;
  672. } else if (V4L2_FIELD_INTERLACED != pix->field) {
  673. dev_err(&icd->dev, "Field type invalid.\n");
  674. return -EINVAL;
  675. }
  676. /*
  677. * select suitable norm
  678. */
  679. scale = tw9910_select_norm(icd, pix->width, pix->height);
  680. if (!scale)
  681. return -EINVAL;
  682. pix->width = scale->width;
  683. pix->height = scale->height;
  684. return 0;
  685. }
  686. static int tw9910_video_probe(struct soc_camera_device *icd,
  687. struct i2c_client *client)
  688. {
  689. struct tw9910_priv *priv = i2c_get_clientdata(client);
  690. s32 val;
  691. int ret;
  692. /*
  693. * We must have a parent by now. And it cannot be a wrong one.
  694. * So this entire test is completely redundant.
  695. */
  696. if (!icd->dev.parent ||
  697. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  698. return -ENODEV;
  699. /*
  700. * tw9910 only use 8 or 16 bit bus width
  701. */
  702. if (SOCAM_DATAWIDTH_16 != priv->info->buswidth &&
  703. SOCAM_DATAWIDTH_8 != priv->info->buswidth) {
  704. dev_err(&icd->dev, "bus width error\n");
  705. return -ENODEV;
  706. }
  707. icd->formats = tw9910_color_fmt;
  708. icd->num_formats = ARRAY_SIZE(tw9910_color_fmt);
  709. /* Switch master clock on */
  710. ret = soc_camera_video_start(icd, &client->dev);
  711. if (ret)
  712. return ret;
  713. /*
  714. * check and show Product ID
  715. */
  716. val = i2c_smbus_read_byte_data(client, ID);
  717. soc_camera_video_stop(icd);
  718. if (0x0B != GET_ID(val) ||
  719. 0x00 != GET_ReV(val)) {
  720. dev_err(&icd->dev,
  721. "Product ID error %x:%x\n", GET_ID(val), GET_ReV(val));
  722. return -ENODEV;
  723. }
  724. dev_info(&icd->dev,
  725. "tw9910 Product ID %0x:%0x\n", GET_ID(val), GET_ReV(val));
  726. icd->vdev->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL;
  727. icd->vdev->current_norm = V4L2_STD_NTSC;
  728. return ret;
  729. }
  730. static struct soc_camera_ops tw9910_ops = {
  731. .owner = THIS_MODULE,
  732. .init = tw9910_init,
  733. .release = tw9910_release,
  734. .start_capture = tw9910_start_capture,
  735. .stop_capture = tw9910_stop_capture,
  736. .set_crop = tw9910_set_crop,
  737. .set_fmt = tw9910_set_fmt,
  738. .try_fmt = tw9910_try_fmt,
  739. .set_bus_param = tw9910_set_bus_param,
  740. .query_bus_param = tw9910_query_bus_param,
  741. .get_chip_id = tw9910_get_chip_id,
  742. .set_std = tw9910_set_std,
  743. .enum_input = tw9910_enum_input,
  744. #ifdef CONFIG_VIDEO_ADV_DEBUG
  745. .get_register = tw9910_get_register,
  746. .set_register = tw9910_set_register,
  747. #endif
  748. };
  749. /*
  750. * i2c_driver function
  751. */
  752. static int tw9910_probe(struct i2c_client *client,
  753. const struct i2c_device_id *did)
  754. {
  755. struct tw9910_priv *priv;
  756. struct tw9910_video_info *info;
  757. struct soc_camera_device *icd = client->dev.platform_data;
  758. struct i2c_adapter *adapter =
  759. to_i2c_adapter(client->dev.parent);
  760. struct soc_camera_link *icl;
  761. const struct tw9910_scale_ctrl *scale;
  762. int i, ret;
  763. if (!icd) {
  764. dev_err(&client->dev, "TW9910: missing soc-camera data!\n");
  765. return -EINVAL;
  766. }
  767. icl = to_soc_camera_link(icd);
  768. if (!icl)
  769. return -EINVAL;
  770. info = container_of(icl, struct tw9910_video_info, link);
  771. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  772. dev_err(&client->dev,
  773. "I2C-Adapter doesn't support "
  774. "I2C_FUNC_SMBUS_BYTE_DATA\n");
  775. return -EIO;
  776. }
  777. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  778. if (!priv)
  779. return -ENOMEM;
  780. priv->info = info;
  781. i2c_set_clientdata(client, priv);
  782. icd->ops = &tw9910_ops;
  783. icd->iface = info->link.bus_id;
  784. /*
  785. * set width and height
  786. */
  787. icd->width_max = tw9910_ntsc_scales[0].width; /* set default */
  788. icd->width_min = tw9910_ntsc_scales[0].width;
  789. icd->height_max = tw9910_ntsc_scales[0].height;
  790. icd->height_min = tw9910_ntsc_scales[0].height;
  791. scale = tw9910_ntsc_scales;
  792. for (i = 0; i < ARRAY_SIZE(tw9910_ntsc_scales); i++) {
  793. icd->width_max = max(scale[i].width, icd->width_max);
  794. icd->width_min = min(scale[i].width, icd->width_min);
  795. icd->height_max = max(scale[i].height, icd->height_max);
  796. icd->height_min = min(scale[i].height, icd->height_min);
  797. }
  798. scale = tw9910_pal_scales;
  799. for (i = 0; i < ARRAY_SIZE(tw9910_pal_scales); i++) {
  800. icd->width_max = max(scale[i].width, icd->width_max);
  801. icd->width_min = min(scale[i].width, icd->width_min);
  802. icd->height_max = max(scale[i].height, icd->height_max);
  803. icd->height_min = min(scale[i].height, icd->height_min);
  804. }
  805. ret = tw9910_video_probe(icd, client);
  806. if (ret) {
  807. icd->ops = NULL;
  808. i2c_set_clientdata(client, NULL);
  809. kfree(priv);
  810. }
  811. return ret;
  812. }
  813. static int tw9910_remove(struct i2c_client *client)
  814. {
  815. struct tw9910_priv *priv = i2c_get_clientdata(client);
  816. struct soc_camera_device *icd = client->dev.platform_data;
  817. icd->ops = NULL;
  818. i2c_set_clientdata(client, NULL);
  819. kfree(priv);
  820. return 0;
  821. }
  822. static const struct i2c_device_id tw9910_id[] = {
  823. { "tw9910", 0 },
  824. { }
  825. };
  826. MODULE_DEVICE_TABLE(i2c, tw9910_id);
  827. static struct i2c_driver tw9910_i2c_driver = {
  828. .driver = {
  829. .name = "tw9910",
  830. },
  831. .probe = tw9910_probe,
  832. .remove = tw9910_remove,
  833. .id_table = tw9910_id,
  834. };
  835. /*
  836. * module function
  837. */
  838. static int __init tw9910_module_init(void)
  839. {
  840. return i2c_add_driver(&tw9910_i2c_driver);
  841. }
  842. static void __exit tw9910_module_exit(void)
  843. {
  844. i2c_del_driver(&tw9910_i2c_driver);
  845. }
  846. module_init(tw9910_module_init);
  847. module_exit(tw9910_module_exit);
  848. MODULE_DESCRIPTION("SoC Camera driver for tw9910");
  849. MODULE_AUTHOR("Kuninori Morimoto");
  850. MODULE_LICENSE("GPL v2");