tvp514x.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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/delay.h>
  32. #include <linux/videodev2.h>
  33. #include <media/v4l2-int-device.h>
  34. #include <media/tvp514x.h>
  35. #include "tvp514x_regs.h"
  36. /* Module Name */
  37. #define TVP514X_MODULE_NAME "tvp514x"
  38. /* Private macros for TVP */
  39. #define I2C_RETRY_COUNT (5)
  40. #define LOCK_RETRY_COUNT (5)
  41. #define LOCK_RETRY_DELAY (200)
  42. /* Debug functions */
  43. static int debug;
  44. module_param(debug, bool, 0644);
  45. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  46. #define dump_reg(client, reg, val) \
  47. do { \
  48. val = tvp514x_read_reg(client, reg); \
  49. v4l_info(client, "Reg(0x%.2X): 0x%.2X\n", reg, val); \
  50. } while (0)
  51. /**
  52. * enum tvp514x_std - enum for supported standards
  53. */
  54. enum tvp514x_std {
  55. STD_NTSC_MJ = 0,
  56. STD_PAL_BDGHIN,
  57. STD_INVALID
  58. };
  59. /**
  60. * enum tvp514x_state - enum for different decoder states
  61. */
  62. enum tvp514x_state {
  63. STATE_NOT_DETECTED,
  64. STATE_DETECTED
  65. };
  66. /**
  67. * struct tvp514x_std_info - Structure to store standard informations
  68. * @width: Line width in pixels
  69. * @height:Number of active lines
  70. * @video_std: Value to write in REG_VIDEO_STD register
  71. * @standard: v4l2 standard structure information
  72. */
  73. struct tvp514x_std_info {
  74. unsigned long width;
  75. unsigned long height;
  76. u8 video_std;
  77. struct v4l2_standard standard;
  78. };
  79. static struct tvp514x_reg tvp514x_reg_list_default[0x40];
  80. /**
  81. * struct tvp514x_decoder - TVP5146/47 decoder object
  82. * @v4l2_int_device: Slave handle
  83. * @tvp514x_slave: Slave pointer which is used by @v4l2_int_device
  84. * @tvp514x_regs: copy of hw's regs with preset values.
  85. * @pdata: Board specific
  86. * @client: I2C client data
  87. * @id: Entry from I2C table
  88. * @ver: Chip version
  89. * @state: TVP5146/47 decoder state - detected or not-detected
  90. * @pix: Current pixel format
  91. * @num_fmts: Number of formats
  92. * @fmt_list: Format list
  93. * @current_std: Current standard
  94. * @num_stds: Number of standards
  95. * @std_list: Standards list
  96. * @route: input and output routing at chip level
  97. */
  98. struct tvp514x_decoder {
  99. struct v4l2_int_device v4l2_int_device;
  100. struct v4l2_int_slave tvp514x_slave;
  101. struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
  102. const struct tvp514x_platform_data *pdata;
  103. struct i2c_client *client;
  104. struct i2c_device_id *id;
  105. int ver;
  106. enum tvp514x_state state;
  107. struct v4l2_pix_format pix;
  108. int num_fmts;
  109. const struct v4l2_fmtdesc *fmt_list;
  110. enum tvp514x_std current_std;
  111. int num_stds;
  112. struct tvp514x_std_info *std_list;
  113. struct v4l2_routing route;
  114. };
  115. /* TVP514x default register values */
  116. static struct tvp514x_reg tvp514x_reg_list_default[] = {
  117. {TOK_WRITE, REG_INPUT_SEL, 0x05}, /* Composite selected */
  118. {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
  119. {TOK_WRITE, REG_VIDEO_STD, 0x00}, /* Auto mode */
  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. {TOK_SKIP, 0x0F, 0x00}, /* Reserved */
  133. {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
  134. {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
  135. {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
  136. {TOK_SKIP, 0x13, 0x00}, /* Reserved */
  137. {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
  138. {TOK_SKIP, 0x15, 0x00}, /* Reserved */
  139. {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55}, /* NTSC timing */
  140. {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
  141. {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
  142. {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
  143. {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00}, /* NTSC timing */
  144. {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
  145. {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
  146. {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
  147. {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04}, /* NTSC timing */
  148. {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
  149. {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
  150. {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
  151. {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01}, /* NTSC timing */
  152. {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
  153. {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
  154. {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
  155. {TOK_SKIP, 0x26, 0x00}, /* Reserved */
  156. {TOK_SKIP, 0x27, 0x00}, /* Reserved */
  157. {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
  158. {TOK_SKIP, 0x29, 0x00}, /* Reserved */
  159. {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
  160. {TOK_SKIP, 0x2B, 0x00}, /* Reserved */
  161. {TOK_SKIP, REG_SCART_DELAY, 0x00},
  162. {TOK_SKIP, REG_CTI_DELAY, 0x00},
  163. {TOK_SKIP, REG_CTI_CONTROL, 0x00},
  164. {TOK_SKIP, 0x2F, 0x00}, /* Reserved */
  165. {TOK_SKIP, 0x30, 0x00}, /* Reserved */
  166. {TOK_SKIP, 0x31, 0x00}, /* Reserved */
  167. {TOK_WRITE, REG_SYNC_CONTROL, 0x00}, /* HS, VS active high */
  168. {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00}, /* 10-bit BT.656 */
  169. {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11}, /* Enable clk & data */
  170. {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE}, /* Enable AVID & FLD */
  171. {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF}, /* Enable VS & HS */
  172. {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
  173. {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
  174. {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01}, /* Clear status */
  175. {TOK_TERM, 0, 0},
  176. };
  177. /* List of image formats supported by TVP5146/47 decoder
  178. * Currently we are using 8 bit mode only, but can be
  179. * extended to 10/20 bit mode.
  180. */
  181. static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
  182. {
  183. .index = 0,
  184. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  185. .flags = 0,
  186. .description = "8-bit UYVY 4:2:2 Format",
  187. .pixelformat = V4L2_PIX_FMT_UYVY,
  188. },
  189. };
  190. /*
  191. * Supported standards -
  192. *
  193. * Currently supports two standards only, need to add support for rest of the
  194. * modes, like SECAM, etc...
  195. */
  196. static struct tvp514x_std_info tvp514x_std_list[] = {
  197. /* Standard: STD_NTSC_MJ */
  198. [STD_NTSC_MJ] = {
  199. .width = NTSC_NUM_ACTIVE_PIXELS,
  200. .height = NTSC_NUM_ACTIVE_LINES,
  201. .video_std = VIDEO_STD_NTSC_MJ_BIT,
  202. .standard = {
  203. .index = 0,
  204. .id = V4L2_STD_NTSC,
  205. .name = "NTSC",
  206. .frameperiod = {1001, 30000},
  207. .framelines = 525
  208. },
  209. /* Standard: STD_PAL_BDGHIN */
  210. },
  211. [STD_PAL_BDGHIN] = {
  212. .width = PAL_NUM_ACTIVE_PIXELS,
  213. .height = PAL_NUM_ACTIVE_LINES,
  214. .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
  215. .standard = {
  216. .index = 1,
  217. .id = V4L2_STD_PAL,
  218. .name = "PAL",
  219. .frameperiod = {1, 25},
  220. .framelines = 625
  221. },
  222. },
  223. /* Standard: need to add for additional standard */
  224. };
  225. /*
  226. * Control structure for Auto Gain
  227. * This is temporary data, will get replaced once
  228. * v4l2_ctrl_query_fill supports it.
  229. */
  230. static const struct v4l2_queryctrl tvp514x_autogain_ctrl = {
  231. .id = V4L2_CID_AUTOGAIN,
  232. .name = "Gain, Automatic",
  233. .type = V4L2_CTRL_TYPE_BOOLEAN,
  234. .minimum = 0,
  235. .maximum = 1,
  236. .step = 1,
  237. .default_value = 1,
  238. };
  239. /*
  240. * Read a value from a register in an TVP5146/47 decoder device.
  241. * Returns value read if successful, or non-zero (-1) otherwise.
  242. */
  243. static int tvp514x_read_reg(struct i2c_client *client, u8 reg)
  244. {
  245. int err;
  246. int retry = 0;
  247. read_again:
  248. err = i2c_smbus_read_byte_data(client, reg);
  249. if (err == -1) {
  250. if (retry <= I2C_RETRY_COUNT) {
  251. v4l_warn(client, "Read: retry ... %d\n", retry);
  252. retry++;
  253. msleep_interruptible(10);
  254. goto read_again;
  255. }
  256. }
  257. return err;
  258. }
  259. /*
  260. * Write a value to a register in an TVP5146/47 decoder device.
  261. * Returns zero if successful, or non-zero otherwise.
  262. */
  263. static int tvp514x_write_reg(struct i2c_client *client, u8 reg, u8 val)
  264. {
  265. int err;
  266. int retry = 0;
  267. write_again:
  268. err = i2c_smbus_write_byte_data(client, reg, val);
  269. if (err) {
  270. if (retry <= I2C_RETRY_COUNT) {
  271. v4l_warn(client, "Write: retry ... %d\n", retry);
  272. retry++;
  273. msleep_interruptible(10);
  274. goto write_again;
  275. }
  276. }
  277. return err;
  278. }
  279. /*
  280. * tvp514x_write_regs : Initializes a list of TVP5146/47 registers
  281. * if token is TOK_TERM, then entire write operation terminates
  282. * if token is TOK_DELAY, then a delay of 'val' msec is introduced
  283. * if token is TOK_SKIP, then the register write is skipped
  284. * if token is TOK_WRITE, then the register write is performed
  285. *
  286. * reglist - list of registers to be written
  287. * Returns zero if successful, or non-zero otherwise.
  288. */
  289. static int tvp514x_write_regs(struct i2c_client *client,
  290. const struct tvp514x_reg reglist[])
  291. {
  292. int err;
  293. const struct tvp514x_reg *next = reglist;
  294. for (; next->token != TOK_TERM; next++) {
  295. if (next->token == TOK_DELAY) {
  296. msleep(next->val);
  297. continue;
  298. }
  299. if (next->token == TOK_SKIP)
  300. continue;
  301. err = tvp514x_write_reg(client, next->reg, (u8) next->val);
  302. if (err) {
  303. v4l_err(client, "Write failed. Err[%d]\n", err);
  304. return err;
  305. }
  306. }
  307. return 0;
  308. }
  309. /*
  310. * tvp514x_get_current_std:
  311. * Returns the current standard detected by TVP5146/47
  312. */
  313. static enum tvp514x_std tvp514x_get_current_std(struct tvp514x_decoder
  314. *decoder)
  315. {
  316. u8 std, std_status;
  317. std = tvp514x_read_reg(decoder->client, REG_VIDEO_STD);
  318. if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT) {
  319. /* use the standard status register */
  320. std_status = tvp514x_read_reg(decoder->client,
  321. REG_VIDEO_STD_STATUS);
  322. } else
  323. std_status = std; /* use the standard register itself */
  324. switch (std_status & VIDEO_STD_MASK) {
  325. case VIDEO_STD_NTSC_MJ_BIT:
  326. return STD_NTSC_MJ;
  327. case VIDEO_STD_PAL_BDGHIN_BIT:
  328. return STD_PAL_BDGHIN;
  329. default:
  330. return STD_INVALID;
  331. }
  332. return STD_INVALID;
  333. }
  334. /*
  335. * TVP5146/47 register dump function
  336. */
  337. static void tvp514x_reg_dump(struct tvp514x_decoder *decoder)
  338. {
  339. u8 value;
  340. dump_reg(decoder->client, REG_INPUT_SEL, value);
  341. dump_reg(decoder->client, REG_AFE_GAIN_CTRL, value);
  342. dump_reg(decoder->client, REG_VIDEO_STD, value);
  343. dump_reg(decoder->client, REG_OPERATION_MODE, value);
  344. dump_reg(decoder->client, REG_COLOR_KILLER, value);
  345. dump_reg(decoder->client, REG_LUMA_CONTROL1, value);
  346. dump_reg(decoder->client, REG_LUMA_CONTROL2, value);
  347. dump_reg(decoder->client, REG_LUMA_CONTROL3, value);
  348. dump_reg(decoder->client, REG_BRIGHTNESS, value);
  349. dump_reg(decoder->client, REG_CONTRAST, value);
  350. dump_reg(decoder->client, REG_SATURATION, value);
  351. dump_reg(decoder->client, REG_HUE, value);
  352. dump_reg(decoder->client, REG_CHROMA_CONTROL1, value);
  353. dump_reg(decoder->client, REG_CHROMA_CONTROL2, value);
  354. dump_reg(decoder->client, REG_COMP_PR_SATURATION, value);
  355. dump_reg(decoder->client, REG_COMP_Y_CONTRAST, value);
  356. dump_reg(decoder->client, REG_COMP_PB_SATURATION, value);
  357. dump_reg(decoder->client, REG_COMP_Y_BRIGHTNESS, value);
  358. dump_reg(decoder->client, REG_AVID_START_PIXEL_LSB, value);
  359. dump_reg(decoder->client, REG_AVID_START_PIXEL_MSB, value);
  360. dump_reg(decoder->client, REG_AVID_STOP_PIXEL_LSB, value);
  361. dump_reg(decoder->client, REG_AVID_STOP_PIXEL_MSB, value);
  362. dump_reg(decoder->client, REG_HSYNC_START_PIXEL_LSB, value);
  363. dump_reg(decoder->client, REG_HSYNC_START_PIXEL_MSB, value);
  364. dump_reg(decoder->client, REG_HSYNC_STOP_PIXEL_LSB, value);
  365. dump_reg(decoder->client, REG_HSYNC_STOP_PIXEL_MSB, value);
  366. dump_reg(decoder->client, REG_VSYNC_START_LINE_LSB, value);
  367. dump_reg(decoder->client, REG_VSYNC_START_LINE_MSB, value);
  368. dump_reg(decoder->client, REG_VSYNC_STOP_LINE_LSB, value);
  369. dump_reg(decoder->client, REG_VSYNC_STOP_LINE_MSB, value);
  370. dump_reg(decoder->client, REG_VBLK_START_LINE_LSB, value);
  371. dump_reg(decoder->client, REG_VBLK_START_LINE_MSB, value);
  372. dump_reg(decoder->client, REG_VBLK_STOP_LINE_LSB, value);
  373. dump_reg(decoder->client, REG_VBLK_STOP_LINE_MSB, value);
  374. dump_reg(decoder->client, REG_SYNC_CONTROL, value);
  375. dump_reg(decoder->client, REG_OUTPUT_FORMATTER1, value);
  376. dump_reg(decoder->client, REG_OUTPUT_FORMATTER2, value);
  377. dump_reg(decoder->client, REG_OUTPUT_FORMATTER3, value);
  378. dump_reg(decoder->client, REG_OUTPUT_FORMATTER4, value);
  379. dump_reg(decoder->client, REG_OUTPUT_FORMATTER5, value);
  380. dump_reg(decoder->client, REG_OUTPUT_FORMATTER6, value);
  381. dump_reg(decoder->client, REG_CLEAR_LOST_LOCK, value);
  382. }
  383. /*
  384. * Configure the TVP5146/47 with the current register settings
  385. * Returns zero if successful, or non-zero otherwise.
  386. */
  387. static int tvp514x_configure(struct tvp514x_decoder *decoder)
  388. {
  389. int err;
  390. /* common register initialization */
  391. err =
  392. tvp514x_write_regs(decoder->client, decoder->tvp514x_regs);
  393. if (err)
  394. return err;
  395. if (debug)
  396. tvp514x_reg_dump(decoder);
  397. return 0;
  398. }
  399. /*
  400. * Detect if an tvp514x is present, and if so which revision.
  401. * A device is considered to be detected if the chip ID (LSB and MSB)
  402. * registers match the expected values.
  403. * Any value of the rom version register is accepted.
  404. * Returns ENODEV error number if no device is detected, or zero
  405. * if a device is detected.
  406. */
  407. static int tvp514x_detect(struct tvp514x_decoder *decoder)
  408. {
  409. u8 chip_id_msb, chip_id_lsb, rom_ver;
  410. chip_id_msb = tvp514x_read_reg(decoder->client, REG_CHIP_ID_MSB);
  411. chip_id_lsb = tvp514x_read_reg(decoder->client, REG_CHIP_ID_LSB);
  412. rom_ver = tvp514x_read_reg(decoder->client, REG_ROM_VERSION);
  413. v4l_dbg(1, debug, decoder->client,
  414. "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
  415. chip_id_msb, chip_id_lsb, rom_ver);
  416. if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
  417. || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
  418. && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
  419. /* We didn't read the values we expected, so this must not be
  420. * an TVP5146/47.
  421. */
  422. v4l_err(decoder->client,
  423. "chip id mismatch msb:0x%x lsb:0x%x\n",
  424. chip_id_msb, chip_id_lsb);
  425. return -ENODEV;
  426. }
  427. decoder->ver = rom_ver;
  428. decoder->state = STATE_DETECTED;
  429. v4l_info(decoder->client,
  430. "%s found at 0x%x (%s)\n", decoder->client->name,
  431. decoder->client->addr << 1,
  432. decoder->client->adapter->name);
  433. return 0;
  434. }
  435. /*
  436. * Following are decoder interface functions implemented by
  437. * TVP5146/47 decoder driver.
  438. */
  439. /**
  440. * ioctl_querystd - V4L2 decoder interface handler for VIDIOC_QUERYSTD ioctl
  441. * @s: pointer to standard V4L2 device structure
  442. * @std_id: standard V4L2 std_id ioctl enum
  443. *
  444. * Returns the current standard detected by TVP5146/47. If no active input is
  445. * detected, returns -EINVAL
  446. */
  447. static int ioctl_querystd(struct v4l2_int_device *s, v4l2_std_id *std_id)
  448. {
  449. struct tvp514x_decoder *decoder = s->priv;
  450. enum tvp514x_std current_std;
  451. enum tvp514x_input input_sel;
  452. u8 sync_lock_status, lock_mask;
  453. if (std_id == NULL)
  454. return -EINVAL;
  455. /* get the current standard */
  456. current_std = tvp514x_get_current_std(decoder);
  457. if (current_std == STD_INVALID)
  458. return -EINVAL;
  459. input_sel = decoder->route.input;
  460. switch (input_sel) {
  461. case INPUT_CVBS_VI1A:
  462. case INPUT_CVBS_VI1B:
  463. case INPUT_CVBS_VI1C:
  464. case INPUT_CVBS_VI2A:
  465. case INPUT_CVBS_VI2B:
  466. case INPUT_CVBS_VI2C:
  467. case INPUT_CVBS_VI3A:
  468. case INPUT_CVBS_VI3B:
  469. case INPUT_CVBS_VI3C:
  470. case INPUT_CVBS_VI4A:
  471. lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
  472. STATUS_HORZ_SYNC_LOCK_BIT |
  473. STATUS_VIRT_SYNC_LOCK_BIT;
  474. break;
  475. case INPUT_SVIDEO_VI2A_VI1A:
  476. case INPUT_SVIDEO_VI2B_VI1B:
  477. case INPUT_SVIDEO_VI2C_VI1C:
  478. case INPUT_SVIDEO_VI2A_VI3A:
  479. case INPUT_SVIDEO_VI2B_VI3B:
  480. case INPUT_SVIDEO_VI2C_VI3C:
  481. case INPUT_SVIDEO_VI4A_VI1A:
  482. case INPUT_SVIDEO_VI4A_VI1B:
  483. case INPUT_SVIDEO_VI4A_VI1C:
  484. case INPUT_SVIDEO_VI4A_VI3A:
  485. case INPUT_SVIDEO_VI4A_VI3B:
  486. case INPUT_SVIDEO_VI4A_VI3C:
  487. lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
  488. STATUS_VIRT_SYNC_LOCK_BIT;
  489. break;
  490. /*Need to add other interfaces*/
  491. default:
  492. return -EINVAL;
  493. }
  494. /* check whether signal is locked */
  495. sync_lock_status = tvp514x_read_reg(decoder->client, REG_STATUS1);
  496. if (lock_mask != (sync_lock_status & lock_mask))
  497. return -EINVAL; /* No input detected */
  498. decoder->current_std = current_std;
  499. *std_id = decoder->std_list[current_std].standard.id;
  500. v4l_dbg(1, debug, decoder->client, "Current STD: %s",
  501. decoder->std_list[current_std].standard.name);
  502. return 0;
  503. }
  504. /**
  505. * ioctl_s_std - V4L2 decoder interface handler for VIDIOC_S_STD ioctl
  506. * @s: pointer to standard V4L2 device structure
  507. * @std_id: standard V4L2 v4l2_std_id ioctl enum
  508. *
  509. * If std_id is supported, sets the requested standard. Otherwise, returns
  510. * -EINVAL
  511. */
  512. static int ioctl_s_std(struct v4l2_int_device *s, v4l2_std_id *std_id)
  513. {
  514. struct tvp514x_decoder *decoder = s->priv;
  515. int err, i;
  516. if (std_id == NULL)
  517. return -EINVAL;
  518. for (i = 0; i < decoder->num_stds; i++)
  519. if (*std_id & decoder->std_list[i].standard.id)
  520. break;
  521. if ((i == decoder->num_stds) || (i == STD_INVALID))
  522. return -EINVAL;
  523. err = tvp514x_write_reg(decoder->client, REG_VIDEO_STD,
  524. decoder->std_list[i].video_std);
  525. if (err)
  526. return err;
  527. decoder->current_std = i;
  528. decoder->tvp514x_regs[REG_VIDEO_STD].val =
  529. decoder->std_list[i].video_std;
  530. v4l_dbg(1, debug, decoder->client, "Standard set to: %s",
  531. decoder->std_list[i].standard.name);
  532. return 0;
  533. }
  534. /**
  535. * ioctl_s_routing - V4L2 decoder interface handler for VIDIOC_S_INPUT ioctl
  536. * @s: pointer to standard V4L2 device structure
  537. * @index: number of the input
  538. *
  539. * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
  540. * the input is not supported or there is no active signal present in the
  541. * selected input.
  542. */
  543. static int ioctl_s_routing(struct v4l2_int_device *s,
  544. struct v4l2_routing *route)
  545. {
  546. struct tvp514x_decoder *decoder = s->priv;
  547. int err;
  548. enum tvp514x_input input_sel;
  549. enum tvp514x_output output_sel;
  550. enum tvp514x_std current_std = STD_INVALID;
  551. u8 sync_lock_status, lock_mask;
  552. int try_count = LOCK_RETRY_COUNT;
  553. if ((!route) || (route->input >= INPUT_INVALID) ||
  554. (route->output >= OUTPUT_INVALID))
  555. return -EINVAL; /* Index out of bound */
  556. input_sel = route->input;
  557. output_sel = route->output;
  558. err = tvp514x_write_reg(decoder->client, REG_INPUT_SEL, input_sel);
  559. if (err)
  560. return err;
  561. output_sel |= tvp514x_read_reg(decoder->client,
  562. REG_OUTPUT_FORMATTER1) & 0x7;
  563. err = tvp514x_write_reg(decoder->client, REG_OUTPUT_FORMATTER1,
  564. output_sel);
  565. if (err)
  566. return err;
  567. decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
  568. decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
  569. /* Clear status */
  570. msleep(LOCK_RETRY_DELAY);
  571. err =
  572. tvp514x_write_reg(decoder->client, REG_CLEAR_LOST_LOCK, 0x01);
  573. if (err)
  574. return err;
  575. switch (input_sel) {
  576. case INPUT_CVBS_VI1A:
  577. case INPUT_CVBS_VI1B:
  578. case INPUT_CVBS_VI1C:
  579. case INPUT_CVBS_VI2A:
  580. case INPUT_CVBS_VI2B:
  581. case INPUT_CVBS_VI2C:
  582. case INPUT_CVBS_VI3A:
  583. case INPUT_CVBS_VI3B:
  584. case INPUT_CVBS_VI3C:
  585. case INPUT_CVBS_VI4A:
  586. lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
  587. STATUS_HORZ_SYNC_LOCK_BIT |
  588. STATUS_VIRT_SYNC_LOCK_BIT;
  589. break;
  590. case INPUT_SVIDEO_VI2A_VI1A:
  591. case INPUT_SVIDEO_VI2B_VI1B:
  592. case INPUT_SVIDEO_VI2C_VI1C:
  593. case INPUT_SVIDEO_VI2A_VI3A:
  594. case INPUT_SVIDEO_VI2B_VI3B:
  595. case INPUT_SVIDEO_VI2C_VI3C:
  596. case INPUT_SVIDEO_VI4A_VI1A:
  597. case INPUT_SVIDEO_VI4A_VI1B:
  598. case INPUT_SVIDEO_VI4A_VI1C:
  599. case INPUT_SVIDEO_VI4A_VI3A:
  600. case INPUT_SVIDEO_VI4A_VI3B:
  601. case INPUT_SVIDEO_VI4A_VI3C:
  602. lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
  603. STATUS_VIRT_SYNC_LOCK_BIT;
  604. break;
  605. /*Need to add other interfaces*/
  606. default:
  607. return -EINVAL;
  608. }
  609. while (try_count-- > 0) {
  610. /* Allow decoder to sync up with new input */
  611. msleep(LOCK_RETRY_DELAY);
  612. /* get the current standard for future reference */
  613. current_std = tvp514x_get_current_std(decoder);
  614. if (current_std == STD_INVALID)
  615. continue;
  616. sync_lock_status = tvp514x_read_reg(decoder->client,
  617. REG_STATUS1);
  618. if (lock_mask == (sync_lock_status & lock_mask))
  619. break; /* Input detected */
  620. }
  621. if ((current_std == STD_INVALID) || (try_count <= 0))
  622. return -EINVAL;
  623. decoder->current_std = current_std;
  624. decoder->route.input = route->input;
  625. decoder->route.output = route->output;
  626. v4l_dbg(1, debug, decoder->client,
  627. "Input set to: %d, std : %d",
  628. input_sel, current_std);
  629. return 0;
  630. }
  631. /**
  632. * ioctl_queryctrl - V4L2 decoder interface handler for VIDIOC_QUERYCTRL ioctl
  633. * @s: pointer to standard V4L2 device structure
  634. * @qctrl: standard V4L2 v4l2_queryctrl structure
  635. *
  636. * If the requested control is supported, returns the control information.
  637. * Otherwise, returns -EINVAL if the control is not supported.
  638. */
  639. static int
  640. ioctl_queryctrl(struct v4l2_int_device *s, struct v4l2_queryctrl *qctrl)
  641. {
  642. struct tvp514x_decoder *decoder = s->priv;
  643. int err = -EINVAL;
  644. if (qctrl == NULL)
  645. return err;
  646. switch (qctrl->id) {
  647. case V4L2_CID_BRIGHTNESS:
  648. /* Brightness supported is same as standard one (0-255),
  649. * so make use of standard API provided.
  650. */
  651. err = v4l2_ctrl_query_fill_std(qctrl);
  652. break;
  653. case V4L2_CID_CONTRAST:
  654. case V4L2_CID_SATURATION:
  655. /* Saturation and Contrast supported is -
  656. * Contrast: 0 - 255 (Default - 128)
  657. * Saturation: 0 - 255 (Default - 128)
  658. */
  659. err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
  660. break;
  661. case V4L2_CID_HUE:
  662. /* Hue Supported is -
  663. * Hue - -180 - +180 (Default - 0, Step - +180)
  664. */
  665. err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
  666. break;
  667. case V4L2_CID_AUTOGAIN:
  668. /* Autogain is either 0 or 1*/
  669. memcpy(qctrl, &tvp514x_autogain_ctrl,
  670. sizeof(struct v4l2_queryctrl));
  671. err = 0;
  672. break;
  673. default:
  674. v4l_err(decoder->client,
  675. "invalid control id %d\n", qctrl->id);
  676. return err;
  677. }
  678. v4l_dbg(1, debug, decoder->client,
  679. "Query Control: %s : Min - %d, Max - %d, Def - %d",
  680. qctrl->name,
  681. qctrl->minimum,
  682. qctrl->maximum,
  683. qctrl->default_value);
  684. return err;
  685. }
  686. /**
  687. * ioctl_g_ctrl - V4L2 decoder interface handler for VIDIOC_G_CTRL ioctl
  688. * @s: pointer to standard V4L2 device structure
  689. * @ctrl: pointer to v4l2_control structure
  690. *
  691. * If the requested control is supported, returns the control's current
  692. * value from the decoder. Otherwise, returns -EINVAL if the control is not
  693. * supported.
  694. */
  695. static int
  696. ioctl_g_ctrl(struct v4l2_int_device *s, struct v4l2_control *ctrl)
  697. {
  698. struct tvp514x_decoder *decoder = s->priv;
  699. if (ctrl == NULL)
  700. return -EINVAL;
  701. switch (ctrl->id) {
  702. case V4L2_CID_BRIGHTNESS:
  703. ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
  704. break;
  705. case V4L2_CID_CONTRAST:
  706. ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
  707. break;
  708. case V4L2_CID_SATURATION:
  709. ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
  710. break;
  711. case V4L2_CID_HUE:
  712. ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
  713. if (ctrl->value == 0x7F)
  714. ctrl->value = 180;
  715. else if (ctrl->value == 0x80)
  716. ctrl->value = -180;
  717. else
  718. ctrl->value = 0;
  719. break;
  720. case V4L2_CID_AUTOGAIN:
  721. ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
  722. if ((ctrl->value & 0x3) == 3)
  723. ctrl->value = 1;
  724. else
  725. ctrl->value = 0;
  726. break;
  727. default:
  728. v4l_err(decoder->client,
  729. "invalid control id %d\n", ctrl->id);
  730. return -EINVAL;
  731. }
  732. v4l_dbg(1, debug, decoder->client,
  733. "Get Control: ID - %d - %d",
  734. ctrl->id, ctrl->value);
  735. return 0;
  736. }
  737. /**
  738. * ioctl_s_ctrl - V4L2 decoder interface handler for VIDIOC_S_CTRL ioctl
  739. * @s: pointer to standard V4L2 device structure
  740. * @ctrl: pointer to v4l2_control structure
  741. *
  742. * If the requested control is supported, sets the control's current
  743. * value in HW. Otherwise, returns -EINVAL if the control is not supported.
  744. */
  745. static int
  746. ioctl_s_ctrl(struct v4l2_int_device *s, struct v4l2_control *ctrl)
  747. {
  748. struct tvp514x_decoder *decoder = s->priv;
  749. int err = -EINVAL, value;
  750. if (ctrl == NULL)
  751. return err;
  752. value = (__s32) ctrl->value;
  753. switch (ctrl->id) {
  754. case V4L2_CID_BRIGHTNESS:
  755. if (ctrl->value < 0 || ctrl->value > 255) {
  756. v4l_err(decoder->client,
  757. "invalid brightness setting %d\n",
  758. ctrl->value);
  759. return -ERANGE;
  760. }
  761. err = tvp514x_write_reg(decoder->client, REG_BRIGHTNESS,
  762. value);
  763. if (err)
  764. return err;
  765. decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
  766. break;
  767. case V4L2_CID_CONTRAST:
  768. if (ctrl->value < 0 || ctrl->value > 255) {
  769. v4l_err(decoder->client,
  770. "invalid contrast setting %d\n",
  771. ctrl->value);
  772. return -ERANGE;
  773. }
  774. err = tvp514x_write_reg(decoder->client, REG_CONTRAST,
  775. 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. v4l_err(decoder->client,
  783. "invalid saturation setting %d\n",
  784. ctrl->value);
  785. return -ERANGE;
  786. }
  787. err = tvp514x_write_reg(decoder->client, REG_SATURATION,
  788. value);
  789. if (err)
  790. return err;
  791. decoder->tvp514x_regs[REG_SATURATION].val = value;
  792. break;
  793. case V4L2_CID_HUE:
  794. if (value == 180)
  795. value = 0x7F;
  796. else if (value == -180)
  797. value = 0x80;
  798. else if (value == 0)
  799. value = 0;
  800. else {
  801. v4l_err(decoder->client,
  802. "invalid hue setting %d\n",
  803. ctrl->value);
  804. return -ERANGE;
  805. }
  806. err = tvp514x_write_reg(decoder->client, REG_HUE,
  807. value);
  808. if (err)
  809. return err;
  810. decoder->tvp514x_regs[REG_HUE].val = value;
  811. break;
  812. case V4L2_CID_AUTOGAIN:
  813. if (value == 1)
  814. value = 0x0F;
  815. else if (value == 0)
  816. value = 0x0C;
  817. else {
  818. v4l_err(decoder->client,
  819. "invalid auto gain setting %d\n",
  820. ctrl->value);
  821. return -ERANGE;
  822. }
  823. err = tvp514x_write_reg(decoder->client, REG_AFE_GAIN_CTRL,
  824. value);
  825. if (err)
  826. return err;
  827. decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
  828. break;
  829. default:
  830. v4l_err(decoder->client,
  831. "invalid control id %d\n", ctrl->id);
  832. return err;
  833. }
  834. v4l_dbg(1, debug, decoder->client,
  835. "Set Control: ID - %d - %d",
  836. ctrl->id, ctrl->value);
  837. return err;
  838. }
  839. /**
  840. * ioctl_enum_fmt_cap - Implement the CAPTURE buffer VIDIOC_ENUM_FMT ioctl
  841. * @s: pointer to standard V4L2 device structure
  842. * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
  843. *
  844. * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
  845. */
  846. static int
  847. ioctl_enum_fmt_cap(struct v4l2_int_device *s, struct v4l2_fmtdesc *fmt)
  848. {
  849. struct tvp514x_decoder *decoder = s->priv;
  850. int index;
  851. if (fmt == NULL)
  852. return -EINVAL;
  853. index = fmt->index;
  854. if ((index >= decoder->num_fmts) || (index < 0))
  855. return -EINVAL; /* Index out of bound */
  856. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  857. return -EINVAL; /* only capture is supported */
  858. memcpy(fmt, &decoder->fmt_list[index],
  859. sizeof(struct v4l2_fmtdesc));
  860. v4l_dbg(1, debug, decoder->client,
  861. "Current FMT: index - %d (%s)",
  862. decoder->fmt_list[index].index,
  863. decoder->fmt_list[index].description);
  864. return 0;
  865. }
  866. /**
  867. * ioctl_try_fmt_cap - Implement the CAPTURE buffer VIDIOC_TRY_FMT ioctl
  868. * @s: pointer to standard V4L2 device structure
  869. * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
  870. *
  871. * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
  872. * ioctl is used to negotiate the image capture size and pixel format
  873. * without actually making it take effect.
  874. */
  875. static int
  876. ioctl_try_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
  877. {
  878. struct tvp514x_decoder *decoder = s->priv;
  879. int ifmt;
  880. struct v4l2_pix_format *pix;
  881. enum tvp514x_std current_std;
  882. if (f == NULL)
  883. return -EINVAL;
  884. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  885. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  886. pix = &f->fmt.pix;
  887. /* Calculate height and width based on current standard */
  888. current_std = tvp514x_get_current_std(decoder);
  889. if (current_std == STD_INVALID)
  890. return -EINVAL;
  891. decoder->current_std = current_std;
  892. pix->width = decoder->std_list[current_std].width;
  893. pix->height = decoder->std_list[current_std].height;
  894. for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
  895. if (pix->pixelformat ==
  896. decoder->fmt_list[ifmt].pixelformat)
  897. break;
  898. }
  899. if (ifmt == decoder->num_fmts)
  900. ifmt = 0; /* None of the format matched, select default */
  901. pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;
  902. pix->field = V4L2_FIELD_INTERLACED;
  903. pix->bytesperline = pix->width * 2;
  904. pix->sizeimage = pix->bytesperline * pix->height;
  905. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  906. pix->priv = 0;
  907. v4l_dbg(1, debug, decoder->client,
  908. "Try FMT: pixelformat - %s, bytesperline - %d"
  909. "Width - %d, Height - %d",
  910. decoder->fmt_list[ifmt].description, pix->bytesperline,
  911. pix->width, pix->height);
  912. return 0;
  913. }
  914. /**
  915. * ioctl_s_fmt_cap - V4L2 decoder interface handler for VIDIOC_S_FMT ioctl
  916. * @s: pointer to standard V4L2 device structure
  917. * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
  918. *
  919. * If the requested format is supported, configures the HW to use that
  920. * format, returns error code if format not supported or HW can't be
  921. * correctly configured.
  922. */
  923. static int
  924. ioctl_s_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
  925. {
  926. struct tvp514x_decoder *decoder = s->priv;
  927. struct v4l2_pix_format *pix;
  928. int rval;
  929. if (f == NULL)
  930. return -EINVAL;
  931. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  932. return -EINVAL; /* only capture is supported */
  933. pix = &f->fmt.pix;
  934. rval = ioctl_try_fmt_cap(s, f);
  935. if (rval)
  936. return rval;
  937. decoder->pix = *pix;
  938. return rval;
  939. }
  940. /**
  941. * ioctl_g_fmt_cap - V4L2 decoder interface handler for ioctl_g_fmt_cap
  942. * @s: pointer to standard V4L2 device structure
  943. * @f: pointer to standard V4L2 v4l2_format structure
  944. *
  945. * Returns the decoder's current pixel format in the v4l2_format
  946. * parameter.
  947. */
  948. static int
  949. ioctl_g_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
  950. {
  951. struct tvp514x_decoder *decoder = s->priv;
  952. if (f == NULL)
  953. return -EINVAL;
  954. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  955. return -EINVAL; /* only capture is supported */
  956. f->fmt.pix = decoder->pix;
  957. v4l_dbg(1, debug, decoder->client,
  958. "Current FMT: bytesperline - %d"
  959. "Width - %d, Height - %d",
  960. decoder->pix.bytesperline,
  961. decoder->pix.width, decoder->pix.height);
  962. return 0;
  963. }
  964. /**
  965. * ioctl_g_parm - V4L2 decoder interface handler for VIDIOC_G_PARM ioctl
  966. * @s: pointer to standard V4L2 device structure
  967. * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
  968. *
  969. * Returns the decoder's video CAPTURE parameters.
  970. */
  971. static int
  972. ioctl_g_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
  973. {
  974. struct tvp514x_decoder *decoder = s->priv;
  975. struct v4l2_captureparm *cparm;
  976. enum tvp514x_std current_std;
  977. if (a == NULL)
  978. return -EINVAL;
  979. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  980. return -EINVAL; /* only capture is supported */
  981. memset(a, 0, sizeof(*a));
  982. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  983. /* get the current standard */
  984. current_std = tvp514x_get_current_std(decoder);
  985. if (current_std == STD_INVALID)
  986. return -EINVAL;
  987. decoder->current_std = current_std;
  988. cparm = &a->parm.capture;
  989. cparm->capability = V4L2_CAP_TIMEPERFRAME;
  990. cparm->timeperframe =
  991. decoder->std_list[current_std].standard.frameperiod;
  992. return 0;
  993. }
  994. /**
  995. * ioctl_s_parm - V4L2 decoder interface handler for VIDIOC_S_PARM ioctl
  996. * @s: pointer to standard V4L2 device structure
  997. * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
  998. *
  999. * Configures the decoder to use the input parameters, if possible. If
  1000. * not possible, returns the appropriate error code.
  1001. */
  1002. static int
  1003. ioctl_s_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
  1004. {
  1005. struct tvp514x_decoder *decoder = s->priv;
  1006. struct v4l2_fract *timeperframe;
  1007. enum tvp514x_std current_std;
  1008. if (a == NULL)
  1009. return -EINVAL;
  1010. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1011. return -EINVAL; /* only capture is supported */
  1012. timeperframe = &a->parm.capture.timeperframe;
  1013. /* get the current standard */
  1014. current_std = tvp514x_get_current_std(decoder);
  1015. if (current_std == STD_INVALID)
  1016. return -EINVAL;
  1017. decoder->current_std = current_std;
  1018. *timeperframe =
  1019. decoder->std_list[current_std].standard.frameperiod;
  1020. return 0;
  1021. }
  1022. /**
  1023. * ioctl_g_ifparm - V4L2 decoder interface handler for vidioc_int_g_ifparm_num
  1024. * @s: pointer to standard V4L2 device structure
  1025. * @p: pointer to standard V4L2 vidioc_int_g_ifparm_num ioctl structure
  1026. *
  1027. * Gets slave interface parameters.
  1028. * Calculates the required xclk value to support the requested
  1029. * clock parameters in p. This value is returned in the p
  1030. * parameter.
  1031. */
  1032. static int ioctl_g_ifparm(struct v4l2_int_device *s, struct v4l2_ifparm *p)
  1033. {
  1034. struct tvp514x_decoder *decoder = s->priv;
  1035. int rval;
  1036. if (p == NULL)
  1037. return -EINVAL;
  1038. if (NULL == decoder->pdata->ifparm)
  1039. return -EINVAL;
  1040. rval = decoder->pdata->ifparm(p);
  1041. if (rval) {
  1042. v4l_err(decoder->client, "g_ifparm.Err[%d]\n", rval);
  1043. return rval;
  1044. }
  1045. p->u.bt656.clock_curr = TVP514X_XCLK_BT656;
  1046. return 0;
  1047. }
  1048. /**
  1049. * ioctl_g_priv - V4L2 decoder interface handler for vidioc_int_g_priv_num
  1050. * @s: pointer to standard V4L2 device structure
  1051. * @p: void pointer to hold decoder's private data address
  1052. *
  1053. * Returns device's (decoder's) private data area address in p parameter
  1054. */
  1055. static int ioctl_g_priv(struct v4l2_int_device *s, void *p)
  1056. {
  1057. struct tvp514x_decoder *decoder = s->priv;
  1058. if (NULL == decoder->pdata->priv_data_set)
  1059. return -EINVAL;
  1060. return decoder->pdata->priv_data_set(p);
  1061. }
  1062. /**
  1063. * ioctl_s_power - V4L2 decoder interface handler for vidioc_int_s_power_num
  1064. * @s: pointer to standard V4L2 device structure
  1065. * @on: power state to which device is to be set
  1066. *
  1067. * Sets devices power state to requrested state, if possible.
  1068. */
  1069. static int ioctl_s_power(struct v4l2_int_device *s, enum v4l2_power on)
  1070. {
  1071. struct tvp514x_decoder *decoder = s->priv;
  1072. int err = 0;
  1073. switch (on) {
  1074. case V4L2_POWER_OFF:
  1075. /* Power Down Sequence */
  1076. err =
  1077. tvp514x_write_reg(decoder->client, REG_OPERATION_MODE,
  1078. 0x01);
  1079. /* Disable mux for TVP5146/47 decoder data path */
  1080. if (decoder->pdata->power_set)
  1081. err |= decoder->pdata->power_set(on);
  1082. decoder->state = STATE_NOT_DETECTED;
  1083. break;
  1084. case V4L2_POWER_STANDBY:
  1085. if (decoder->pdata->power_set)
  1086. err = decoder->pdata->power_set(on);
  1087. break;
  1088. case V4L2_POWER_ON:
  1089. /* Enable mux for TVP5146/47 decoder data path */
  1090. if ((decoder->pdata->power_set) &&
  1091. (decoder->state == STATE_NOT_DETECTED)) {
  1092. int i;
  1093. struct tvp514x_init_seq *int_seq =
  1094. (struct tvp514x_init_seq *)
  1095. decoder->id->driver_data;
  1096. err = decoder->pdata->power_set(on);
  1097. /* Power Up Sequence */
  1098. for (i = 0; i < int_seq->no_regs; i++) {
  1099. err |= tvp514x_write_reg(decoder->client,
  1100. int_seq->init_reg_seq[i].reg,
  1101. int_seq->init_reg_seq[i].val);
  1102. }
  1103. /* Detect the sensor is not already detected */
  1104. err |= tvp514x_detect(decoder);
  1105. if (err) {
  1106. v4l_err(decoder->client,
  1107. "Unable to detect decoder\n");
  1108. return err;
  1109. }
  1110. }
  1111. err |= tvp514x_configure(decoder);
  1112. break;
  1113. default:
  1114. err = -ENODEV;
  1115. break;
  1116. }
  1117. return err;
  1118. }
  1119. /**
  1120. * ioctl_init - V4L2 decoder interface handler for VIDIOC_INT_INIT
  1121. * @s: pointer to standard V4L2 device structure
  1122. *
  1123. * Initialize the decoder device (calls tvp514x_configure())
  1124. */
  1125. static int ioctl_init(struct v4l2_int_device *s)
  1126. {
  1127. struct tvp514x_decoder *decoder = s->priv;
  1128. /* Set default standard to auto */
  1129. decoder->tvp514x_regs[REG_VIDEO_STD].val =
  1130. VIDEO_STD_AUTO_SWITCH_BIT;
  1131. return tvp514x_configure(decoder);
  1132. }
  1133. /**
  1134. * ioctl_dev_exit - V4L2 decoder interface handler for vidioc_int_dev_exit_num
  1135. * @s: pointer to standard V4L2 device structure
  1136. *
  1137. * Delinitialise the dev. at slave detach. The complement of ioctl_dev_init.
  1138. */
  1139. static int ioctl_dev_exit(struct v4l2_int_device *s)
  1140. {
  1141. return 0;
  1142. }
  1143. /**
  1144. * ioctl_dev_init - V4L2 decoder interface handler for vidioc_int_dev_init_num
  1145. * @s: pointer to standard V4L2 device structure
  1146. *
  1147. * Initialise the device when slave attaches to the master. Returns 0 if
  1148. * TVP5146/47 device could be found, otherwise returns appropriate error.
  1149. */
  1150. static int ioctl_dev_init(struct v4l2_int_device *s)
  1151. {
  1152. struct tvp514x_decoder *decoder = s->priv;
  1153. int err;
  1154. err = tvp514x_detect(decoder);
  1155. if (err < 0) {
  1156. v4l_err(decoder->client,
  1157. "Unable to detect decoder\n");
  1158. return err;
  1159. }
  1160. v4l_info(decoder->client,
  1161. "chip version 0x%.2x detected\n", decoder->ver);
  1162. return 0;
  1163. }
  1164. static struct v4l2_int_ioctl_desc tvp514x_ioctl_desc[] = {
  1165. {vidioc_int_dev_init_num, (v4l2_int_ioctl_func*) ioctl_dev_init},
  1166. {vidioc_int_dev_exit_num, (v4l2_int_ioctl_func*) ioctl_dev_exit},
  1167. {vidioc_int_s_power_num, (v4l2_int_ioctl_func*) ioctl_s_power},
  1168. {vidioc_int_g_priv_num, (v4l2_int_ioctl_func*) ioctl_g_priv},
  1169. {vidioc_int_g_ifparm_num, (v4l2_int_ioctl_func*) ioctl_g_ifparm},
  1170. {vidioc_int_init_num, (v4l2_int_ioctl_func*) ioctl_init},
  1171. {vidioc_int_enum_fmt_cap_num,
  1172. (v4l2_int_ioctl_func *) ioctl_enum_fmt_cap},
  1173. {vidioc_int_try_fmt_cap_num,
  1174. (v4l2_int_ioctl_func *) ioctl_try_fmt_cap},
  1175. {vidioc_int_g_fmt_cap_num,
  1176. (v4l2_int_ioctl_func *) ioctl_g_fmt_cap},
  1177. {vidioc_int_s_fmt_cap_num,
  1178. (v4l2_int_ioctl_func *) ioctl_s_fmt_cap},
  1179. {vidioc_int_g_parm_num, (v4l2_int_ioctl_func *) ioctl_g_parm},
  1180. {vidioc_int_s_parm_num, (v4l2_int_ioctl_func *) ioctl_s_parm},
  1181. {vidioc_int_queryctrl_num,
  1182. (v4l2_int_ioctl_func *) ioctl_queryctrl},
  1183. {vidioc_int_g_ctrl_num, (v4l2_int_ioctl_func *) ioctl_g_ctrl},
  1184. {vidioc_int_s_ctrl_num, (v4l2_int_ioctl_func *) ioctl_s_ctrl},
  1185. {vidioc_int_querystd_num, (v4l2_int_ioctl_func *) ioctl_querystd},
  1186. {vidioc_int_s_std_num, (v4l2_int_ioctl_func *) ioctl_s_std},
  1187. {vidioc_int_s_video_routing_num,
  1188. (v4l2_int_ioctl_func *) ioctl_s_routing},
  1189. };
  1190. static struct tvp514x_decoder tvp514x_dev = {
  1191. .state = STATE_NOT_DETECTED,
  1192. .fmt_list = tvp514x_fmt_list,
  1193. .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
  1194. .pix = { /* Default to NTSC 8-bit YUV 422 */
  1195. .width = NTSC_NUM_ACTIVE_PIXELS,
  1196. .height = NTSC_NUM_ACTIVE_LINES,
  1197. .pixelformat = V4L2_PIX_FMT_UYVY,
  1198. .field = V4L2_FIELD_INTERLACED,
  1199. .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
  1200. .sizeimage =
  1201. NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
  1202. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  1203. },
  1204. .current_std = STD_NTSC_MJ,
  1205. .std_list = tvp514x_std_list,
  1206. .num_stds = ARRAY_SIZE(tvp514x_std_list),
  1207. .v4l2_int_device = {
  1208. .module = THIS_MODULE,
  1209. .name = TVP514X_MODULE_NAME,
  1210. .type = v4l2_int_type_slave,
  1211. },
  1212. .tvp514x_slave = {
  1213. .ioctls = tvp514x_ioctl_desc,
  1214. .num_ioctls = ARRAY_SIZE(tvp514x_ioctl_desc),
  1215. },
  1216. };
  1217. /**
  1218. * tvp514x_probe - decoder driver i2c probe handler
  1219. * @client: i2c driver client device structure
  1220. *
  1221. * Register decoder as an i2c client device and V4L2
  1222. * device.
  1223. */
  1224. static int
  1225. tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1226. {
  1227. struct tvp514x_decoder *decoder;
  1228. int err;
  1229. /* Check if the adapter supports the needed features */
  1230. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1231. return -EIO;
  1232. decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
  1233. if (!decoder)
  1234. return -ENOMEM;
  1235. if (!client->dev.platform_data) {
  1236. v4l_err(client, "No platform data!!\n");
  1237. err = -ENODEV;
  1238. goto out_free;
  1239. }
  1240. *decoder = tvp514x_dev;
  1241. decoder->v4l2_int_device.priv = decoder;
  1242. decoder->pdata = client->dev.platform_data;
  1243. decoder->v4l2_int_device.u.slave = &decoder->tvp514x_slave;
  1244. memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
  1245. sizeof(tvp514x_reg_list_default));
  1246. /*
  1247. * Fetch platform specific data, and configure the
  1248. * tvp514x_reg_list[] accordingly. Since this is one
  1249. * time configuration, no need to preserve.
  1250. */
  1251. decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
  1252. (decoder->pdata->clk_polarity << 1);
  1253. decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
  1254. ((decoder->pdata->hs_polarity << 2) |
  1255. (decoder->pdata->vs_polarity << 3));
  1256. /*
  1257. * Save the id data, required for power up sequence
  1258. */
  1259. decoder->id = (struct i2c_device_id *)id;
  1260. /* Attach to Master */
  1261. strcpy(decoder->v4l2_int_device.u.slave->attach_to,
  1262. decoder->pdata->master);
  1263. decoder->client = client;
  1264. i2c_set_clientdata(client, decoder);
  1265. /* Register with V4L2 layer as slave device */
  1266. err = v4l2_int_device_register(&decoder->v4l2_int_device);
  1267. if (err) {
  1268. i2c_set_clientdata(client, NULL);
  1269. v4l_err(client,
  1270. "Unable to register to v4l2. Err[%d]\n", err);
  1271. goto out_free;
  1272. } else
  1273. v4l_info(client, "Registered to v4l2 master %s!!\n",
  1274. decoder->pdata->master);
  1275. return 0;
  1276. out_free:
  1277. kfree(decoder);
  1278. return err;
  1279. }
  1280. /**
  1281. * tvp514x_remove - decoder driver i2c remove handler
  1282. * @client: i2c driver client device structure
  1283. *
  1284. * Unregister decoder as an i2c client device and V4L2
  1285. * device. Complement of tvp514x_probe().
  1286. */
  1287. static int __exit tvp514x_remove(struct i2c_client *client)
  1288. {
  1289. struct tvp514x_decoder *decoder = i2c_get_clientdata(client);
  1290. if (!client->adapter)
  1291. return -ENODEV; /* our client isn't attached */
  1292. v4l2_int_device_unregister(&decoder->v4l2_int_device);
  1293. i2c_set_clientdata(client, NULL);
  1294. kfree(decoder);
  1295. return 0;
  1296. }
  1297. /*
  1298. * TVP5146 Init/Power on Sequence
  1299. */
  1300. static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
  1301. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1302. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1303. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1304. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1305. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1306. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1307. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1308. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1309. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1310. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1311. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1312. };
  1313. static const struct tvp514x_init_seq tvp5146_init = {
  1314. .no_regs = ARRAY_SIZE(tvp5146_init_reg_seq),
  1315. .init_reg_seq = tvp5146_init_reg_seq,
  1316. };
  1317. /*
  1318. * TVP5147 Init/Power on Sequence
  1319. */
  1320. static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
  1321. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1322. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1323. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1324. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1325. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1326. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1327. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1328. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1329. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
  1330. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1331. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
  1332. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
  1333. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1334. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1335. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1336. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1337. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1338. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1339. };
  1340. static const struct tvp514x_init_seq tvp5147_init = {
  1341. .no_regs = ARRAY_SIZE(tvp5147_init_reg_seq),
  1342. .init_reg_seq = tvp5147_init_reg_seq,
  1343. };
  1344. /*
  1345. * TVP5146M2/TVP5147M1 Init/Power on Sequence
  1346. */
  1347. static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
  1348. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1349. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1350. };
  1351. static const struct tvp514x_init_seq tvp514xm_init = {
  1352. .no_regs = ARRAY_SIZE(tvp514xm_init_reg_seq),
  1353. .init_reg_seq = tvp514xm_init_reg_seq,
  1354. };
  1355. /*
  1356. * I2C Device Table -
  1357. *
  1358. * name - Name of the actual device/chip.
  1359. * driver_data - Driver data
  1360. */
  1361. static const struct i2c_device_id tvp514x_id[] = {
  1362. {"tvp5146", (unsigned long)&tvp5146_init},
  1363. {"tvp5146m2", (unsigned long)&tvp514xm_init},
  1364. {"tvp5147", (unsigned long)&tvp5147_init},
  1365. {"tvp5147m1", (unsigned long)&tvp514xm_init},
  1366. {},
  1367. };
  1368. MODULE_DEVICE_TABLE(i2c, tvp514x_id);
  1369. static struct i2c_driver tvp514x_i2c_driver = {
  1370. .driver = {
  1371. .name = TVP514X_MODULE_NAME,
  1372. .owner = THIS_MODULE,
  1373. },
  1374. .probe = tvp514x_probe,
  1375. .remove = __exit_p(tvp514x_remove),
  1376. .id_table = tvp514x_id,
  1377. };
  1378. /**
  1379. * tvp514x_init
  1380. *
  1381. * Module init function
  1382. */
  1383. static int __init tvp514x_init(void)
  1384. {
  1385. return i2c_add_driver(&tvp514x_i2c_driver);
  1386. }
  1387. /**
  1388. * tvp514x_cleanup
  1389. *
  1390. * Module exit function
  1391. */
  1392. static void __exit tvp514x_cleanup(void)
  1393. {
  1394. i2c_del_driver(&tvp514x_i2c_driver);
  1395. }
  1396. module_init(tvp514x_init);
  1397. module_exit(tvp514x_cleanup);
  1398. MODULE_AUTHOR("Texas Instruments");
  1399. MODULE_DESCRIPTION("TVP514X linux decoder driver");
  1400. MODULE_LICENSE("GPL");