tvp514x.c 34 KB

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