tw9910.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * tw9910 Video Driver
  4. *
  5. * Copyright (C) 2017 Jacopo Mondi <jacopo+renesas@jmondi.org>
  6. *
  7. * Copyright (C) 2008 Renesas Solutions Corp.
  8. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  9. *
  10. * Based on ov772x driver,
  11. *
  12. * Copyright (C) 2008 Kuninori Morimoto <morimoto.kuninori@renesas.com>
  13. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  14. * Copyright (C) 2008 Magnus Damm
  15. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/i2c.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/v4l2-mediabus.h>
  26. #include <linux/videodev2.h>
  27. #include <media/i2c/tw9910.h>
  28. #include <media/v4l2-subdev.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. /* HSYNC start and end are constant for now */
  195. #define HSYNC_START 0x0260
  196. #define HSYNC_END 0x0300
  197. /*
  198. * structure
  199. */
  200. struct tw9910_scale_ctrl {
  201. char *name;
  202. unsigned short width;
  203. unsigned short height;
  204. u16 hscale;
  205. u16 vscale;
  206. };
  207. struct tw9910_priv {
  208. struct v4l2_subdev subdev;
  209. struct clk *clk;
  210. struct tw9910_video_info *info;
  211. struct gpio_desc *pdn_gpio;
  212. struct gpio_desc *rstb_gpio;
  213. const struct tw9910_scale_ctrl *scale;
  214. v4l2_std_id norm;
  215. u32 revision;
  216. };
  217. static const struct tw9910_scale_ctrl tw9910_ntsc_scales[] = {
  218. {
  219. .name = "NTSC SQ",
  220. .width = 640,
  221. .height = 480,
  222. .hscale = 0x0100,
  223. .vscale = 0x0100,
  224. },
  225. {
  226. .name = "NTSC CCIR601",
  227. .width = 720,
  228. .height = 480,
  229. .hscale = 0x0100,
  230. .vscale = 0x0100,
  231. },
  232. {
  233. .name = "NTSC SQ (CIF)",
  234. .width = 320,
  235. .height = 240,
  236. .hscale = 0x0200,
  237. .vscale = 0x0200,
  238. },
  239. {
  240. .name = "NTSC CCIR601 (CIF)",
  241. .width = 360,
  242. .height = 240,
  243. .hscale = 0x0200,
  244. .vscale = 0x0200,
  245. },
  246. {
  247. .name = "NTSC SQ (QCIF)",
  248. .width = 160,
  249. .height = 120,
  250. .hscale = 0x0400,
  251. .vscale = 0x0400,
  252. },
  253. {
  254. .name = "NTSC CCIR601 (QCIF)",
  255. .width = 180,
  256. .height = 120,
  257. .hscale = 0x0400,
  258. .vscale = 0x0400,
  259. },
  260. };
  261. static const struct tw9910_scale_ctrl tw9910_pal_scales[] = {
  262. {
  263. .name = "PAL SQ",
  264. .width = 768,
  265. .height = 576,
  266. .hscale = 0x0100,
  267. .vscale = 0x0100,
  268. },
  269. {
  270. .name = "PAL CCIR601",
  271. .width = 720,
  272. .height = 576,
  273. .hscale = 0x0100,
  274. .vscale = 0x0100,
  275. },
  276. {
  277. .name = "PAL SQ (CIF)",
  278. .width = 384,
  279. .height = 288,
  280. .hscale = 0x0200,
  281. .vscale = 0x0200,
  282. },
  283. {
  284. .name = "PAL CCIR601 (CIF)",
  285. .width = 360,
  286. .height = 288,
  287. .hscale = 0x0200,
  288. .vscale = 0x0200,
  289. },
  290. {
  291. .name = "PAL SQ (QCIF)",
  292. .width = 192,
  293. .height = 144,
  294. .hscale = 0x0400,
  295. .vscale = 0x0400,
  296. },
  297. {
  298. .name = "PAL CCIR601 (QCIF)",
  299. .width = 180,
  300. .height = 144,
  301. .hscale = 0x0400,
  302. .vscale = 0x0400,
  303. },
  304. };
  305. /*
  306. * general function
  307. */
  308. static struct tw9910_priv *to_tw9910(const struct i2c_client *client)
  309. {
  310. return container_of(i2c_get_clientdata(client), struct tw9910_priv,
  311. subdev);
  312. }
  313. static int tw9910_mask_set(struct i2c_client *client, u8 command,
  314. u8 mask, u8 set)
  315. {
  316. s32 val = i2c_smbus_read_byte_data(client, command);
  317. if (val < 0)
  318. return val;
  319. val &= ~mask;
  320. val |= set & mask;
  321. return i2c_smbus_write_byte_data(client, command, val);
  322. }
  323. static int tw9910_set_scale(struct i2c_client *client,
  324. const struct tw9910_scale_ctrl *scale)
  325. {
  326. int ret;
  327. ret = i2c_smbus_write_byte_data(client, SCALE_HI,
  328. (scale->vscale & 0x0F00) >> 4 |
  329. (scale->hscale & 0x0F00) >> 8);
  330. if (ret < 0)
  331. return ret;
  332. ret = i2c_smbus_write_byte_data(client, HSCALE_LO,
  333. scale->hscale & 0x00FF);
  334. if (ret < 0)
  335. return ret;
  336. ret = i2c_smbus_write_byte_data(client, VSCALE_LO,
  337. scale->vscale & 0x00FF);
  338. return ret;
  339. }
  340. static int tw9910_set_hsync(struct i2c_client *client)
  341. {
  342. struct tw9910_priv *priv = to_tw9910(client);
  343. int ret;
  344. /* bit 10 - 3 */
  345. ret = i2c_smbus_write_byte_data(client, HSBEGIN,
  346. (HSYNC_START & 0x07F8) >> 3);
  347. if (ret < 0)
  348. return ret;
  349. /* bit 10 - 3 */
  350. ret = i2c_smbus_write_byte_data(client, HSEND,
  351. (HSYNC_END & 0x07F8) >> 3);
  352. if (ret < 0)
  353. return ret;
  354. /* So far only revisions 0 and 1 have been seen. */
  355. /* bit 2 - 0 */
  356. if (priv->revision == 1)
  357. ret = tw9910_mask_set(client, HSLOWCTL, 0x77,
  358. (HSYNC_START & 0x0007) << 4 |
  359. (HSYNC_END & 0x0007));
  360. return ret;
  361. }
  362. static void tw9910_reset(struct i2c_client *client)
  363. {
  364. tw9910_mask_set(client, ACNTL1, SRESET, SRESET);
  365. usleep_range(1000, 5000);
  366. }
  367. static int tw9910_power(struct i2c_client *client, int enable)
  368. {
  369. int ret;
  370. u8 acntl1;
  371. u8 acntl2;
  372. if (enable) {
  373. acntl1 = 0;
  374. acntl2 = 0;
  375. } else {
  376. acntl1 = CLK_PDN | Y_PDN | C_PDN;
  377. acntl2 = PLL_PDN;
  378. }
  379. ret = tw9910_mask_set(client, ACNTL1, ACNTL1_PDN_MASK, acntl1);
  380. if (ret < 0)
  381. return ret;
  382. return tw9910_mask_set(client, ACNTL2, ACNTL2_PDN_MASK, acntl2);
  383. }
  384. static const struct tw9910_scale_ctrl *tw9910_select_norm(v4l2_std_id norm,
  385. u32 width, u32 height)
  386. {
  387. const struct tw9910_scale_ctrl *scale;
  388. const struct tw9910_scale_ctrl *ret = NULL;
  389. __u32 diff = 0xffffffff, tmp;
  390. int size, i;
  391. if (norm & V4L2_STD_NTSC) {
  392. scale = tw9910_ntsc_scales;
  393. size = ARRAY_SIZE(tw9910_ntsc_scales);
  394. } else if (norm & V4L2_STD_PAL) {
  395. scale = tw9910_pal_scales;
  396. size = ARRAY_SIZE(tw9910_pal_scales);
  397. } else {
  398. return NULL;
  399. }
  400. for (i = 0; i < size; i++) {
  401. tmp = abs(width - scale[i].width) +
  402. abs(height - scale[i].height);
  403. if (tmp < diff) {
  404. diff = tmp;
  405. ret = scale + i;
  406. }
  407. }
  408. return ret;
  409. }
  410. /*
  411. * subdevice operations
  412. */
  413. static int tw9910_s_stream(struct v4l2_subdev *sd, int enable)
  414. {
  415. struct i2c_client *client = v4l2_get_subdevdata(sd);
  416. struct tw9910_priv *priv = to_tw9910(client);
  417. u8 val;
  418. int ret;
  419. if (!enable) {
  420. switch (priv->revision) {
  421. case 0:
  422. val = OEN_TRI_SEL_ALL_OFF_r0;
  423. break;
  424. case 1:
  425. val = OEN_TRI_SEL_ALL_OFF_r1;
  426. break;
  427. default:
  428. dev_err(&client->dev, "un-supported revision\n");
  429. return -EINVAL;
  430. }
  431. } else {
  432. val = OEN_TRI_SEL_ALL_ON;
  433. if (!priv->scale) {
  434. dev_err(&client->dev, "norm select error\n");
  435. return -EPERM;
  436. }
  437. dev_dbg(&client->dev, "%s %dx%d\n",
  438. priv->scale->name,
  439. priv->scale->width,
  440. priv->scale->height);
  441. }
  442. ret = tw9910_mask_set(client, OPFORM, OEN_TRI_SEL_MASK, val);
  443. if (ret < 0)
  444. return ret;
  445. return tw9910_power(client, enable);
  446. }
  447. static int tw9910_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
  448. {
  449. struct i2c_client *client = v4l2_get_subdevdata(sd);
  450. struct tw9910_priv *priv = to_tw9910(client);
  451. *norm = priv->norm;
  452. return 0;
  453. }
  454. static int tw9910_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  455. {
  456. struct i2c_client *client = v4l2_get_subdevdata(sd);
  457. struct tw9910_priv *priv = to_tw9910(client);
  458. const unsigned int hact = 720;
  459. const unsigned int hdelay = 15;
  460. unsigned int vact;
  461. unsigned int vdelay;
  462. int ret;
  463. if (!(norm & (V4L2_STD_NTSC | V4L2_STD_PAL)))
  464. return -EINVAL;
  465. priv->norm = norm;
  466. if (norm & V4L2_STD_525_60) {
  467. vact = 240;
  468. vdelay = 18;
  469. ret = tw9910_mask_set(client, VVBI, 0x10, 0x10);
  470. } else {
  471. vact = 288;
  472. vdelay = 24;
  473. ret = tw9910_mask_set(client, VVBI, 0x10, 0x00);
  474. }
  475. if (!ret)
  476. ret = i2c_smbus_write_byte_data(client, CROP_HI,
  477. ((vdelay >> 2) & 0xc0) |
  478. ((vact >> 4) & 0x30) |
  479. ((hdelay >> 6) & 0x0c) |
  480. ((hact >> 8) & 0x03));
  481. if (!ret)
  482. ret = i2c_smbus_write_byte_data(client, VDELAY_LO,
  483. vdelay & 0xff);
  484. if (!ret)
  485. ret = i2c_smbus_write_byte_data(client, VACTIVE_LO,
  486. vact & 0xff);
  487. return ret;
  488. }
  489. #ifdef CONFIG_VIDEO_ADV_DEBUG
  490. static int tw9910_g_register(struct v4l2_subdev *sd,
  491. struct v4l2_dbg_register *reg)
  492. {
  493. struct i2c_client *client = v4l2_get_subdevdata(sd);
  494. int ret;
  495. if (reg->reg > 0xff)
  496. return -EINVAL;
  497. reg->size = 1;
  498. ret = i2c_smbus_read_byte_data(client, reg->reg);
  499. if (ret < 0)
  500. return ret;
  501. /*
  502. * ret = int
  503. * reg->val = __u64
  504. */
  505. reg->val = (__u64)ret;
  506. return 0;
  507. }
  508. static int tw9910_s_register(struct v4l2_subdev *sd,
  509. const struct v4l2_dbg_register *reg)
  510. {
  511. struct i2c_client *client = v4l2_get_subdevdata(sd);
  512. if (reg->reg > 0xff ||
  513. reg->val > 0xff)
  514. return -EINVAL;
  515. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  516. }
  517. #endif
  518. static void tw9910_set_gpio_value(struct gpio_desc *desc, int value)
  519. {
  520. if (desc) {
  521. gpiod_set_value(desc, value);
  522. usleep_range(500, 1000);
  523. }
  524. }
  525. static int tw9910_power_on(struct tw9910_priv *priv)
  526. {
  527. struct i2c_client *client = v4l2_get_subdevdata(&priv->subdev);
  528. int ret;
  529. if (priv->clk) {
  530. ret = clk_prepare_enable(priv->clk);
  531. if (ret)
  532. return ret;
  533. }
  534. tw9910_set_gpio_value(priv->pdn_gpio, 0);
  535. /*
  536. * FIXME: The reset signal is connected to a shared GPIO on some
  537. * platforms (namely the SuperH Migo-R). Until a framework becomes
  538. * available to handle this cleanly, request the GPIO temporarily
  539. * to avoid conflicts.
  540. */
  541. priv->rstb_gpio = gpiod_get_optional(&client->dev, "rstb",
  542. GPIOD_OUT_LOW);
  543. if (IS_ERR(priv->rstb_gpio)) {
  544. dev_info(&client->dev, "Unable to get GPIO \"rstb\"");
  545. clk_disable_unprepare(priv->clk);
  546. tw9910_set_gpio_value(priv->pdn_gpio, 1);
  547. return PTR_ERR(priv->rstb_gpio);
  548. }
  549. if (priv->rstb_gpio) {
  550. tw9910_set_gpio_value(priv->rstb_gpio, 1);
  551. tw9910_set_gpio_value(priv->rstb_gpio, 0);
  552. gpiod_put(priv->rstb_gpio);
  553. }
  554. return 0;
  555. }
  556. static int tw9910_power_off(struct tw9910_priv *priv)
  557. {
  558. clk_disable_unprepare(priv->clk);
  559. tw9910_set_gpio_value(priv->pdn_gpio, 1);
  560. return 0;
  561. }
  562. static int tw9910_s_power(struct v4l2_subdev *sd, int on)
  563. {
  564. struct i2c_client *client = v4l2_get_subdevdata(sd);
  565. struct tw9910_priv *priv = to_tw9910(client);
  566. return on ? tw9910_power_on(priv) : tw9910_power_off(priv);
  567. }
  568. static int tw9910_set_frame(struct v4l2_subdev *sd, u32 *width, u32 *height)
  569. {
  570. struct i2c_client *client = v4l2_get_subdevdata(sd);
  571. struct tw9910_priv *priv = to_tw9910(client);
  572. int ret = -EINVAL;
  573. u8 val;
  574. /* Select suitable norm. */
  575. priv->scale = tw9910_select_norm(priv->norm, *width, *height);
  576. if (!priv->scale)
  577. goto tw9910_set_fmt_error;
  578. /* Reset hardware. */
  579. tw9910_reset(client);
  580. /* Set bus width. */
  581. val = 0x00;
  582. if (priv->info->buswidth == 16)
  583. val = LEN;
  584. ret = tw9910_mask_set(client, OPFORM, LEN, val);
  585. if (ret < 0)
  586. goto tw9910_set_fmt_error;
  587. /* Select MPOUT behavior. */
  588. switch (priv->info->mpout) {
  589. case TW9910_MPO_VLOSS:
  590. val = RTSEL_VLOSS; break;
  591. case TW9910_MPO_HLOCK:
  592. val = RTSEL_HLOCK; break;
  593. case TW9910_MPO_SLOCK:
  594. val = RTSEL_SLOCK; break;
  595. case TW9910_MPO_VLOCK:
  596. val = RTSEL_VLOCK; break;
  597. case TW9910_MPO_MONO:
  598. val = RTSEL_MONO; break;
  599. case TW9910_MPO_DET50:
  600. val = RTSEL_DET50; break;
  601. case TW9910_MPO_FIELD:
  602. val = RTSEL_FIELD; break;
  603. case TW9910_MPO_RTCO:
  604. val = RTSEL_RTCO; break;
  605. default:
  606. val = 0;
  607. }
  608. ret = tw9910_mask_set(client, VBICNTL, RTSEL_MASK, val);
  609. if (ret < 0)
  610. goto tw9910_set_fmt_error;
  611. /* Set scale. */
  612. ret = tw9910_set_scale(client, priv->scale);
  613. if (ret < 0)
  614. goto tw9910_set_fmt_error;
  615. /* Set hsync. */
  616. ret = tw9910_set_hsync(client);
  617. if (ret < 0)
  618. goto tw9910_set_fmt_error;
  619. *width = priv->scale->width;
  620. *height = priv->scale->height;
  621. return ret;
  622. tw9910_set_fmt_error:
  623. tw9910_reset(client);
  624. priv->scale = NULL;
  625. return ret;
  626. }
  627. static int tw9910_get_selection(struct v4l2_subdev *sd,
  628. struct v4l2_subdev_state *sd_state,
  629. struct v4l2_subdev_selection *sel)
  630. {
  631. struct i2c_client *client = v4l2_get_subdevdata(sd);
  632. struct tw9910_priv *priv = to_tw9910(client);
  633. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  634. return -EINVAL;
  635. /* Only CROP, CROP_DEFAULT and CROP_BOUNDS are supported. */
  636. if (sel->target > V4L2_SEL_TGT_CROP_BOUNDS)
  637. return -EINVAL;
  638. sel->r.left = 0;
  639. sel->r.top = 0;
  640. if (priv->norm & V4L2_STD_NTSC) {
  641. sel->r.width = 640;
  642. sel->r.height = 480;
  643. } else {
  644. sel->r.width = 768;
  645. sel->r.height = 576;
  646. }
  647. return 0;
  648. }
  649. static int tw9910_get_fmt(struct v4l2_subdev *sd,
  650. struct v4l2_subdev_state *sd_state,
  651. struct v4l2_subdev_format *format)
  652. {
  653. struct v4l2_mbus_framefmt *mf = &format->format;
  654. struct i2c_client *client = v4l2_get_subdevdata(sd);
  655. struct tw9910_priv *priv = to_tw9910(client);
  656. if (format->pad)
  657. return -EINVAL;
  658. if (!priv->scale) {
  659. priv->scale = tw9910_select_norm(priv->norm, 640, 480);
  660. if (!priv->scale)
  661. return -EINVAL;
  662. }
  663. mf->width = priv->scale->width;
  664. mf->height = priv->scale->height;
  665. mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
  666. mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
  667. mf->field = V4L2_FIELD_INTERLACED_BT;
  668. return 0;
  669. }
  670. static int tw9910_s_fmt(struct v4l2_subdev *sd,
  671. struct v4l2_mbus_framefmt *mf)
  672. {
  673. u32 width = mf->width, height = mf->height;
  674. int ret;
  675. WARN_ON(mf->field != V4L2_FIELD_ANY &&
  676. mf->field != V4L2_FIELD_INTERLACED_BT);
  677. /* Check color format. */
  678. if (mf->code != MEDIA_BUS_FMT_UYVY8_2X8)
  679. return -EINVAL;
  680. mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
  681. ret = tw9910_set_frame(sd, &width, &height);
  682. if (ret)
  683. return ret;
  684. mf->width = width;
  685. mf->height = height;
  686. return 0;
  687. }
  688. static int tw9910_set_fmt(struct v4l2_subdev *sd,
  689. struct v4l2_subdev_state *sd_state,
  690. struct v4l2_subdev_format *format)
  691. {
  692. struct v4l2_mbus_framefmt *mf = &format->format;
  693. struct i2c_client *client = v4l2_get_subdevdata(sd);
  694. struct tw9910_priv *priv = to_tw9910(client);
  695. const struct tw9910_scale_ctrl *scale;
  696. if (format->pad)
  697. return -EINVAL;
  698. if (mf->field == V4L2_FIELD_ANY) {
  699. mf->field = V4L2_FIELD_INTERLACED_BT;
  700. } else if (mf->field != V4L2_FIELD_INTERLACED_BT) {
  701. dev_err(&client->dev, "Field type %d invalid\n", mf->field);
  702. return -EINVAL;
  703. }
  704. mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
  705. mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
  706. /* Select suitable norm. */
  707. scale = tw9910_select_norm(priv->norm, mf->width, mf->height);
  708. if (!scale)
  709. return -EINVAL;
  710. mf->width = scale->width;
  711. mf->height = scale->height;
  712. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  713. return tw9910_s_fmt(sd, mf);
  714. return 0;
  715. }
  716. static int tw9910_video_probe(struct i2c_client *client)
  717. {
  718. struct tw9910_priv *priv = to_tw9910(client);
  719. s32 id;
  720. int ret;
  721. /* TW9910 only use 8 or 16 bit bus width. */
  722. if (priv->info->buswidth != 16 && priv->info->buswidth != 8) {
  723. dev_err(&client->dev, "bus width error\n");
  724. return -ENODEV;
  725. }
  726. ret = tw9910_s_power(&priv->subdev, 1);
  727. if (ret < 0)
  728. return ret;
  729. /*
  730. * Check and show Product ID.
  731. * So far only revisions 0 and 1 have been seen.
  732. */
  733. id = i2c_smbus_read_byte_data(client, ID);
  734. priv->revision = GET_REV(id);
  735. id = GET_ID(id);
  736. if (id != 0x0b || priv->revision > 0x01) {
  737. dev_err(&client->dev, "Product ID error %x:%x\n",
  738. id, priv->revision);
  739. ret = -ENODEV;
  740. goto done;
  741. }
  742. dev_info(&client->dev, "tw9910 Product ID %0x:%0x\n",
  743. id, priv->revision);
  744. priv->norm = V4L2_STD_NTSC;
  745. priv->scale = &tw9910_ntsc_scales[0];
  746. done:
  747. tw9910_s_power(&priv->subdev, 0);
  748. return ret;
  749. }
  750. static const struct v4l2_subdev_core_ops tw9910_subdev_core_ops = {
  751. #ifdef CONFIG_VIDEO_ADV_DEBUG
  752. .g_register = tw9910_g_register,
  753. .s_register = tw9910_s_register,
  754. #endif
  755. .s_power = tw9910_s_power,
  756. };
  757. static int tw9910_enum_mbus_code(struct v4l2_subdev *sd,
  758. struct v4l2_subdev_state *sd_state,
  759. struct v4l2_subdev_mbus_code_enum *code)
  760. {
  761. if (code->pad || code->index)
  762. return -EINVAL;
  763. code->code = MEDIA_BUS_FMT_UYVY8_2X8;
  764. return 0;
  765. }
  766. static int tw9910_g_tvnorms(struct v4l2_subdev *sd, v4l2_std_id *norm)
  767. {
  768. *norm = V4L2_STD_NTSC | V4L2_STD_PAL;
  769. return 0;
  770. }
  771. static const struct v4l2_subdev_video_ops tw9910_subdev_video_ops = {
  772. .s_std = tw9910_s_std,
  773. .g_std = tw9910_g_std,
  774. .s_stream = tw9910_s_stream,
  775. .g_tvnorms = tw9910_g_tvnorms,
  776. };
  777. static const struct v4l2_subdev_pad_ops tw9910_subdev_pad_ops = {
  778. .enum_mbus_code = tw9910_enum_mbus_code,
  779. .get_selection = tw9910_get_selection,
  780. .get_fmt = tw9910_get_fmt,
  781. .set_fmt = tw9910_set_fmt,
  782. };
  783. static const struct v4l2_subdev_ops tw9910_subdev_ops = {
  784. .core = &tw9910_subdev_core_ops,
  785. .video = &tw9910_subdev_video_ops,
  786. .pad = &tw9910_subdev_pad_ops,
  787. };
  788. /*
  789. * i2c_driver function
  790. */
  791. static int tw9910_probe(struct i2c_client *client)
  792. {
  793. struct tw9910_priv *priv;
  794. struct tw9910_video_info *info;
  795. struct i2c_adapter *adapter = client->adapter;
  796. int ret;
  797. if (!client->dev.platform_data) {
  798. dev_err(&client->dev, "TW9910: missing platform data!\n");
  799. return -EINVAL;
  800. }
  801. info = client->dev.platform_data;
  802. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  803. dev_err(&client->dev,
  804. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE_DATA\n");
  805. return -EIO;
  806. }
  807. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  808. if (!priv)
  809. return -ENOMEM;
  810. priv->info = info;
  811. v4l2_i2c_subdev_init(&priv->subdev, client, &tw9910_subdev_ops);
  812. priv->clk = clk_get(&client->dev, "xti");
  813. if (PTR_ERR(priv->clk) == -ENOENT) {
  814. priv->clk = NULL;
  815. } else if (IS_ERR(priv->clk)) {
  816. dev_err(&client->dev, "Unable to get xti clock\n");
  817. return PTR_ERR(priv->clk);
  818. }
  819. priv->pdn_gpio = gpiod_get_optional(&client->dev, "pdn",
  820. GPIOD_OUT_HIGH);
  821. if (IS_ERR(priv->pdn_gpio)) {
  822. dev_info(&client->dev, "Unable to get GPIO \"pdn\"");
  823. ret = PTR_ERR(priv->pdn_gpio);
  824. goto error_clk_put;
  825. }
  826. ret = tw9910_video_probe(client);
  827. if (ret < 0)
  828. goto error_gpio_put;
  829. ret = v4l2_async_register_subdev(&priv->subdev);
  830. if (ret)
  831. goto error_gpio_put;
  832. return ret;
  833. error_gpio_put:
  834. if (priv->pdn_gpio)
  835. gpiod_put(priv->pdn_gpio);
  836. error_clk_put:
  837. clk_put(priv->clk);
  838. return ret;
  839. }
  840. static void tw9910_remove(struct i2c_client *client)
  841. {
  842. struct tw9910_priv *priv = to_tw9910(client);
  843. if (priv->pdn_gpio)
  844. gpiod_put(priv->pdn_gpio);
  845. clk_put(priv->clk);
  846. v4l2_async_unregister_subdev(&priv->subdev);
  847. }
  848. static const struct i2c_device_id tw9910_id[] = {
  849. { "tw9910" },
  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. module_i2c_driver(tw9910_i2c_driver);
  862. MODULE_DESCRIPTION("V4L2 driver for TW9910 video decoder");
  863. MODULE_AUTHOR("Kuninori Morimoto");
  864. MODULE_LICENSE("GPL v2");