tw9910.c 24 KB

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