tw9910.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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/soc_camera.h>
  26. #include <media/soc_mediabus.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. u32 revision;
  217. };
  218. static const struct tw9910_scale_ctrl tw9910_ntsc_scales[] = {
  219. {
  220. .name = "NTSC SQ",
  221. .width = 640,
  222. .height = 480,
  223. .hscale = 0x0100,
  224. .vscale = 0x0100,
  225. },
  226. {
  227. .name = "NTSC CCIR601",
  228. .width = 720,
  229. .height = 480,
  230. .hscale = 0x0100,
  231. .vscale = 0x0100,
  232. },
  233. {
  234. .name = "NTSC SQ (CIF)",
  235. .width = 320,
  236. .height = 240,
  237. .hscale = 0x0200,
  238. .vscale = 0x0200,
  239. },
  240. {
  241. .name = "NTSC CCIR601 (CIF)",
  242. .width = 360,
  243. .height = 240,
  244. .hscale = 0x0200,
  245. .vscale = 0x0200,
  246. },
  247. {
  248. .name = "NTSC SQ (QCIF)",
  249. .width = 160,
  250. .height = 120,
  251. .hscale = 0x0400,
  252. .vscale = 0x0400,
  253. },
  254. {
  255. .name = "NTSC CCIR601 (QCIF)",
  256. .width = 180,
  257. .height = 120,
  258. .hscale = 0x0400,
  259. .vscale = 0x0400,
  260. },
  261. };
  262. static const struct tw9910_scale_ctrl tw9910_pal_scales[] = {
  263. {
  264. .name = "PAL SQ",
  265. .width = 768,
  266. .height = 576,
  267. .hscale = 0x0100,
  268. .vscale = 0x0100,
  269. },
  270. {
  271. .name = "PAL CCIR601",
  272. .width = 720,
  273. .height = 576,
  274. .hscale = 0x0100,
  275. .vscale = 0x0100,
  276. },
  277. {
  278. .name = "PAL SQ (CIF)",
  279. .width = 384,
  280. .height = 288,
  281. .hscale = 0x0200,
  282. .vscale = 0x0200,
  283. },
  284. {
  285. .name = "PAL CCIR601 (CIF)",
  286. .width = 360,
  287. .height = 288,
  288. .hscale = 0x0200,
  289. .vscale = 0x0200,
  290. },
  291. {
  292. .name = "PAL SQ (QCIF)",
  293. .width = 192,
  294. .height = 144,
  295. .hscale = 0x0400,
  296. .vscale = 0x0400,
  297. },
  298. {
  299. .name = "PAL CCIR601 (QCIF)",
  300. .width = 180,
  301. .height = 144,
  302. .hscale = 0x0400,
  303. .vscale = 0x0400,
  304. },
  305. };
  306. /*
  307. * general function
  308. */
  309. static struct tw9910_priv *to_tw9910(const struct i2c_client *client)
  310. {
  311. return container_of(i2c_get_clientdata(client), struct tw9910_priv,
  312. subdev);
  313. }
  314. static int tw9910_mask_set(struct i2c_client *client, u8 command,
  315. u8 mask, u8 set)
  316. {
  317. s32 val = i2c_smbus_read_byte_data(client, command);
  318. if (val < 0)
  319. return val;
  320. val &= ~mask;
  321. val |= set & mask;
  322. return i2c_smbus_write_byte_data(client, command, val);
  323. }
  324. static int tw9910_set_scale(struct i2c_client *client,
  325. const struct tw9910_scale_ctrl *scale)
  326. {
  327. int ret;
  328. ret = i2c_smbus_write_byte_data(client, SCALE_HI,
  329. (scale->vscale & 0x0F00) >> 4 |
  330. (scale->hscale & 0x0F00) >> 8);
  331. if (ret < 0)
  332. return ret;
  333. ret = i2c_smbus_write_byte_data(client, HSCALE_LO,
  334. scale->hscale & 0x00FF);
  335. if (ret < 0)
  336. return ret;
  337. ret = i2c_smbus_write_byte_data(client, VSCALE_LO,
  338. scale->vscale & 0x00FF);
  339. return ret;
  340. }
  341. static int tw9910_set_hsync(struct i2c_client *client)
  342. {
  343. struct tw9910_priv *priv = to_tw9910(client);
  344. int ret;
  345. /* bit 10 - 3 */
  346. ret = i2c_smbus_write_byte_data(client, HSBEGIN,
  347. (HSYNC_START & 0x07F8) >> 3);
  348. if (ret < 0)
  349. return ret;
  350. /* bit 10 - 3 */
  351. ret = i2c_smbus_write_byte_data(client, HSEND,
  352. (HSYNC_END & 0x07F8) >> 3);
  353. if (ret < 0)
  354. return ret;
  355. /* So far only revisions 0 and 1 have been seen */
  356. /* bit 2 - 0 */
  357. if (1 == priv->revision)
  358. ret = tw9910_mask_set(client, HSLOWCTL, 0x77,
  359. (HSYNC_START & 0x0007) << 4 |
  360. (HSYNC_END & 0x0007));
  361. return ret;
  362. }
  363. static void tw9910_reset(struct i2c_client *client)
  364. {
  365. tw9910_mask_set(client, ACNTL1, SRESET, SRESET);
  366. msleep(1);
  367. }
  368. static int tw9910_power(struct i2c_client *client, int enable)
  369. {
  370. int ret;
  371. u8 acntl1;
  372. u8 acntl2;
  373. if (enable) {
  374. acntl1 = 0;
  375. acntl2 = 0;
  376. } else {
  377. acntl1 = CLK_PDN | Y_PDN | C_PDN;
  378. acntl2 = PLL_PDN;
  379. }
  380. ret = tw9910_mask_set(client, ACNTL1, ACNTL1_PDN_MASK, acntl1);
  381. if (ret < 0)
  382. return ret;
  383. return tw9910_mask_set(client, ACNTL2, ACNTL2_PDN_MASK, acntl2);
  384. }
  385. static const struct tw9910_scale_ctrl *tw9910_select_norm(struct soc_camera_device *icd,
  386. u32 width, u32 height)
  387. {
  388. const struct tw9910_scale_ctrl *scale;
  389. const struct tw9910_scale_ctrl *ret = NULL;
  390. v4l2_std_id norm = icd->vdev->current_norm;
  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. * soc_camera_ops function
  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_set_bus_param(struct soc_camera_device *icd,
  450. unsigned long flags)
  451. {
  452. struct soc_camera_link *icl = to_soc_camera_link(icd);
  453. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  454. struct i2c_client *client = v4l2_get_subdevdata(sd);
  455. u8 val = VSSL_VVALID | HSSL_DVALID;
  456. flags = soc_camera_apply_sensor_flags(icl, flags);
  457. /*
  458. * set OUTCTR1
  459. *
  460. * We use VVALID and DVALID signals to control VSYNC and HSYNC
  461. * outputs, in this mode their polarity is inverted.
  462. */
  463. if (flags & SOCAM_HSYNC_ACTIVE_LOW)
  464. val |= HSP_HI;
  465. if (flags & SOCAM_VSYNC_ACTIVE_LOW)
  466. val |= VSP_HI;
  467. return i2c_smbus_write_byte_data(client, OUTCTR1, val);
  468. }
  469. static unsigned long tw9910_query_bus_param(struct soc_camera_device *icd)
  470. {
  471. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  472. struct tw9910_priv *priv = to_tw9910(client);
  473. struct soc_camera_link *icl = to_soc_camera_link(icd);
  474. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  475. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  476. SOCAM_VSYNC_ACTIVE_LOW | SOCAM_HSYNC_ACTIVE_LOW |
  477. SOCAM_DATA_ACTIVE_HIGH | priv->info->buswidth;
  478. return soc_camera_apply_sensor_flags(icl, flags);
  479. }
  480. static int tw9910_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  481. {
  482. int ret = -EINVAL;
  483. if (norm & (V4L2_STD_NTSC | V4L2_STD_PAL))
  484. ret = 0;
  485. return ret;
  486. }
  487. static int tw9910_g_chip_ident(struct v4l2_subdev *sd,
  488. struct v4l2_dbg_chip_ident *id)
  489. {
  490. struct i2c_client *client = v4l2_get_subdevdata(sd);
  491. struct tw9910_priv *priv = to_tw9910(client);
  492. id->ident = V4L2_IDENT_TW9910;
  493. id->revision = priv->revision;
  494. return 0;
  495. }
  496. #ifdef CONFIG_VIDEO_ADV_DEBUG
  497. static int tw9910_g_register(struct v4l2_subdev *sd,
  498. struct v4l2_dbg_register *reg)
  499. {
  500. struct i2c_client *client = v4l2_get_subdevdata(sd);
  501. int ret;
  502. if (reg->reg > 0xff)
  503. return -EINVAL;
  504. ret = i2c_smbus_read_byte_data(client, reg->reg);
  505. if (ret < 0)
  506. return ret;
  507. /*
  508. * ret = int
  509. * reg->val = __u64
  510. */
  511. reg->val = (__u64)ret;
  512. return 0;
  513. }
  514. static int tw9910_s_register(struct v4l2_subdev *sd,
  515. struct v4l2_dbg_register *reg)
  516. {
  517. struct i2c_client *client = v4l2_get_subdevdata(sd);
  518. if (reg->reg > 0xff ||
  519. reg->val > 0xff)
  520. return -EINVAL;
  521. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  522. }
  523. #endif
  524. static int tw9910_set_frame(struct v4l2_subdev *sd, u32 *width, u32 *height)
  525. {
  526. struct i2c_client *client = v4l2_get_subdevdata(sd);
  527. struct tw9910_priv *priv = to_tw9910(client);
  528. struct soc_camera_device *icd = client->dev.platform_data;
  529. int ret = -EINVAL;
  530. u8 val;
  531. /*
  532. * select suitable norm
  533. */
  534. priv->scale = tw9910_select_norm(icd, *width, *height);
  535. if (!priv->scale)
  536. goto tw9910_set_fmt_error;
  537. /*
  538. * reset hardware
  539. */
  540. tw9910_reset(client);
  541. /*
  542. * set bus width
  543. */
  544. val = 0x00;
  545. if (SOCAM_DATAWIDTH_16 == priv->info->buswidth)
  546. val = LEN;
  547. ret = tw9910_mask_set(client, OPFORM, LEN, val);
  548. if (ret < 0)
  549. goto tw9910_set_fmt_error;
  550. /*
  551. * select MPOUT behavior
  552. */
  553. switch (priv->info->mpout) {
  554. case TW9910_MPO_VLOSS:
  555. val = RTSEL_VLOSS; break;
  556. case TW9910_MPO_HLOCK:
  557. val = RTSEL_HLOCK; break;
  558. case TW9910_MPO_SLOCK:
  559. val = RTSEL_SLOCK; break;
  560. case TW9910_MPO_VLOCK:
  561. val = RTSEL_VLOCK; break;
  562. case TW9910_MPO_MONO:
  563. val = RTSEL_MONO; break;
  564. case TW9910_MPO_DET50:
  565. val = RTSEL_DET50; break;
  566. case TW9910_MPO_FIELD:
  567. val = RTSEL_FIELD; break;
  568. case TW9910_MPO_RTCO:
  569. val = RTSEL_RTCO; break;
  570. default:
  571. val = 0;
  572. }
  573. ret = tw9910_mask_set(client, VBICNTL, RTSEL_MASK, val);
  574. if (ret < 0)
  575. goto tw9910_set_fmt_error;
  576. /*
  577. * set scale
  578. */
  579. ret = tw9910_set_scale(client, priv->scale);
  580. if (ret < 0)
  581. goto tw9910_set_fmt_error;
  582. /*
  583. * set hsync
  584. */
  585. ret = tw9910_set_hsync(client);
  586. if (ret < 0)
  587. goto tw9910_set_fmt_error;
  588. *width = priv->scale->width;
  589. *height = priv->scale->height;
  590. return ret;
  591. tw9910_set_fmt_error:
  592. tw9910_reset(client);
  593. priv->scale = NULL;
  594. return ret;
  595. }
  596. static int tw9910_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  597. {
  598. struct i2c_client *client = v4l2_get_subdevdata(sd);
  599. struct soc_camera_device *icd = client->dev.platform_data;
  600. a->c.left = 0;
  601. a->c.top = 0;
  602. if (icd->vdev->current_norm & V4L2_STD_NTSC) {
  603. a->c.width = 640;
  604. a->c.height = 480;
  605. } else {
  606. a->c.width = 768;
  607. a->c.height = 576;
  608. }
  609. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  610. return 0;
  611. }
  612. static int tw9910_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  613. {
  614. struct i2c_client *client = v4l2_get_subdevdata(sd);
  615. struct soc_camera_device *icd = client->dev.platform_data;
  616. a->bounds.left = 0;
  617. a->bounds.top = 0;
  618. if (icd->vdev->current_norm & V4L2_STD_NTSC) {
  619. a->bounds.width = 640;
  620. a->bounds.height = 480;
  621. } else {
  622. a->bounds.width = 768;
  623. a->bounds.height = 576;
  624. }
  625. a->defrect = a->bounds;
  626. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  627. a->pixelaspect.numerator = 1;
  628. a->pixelaspect.denominator = 1;
  629. return 0;
  630. }
  631. static int tw9910_g_fmt(struct v4l2_subdev *sd,
  632. struct v4l2_mbus_framefmt *mf)
  633. {
  634. struct i2c_client *client = v4l2_get_subdevdata(sd);
  635. struct tw9910_priv *priv = to_tw9910(client);
  636. if (!priv->scale) {
  637. int ret;
  638. u32 width = 640, height = 480;
  639. ret = tw9910_set_frame(sd, &width, &height);
  640. if (ret < 0)
  641. return ret;
  642. }
  643. mf->width = priv->scale->width;
  644. mf->height = priv->scale->height;
  645. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  646. mf->colorspace = V4L2_COLORSPACE_JPEG;
  647. mf->field = V4L2_FIELD_INTERLACED_BT;
  648. return 0;
  649. }
  650. static int tw9910_s_fmt(struct v4l2_subdev *sd,
  651. struct v4l2_mbus_framefmt *mf)
  652. {
  653. u32 width = mf->width, height = mf->height;
  654. int ret;
  655. WARN_ON(mf->field != V4L2_FIELD_ANY &&
  656. mf->field != V4L2_FIELD_INTERLACED_BT);
  657. /*
  658. * check color format
  659. */
  660. if (mf->code != V4L2_MBUS_FMT_UYVY8_2X8)
  661. return -EINVAL;
  662. mf->colorspace = V4L2_COLORSPACE_JPEG;
  663. ret = tw9910_set_frame(sd, &width, &height);
  664. if (!ret) {
  665. mf->width = width;
  666. mf->height = height;
  667. }
  668. return ret;
  669. }
  670. static int tw9910_try_fmt(struct v4l2_subdev *sd,
  671. struct v4l2_mbus_framefmt *mf)
  672. {
  673. struct i2c_client *client = v4l2_get_subdevdata(sd);
  674. struct soc_camera_device *icd = client->dev.platform_data;
  675. const struct tw9910_scale_ctrl *scale;
  676. if (V4L2_FIELD_ANY == mf->field) {
  677. mf->field = V4L2_FIELD_INTERLACED_BT;
  678. } else if (V4L2_FIELD_INTERLACED_BT != mf->field) {
  679. dev_err(&client->dev, "Field type %d invalid.\n", mf->field);
  680. return -EINVAL;
  681. }
  682. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  683. mf->colorspace = V4L2_COLORSPACE_JPEG;
  684. /*
  685. * select suitable norm
  686. */
  687. scale = tw9910_select_norm(icd, mf->width, mf->height);
  688. if (!scale)
  689. return -EINVAL;
  690. mf->width = scale->width;
  691. mf->height = scale->height;
  692. return 0;
  693. }
  694. static int tw9910_video_probe(struct soc_camera_device *icd,
  695. struct i2c_client *client)
  696. {
  697. struct tw9910_priv *priv = to_tw9910(client);
  698. s32 id;
  699. /* We must have a parent by now. And it cannot be a wrong one. */
  700. BUG_ON(!icd->parent ||
  701. to_soc_camera_host(icd->parent)->nr != icd->iface);
  702. /*
  703. * tw9910 only use 8 or 16 bit bus width
  704. */
  705. if (SOCAM_DATAWIDTH_16 != priv->info->buswidth &&
  706. SOCAM_DATAWIDTH_8 != priv->info->buswidth) {
  707. dev_err(&client->dev, "bus width error\n");
  708. return -ENODEV;
  709. }
  710. /*
  711. * check and show Product ID
  712. * So far only revisions 0 and 1 have been seen
  713. */
  714. id = i2c_smbus_read_byte_data(client, ID);
  715. priv->revision = GET_REV(id);
  716. id = GET_ID(id);
  717. if (0x0B != id ||
  718. 0x01 < priv->revision) {
  719. dev_err(&client->dev,
  720. "Product ID error %x:%x\n",
  721. id, priv->revision);
  722. return -ENODEV;
  723. }
  724. dev_info(&client->dev,
  725. "tw9910 Product ID %0x:%0x\n", id, priv->revision);
  726. icd->vdev->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL;
  727. icd->vdev->current_norm = V4L2_STD_NTSC;
  728. return 0;
  729. }
  730. static struct soc_camera_ops tw9910_ops = {
  731. .set_bus_param = tw9910_set_bus_param,
  732. .query_bus_param = tw9910_query_bus_param,
  733. };
  734. static struct v4l2_subdev_core_ops tw9910_subdev_core_ops = {
  735. .g_chip_ident = tw9910_g_chip_ident,
  736. .s_std = tw9910_s_std,
  737. #ifdef CONFIG_VIDEO_ADV_DEBUG
  738. .g_register = tw9910_g_register,
  739. .s_register = tw9910_s_register,
  740. #endif
  741. };
  742. static int tw9910_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  743. enum v4l2_mbus_pixelcode *code)
  744. {
  745. if (index)
  746. return -EINVAL;
  747. *code = V4L2_MBUS_FMT_UYVY8_2X8;
  748. return 0;
  749. }
  750. static int tw9910_g_mbus_config(struct v4l2_subdev *sd,
  751. struct v4l2_mbus_config *cfg)
  752. {
  753. struct i2c_client *client = v4l2_get_subdevdata(sd);
  754. struct soc_camera_device *icd = client->dev.platform_data;
  755. struct soc_camera_link *icl = to_soc_camera_link(icd);
  756. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
  757. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  758. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  759. V4L2_MBUS_DATA_ACTIVE_HIGH;
  760. cfg->type = V4L2_MBUS_PARALLEL;
  761. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  762. return 0;
  763. }
  764. static int tw9910_s_mbus_config(struct v4l2_subdev *sd,
  765. const struct v4l2_mbus_config *cfg)
  766. {
  767. struct i2c_client *client = v4l2_get_subdevdata(sd);
  768. struct soc_camera_device *icd = client->dev.platform_data;
  769. struct soc_camera_link *icl = to_soc_camera_link(icd);
  770. u8 val = VSSL_VVALID | HSSL_DVALID;
  771. unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
  772. /*
  773. * set OUTCTR1
  774. *
  775. * We use VVALID and DVALID signals to control VSYNC and HSYNC
  776. * outputs, in this mode their polarity is inverted.
  777. */
  778. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  779. val |= HSP_HI;
  780. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  781. val |= VSP_HI;
  782. return i2c_smbus_write_byte_data(client, OUTCTR1, val);
  783. }
  784. static struct v4l2_subdev_video_ops tw9910_subdev_video_ops = {
  785. .s_stream = tw9910_s_stream,
  786. .g_mbus_fmt = tw9910_g_fmt,
  787. .s_mbus_fmt = tw9910_s_fmt,
  788. .try_mbus_fmt = tw9910_try_fmt,
  789. .cropcap = tw9910_cropcap,
  790. .g_crop = tw9910_g_crop,
  791. .enum_mbus_fmt = tw9910_enum_fmt,
  792. .g_mbus_config = tw9910_g_mbus_config,
  793. .s_mbus_config = tw9910_s_mbus_config,
  794. };
  795. static struct v4l2_subdev_ops tw9910_subdev_ops = {
  796. .core = &tw9910_subdev_core_ops,
  797. .video = &tw9910_subdev_video_ops,
  798. };
  799. /*
  800. * i2c_driver function
  801. */
  802. static int tw9910_probe(struct i2c_client *client,
  803. const struct i2c_device_id *did)
  804. {
  805. struct tw9910_priv *priv;
  806. struct tw9910_video_info *info;
  807. struct soc_camera_device *icd = client->dev.platform_data;
  808. struct i2c_adapter *adapter =
  809. to_i2c_adapter(client->dev.parent);
  810. struct soc_camera_link *icl;
  811. int ret;
  812. if (!icd) {
  813. dev_err(&client->dev, "TW9910: missing soc-camera data!\n");
  814. return -EINVAL;
  815. }
  816. icl = to_soc_camera_link(icd);
  817. if (!icl || !icl->priv)
  818. return -EINVAL;
  819. info = icl->priv;
  820. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  821. dev_err(&client->dev,
  822. "I2C-Adapter doesn't support "
  823. "I2C_FUNC_SMBUS_BYTE_DATA\n");
  824. return -EIO;
  825. }
  826. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  827. if (!priv)
  828. return -ENOMEM;
  829. priv->info = info;
  830. v4l2_i2c_subdev_init(&priv->subdev, client, &tw9910_subdev_ops);
  831. icd->ops = &tw9910_ops;
  832. icd->iface = icl->bus_id;
  833. ret = tw9910_video_probe(icd, client);
  834. if (ret) {
  835. icd->ops = NULL;
  836. kfree(priv);
  837. }
  838. return ret;
  839. }
  840. static int tw9910_remove(struct i2c_client *client)
  841. {
  842. struct tw9910_priv *priv = to_tw9910(client);
  843. struct soc_camera_device *icd = client->dev.platform_data;
  844. icd->ops = NULL;
  845. kfree(priv);
  846. return 0;
  847. }
  848. static const struct i2c_device_id tw9910_id[] = {
  849. { "tw9910", 0 },
  850. { }
  851. };
  852. MODULE_DEVICE_TABLE(i2c, tw9910_id);
  853. static struct i2c_driver tw9910_i2c_driver = {
  854. .driver = {
  855. .name = "tw9910",
  856. },
  857. .probe = tw9910_probe,
  858. .remove = tw9910_remove,
  859. .id_table = tw9910_id,
  860. };
  861. /*
  862. * module function
  863. */
  864. static int __init tw9910_module_init(void)
  865. {
  866. return i2c_add_driver(&tw9910_i2c_driver);
  867. }
  868. static void __exit tw9910_module_exit(void)
  869. {
  870. i2c_del_driver(&tw9910_i2c_driver);
  871. }
  872. module_init(tw9910_module_init);
  873. module_exit(tw9910_module_exit);
  874. MODULE_DESCRIPTION("SoC Camera driver for tw9910");
  875. MODULE_AUTHOR("Kuninori Morimoto");
  876. MODULE_LICENSE("GPL v2");