tvp514x.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. /*
  2. * drivers/media/i2c/tvp514x.c
  3. *
  4. * TI TVP5146/47 decoder driver
  5. *
  6. * Copyright (C) 2008 Texas Instruments Inc
  7. * Author: Vaibhav Hiremath <hvaibhav@ti.com>
  8. *
  9. * Contributors:
  10. * Sivaraj R <sivaraj@ti.com>
  11. * Brijesh R Jadav <brijesh.j@ti.com>
  12. * Hardik Shah <hardik.shah@ti.com>
  13. * Manjunath Hadli <mrh@ti.com>
  14. * Karicheri Muralidharan <m-karicheri2@ti.com>
  15. * Prabhakar Lad <prabhakar.lad@ti.com>
  16. *
  17. * This package is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. */
  31. #include <linux/i2c.h>
  32. #include <linux/slab.h>
  33. #include <linux/delay.h>
  34. #include <linux/videodev2.h>
  35. #include <linux/module.h>
  36. #include <linux/v4l2-mediabus.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-common.h>
  39. #include <media/v4l2-mediabus.h>
  40. #include <media/v4l2-chip-ident.h>
  41. #include <media/v4l2-ctrls.h>
  42. #include <media/tvp514x.h>
  43. #include <media/media-entity.h>
  44. #include "tvp514x_regs.h"
  45. /* Private macros for TVP */
  46. #define I2C_RETRY_COUNT (5)
  47. #define LOCK_RETRY_COUNT (5)
  48. #define LOCK_RETRY_DELAY (200)
  49. /* Debug functions */
  50. static bool debug;
  51. module_param(debug, bool, 0644);
  52. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  53. MODULE_AUTHOR("Texas Instruments");
  54. MODULE_DESCRIPTION("TVP514X linux decoder driver");
  55. MODULE_LICENSE("GPL");
  56. /* enum tvp514x_std - enum for supported standards */
  57. enum tvp514x_std {
  58. STD_NTSC_MJ = 0,
  59. STD_PAL_BDGHIN,
  60. STD_INVALID
  61. };
  62. /**
  63. * struct tvp514x_std_info - Structure to store standard informations
  64. * @width: Line width in pixels
  65. * @height:Number of active lines
  66. * @video_std: Value to write in REG_VIDEO_STD register
  67. * @standard: v4l2 standard structure information
  68. */
  69. struct tvp514x_std_info {
  70. unsigned long width;
  71. unsigned long height;
  72. u8 video_std;
  73. struct v4l2_standard standard;
  74. };
  75. static struct tvp514x_reg tvp514x_reg_list_default[0x40];
  76. static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
  77. /**
  78. * struct tvp514x_decoder - TVP5146/47 decoder object
  79. * @sd: Subdevice Slave handle
  80. * @tvp514x_regs: copy of hw's regs with preset values.
  81. * @pdata: Board specific
  82. * @ver: Chip version
  83. * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
  84. * @pix: Current pixel format
  85. * @num_fmts: Number of formats
  86. * @fmt_list: Format list
  87. * @current_std: Current standard
  88. * @num_stds: Number of standards
  89. * @std_list: Standards list
  90. * @input: Input routing at chip level
  91. * @output: Output routing at chip level
  92. */
  93. struct tvp514x_decoder {
  94. struct v4l2_subdev sd;
  95. struct v4l2_ctrl_handler hdl;
  96. struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
  97. const struct tvp514x_platform_data *pdata;
  98. int ver;
  99. int streaming;
  100. struct v4l2_pix_format pix;
  101. int num_fmts;
  102. const struct v4l2_fmtdesc *fmt_list;
  103. enum tvp514x_std current_std;
  104. int num_stds;
  105. const struct tvp514x_std_info *std_list;
  106. /* Input and Output Routing parameters */
  107. u32 input;
  108. u32 output;
  109. /* mc related members */
  110. struct media_pad pad;
  111. struct v4l2_mbus_framefmt format;
  112. };
  113. /* TVP514x default register values */
  114. static struct tvp514x_reg tvp514x_reg_list_default[] = {
  115. /* Composite selected */
  116. {TOK_WRITE, REG_INPUT_SEL, 0x05},
  117. {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
  118. /* Auto mode */
  119. {TOK_WRITE, REG_VIDEO_STD, 0x00},
  120. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  121. {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
  122. {TOK_WRITE, REG_COLOR_KILLER, 0x10},
  123. {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
  124. {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
  125. {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
  126. {TOK_WRITE, REG_BRIGHTNESS, 0x80},
  127. {TOK_WRITE, REG_CONTRAST, 0x80},
  128. {TOK_WRITE, REG_SATURATION, 0x80},
  129. {TOK_WRITE, REG_HUE, 0x00},
  130. {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
  131. {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
  132. /* Reserved */
  133. {TOK_SKIP, 0x0F, 0x00},
  134. {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
  135. {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
  136. {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
  137. /* Reserved */
  138. {TOK_SKIP, 0x13, 0x00},
  139. {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
  140. /* Reserved */
  141. {TOK_SKIP, 0x15, 0x00},
  142. /* NTSC timing */
  143. {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
  144. {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
  145. {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
  146. {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
  147. /* NTSC timing */
  148. {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
  149. {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
  150. {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
  151. {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
  152. /* NTSC timing */
  153. {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
  154. {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
  155. {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
  156. {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
  157. /* NTSC timing */
  158. {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
  159. {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
  160. {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
  161. {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
  162. /* Reserved */
  163. {TOK_SKIP, 0x26, 0x00},
  164. /* Reserved */
  165. {TOK_SKIP, 0x27, 0x00},
  166. {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
  167. /* Reserved */
  168. {TOK_SKIP, 0x29, 0x00},
  169. {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
  170. /* Reserved */
  171. {TOK_SKIP, 0x2B, 0x00},
  172. {TOK_SKIP, REG_SCART_DELAY, 0x00},
  173. {TOK_SKIP, REG_CTI_DELAY, 0x00},
  174. {TOK_SKIP, REG_CTI_CONTROL, 0x00},
  175. /* Reserved */
  176. {TOK_SKIP, 0x2F, 0x00},
  177. /* Reserved */
  178. {TOK_SKIP, 0x30, 0x00},
  179. /* Reserved */
  180. {TOK_SKIP, 0x31, 0x00},
  181. /* HS, VS active high */
  182. {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
  183. /* 10-bit BT.656 */
  184. {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
  185. /* Enable clk & data */
  186. {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
  187. /* Enable AVID & FLD */
  188. {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
  189. /* Enable VS & HS */
  190. {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
  191. {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
  192. {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
  193. /* Clear status */
  194. {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
  195. {TOK_TERM, 0, 0},
  196. };
  197. /**
  198. * List of image formats supported by TVP5146/47 decoder
  199. * Currently we are using 8 bit mode only, but can be
  200. * extended to 10/20 bit mode.
  201. */
  202. static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
  203. {
  204. .index = 0,
  205. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  206. .flags = 0,
  207. .description = "8-bit UYVY 4:2:2 Format",
  208. .pixelformat = V4L2_PIX_FMT_UYVY,
  209. },
  210. };
  211. /**
  212. * Supported standards -
  213. *
  214. * Currently supports two standards only, need to add support for rest of the
  215. * modes, like SECAM, etc...
  216. */
  217. static const struct tvp514x_std_info tvp514x_std_list[] = {
  218. /* Standard: STD_NTSC_MJ */
  219. [STD_NTSC_MJ] = {
  220. .width = NTSC_NUM_ACTIVE_PIXELS,
  221. .height = NTSC_NUM_ACTIVE_LINES,
  222. .video_std = VIDEO_STD_NTSC_MJ_BIT,
  223. .standard = {
  224. .index = 0,
  225. .id = V4L2_STD_NTSC,
  226. .name = "NTSC",
  227. .frameperiod = {1001, 30000},
  228. .framelines = 525
  229. },
  230. /* Standard: STD_PAL_BDGHIN */
  231. },
  232. [STD_PAL_BDGHIN] = {
  233. .width = PAL_NUM_ACTIVE_PIXELS,
  234. .height = PAL_NUM_ACTIVE_LINES,
  235. .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
  236. .standard = {
  237. .index = 1,
  238. .id = V4L2_STD_PAL,
  239. .name = "PAL",
  240. .frameperiod = {1, 25},
  241. .framelines = 625
  242. },
  243. },
  244. /* Standard: need to add for additional standard */
  245. };
  246. static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
  247. {
  248. return container_of(sd, struct tvp514x_decoder, sd);
  249. }
  250. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  251. {
  252. return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd;
  253. }
  254. /**
  255. * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
  256. * @sd: ptr to v4l2_subdev struct
  257. * @reg: TVP5146/47 register address
  258. *
  259. * Returns value read if successful, or non-zero (-1) otherwise.
  260. */
  261. static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
  262. {
  263. int err, retry = 0;
  264. struct i2c_client *client = v4l2_get_subdevdata(sd);
  265. read_again:
  266. err = i2c_smbus_read_byte_data(client, reg);
  267. if (err < 0) {
  268. if (retry <= I2C_RETRY_COUNT) {
  269. v4l2_warn(sd, "Read: retry ... %d\n", retry);
  270. retry++;
  271. msleep_interruptible(10);
  272. goto read_again;
  273. }
  274. }
  275. return err;
  276. }
  277. /**
  278. * dump_reg() - dump the register content of TVP5146/47.
  279. * @sd: ptr to v4l2_subdev struct
  280. * @reg: TVP5146/47 register address
  281. */
  282. static void dump_reg(struct v4l2_subdev *sd, u8 reg)
  283. {
  284. u32 val;
  285. val = tvp514x_read_reg(sd, reg);
  286. v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
  287. }
  288. /**
  289. * tvp514x_write_reg() - Write a value to a register in TVP5146/47
  290. * @sd: ptr to v4l2_subdev struct
  291. * @reg: TVP5146/47 register address
  292. * @val: value to be written to the register
  293. *
  294. * Write a value to a register in an TVP5146/47 decoder device.
  295. * Returns zero if successful, or non-zero otherwise.
  296. */
  297. static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
  298. {
  299. int err, retry = 0;
  300. struct i2c_client *client = v4l2_get_subdevdata(sd);
  301. write_again:
  302. err = i2c_smbus_write_byte_data(client, reg, val);
  303. if (err) {
  304. if (retry <= I2C_RETRY_COUNT) {
  305. v4l2_warn(sd, "Write: retry ... %d\n", retry);
  306. retry++;
  307. msleep_interruptible(10);
  308. goto write_again;
  309. }
  310. }
  311. return err;
  312. }
  313. /**
  314. * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
  315. * @sd: ptr to v4l2_subdev struct
  316. * @reglist: list of TVP5146/47 registers and values
  317. *
  318. * Initializes a list of TVP5146/47 registers:-
  319. * if token is TOK_TERM, then entire write operation terminates
  320. * if token is TOK_DELAY, then a delay of 'val' msec is introduced
  321. * if token is TOK_SKIP, then the register write is skipped
  322. * if token is TOK_WRITE, then the register write is performed
  323. * Returns zero if successful, or non-zero otherwise.
  324. */
  325. static int tvp514x_write_regs(struct v4l2_subdev *sd,
  326. const struct tvp514x_reg reglist[])
  327. {
  328. int err;
  329. const struct tvp514x_reg *next = reglist;
  330. for (; next->token != TOK_TERM; next++) {
  331. if (next->token == TOK_DELAY) {
  332. msleep(next->val);
  333. continue;
  334. }
  335. if (next->token == TOK_SKIP)
  336. continue;
  337. err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
  338. if (err) {
  339. v4l2_err(sd, "Write failed. Err[%d]\n", err);
  340. return err;
  341. }
  342. }
  343. return 0;
  344. }
  345. /**
  346. * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
  347. * @sd: ptr to v4l2_subdev struct
  348. *
  349. * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
  350. * standard detected.
  351. */
  352. static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
  353. {
  354. u8 std, std_status;
  355. std = tvp514x_read_reg(sd, REG_VIDEO_STD);
  356. if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
  357. /* use the standard status register */
  358. std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
  359. else
  360. /* use the standard register itself */
  361. std_status = std;
  362. switch (std_status & VIDEO_STD_MASK) {
  363. case VIDEO_STD_NTSC_MJ_BIT:
  364. return STD_NTSC_MJ;
  365. case VIDEO_STD_PAL_BDGHIN_BIT:
  366. return STD_PAL_BDGHIN;
  367. default:
  368. return STD_INVALID;
  369. }
  370. return STD_INVALID;
  371. }
  372. /* TVP5146/47 register dump function */
  373. static void tvp514x_reg_dump(struct v4l2_subdev *sd)
  374. {
  375. dump_reg(sd, REG_INPUT_SEL);
  376. dump_reg(sd, REG_AFE_GAIN_CTRL);
  377. dump_reg(sd, REG_VIDEO_STD);
  378. dump_reg(sd, REG_OPERATION_MODE);
  379. dump_reg(sd, REG_COLOR_KILLER);
  380. dump_reg(sd, REG_LUMA_CONTROL1);
  381. dump_reg(sd, REG_LUMA_CONTROL2);
  382. dump_reg(sd, REG_LUMA_CONTROL3);
  383. dump_reg(sd, REG_BRIGHTNESS);
  384. dump_reg(sd, REG_CONTRAST);
  385. dump_reg(sd, REG_SATURATION);
  386. dump_reg(sd, REG_HUE);
  387. dump_reg(sd, REG_CHROMA_CONTROL1);
  388. dump_reg(sd, REG_CHROMA_CONTROL2);
  389. dump_reg(sd, REG_COMP_PR_SATURATION);
  390. dump_reg(sd, REG_COMP_Y_CONTRAST);
  391. dump_reg(sd, REG_COMP_PB_SATURATION);
  392. dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
  393. dump_reg(sd, REG_AVID_START_PIXEL_LSB);
  394. dump_reg(sd, REG_AVID_START_PIXEL_MSB);
  395. dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
  396. dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
  397. dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
  398. dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
  399. dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
  400. dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
  401. dump_reg(sd, REG_VSYNC_START_LINE_LSB);
  402. dump_reg(sd, REG_VSYNC_START_LINE_MSB);
  403. dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
  404. dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
  405. dump_reg(sd, REG_VBLK_START_LINE_LSB);
  406. dump_reg(sd, REG_VBLK_START_LINE_MSB);
  407. dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
  408. dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
  409. dump_reg(sd, REG_SYNC_CONTROL);
  410. dump_reg(sd, REG_OUTPUT_FORMATTER1);
  411. dump_reg(sd, REG_OUTPUT_FORMATTER2);
  412. dump_reg(sd, REG_OUTPUT_FORMATTER3);
  413. dump_reg(sd, REG_OUTPUT_FORMATTER4);
  414. dump_reg(sd, REG_OUTPUT_FORMATTER5);
  415. dump_reg(sd, REG_OUTPUT_FORMATTER6);
  416. dump_reg(sd, REG_CLEAR_LOST_LOCK);
  417. }
  418. /**
  419. * tvp514x_configure() - Configure the TVP5146/47 registers
  420. * @sd: ptr to v4l2_subdev struct
  421. * @decoder: ptr to tvp514x_decoder structure
  422. *
  423. * Returns zero if successful, or non-zero otherwise.
  424. */
  425. static int tvp514x_configure(struct v4l2_subdev *sd,
  426. struct tvp514x_decoder *decoder)
  427. {
  428. int err;
  429. /* common register initialization */
  430. err =
  431. tvp514x_write_regs(sd, decoder->tvp514x_regs);
  432. if (err)
  433. return err;
  434. if (debug)
  435. tvp514x_reg_dump(sd);
  436. return 0;
  437. }
  438. /**
  439. * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
  440. * @sd: pointer to standard V4L2 sub-device structure
  441. * @decoder: pointer to tvp514x_decoder structure
  442. *
  443. * A device is considered to be detected if the chip ID (LSB and MSB)
  444. * registers match the expected values.
  445. * Any value of the rom version register is accepted.
  446. * Returns ENODEV error number if no device is detected, or zero
  447. * if a device is detected.
  448. */
  449. static int tvp514x_detect(struct v4l2_subdev *sd,
  450. struct tvp514x_decoder *decoder)
  451. {
  452. u8 chip_id_msb, chip_id_lsb, rom_ver;
  453. struct i2c_client *client = v4l2_get_subdevdata(sd);
  454. chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
  455. chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
  456. rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
  457. v4l2_dbg(1, debug, sd,
  458. "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
  459. chip_id_msb, chip_id_lsb, rom_ver);
  460. if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
  461. || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
  462. && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
  463. /* We didn't read the values we expected, so this must not be
  464. * an TVP5146/47.
  465. */
  466. v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
  467. chip_id_msb, chip_id_lsb);
  468. return -ENODEV;
  469. }
  470. decoder->ver = rom_ver;
  471. v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
  472. client->name, decoder->ver,
  473. client->addr << 1, client->adapter->name);
  474. return 0;
  475. }
  476. /**
  477. * tvp514x_querystd() - V4L2 decoder interface handler for querystd
  478. * @sd: pointer to standard V4L2 sub-device structure
  479. * @std_id: standard V4L2 std_id ioctl enum
  480. *
  481. * Returns the current standard detected by TVP5146/47. If no active input is
  482. * detected then *std_id is set to 0 and the function returns 0.
  483. */
  484. static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
  485. {
  486. struct tvp514x_decoder *decoder = to_decoder(sd);
  487. enum tvp514x_std current_std;
  488. enum tvp514x_input input_sel;
  489. u8 sync_lock_status, lock_mask;
  490. if (std_id == NULL)
  491. return -EINVAL;
  492. *std_id = V4L2_STD_UNKNOWN;
  493. /* To query the standard the TVP514x must power on the ADCs. */
  494. if (!decoder->streaming) {
  495. tvp514x_s_stream(sd, 1);
  496. msleep(LOCK_RETRY_DELAY);
  497. }
  498. /* query the current standard */
  499. current_std = tvp514x_query_current_std(sd);
  500. if (current_std == STD_INVALID)
  501. return 0;
  502. input_sel = decoder->input;
  503. switch (input_sel) {
  504. case INPUT_CVBS_VI1A:
  505. case INPUT_CVBS_VI1B:
  506. case INPUT_CVBS_VI1C:
  507. case INPUT_CVBS_VI2A:
  508. case INPUT_CVBS_VI2B:
  509. case INPUT_CVBS_VI2C:
  510. case INPUT_CVBS_VI3A:
  511. case INPUT_CVBS_VI3B:
  512. case INPUT_CVBS_VI3C:
  513. case INPUT_CVBS_VI4A:
  514. lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
  515. STATUS_HORZ_SYNC_LOCK_BIT |
  516. STATUS_VIRT_SYNC_LOCK_BIT;
  517. break;
  518. case INPUT_SVIDEO_VI2A_VI1A:
  519. case INPUT_SVIDEO_VI2B_VI1B:
  520. case INPUT_SVIDEO_VI2C_VI1C:
  521. case INPUT_SVIDEO_VI2A_VI3A:
  522. case INPUT_SVIDEO_VI2B_VI3B:
  523. case INPUT_SVIDEO_VI2C_VI3C:
  524. case INPUT_SVIDEO_VI4A_VI1A:
  525. case INPUT_SVIDEO_VI4A_VI1B:
  526. case INPUT_SVIDEO_VI4A_VI1C:
  527. case INPUT_SVIDEO_VI4A_VI3A:
  528. case INPUT_SVIDEO_VI4A_VI3B:
  529. case INPUT_SVIDEO_VI4A_VI3C:
  530. lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
  531. STATUS_VIRT_SYNC_LOCK_BIT;
  532. break;
  533. /*Need to add other interfaces*/
  534. default:
  535. return -EINVAL;
  536. }
  537. /* check whether signal is locked */
  538. sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
  539. if (lock_mask != (sync_lock_status & lock_mask))
  540. return 0; /* No input detected */
  541. *std_id = decoder->std_list[current_std].standard.id;
  542. v4l2_dbg(1, debug, sd, "Current STD: %s\n",
  543. decoder->std_list[current_std].standard.name);
  544. return 0;
  545. }
  546. /**
  547. * tvp514x_s_std() - V4L2 decoder interface handler for s_std
  548. * @sd: pointer to standard V4L2 sub-device structure
  549. * @std_id: standard V4L2 v4l2_std_id ioctl enum
  550. *
  551. * If std_id is supported, sets the requested standard. Otherwise, returns
  552. * -EINVAL
  553. */
  554. static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
  555. {
  556. struct tvp514x_decoder *decoder = to_decoder(sd);
  557. int err, i;
  558. for (i = 0; i < decoder->num_stds; i++)
  559. if (std_id & decoder->std_list[i].standard.id)
  560. break;
  561. if ((i == decoder->num_stds) || (i == STD_INVALID))
  562. return -EINVAL;
  563. err = tvp514x_write_reg(sd, REG_VIDEO_STD,
  564. decoder->std_list[i].video_std);
  565. if (err)
  566. return err;
  567. decoder->current_std = i;
  568. decoder->tvp514x_regs[REG_VIDEO_STD].val =
  569. decoder->std_list[i].video_std;
  570. v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
  571. decoder->std_list[i].standard.name);
  572. return 0;
  573. }
  574. /**
  575. * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
  576. * @sd: pointer to standard V4L2 sub-device structure
  577. * @input: input selector for routing the signal
  578. * @output: output selector for routing the signal
  579. * @config: config value. Not used
  580. *
  581. * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
  582. * the input is not supported or there is no active signal present in the
  583. * selected input.
  584. */
  585. static int tvp514x_s_routing(struct v4l2_subdev *sd,
  586. u32 input, u32 output, u32 config)
  587. {
  588. struct tvp514x_decoder *decoder = to_decoder(sd);
  589. int err;
  590. enum tvp514x_input input_sel;
  591. enum tvp514x_output output_sel;
  592. if ((input >= INPUT_INVALID) ||
  593. (output >= OUTPUT_INVALID))
  594. /* Index out of bound */
  595. return -EINVAL;
  596. input_sel = input;
  597. output_sel = output;
  598. err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
  599. if (err)
  600. return err;
  601. output_sel |= tvp514x_read_reg(sd,
  602. REG_OUTPUT_FORMATTER1) & 0x7;
  603. err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
  604. output_sel);
  605. if (err)
  606. return err;
  607. decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
  608. decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
  609. decoder->input = input;
  610. decoder->output = output;
  611. v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
  612. return 0;
  613. }
  614. /**
  615. * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
  616. * @ctrl: pointer to v4l2_ctrl structure
  617. *
  618. * If the requested control is supported, sets the control's current
  619. * value in HW. Otherwise, returns -EINVAL if the control is not supported.
  620. */
  621. static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl)
  622. {
  623. struct v4l2_subdev *sd = to_sd(ctrl);
  624. struct tvp514x_decoder *decoder = to_decoder(sd);
  625. int err = -EINVAL, value;
  626. value = ctrl->val;
  627. switch (ctrl->id) {
  628. case V4L2_CID_BRIGHTNESS:
  629. err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value);
  630. if (!err)
  631. decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
  632. break;
  633. case V4L2_CID_CONTRAST:
  634. err = tvp514x_write_reg(sd, REG_CONTRAST, value);
  635. if (!err)
  636. decoder->tvp514x_regs[REG_CONTRAST].val = value;
  637. break;
  638. case V4L2_CID_SATURATION:
  639. err = tvp514x_write_reg(sd, REG_SATURATION, value);
  640. if (!err)
  641. decoder->tvp514x_regs[REG_SATURATION].val = value;
  642. break;
  643. case V4L2_CID_HUE:
  644. if (value == 180)
  645. value = 0x7F;
  646. else if (value == -180)
  647. value = 0x80;
  648. err = tvp514x_write_reg(sd, REG_HUE, value);
  649. if (!err)
  650. decoder->tvp514x_regs[REG_HUE].val = value;
  651. break;
  652. case V4L2_CID_AUTOGAIN:
  653. err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c);
  654. if (!err)
  655. decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
  656. break;
  657. }
  658. v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
  659. ctrl->id, ctrl->val);
  660. return err;
  661. }
  662. /**
  663. * tvp514x_enum_mbus_fmt() - V4L2 decoder interface handler for enum_mbus_fmt
  664. * @sd: pointer to standard V4L2 sub-device structure
  665. * @index: index of pixelcode to retrieve
  666. * @code: receives the pixelcode
  667. *
  668. * Enumerates supported mediabus formats
  669. */
  670. static int
  671. tvp514x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
  672. enum v4l2_mbus_pixelcode *code)
  673. {
  674. if (index)
  675. return -EINVAL;
  676. *code = V4L2_MBUS_FMT_YUYV10_2X10;
  677. return 0;
  678. }
  679. /**
  680. * tvp514x_mbus_fmt() - V4L2 decoder interface handler for try/s/g_mbus_fmt
  681. * @sd: pointer to standard V4L2 sub-device structure
  682. * @f: pointer to the mediabus format structure
  683. *
  684. * Negotiates the image capture size and mediabus format.
  685. */
  686. static int
  687. tvp514x_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
  688. {
  689. struct tvp514x_decoder *decoder = to_decoder(sd);
  690. enum tvp514x_std current_std;
  691. if (f == NULL)
  692. return -EINVAL;
  693. /* Calculate height and width based on current standard */
  694. current_std = decoder->current_std;
  695. f->code = V4L2_MBUS_FMT_YUYV8_2X8;
  696. f->width = decoder->std_list[current_std].width;
  697. f->height = decoder->std_list[current_std].height;
  698. f->field = V4L2_FIELD_INTERLACED;
  699. f->colorspace = V4L2_COLORSPACE_SMPTE170M;
  700. v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d\n",
  701. f->width, f->height);
  702. return 0;
  703. }
  704. /**
  705. * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
  706. * @sd: pointer to standard V4L2 sub-device structure
  707. * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
  708. *
  709. * Returns the decoder's video CAPTURE parameters.
  710. */
  711. static int
  712. tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
  713. {
  714. struct tvp514x_decoder *decoder = to_decoder(sd);
  715. struct v4l2_captureparm *cparm;
  716. enum tvp514x_std current_std;
  717. if (a == NULL)
  718. return -EINVAL;
  719. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  720. /* only capture is supported */
  721. return -EINVAL;
  722. /* get the current standard */
  723. current_std = decoder->current_std;
  724. cparm = &a->parm.capture;
  725. cparm->capability = V4L2_CAP_TIMEPERFRAME;
  726. cparm->timeperframe =
  727. decoder->std_list[current_std].standard.frameperiod;
  728. return 0;
  729. }
  730. /**
  731. * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
  732. * @sd: pointer to standard V4L2 sub-device structure
  733. * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
  734. *
  735. * Configures the decoder to use the input parameters, if possible. If
  736. * not possible, returns the appropriate error code.
  737. */
  738. static int
  739. tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
  740. {
  741. struct tvp514x_decoder *decoder = to_decoder(sd);
  742. struct v4l2_fract *timeperframe;
  743. enum tvp514x_std current_std;
  744. if (a == NULL)
  745. return -EINVAL;
  746. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  747. /* only capture is supported */
  748. return -EINVAL;
  749. timeperframe = &a->parm.capture.timeperframe;
  750. /* get the current standard */
  751. current_std = decoder->current_std;
  752. *timeperframe =
  753. decoder->std_list[current_std].standard.frameperiod;
  754. return 0;
  755. }
  756. /**
  757. * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
  758. * @sd: pointer to standard V4L2 sub-device structure
  759. * @enable: streaming enable or disable
  760. *
  761. * Sets streaming to enable or disable, if possible.
  762. */
  763. static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
  764. {
  765. int err = 0;
  766. struct i2c_client *client = v4l2_get_subdevdata(sd);
  767. struct tvp514x_decoder *decoder = to_decoder(sd);
  768. if (decoder->streaming == enable)
  769. return 0;
  770. switch (enable) {
  771. case 0:
  772. {
  773. /* Power Down Sequence */
  774. err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
  775. if (err) {
  776. v4l2_err(sd, "Unable to turn off decoder\n");
  777. return err;
  778. }
  779. decoder->streaming = enable;
  780. break;
  781. }
  782. case 1:
  783. {
  784. struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
  785. client->driver->id_table->driver_data;
  786. /* Power Up Sequence */
  787. err = tvp514x_write_regs(sd, int_seq);
  788. if (err) {
  789. v4l2_err(sd, "Unable to turn on decoder\n");
  790. return err;
  791. }
  792. /* Detect if not already detected */
  793. err = tvp514x_detect(sd, decoder);
  794. if (err) {
  795. v4l2_err(sd, "Unable to detect decoder\n");
  796. return err;
  797. }
  798. err = tvp514x_configure(sd, decoder);
  799. if (err) {
  800. v4l2_err(sd, "Unable to configure decoder\n");
  801. return err;
  802. }
  803. decoder->streaming = enable;
  804. break;
  805. }
  806. default:
  807. err = -ENODEV;
  808. break;
  809. }
  810. return err;
  811. }
  812. static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
  813. .s_ctrl = tvp514x_s_ctrl,
  814. };
  815. /**
  816. * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code
  817. * @sd: pointer to standard V4L2 sub-device structure
  818. * @fh: file handle
  819. * @code: pointer to v4l2_subdev_mbus_code_enum structure
  820. *
  821. * Enumertaes mbus codes supported
  822. */
  823. static int tvp514x_enum_mbus_code(struct v4l2_subdev *sd,
  824. struct v4l2_subdev_fh *fh,
  825. struct v4l2_subdev_mbus_code_enum *code)
  826. {
  827. u32 pad = code->pad;
  828. u32 index = code->index;
  829. memset(code, 0, sizeof(*code));
  830. code->index = index;
  831. code->pad = pad;
  832. if (index != 0)
  833. return -EINVAL;
  834. code->code = V4L2_MBUS_FMT_YUYV8_2X8;
  835. return 0;
  836. }
  837. /**
  838. * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format
  839. * @sd: pointer to standard V4L2 sub-device structure
  840. * @fh: file handle
  841. * @format: pointer to v4l2_subdev_format structure
  842. *
  843. * Retrieves pad format which is active or tried based on requirement
  844. */
  845. static int tvp514x_get_pad_format(struct v4l2_subdev *sd,
  846. struct v4l2_subdev_fh *fh,
  847. struct v4l2_subdev_format *format)
  848. {
  849. struct tvp514x_decoder *decoder = to_decoder(sd);
  850. __u32 which = format->which;
  851. if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  852. format->format = decoder->format;
  853. return 0;
  854. }
  855. format->format.code = V4L2_MBUS_FMT_YUYV8_2X8;
  856. format->format.width = tvp514x_std_list[decoder->current_std].width;
  857. format->format.height = tvp514x_std_list[decoder->current_std].height;
  858. format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  859. format->format.field = V4L2_FIELD_INTERLACED;
  860. return 0;
  861. }
  862. /**
  863. * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format
  864. * @sd: pointer to standard V4L2 sub-device structure
  865. * @fh: file handle
  866. * @format: pointer to v4l2_subdev_format structure
  867. *
  868. * Set pad format for the output pad
  869. */
  870. static int tvp514x_set_pad_format(struct v4l2_subdev *sd,
  871. struct v4l2_subdev_fh *fh,
  872. struct v4l2_subdev_format *fmt)
  873. {
  874. struct tvp514x_decoder *decoder = to_decoder(sd);
  875. if (fmt->format.field != V4L2_FIELD_INTERLACED ||
  876. fmt->format.code != V4L2_MBUS_FMT_YUYV8_2X8 ||
  877. fmt->format.colorspace != V4L2_COLORSPACE_SMPTE170M ||
  878. fmt->format.width != tvp514x_std_list[decoder->current_std].width ||
  879. fmt->format.height != tvp514x_std_list[decoder->current_std].height)
  880. return -EINVAL;
  881. decoder->format = fmt->format;
  882. return 0;
  883. }
  884. static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
  885. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  886. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  887. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  888. .g_ctrl = v4l2_subdev_g_ctrl,
  889. .s_ctrl = v4l2_subdev_s_ctrl,
  890. .queryctrl = v4l2_subdev_queryctrl,
  891. .querymenu = v4l2_subdev_querymenu,
  892. .s_std = tvp514x_s_std,
  893. };
  894. static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
  895. .s_routing = tvp514x_s_routing,
  896. .querystd = tvp514x_querystd,
  897. .enum_mbus_fmt = tvp514x_enum_mbus_fmt,
  898. .g_mbus_fmt = tvp514x_mbus_fmt,
  899. .try_mbus_fmt = tvp514x_mbus_fmt,
  900. .s_mbus_fmt = tvp514x_mbus_fmt,
  901. .g_parm = tvp514x_g_parm,
  902. .s_parm = tvp514x_s_parm,
  903. .s_stream = tvp514x_s_stream,
  904. };
  905. static const struct v4l2_subdev_pad_ops tvp514x_pad_ops = {
  906. .enum_mbus_code = tvp514x_enum_mbus_code,
  907. .get_fmt = tvp514x_get_pad_format,
  908. .set_fmt = tvp514x_set_pad_format,
  909. };
  910. static const struct v4l2_subdev_ops tvp514x_ops = {
  911. .core = &tvp514x_core_ops,
  912. .video = &tvp514x_video_ops,
  913. .pad = &tvp514x_pad_ops,
  914. };
  915. static struct tvp514x_decoder tvp514x_dev = {
  916. .streaming = 0,
  917. .fmt_list = tvp514x_fmt_list,
  918. .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
  919. .pix = {
  920. /* Default to NTSC 8-bit YUV 422 */
  921. .width = NTSC_NUM_ACTIVE_PIXELS,
  922. .height = NTSC_NUM_ACTIVE_LINES,
  923. .pixelformat = V4L2_PIX_FMT_UYVY,
  924. .field = V4L2_FIELD_INTERLACED,
  925. .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
  926. .sizeimage = NTSC_NUM_ACTIVE_PIXELS * 2 *
  927. NTSC_NUM_ACTIVE_LINES,
  928. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  929. },
  930. .current_std = STD_NTSC_MJ,
  931. .std_list = tvp514x_std_list,
  932. .num_stds = ARRAY_SIZE(tvp514x_std_list),
  933. };
  934. /**
  935. * tvp514x_probe() - decoder driver i2c probe handler
  936. * @client: i2c driver client device structure
  937. * @id: i2c driver id table
  938. *
  939. * Register decoder as an i2c client device and V4L2
  940. * device.
  941. */
  942. static int
  943. tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
  944. {
  945. struct tvp514x_decoder *decoder;
  946. struct v4l2_subdev *sd;
  947. int ret;
  948. /* Check if the adapter supports the needed features */
  949. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  950. return -EIO;
  951. if (!client->dev.platform_data) {
  952. v4l2_err(client, "No platform data!!\n");
  953. return -ENODEV;
  954. }
  955. decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
  956. if (!decoder)
  957. return -ENOMEM;
  958. /* Initialize the tvp514x_decoder with default configuration */
  959. *decoder = tvp514x_dev;
  960. /* Copy default register configuration */
  961. memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
  962. sizeof(tvp514x_reg_list_default));
  963. /* Copy board specific information here */
  964. decoder->pdata = client->dev.platform_data;
  965. /**
  966. * Fetch platform specific data, and configure the
  967. * tvp514x_reg_list[] accordingly. Since this is one
  968. * time configuration, no need to preserve.
  969. */
  970. decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
  971. (decoder->pdata->clk_polarity << 1);
  972. decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
  973. ((decoder->pdata->hs_polarity << 2) |
  974. (decoder->pdata->vs_polarity << 3));
  975. /* Set default standard to auto */
  976. decoder->tvp514x_regs[REG_VIDEO_STD].val =
  977. VIDEO_STD_AUTO_SWITCH_BIT;
  978. /* Register with V4L2 layer as slave device */
  979. sd = &decoder->sd;
  980. v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
  981. strlcpy(sd->name, TVP514X_MODULE_NAME, sizeof(sd->name));
  982. #if defined(CONFIG_MEDIA_CONTROLLER)
  983. decoder->pad.flags = MEDIA_PAD_FL_SOURCE;
  984. decoder->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  985. decoder->sd.entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;
  986. ret = media_entity_init(&decoder->sd.entity, 1, &decoder->pad, 0);
  987. if (ret < 0) {
  988. v4l2_err(sd, "%s decoder driver failed to register !!\n",
  989. sd->name);
  990. kfree(decoder);
  991. return ret;
  992. }
  993. #endif
  994. v4l2_ctrl_handler_init(&decoder->hdl, 5);
  995. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  996. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  997. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  998. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  999. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  1000. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1001. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  1002. V4L2_CID_HUE, -180, 180, 180, 0);
  1003. v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
  1004. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  1005. sd->ctrl_handler = &decoder->hdl;
  1006. if (decoder->hdl.error) {
  1007. ret = decoder->hdl.error;
  1008. v4l2_ctrl_handler_free(&decoder->hdl);
  1009. return ret;
  1010. }
  1011. v4l2_ctrl_handler_setup(&decoder->hdl);
  1012. v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
  1013. return 0;
  1014. }
  1015. /**
  1016. * tvp514x_remove() - decoder driver i2c remove handler
  1017. * @client: i2c driver client device structure
  1018. *
  1019. * Unregister decoder as an i2c client device and V4L2
  1020. * device. Complement of tvp514x_probe().
  1021. */
  1022. static int tvp514x_remove(struct i2c_client *client)
  1023. {
  1024. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1025. struct tvp514x_decoder *decoder = to_decoder(sd);
  1026. v4l2_device_unregister_subdev(sd);
  1027. #if defined(CONFIG_MEDIA_CONTROLLER)
  1028. media_entity_cleanup(&decoder->sd.entity);
  1029. #endif
  1030. v4l2_ctrl_handler_free(&decoder->hdl);
  1031. return 0;
  1032. }
  1033. /* TVP5146 Init/Power on Sequence */
  1034. static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
  1035. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1036. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1037. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1038. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1039. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1040. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1041. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1042. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1043. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1044. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1045. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1046. {TOK_TERM, 0, 0},
  1047. };
  1048. /* TVP5147 Init/Power on Sequence */
  1049. static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
  1050. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1051. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1052. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1053. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1054. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1055. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1056. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1057. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1058. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
  1059. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1060. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
  1061. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
  1062. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1063. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1064. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1065. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1066. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1067. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1068. {TOK_TERM, 0, 0},
  1069. };
  1070. /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
  1071. static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
  1072. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1073. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1074. {TOK_TERM, 0, 0},
  1075. };
  1076. /**
  1077. * I2C Device Table -
  1078. *
  1079. * name - Name of the actual device/chip.
  1080. * driver_data - Driver data
  1081. */
  1082. static const struct i2c_device_id tvp514x_id[] = {
  1083. {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
  1084. {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
  1085. {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
  1086. {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
  1087. {},
  1088. };
  1089. MODULE_DEVICE_TABLE(i2c, tvp514x_id);
  1090. static struct i2c_driver tvp514x_driver = {
  1091. .driver = {
  1092. .owner = THIS_MODULE,
  1093. .name = TVP514X_MODULE_NAME,
  1094. },
  1095. .probe = tvp514x_probe,
  1096. .remove = tvp514x_remove,
  1097. .id_table = tvp514x_id,
  1098. };
  1099. module_i2c_driver(tvp514x_driver);