tw9910.c 25 KB

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