tw9910.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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_hsync_ctrl tw9910_hsync_ctrl = {
  313. .start = 0x0260,
  314. .end = 0x0300,
  315. };
  316. /*
  317. * general function
  318. */
  319. static struct tw9910_priv *to_tw9910(const struct i2c_client *client)
  320. {
  321. return container_of(i2c_get_clientdata(client), struct tw9910_priv,
  322. subdev);
  323. }
  324. static int tw9910_mask_set(struct i2c_client *client, u8 command,
  325. u8 mask, u8 set)
  326. {
  327. s32 val = i2c_smbus_read_byte_data(client, command);
  328. if (val < 0)
  329. return val;
  330. val &= ~mask;
  331. val |= set & mask;
  332. return i2c_smbus_write_byte_data(client, command, val);
  333. }
  334. static int tw9910_set_scale(struct i2c_client *client,
  335. const struct tw9910_scale_ctrl *scale)
  336. {
  337. int ret;
  338. ret = i2c_smbus_write_byte_data(client, SCALE_HI,
  339. (scale->vscale & 0x0F00) >> 4 |
  340. (scale->hscale & 0x0F00) >> 8);
  341. if (ret < 0)
  342. return ret;
  343. ret = i2c_smbus_write_byte_data(client, HSCALE_LO,
  344. scale->hscale & 0x00FF);
  345. if (ret < 0)
  346. return ret;
  347. ret = i2c_smbus_write_byte_data(client, VSCALE_LO,
  348. scale->vscale & 0x00FF);
  349. return ret;
  350. }
  351. static int tw9910_set_hsync(struct i2c_client *client,
  352. const struct tw9910_hsync_ctrl *hsync)
  353. {
  354. struct tw9910_priv *priv = to_tw9910(client);
  355. int ret;
  356. /* bit 10 - 3 */
  357. ret = i2c_smbus_write_byte_data(client, HSBEGIN,
  358. (hsync->start & 0x07F8) >> 3);
  359. if (ret < 0)
  360. return ret;
  361. /* bit 10 - 3 */
  362. ret = i2c_smbus_write_byte_data(client, HSEND,
  363. (hsync->end & 0x07F8) >> 3);
  364. if (ret < 0)
  365. return ret;
  366. /* So far only revisions 0 and 1 have been seen */
  367. /* bit 2 - 0 */
  368. if (1 == priv->revision)
  369. ret = tw9910_mask_set(client, HSLOWCTL, 0x77,
  370. (hsync->start & 0x0007) << 4 |
  371. (hsync->end & 0x0007));
  372. return ret;
  373. }
  374. static void tw9910_reset(struct i2c_client *client)
  375. {
  376. tw9910_mask_set(client, ACNTL1, SRESET, SRESET);
  377. msleep(1);
  378. }
  379. static int tw9910_power(struct i2c_client *client, int enable)
  380. {
  381. int ret;
  382. u8 acntl1;
  383. u8 acntl2;
  384. if (enable) {
  385. acntl1 = 0;
  386. acntl2 = 0;
  387. } else {
  388. acntl1 = CLK_PDN | Y_PDN | C_PDN;
  389. acntl2 = PLL_PDN;
  390. }
  391. ret = tw9910_mask_set(client, ACNTL1, ACNTL1_PDN_MASK, acntl1);
  392. if (ret < 0)
  393. return ret;
  394. return tw9910_mask_set(client, ACNTL2, ACNTL2_PDN_MASK, acntl2);
  395. }
  396. static const struct tw9910_scale_ctrl*
  397. tw9910_select_norm(struct soc_camera_device *icd, u32 width, u32 height)
  398. {
  399. const struct tw9910_scale_ctrl *scale;
  400. const struct tw9910_scale_ctrl *ret = NULL;
  401. v4l2_std_id norm = icd->vdev->current_norm;
  402. __u32 diff = 0xffffffff, tmp;
  403. int size, i;
  404. if (norm & V4L2_STD_NTSC) {
  405. scale = tw9910_ntsc_scales;
  406. size = ARRAY_SIZE(tw9910_ntsc_scales);
  407. } else if (norm & V4L2_STD_PAL) {
  408. scale = tw9910_pal_scales;
  409. size = ARRAY_SIZE(tw9910_pal_scales);
  410. } else {
  411. return NULL;
  412. }
  413. for (i = 0; i < size; i++) {
  414. tmp = abs(width - scale[i].width) +
  415. abs(height - scale[i].height);
  416. if (tmp < diff) {
  417. diff = tmp;
  418. ret = scale + i;
  419. }
  420. }
  421. return ret;
  422. }
  423. /*
  424. * soc_camera_ops function
  425. */
  426. static int tw9910_s_stream(struct v4l2_subdev *sd, int enable)
  427. {
  428. struct i2c_client *client = sd->priv;
  429. struct tw9910_priv *priv = to_tw9910(client);
  430. u8 val;
  431. int ret;
  432. if (!enable) {
  433. switch (priv->revision) {
  434. case 0:
  435. val = OEN_TRI_SEL_ALL_OFF_r0;
  436. break;
  437. case 1:
  438. val = OEN_TRI_SEL_ALL_OFF_r1;
  439. break;
  440. default:
  441. dev_err(&client->dev, "un-supported revision\n");
  442. return -EINVAL;
  443. }
  444. } else {
  445. val = OEN_TRI_SEL_ALL_ON;
  446. if (!priv->scale) {
  447. dev_err(&client->dev, "norm select error\n");
  448. return -EPERM;
  449. }
  450. dev_dbg(&client->dev, "%s %dx%d\n",
  451. priv->scale->name,
  452. priv->scale->width,
  453. priv->scale->height);
  454. }
  455. ret = tw9910_mask_set(client, OPFORM, OEN_TRI_SEL_MASK, val);
  456. if (ret < 0)
  457. return ret;
  458. return tw9910_power(client, enable);
  459. }
  460. static int tw9910_set_bus_param(struct soc_camera_device *icd,
  461. unsigned long flags)
  462. {
  463. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  464. struct i2c_client *client = sd->priv;
  465. u8 val = VSSL_VVALID | HSSL_DVALID;
  466. /*
  467. * set OUTCTR1
  468. *
  469. * We use VVALID and DVALID signals to control VSYNC and HSYNC
  470. * outputs, in this mode their polarity is inverted.
  471. */
  472. if (flags & SOCAM_HSYNC_ACTIVE_LOW)
  473. val |= HSP_HI;
  474. if (flags & SOCAM_VSYNC_ACTIVE_LOW)
  475. val |= VSP_HI;
  476. return i2c_smbus_write_byte_data(client, OUTCTR1, val);
  477. }
  478. static unsigned long tw9910_query_bus_param(struct soc_camera_device *icd)
  479. {
  480. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  481. struct tw9910_priv *priv = to_tw9910(client);
  482. struct soc_camera_link *icl = to_soc_camera_link(icd);
  483. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  484. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  485. SOCAM_VSYNC_ACTIVE_LOW | SOCAM_HSYNC_ACTIVE_LOW |
  486. SOCAM_DATA_ACTIVE_HIGH | priv->info->buswidth;
  487. return soc_camera_apply_sensor_flags(icl, flags);
  488. }
  489. static int tw9910_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  490. {
  491. int ret = -EINVAL;
  492. if (norm & (V4L2_STD_NTSC | V4L2_STD_PAL))
  493. ret = 0;
  494. return ret;
  495. }
  496. static int tw9910_enum_input(struct soc_camera_device *icd,
  497. struct v4l2_input *inp)
  498. {
  499. inp->type = V4L2_INPUT_TYPE_TUNER;
  500. inp->std = V4L2_STD_UNKNOWN;
  501. strcpy(inp->name, "Video");
  502. return 0;
  503. }
  504. static int tw9910_g_chip_ident(struct v4l2_subdev *sd,
  505. struct v4l2_dbg_chip_ident *id)
  506. {
  507. struct i2c_client *client = sd->priv;
  508. struct tw9910_priv *priv = to_tw9910(client);
  509. id->ident = V4L2_IDENT_TW9910;
  510. id->revision = priv->revision;
  511. return 0;
  512. }
  513. #ifdef CONFIG_VIDEO_ADV_DEBUG
  514. static int tw9910_g_register(struct v4l2_subdev *sd,
  515. struct v4l2_dbg_register *reg)
  516. {
  517. struct i2c_client *client = sd->priv;
  518. int ret;
  519. if (reg->reg > 0xff)
  520. return -EINVAL;
  521. ret = i2c_smbus_read_byte_data(client, reg->reg);
  522. if (ret < 0)
  523. return ret;
  524. /*
  525. * ret = int
  526. * reg->val = __u64
  527. */
  528. reg->val = (__u64)ret;
  529. return 0;
  530. }
  531. static int tw9910_s_register(struct v4l2_subdev *sd,
  532. struct v4l2_dbg_register *reg)
  533. {
  534. struct i2c_client *client = sd->priv;
  535. if (reg->reg > 0xff ||
  536. reg->val > 0xff)
  537. return -EINVAL;
  538. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  539. }
  540. #endif
  541. static int tw9910_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  542. {
  543. struct v4l2_rect *rect = &a->c;
  544. struct i2c_client *client = sd->priv;
  545. struct tw9910_priv *priv = to_tw9910(client);
  546. struct soc_camera_device *icd = client->dev.platform_data;
  547. int ret = -EINVAL;
  548. u8 val;
  549. /*
  550. * select suitable norm
  551. */
  552. priv->scale = tw9910_select_norm(icd, rect->width, rect->height);
  553. if (!priv->scale)
  554. goto tw9910_set_fmt_error;
  555. /*
  556. * reset hardware
  557. */
  558. tw9910_reset(client);
  559. /*
  560. * set bus width
  561. */
  562. val = 0x00;
  563. if (SOCAM_DATAWIDTH_16 == priv->info->buswidth)
  564. val = LEN;
  565. ret = tw9910_mask_set(client, OPFORM, LEN, val);
  566. if (ret < 0)
  567. goto tw9910_set_fmt_error;
  568. /*
  569. * select MPOUT behavior
  570. */
  571. switch (priv->info->mpout) {
  572. case TW9910_MPO_VLOSS:
  573. val = RTSEL_VLOSS; break;
  574. case TW9910_MPO_HLOCK:
  575. val = RTSEL_HLOCK; break;
  576. case TW9910_MPO_SLOCK:
  577. val = RTSEL_SLOCK; break;
  578. case TW9910_MPO_VLOCK:
  579. val = RTSEL_VLOCK; break;
  580. case TW9910_MPO_MONO:
  581. val = RTSEL_MONO; break;
  582. case TW9910_MPO_DET50:
  583. val = RTSEL_DET50; break;
  584. case TW9910_MPO_FIELD:
  585. val = RTSEL_FIELD; break;
  586. case TW9910_MPO_RTCO:
  587. val = RTSEL_RTCO; break;
  588. default:
  589. val = 0;
  590. }
  591. ret = tw9910_mask_set(client, VBICNTL, RTSEL_MASK, val);
  592. if (ret < 0)
  593. goto tw9910_set_fmt_error;
  594. /*
  595. * set scale
  596. */
  597. ret = tw9910_set_scale(client, priv->scale);
  598. if (ret < 0)
  599. goto tw9910_set_fmt_error;
  600. /*
  601. * set hsync
  602. */
  603. ret = tw9910_set_hsync(client, &tw9910_hsync_ctrl);
  604. if (ret < 0)
  605. goto tw9910_set_fmt_error;
  606. rect->width = priv->scale->width;
  607. rect->height = priv->scale->height;
  608. rect->left = 0;
  609. rect->top = 0;
  610. return ret;
  611. tw9910_set_fmt_error:
  612. tw9910_reset(client);
  613. priv->scale = NULL;
  614. return ret;
  615. }
  616. static int tw9910_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  617. {
  618. struct i2c_client *client = sd->priv;
  619. struct tw9910_priv *priv = to_tw9910(client);
  620. if (!priv->scale) {
  621. int ret;
  622. struct v4l2_crop crop = {
  623. .c = {
  624. .left = 0,
  625. .top = 0,
  626. .width = 640,
  627. .height = 480,
  628. },
  629. };
  630. ret = tw9910_s_crop(sd, &crop);
  631. if (ret < 0)
  632. return ret;
  633. }
  634. a->c.left = 0;
  635. a->c.top = 0;
  636. a->c.width = priv->scale->width;
  637. a->c.height = priv->scale->height;
  638. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  639. return 0;
  640. }
  641. static int tw9910_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  642. {
  643. a->bounds.left = 0;
  644. a->bounds.top = 0;
  645. a->bounds.width = 768;
  646. a->bounds.height = 576;
  647. a->defrect.left = 0;
  648. a->defrect.top = 0;
  649. a->defrect.width = 640;
  650. a->defrect.height = 480;
  651. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  652. a->pixelaspect.numerator = 1;
  653. a->pixelaspect.denominator = 1;
  654. return 0;
  655. }
  656. static int tw9910_g_fmt(struct v4l2_subdev *sd,
  657. struct v4l2_mbus_framefmt *mf)
  658. {
  659. struct i2c_client *client = sd->priv;
  660. struct tw9910_priv *priv = to_tw9910(client);
  661. if (!priv->scale) {
  662. int ret;
  663. struct v4l2_crop crop = {
  664. .c = {
  665. .left = 0,
  666. .top = 0,
  667. .width = 640,
  668. .height = 480,
  669. },
  670. };
  671. ret = tw9910_s_crop(sd, &crop);
  672. if (ret < 0)
  673. return ret;
  674. }
  675. mf->width = priv->scale->width;
  676. mf->height = priv->scale->height;
  677. mf->code = V4L2_MBUS_FMT_YUYV8_2X8_BE;
  678. mf->colorspace = V4L2_COLORSPACE_JPEG;
  679. mf->field = V4L2_FIELD_INTERLACED_BT;
  680. return 0;
  681. }
  682. static int tw9910_s_fmt(struct v4l2_subdev *sd,
  683. struct v4l2_mbus_framefmt *mf)
  684. {
  685. struct i2c_client *client = sd->priv;
  686. struct tw9910_priv *priv = to_tw9910(client);
  687. /* See tw9910_s_crop() - no proper cropping support */
  688. struct v4l2_crop a = {
  689. .c = {
  690. .left = 0,
  691. .top = 0,
  692. .width = mf->width,
  693. .height = mf->height,
  694. },
  695. };
  696. int ret;
  697. WARN_ON(mf->field != V4L2_FIELD_ANY &&
  698. mf->field != V4L2_FIELD_INTERLACED_BT);
  699. /*
  700. * check color format
  701. */
  702. if (mf->code != V4L2_MBUS_FMT_YUYV8_2X8_BE)
  703. return -EINVAL;
  704. mf->colorspace = V4L2_COLORSPACE_JPEG;
  705. ret = tw9910_s_crop(sd, &a);
  706. if (!ret) {
  707. mf->width = priv->scale->width;
  708. mf->height = priv->scale->height;
  709. }
  710. return ret;
  711. }
  712. static int tw9910_try_fmt(struct v4l2_subdev *sd,
  713. struct v4l2_mbus_framefmt *mf)
  714. {
  715. struct i2c_client *client = sd->priv;
  716. struct soc_camera_device *icd = client->dev.platform_data;
  717. const struct tw9910_scale_ctrl *scale;
  718. if (V4L2_FIELD_ANY == mf->field) {
  719. mf->field = V4L2_FIELD_INTERLACED_BT;
  720. } else if (V4L2_FIELD_INTERLACED_BT != mf->field) {
  721. dev_err(&client->dev, "Field type %d invalid.\n", mf->field);
  722. return -EINVAL;
  723. }
  724. mf->code = V4L2_MBUS_FMT_YUYV8_2X8_BE;
  725. mf->colorspace = V4L2_COLORSPACE_JPEG;
  726. /*
  727. * select suitable norm
  728. */
  729. scale = tw9910_select_norm(icd, mf->width, mf->height);
  730. if (!scale)
  731. return -EINVAL;
  732. mf->width = scale->width;
  733. mf->height = scale->height;
  734. return 0;
  735. }
  736. static int tw9910_video_probe(struct soc_camera_device *icd,
  737. struct i2c_client *client)
  738. {
  739. struct tw9910_priv *priv = to_tw9910(client);
  740. s32 id;
  741. /*
  742. * We must have a parent by now. And it cannot be a wrong one.
  743. * So this entire test is completely redundant.
  744. */
  745. if (!icd->dev.parent ||
  746. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  747. return -ENODEV;
  748. /*
  749. * tw9910 only use 8 or 16 bit bus width
  750. */
  751. if (SOCAM_DATAWIDTH_16 != priv->info->buswidth &&
  752. SOCAM_DATAWIDTH_8 != priv->info->buswidth) {
  753. dev_err(&client->dev, "bus width error\n");
  754. return -ENODEV;
  755. }
  756. /*
  757. * check and show Product ID
  758. * So far only revisions 0 and 1 have been seen
  759. */
  760. id = i2c_smbus_read_byte_data(client, ID);
  761. priv->revision = GET_REV(id);
  762. id = GET_ID(id);
  763. if (0x0B != id ||
  764. 0x01 < priv->revision) {
  765. dev_err(&client->dev,
  766. "Product ID error %x:%x\n",
  767. id, priv->revision);
  768. return -ENODEV;
  769. }
  770. dev_info(&client->dev,
  771. "tw9910 Product ID %0x:%0x\n", id, priv->revision);
  772. icd->vdev->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL;
  773. icd->vdev->current_norm = V4L2_STD_NTSC;
  774. return 0;
  775. }
  776. static struct soc_camera_ops tw9910_ops = {
  777. .set_bus_param = tw9910_set_bus_param,
  778. .query_bus_param = tw9910_query_bus_param,
  779. .enum_input = tw9910_enum_input,
  780. };
  781. static struct v4l2_subdev_core_ops tw9910_subdev_core_ops = {
  782. .g_chip_ident = tw9910_g_chip_ident,
  783. .s_std = tw9910_s_std,
  784. #ifdef CONFIG_VIDEO_ADV_DEBUG
  785. .g_register = tw9910_g_register,
  786. .s_register = tw9910_s_register,
  787. #endif
  788. };
  789. static int tw9910_enum_fmt(struct v4l2_subdev *sd, int index,
  790. enum v4l2_mbus_pixelcode *code)
  791. {
  792. if (index)
  793. return -EINVAL;
  794. *code = V4L2_MBUS_FMT_YUYV8_2X8_BE;
  795. return 0;
  796. }
  797. static struct v4l2_subdev_video_ops tw9910_subdev_video_ops = {
  798. .s_stream = tw9910_s_stream,
  799. .g_mbus_fmt = tw9910_g_fmt,
  800. .s_mbus_fmt = tw9910_s_fmt,
  801. .try_mbus_fmt = tw9910_try_fmt,
  802. .cropcap = tw9910_cropcap,
  803. .g_crop = tw9910_g_crop,
  804. .s_crop = tw9910_s_crop,
  805. .enum_mbus_fmt = tw9910_enum_fmt,
  806. };
  807. static struct v4l2_subdev_ops tw9910_subdev_ops = {
  808. .core = &tw9910_subdev_core_ops,
  809. .video = &tw9910_subdev_video_ops,
  810. };
  811. /*
  812. * i2c_driver function
  813. */
  814. static int tw9910_probe(struct i2c_client *client,
  815. const struct i2c_device_id *did)
  816. {
  817. struct tw9910_priv *priv;
  818. struct tw9910_video_info *info;
  819. struct soc_camera_device *icd = client->dev.platform_data;
  820. struct i2c_adapter *adapter =
  821. to_i2c_adapter(client->dev.parent);
  822. struct soc_camera_link *icl;
  823. int ret;
  824. if (!icd) {
  825. dev_err(&client->dev, "TW9910: missing soc-camera data!\n");
  826. return -EINVAL;
  827. }
  828. icl = to_soc_camera_link(icd);
  829. if (!icl || !icl->priv)
  830. return -EINVAL;
  831. info = icl->priv;
  832. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  833. dev_err(&client->dev,
  834. "I2C-Adapter doesn't support "
  835. "I2C_FUNC_SMBUS_BYTE_DATA\n");
  836. return -EIO;
  837. }
  838. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  839. if (!priv)
  840. return -ENOMEM;
  841. priv->info = info;
  842. v4l2_i2c_subdev_init(&priv->subdev, client, &tw9910_subdev_ops);
  843. icd->ops = &tw9910_ops;
  844. icd->iface = icl->bus_id;
  845. ret = tw9910_video_probe(icd, client);
  846. if (ret) {
  847. icd->ops = NULL;
  848. i2c_set_clientdata(client, NULL);
  849. kfree(priv);
  850. }
  851. return ret;
  852. }
  853. static int tw9910_remove(struct i2c_client *client)
  854. {
  855. struct tw9910_priv *priv = to_tw9910(client);
  856. struct soc_camera_device *icd = client->dev.platform_data;
  857. icd->ops = NULL;
  858. i2c_set_clientdata(client, NULL);
  859. kfree(priv);
  860. return 0;
  861. }
  862. static const struct i2c_device_id tw9910_id[] = {
  863. { "tw9910", 0 },
  864. { }
  865. };
  866. MODULE_DEVICE_TABLE(i2c, tw9910_id);
  867. static struct i2c_driver tw9910_i2c_driver = {
  868. .driver = {
  869. .name = "tw9910",
  870. },
  871. .probe = tw9910_probe,
  872. .remove = tw9910_remove,
  873. .id_table = tw9910_id,
  874. };
  875. /*
  876. * module function
  877. */
  878. static int __init tw9910_module_init(void)
  879. {
  880. return i2c_add_driver(&tw9910_i2c_driver);
  881. }
  882. static void __exit tw9910_module_exit(void)
  883. {
  884. i2c_del_driver(&tw9910_i2c_driver);
  885. }
  886. module_init(tw9910_module_init);
  887. module_exit(tw9910_module_exit);
  888. MODULE_DESCRIPTION("SoC Camera driver for tw9910");
  889. MODULE_AUTHOR("Kuninori Morimoto");
  890. MODULE_LICENSE("GPL v2");