tvp514x.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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 (0-255),
  649. */
  650. err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
  651. break;
  652. case V4L2_CID_CONTRAST:
  653. case V4L2_CID_SATURATION:
  654. /* Saturation and Contrast supported is -
  655. * Contrast: 0 - 255 (Default - 128)
  656. * Saturation: 0 - 255 (Default - 128)
  657. */
  658. err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
  659. break;
  660. case V4L2_CID_HUE:
  661. /* Hue Supported is -
  662. * Hue - -180 - +180 (Default - 0, Step - +180)
  663. */
  664. err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
  665. break;
  666. case V4L2_CID_AUTOGAIN:
  667. /* Autogain is either 0 or 1*/
  668. memcpy(qctrl, &tvp514x_autogain_ctrl,
  669. sizeof(struct v4l2_queryctrl));
  670. err = 0;
  671. break;
  672. default:
  673. v4l_err(decoder->client,
  674. "invalid control id %d\n", qctrl->id);
  675. return err;
  676. }
  677. v4l_dbg(1, debug, decoder->client,
  678. "Query Control: %s : Min - %d, Max - %d, Def - %d",
  679. qctrl->name,
  680. qctrl->minimum,
  681. qctrl->maximum,
  682. qctrl->default_value);
  683. return err;
  684. }
  685. /**
  686. * ioctl_g_ctrl - V4L2 decoder interface handler for VIDIOC_G_CTRL ioctl
  687. * @s: pointer to standard V4L2 device structure
  688. * @ctrl: pointer to v4l2_control structure
  689. *
  690. * If the requested control is supported, returns the control's current
  691. * value from the decoder. Otherwise, returns -EINVAL if the control is not
  692. * supported.
  693. */
  694. static int
  695. ioctl_g_ctrl(struct v4l2_int_device *s, struct v4l2_control *ctrl)
  696. {
  697. struct tvp514x_decoder *decoder = s->priv;
  698. if (ctrl == NULL)
  699. return -EINVAL;
  700. switch (ctrl->id) {
  701. case V4L2_CID_BRIGHTNESS:
  702. ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
  703. break;
  704. case V4L2_CID_CONTRAST:
  705. ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
  706. break;
  707. case V4L2_CID_SATURATION:
  708. ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
  709. break;
  710. case V4L2_CID_HUE:
  711. ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
  712. if (ctrl->value == 0x7F)
  713. ctrl->value = 180;
  714. else if (ctrl->value == 0x80)
  715. ctrl->value = -180;
  716. else
  717. ctrl->value = 0;
  718. break;
  719. case V4L2_CID_AUTOGAIN:
  720. ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
  721. if ((ctrl->value & 0x3) == 3)
  722. ctrl->value = 1;
  723. else
  724. ctrl->value = 0;
  725. break;
  726. default:
  727. v4l_err(decoder->client,
  728. "invalid control id %d\n", ctrl->id);
  729. return -EINVAL;
  730. }
  731. v4l_dbg(1, debug, decoder->client,
  732. "Get Control: ID - %d - %d",
  733. ctrl->id, ctrl->value);
  734. return 0;
  735. }
  736. /**
  737. * ioctl_s_ctrl - V4L2 decoder interface handler for VIDIOC_S_CTRL ioctl
  738. * @s: pointer to standard V4L2 device structure
  739. * @ctrl: pointer to v4l2_control structure
  740. *
  741. * If the requested control is supported, sets the control's current
  742. * value in HW. Otherwise, returns -EINVAL if the control is not supported.
  743. */
  744. static int
  745. ioctl_s_ctrl(struct v4l2_int_device *s, struct v4l2_control *ctrl)
  746. {
  747. struct tvp514x_decoder *decoder = s->priv;
  748. int err = -EINVAL, value;
  749. if (ctrl == NULL)
  750. return err;
  751. value = (__s32) ctrl->value;
  752. switch (ctrl->id) {
  753. case V4L2_CID_BRIGHTNESS:
  754. if (ctrl->value < 0 || ctrl->value > 255) {
  755. v4l_err(decoder->client,
  756. "invalid brightness setting %d\n",
  757. ctrl->value);
  758. return -ERANGE;
  759. }
  760. err = tvp514x_write_reg(decoder->client, REG_BRIGHTNESS,
  761. value);
  762. if (err)
  763. return err;
  764. decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
  765. break;
  766. case V4L2_CID_CONTRAST:
  767. if (ctrl->value < 0 || ctrl->value > 255) {
  768. v4l_err(decoder->client,
  769. "invalid contrast setting %d\n",
  770. ctrl->value);
  771. return -ERANGE;
  772. }
  773. err = tvp514x_write_reg(decoder->client, REG_CONTRAST,
  774. value);
  775. if (err)
  776. return err;
  777. decoder->tvp514x_regs[REG_CONTRAST].val = value;
  778. break;
  779. case V4L2_CID_SATURATION:
  780. if (ctrl->value < 0 || ctrl->value > 255) {
  781. v4l_err(decoder->client,
  782. "invalid saturation setting %d\n",
  783. ctrl->value);
  784. return -ERANGE;
  785. }
  786. err = tvp514x_write_reg(decoder->client, REG_SATURATION,
  787. value);
  788. if (err)
  789. return err;
  790. decoder->tvp514x_regs[REG_SATURATION].val = value;
  791. break;
  792. case V4L2_CID_HUE:
  793. if (value == 180)
  794. value = 0x7F;
  795. else if (value == -180)
  796. value = 0x80;
  797. else if (value == 0)
  798. value = 0;
  799. else {
  800. v4l_err(decoder->client,
  801. "invalid hue setting %d\n",
  802. ctrl->value);
  803. return -ERANGE;
  804. }
  805. err = tvp514x_write_reg(decoder->client, REG_HUE,
  806. value);
  807. if (err)
  808. return err;
  809. decoder->tvp514x_regs[REG_HUE].val = value;
  810. break;
  811. case V4L2_CID_AUTOGAIN:
  812. if (value == 1)
  813. value = 0x0F;
  814. else if (value == 0)
  815. value = 0x0C;
  816. else {
  817. v4l_err(decoder->client,
  818. "invalid auto gain setting %d\n",
  819. ctrl->value);
  820. return -ERANGE;
  821. }
  822. err = tvp514x_write_reg(decoder->client, REG_AFE_GAIN_CTRL,
  823. value);
  824. if (err)
  825. return err;
  826. decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
  827. break;
  828. default:
  829. v4l_err(decoder->client,
  830. "invalid control id %d\n", ctrl->id);
  831. return err;
  832. }
  833. v4l_dbg(1, debug, decoder->client,
  834. "Set Control: ID - %d - %d",
  835. ctrl->id, ctrl->value);
  836. return err;
  837. }
  838. /**
  839. * ioctl_enum_fmt_cap - Implement the CAPTURE buffer VIDIOC_ENUM_FMT ioctl
  840. * @s: pointer to standard V4L2 device structure
  841. * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
  842. *
  843. * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
  844. */
  845. static int
  846. ioctl_enum_fmt_cap(struct v4l2_int_device *s, struct v4l2_fmtdesc *fmt)
  847. {
  848. struct tvp514x_decoder *decoder = s->priv;
  849. int index;
  850. if (fmt == NULL)
  851. return -EINVAL;
  852. index = fmt->index;
  853. if ((index >= decoder->num_fmts) || (index < 0))
  854. return -EINVAL; /* Index out of bound */
  855. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  856. return -EINVAL; /* only capture is supported */
  857. memcpy(fmt, &decoder->fmt_list[index],
  858. sizeof(struct v4l2_fmtdesc));
  859. v4l_dbg(1, debug, decoder->client,
  860. "Current FMT: index - %d (%s)",
  861. decoder->fmt_list[index].index,
  862. decoder->fmt_list[index].description);
  863. return 0;
  864. }
  865. /**
  866. * ioctl_try_fmt_cap - Implement the CAPTURE buffer VIDIOC_TRY_FMT ioctl
  867. * @s: pointer to standard V4L2 device structure
  868. * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
  869. *
  870. * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
  871. * ioctl is used to negotiate the image capture size and pixel format
  872. * without actually making it take effect.
  873. */
  874. static int
  875. ioctl_try_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
  876. {
  877. struct tvp514x_decoder *decoder = s->priv;
  878. int ifmt;
  879. struct v4l2_pix_format *pix;
  880. enum tvp514x_std current_std;
  881. if (f == NULL)
  882. return -EINVAL;
  883. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  884. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  885. pix = &f->fmt.pix;
  886. /* Calculate height and width based on current standard */
  887. current_std = tvp514x_get_current_std(decoder);
  888. if (current_std == STD_INVALID)
  889. return -EINVAL;
  890. decoder->current_std = current_std;
  891. pix->width = decoder->std_list[current_std].width;
  892. pix->height = decoder->std_list[current_std].height;
  893. for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
  894. if (pix->pixelformat ==
  895. decoder->fmt_list[ifmt].pixelformat)
  896. break;
  897. }
  898. if (ifmt == decoder->num_fmts)
  899. ifmt = 0; /* None of the format matched, select default */
  900. pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;
  901. pix->field = V4L2_FIELD_INTERLACED;
  902. pix->bytesperline = pix->width * 2;
  903. pix->sizeimage = pix->bytesperline * pix->height;
  904. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  905. pix->priv = 0;
  906. v4l_dbg(1, debug, decoder->client,
  907. "Try FMT: pixelformat - %s, bytesperline - %d"
  908. "Width - %d, Height - %d",
  909. decoder->fmt_list[ifmt].description, pix->bytesperline,
  910. pix->width, pix->height);
  911. return 0;
  912. }
  913. /**
  914. * ioctl_s_fmt_cap - V4L2 decoder interface handler for VIDIOC_S_FMT ioctl
  915. * @s: pointer to standard V4L2 device structure
  916. * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
  917. *
  918. * If the requested format is supported, configures the HW to use that
  919. * format, returns error code if format not supported or HW can't be
  920. * correctly configured.
  921. */
  922. static int
  923. ioctl_s_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
  924. {
  925. struct tvp514x_decoder *decoder = s->priv;
  926. struct v4l2_pix_format *pix;
  927. int rval;
  928. if (f == NULL)
  929. return -EINVAL;
  930. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  931. return -EINVAL; /* only capture is supported */
  932. pix = &f->fmt.pix;
  933. rval = ioctl_try_fmt_cap(s, f);
  934. if (rval)
  935. return rval;
  936. decoder->pix = *pix;
  937. return rval;
  938. }
  939. /**
  940. * ioctl_g_fmt_cap - V4L2 decoder interface handler for ioctl_g_fmt_cap
  941. * @s: pointer to standard V4L2 device structure
  942. * @f: pointer to standard V4L2 v4l2_format structure
  943. *
  944. * Returns the decoder's current pixel format in the v4l2_format
  945. * parameter.
  946. */
  947. static int
  948. ioctl_g_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
  949. {
  950. struct tvp514x_decoder *decoder = s->priv;
  951. if (f == NULL)
  952. return -EINVAL;
  953. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  954. return -EINVAL; /* only capture is supported */
  955. f->fmt.pix = decoder->pix;
  956. v4l_dbg(1, debug, decoder->client,
  957. "Current FMT: bytesperline - %d"
  958. "Width - %d, Height - %d",
  959. decoder->pix.bytesperline,
  960. decoder->pix.width, decoder->pix.height);
  961. return 0;
  962. }
  963. /**
  964. * ioctl_g_parm - V4L2 decoder interface handler for VIDIOC_G_PARM ioctl
  965. * @s: pointer to standard V4L2 device structure
  966. * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
  967. *
  968. * Returns the decoder's video CAPTURE parameters.
  969. */
  970. static int
  971. ioctl_g_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
  972. {
  973. struct tvp514x_decoder *decoder = s->priv;
  974. struct v4l2_captureparm *cparm;
  975. enum tvp514x_std current_std;
  976. if (a == NULL)
  977. return -EINVAL;
  978. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  979. return -EINVAL; /* only capture is supported */
  980. memset(a, 0, sizeof(*a));
  981. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  982. /* get the current standard */
  983. current_std = tvp514x_get_current_std(decoder);
  984. if (current_std == STD_INVALID)
  985. return -EINVAL;
  986. decoder->current_std = current_std;
  987. cparm = &a->parm.capture;
  988. cparm->capability = V4L2_CAP_TIMEPERFRAME;
  989. cparm->timeperframe =
  990. decoder->std_list[current_std].standard.frameperiod;
  991. return 0;
  992. }
  993. /**
  994. * ioctl_s_parm - V4L2 decoder interface handler for VIDIOC_S_PARM ioctl
  995. * @s: pointer to standard V4L2 device structure
  996. * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
  997. *
  998. * Configures the decoder to use the input parameters, if possible. If
  999. * not possible, returns the appropriate error code.
  1000. */
  1001. static int
  1002. ioctl_s_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
  1003. {
  1004. struct tvp514x_decoder *decoder = s->priv;
  1005. struct v4l2_fract *timeperframe;
  1006. enum tvp514x_std current_std;
  1007. if (a == NULL)
  1008. return -EINVAL;
  1009. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1010. return -EINVAL; /* only capture is supported */
  1011. timeperframe = &a->parm.capture.timeperframe;
  1012. /* get the current standard */
  1013. current_std = tvp514x_get_current_std(decoder);
  1014. if (current_std == STD_INVALID)
  1015. return -EINVAL;
  1016. decoder->current_std = current_std;
  1017. *timeperframe =
  1018. decoder->std_list[current_std].standard.frameperiod;
  1019. return 0;
  1020. }
  1021. /**
  1022. * ioctl_g_ifparm - V4L2 decoder interface handler for vidioc_int_g_ifparm_num
  1023. * @s: pointer to standard V4L2 device structure
  1024. * @p: pointer to standard V4L2 vidioc_int_g_ifparm_num ioctl structure
  1025. *
  1026. * Gets slave interface parameters.
  1027. * Calculates the required xclk value to support the requested
  1028. * clock parameters in p. This value is returned in the p
  1029. * parameter.
  1030. */
  1031. static int ioctl_g_ifparm(struct v4l2_int_device *s, struct v4l2_ifparm *p)
  1032. {
  1033. struct tvp514x_decoder *decoder = s->priv;
  1034. int rval;
  1035. if (p == NULL)
  1036. return -EINVAL;
  1037. if (NULL == decoder->pdata->ifparm)
  1038. return -EINVAL;
  1039. rval = decoder->pdata->ifparm(p);
  1040. if (rval) {
  1041. v4l_err(decoder->client, "g_ifparm.Err[%d]\n", rval);
  1042. return rval;
  1043. }
  1044. p->u.bt656.clock_curr = TVP514X_XCLK_BT656;
  1045. return 0;
  1046. }
  1047. /**
  1048. * ioctl_g_priv - V4L2 decoder interface handler for vidioc_int_g_priv_num
  1049. * @s: pointer to standard V4L2 device structure
  1050. * @p: void pointer to hold decoder's private data address
  1051. *
  1052. * Returns device's (decoder's) private data area address in p parameter
  1053. */
  1054. static int ioctl_g_priv(struct v4l2_int_device *s, void *p)
  1055. {
  1056. struct tvp514x_decoder *decoder = s->priv;
  1057. if (NULL == decoder->pdata->priv_data_set)
  1058. return -EINVAL;
  1059. return decoder->pdata->priv_data_set(p);
  1060. }
  1061. /**
  1062. * ioctl_s_power - V4L2 decoder interface handler for vidioc_int_s_power_num
  1063. * @s: pointer to standard V4L2 device structure
  1064. * @on: power state to which device is to be set
  1065. *
  1066. * Sets devices power state to requrested state, if possible.
  1067. */
  1068. static int ioctl_s_power(struct v4l2_int_device *s, enum v4l2_power on)
  1069. {
  1070. struct tvp514x_decoder *decoder = s->priv;
  1071. int err = 0;
  1072. switch (on) {
  1073. case V4L2_POWER_OFF:
  1074. /* Power Down Sequence */
  1075. err =
  1076. tvp514x_write_reg(decoder->client, REG_OPERATION_MODE,
  1077. 0x01);
  1078. /* Disable mux for TVP5146/47 decoder data path */
  1079. if (decoder->pdata->power_set)
  1080. err |= decoder->pdata->power_set(on);
  1081. decoder->state = STATE_NOT_DETECTED;
  1082. break;
  1083. case V4L2_POWER_STANDBY:
  1084. if (decoder->pdata->power_set)
  1085. err = decoder->pdata->power_set(on);
  1086. break;
  1087. case V4L2_POWER_ON:
  1088. /* Enable mux for TVP5146/47 decoder data path */
  1089. if ((decoder->pdata->power_set) &&
  1090. (decoder->state == STATE_NOT_DETECTED)) {
  1091. int i;
  1092. struct tvp514x_init_seq *int_seq =
  1093. (struct tvp514x_init_seq *)
  1094. decoder->id->driver_data;
  1095. err = decoder->pdata->power_set(on);
  1096. /* Power Up Sequence */
  1097. for (i = 0; i < int_seq->no_regs; i++) {
  1098. err |= tvp514x_write_reg(decoder->client,
  1099. int_seq->init_reg_seq[i].reg,
  1100. int_seq->init_reg_seq[i].val);
  1101. }
  1102. /* Detect the sensor is not already detected */
  1103. err |= tvp514x_detect(decoder);
  1104. if (err) {
  1105. v4l_err(decoder->client,
  1106. "Unable to detect decoder\n");
  1107. return err;
  1108. }
  1109. }
  1110. err |= tvp514x_configure(decoder);
  1111. break;
  1112. default:
  1113. err = -ENODEV;
  1114. break;
  1115. }
  1116. return err;
  1117. }
  1118. /**
  1119. * ioctl_init - V4L2 decoder interface handler for VIDIOC_INT_INIT
  1120. * @s: pointer to standard V4L2 device structure
  1121. *
  1122. * Initialize the decoder device (calls tvp514x_configure())
  1123. */
  1124. static int ioctl_init(struct v4l2_int_device *s)
  1125. {
  1126. struct tvp514x_decoder *decoder = s->priv;
  1127. /* Set default standard to auto */
  1128. decoder->tvp514x_regs[REG_VIDEO_STD].val =
  1129. VIDEO_STD_AUTO_SWITCH_BIT;
  1130. return tvp514x_configure(decoder);
  1131. }
  1132. /**
  1133. * ioctl_dev_exit - V4L2 decoder interface handler for vidioc_int_dev_exit_num
  1134. * @s: pointer to standard V4L2 device structure
  1135. *
  1136. * Delinitialise the dev. at slave detach. The complement of ioctl_dev_init.
  1137. */
  1138. static int ioctl_dev_exit(struct v4l2_int_device *s)
  1139. {
  1140. return 0;
  1141. }
  1142. /**
  1143. * ioctl_dev_init - V4L2 decoder interface handler for vidioc_int_dev_init_num
  1144. * @s: pointer to standard V4L2 device structure
  1145. *
  1146. * Initialise the device when slave attaches to the master. Returns 0 if
  1147. * TVP5146/47 device could be found, otherwise returns appropriate error.
  1148. */
  1149. static int ioctl_dev_init(struct v4l2_int_device *s)
  1150. {
  1151. struct tvp514x_decoder *decoder = s->priv;
  1152. int err;
  1153. err = tvp514x_detect(decoder);
  1154. if (err < 0) {
  1155. v4l_err(decoder->client,
  1156. "Unable to detect decoder\n");
  1157. return err;
  1158. }
  1159. v4l_info(decoder->client,
  1160. "chip version 0x%.2x detected\n", decoder->ver);
  1161. return 0;
  1162. }
  1163. static struct v4l2_int_ioctl_desc tvp514x_ioctl_desc[] = {
  1164. {vidioc_int_dev_init_num, (v4l2_int_ioctl_func*) ioctl_dev_init},
  1165. {vidioc_int_dev_exit_num, (v4l2_int_ioctl_func*) ioctl_dev_exit},
  1166. {vidioc_int_s_power_num, (v4l2_int_ioctl_func*) ioctl_s_power},
  1167. {vidioc_int_g_priv_num, (v4l2_int_ioctl_func*) ioctl_g_priv},
  1168. {vidioc_int_g_ifparm_num, (v4l2_int_ioctl_func*) ioctl_g_ifparm},
  1169. {vidioc_int_init_num, (v4l2_int_ioctl_func*) ioctl_init},
  1170. {vidioc_int_enum_fmt_cap_num,
  1171. (v4l2_int_ioctl_func *) ioctl_enum_fmt_cap},
  1172. {vidioc_int_try_fmt_cap_num,
  1173. (v4l2_int_ioctl_func *) ioctl_try_fmt_cap},
  1174. {vidioc_int_g_fmt_cap_num,
  1175. (v4l2_int_ioctl_func *) ioctl_g_fmt_cap},
  1176. {vidioc_int_s_fmt_cap_num,
  1177. (v4l2_int_ioctl_func *) ioctl_s_fmt_cap},
  1178. {vidioc_int_g_parm_num, (v4l2_int_ioctl_func *) ioctl_g_parm},
  1179. {vidioc_int_s_parm_num, (v4l2_int_ioctl_func *) ioctl_s_parm},
  1180. {vidioc_int_queryctrl_num,
  1181. (v4l2_int_ioctl_func *) ioctl_queryctrl},
  1182. {vidioc_int_g_ctrl_num, (v4l2_int_ioctl_func *) ioctl_g_ctrl},
  1183. {vidioc_int_s_ctrl_num, (v4l2_int_ioctl_func *) ioctl_s_ctrl},
  1184. {vidioc_int_querystd_num, (v4l2_int_ioctl_func *) ioctl_querystd},
  1185. {vidioc_int_s_std_num, (v4l2_int_ioctl_func *) ioctl_s_std},
  1186. {vidioc_int_s_video_routing_num,
  1187. (v4l2_int_ioctl_func *) ioctl_s_routing},
  1188. };
  1189. static struct tvp514x_decoder tvp514x_dev = {
  1190. .state = STATE_NOT_DETECTED,
  1191. .fmt_list = tvp514x_fmt_list,
  1192. .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
  1193. .pix = { /* Default to NTSC 8-bit YUV 422 */
  1194. .width = NTSC_NUM_ACTIVE_PIXELS,
  1195. .height = NTSC_NUM_ACTIVE_LINES,
  1196. .pixelformat = V4L2_PIX_FMT_UYVY,
  1197. .field = V4L2_FIELD_INTERLACED,
  1198. .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
  1199. .sizeimage =
  1200. NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
  1201. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  1202. },
  1203. .current_std = STD_NTSC_MJ,
  1204. .std_list = tvp514x_std_list,
  1205. .num_stds = ARRAY_SIZE(tvp514x_std_list),
  1206. .v4l2_int_device = {
  1207. .module = THIS_MODULE,
  1208. .name = TVP514X_MODULE_NAME,
  1209. .type = v4l2_int_type_slave,
  1210. },
  1211. .tvp514x_slave = {
  1212. .ioctls = tvp514x_ioctl_desc,
  1213. .num_ioctls = ARRAY_SIZE(tvp514x_ioctl_desc),
  1214. },
  1215. };
  1216. /**
  1217. * tvp514x_probe - decoder driver i2c probe handler
  1218. * @client: i2c driver client device structure
  1219. *
  1220. * Register decoder as an i2c client device and V4L2
  1221. * device.
  1222. */
  1223. static int
  1224. tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1225. {
  1226. struct tvp514x_decoder *decoder;
  1227. int err;
  1228. /* Check if the adapter supports the needed features */
  1229. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1230. return -EIO;
  1231. decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
  1232. if (!decoder)
  1233. return -ENOMEM;
  1234. if (!client->dev.platform_data) {
  1235. v4l_err(client, "No platform data!!\n");
  1236. err = -ENODEV;
  1237. goto out_free;
  1238. }
  1239. *decoder = tvp514x_dev;
  1240. decoder->v4l2_int_device.priv = decoder;
  1241. decoder->pdata = client->dev.platform_data;
  1242. decoder->v4l2_int_device.u.slave = &decoder->tvp514x_slave;
  1243. memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
  1244. sizeof(tvp514x_reg_list_default));
  1245. /*
  1246. * Fetch platform specific data, and configure the
  1247. * tvp514x_reg_list[] accordingly. Since this is one
  1248. * time configuration, no need to preserve.
  1249. */
  1250. decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
  1251. (decoder->pdata->clk_polarity << 1);
  1252. decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
  1253. ((decoder->pdata->hs_polarity << 2) |
  1254. (decoder->pdata->vs_polarity << 3));
  1255. /*
  1256. * Save the id data, required for power up sequence
  1257. */
  1258. decoder->id = (struct i2c_device_id *)id;
  1259. /* Attach to Master */
  1260. strcpy(decoder->v4l2_int_device.u.slave->attach_to,
  1261. decoder->pdata->master);
  1262. decoder->client = client;
  1263. i2c_set_clientdata(client, decoder);
  1264. /* Register with V4L2 layer as slave device */
  1265. err = v4l2_int_device_register(&decoder->v4l2_int_device);
  1266. if (err) {
  1267. i2c_set_clientdata(client, NULL);
  1268. v4l_err(client,
  1269. "Unable to register to v4l2. Err[%d]\n", err);
  1270. goto out_free;
  1271. } else
  1272. v4l_info(client, "Registered to v4l2 master %s!!\n",
  1273. decoder->pdata->master);
  1274. return 0;
  1275. out_free:
  1276. kfree(decoder);
  1277. return err;
  1278. }
  1279. /**
  1280. * tvp514x_remove - decoder driver i2c remove handler
  1281. * @client: i2c driver client device structure
  1282. *
  1283. * Unregister decoder as an i2c client device and V4L2
  1284. * device. Complement of tvp514x_probe().
  1285. */
  1286. static int __exit tvp514x_remove(struct i2c_client *client)
  1287. {
  1288. struct tvp514x_decoder *decoder = i2c_get_clientdata(client);
  1289. if (!client->adapter)
  1290. return -ENODEV; /* our client isn't attached */
  1291. v4l2_int_device_unregister(&decoder->v4l2_int_device);
  1292. i2c_set_clientdata(client, NULL);
  1293. kfree(decoder);
  1294. return 0;
  1295. }
  1296. /*
  1297. * TVP5146 Init/Power on Sequence
  1298. */
  1299. static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
  1300. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1301. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1302. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1303. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1304. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1305. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1306. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1307. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1308. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1309. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1310. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1311. };
  1312. static const struct tvp514x_init_seq tvp5146_init = {
  1313. .no_regs = ARRAY_SIZE(tvp5146_init_reg_seq),
  1314. .init_reg_seq = tvp5146_init_reg_seq,
  1315. };
  1316. /*
  1317. * TVP5147 Init/Power on Sequence
  1318. */
  1319. static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
  1320. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
  1321. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1322. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
  1323. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1324. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1325. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1326. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1327. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
  1328. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
  1329. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1330. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
  1331. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
  1332. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
  1333. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
  1334. {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
  1335. {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
  1336. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1337. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1338. };
  1339. static const struct tvp514x_init_seq tvp5147_init = {
  1340. .no_regs = ARRAY_SIZE(tvp5147_init_reg_seq),
  1341. .init_reg_seq = tvp5147_init_reg_seq,
  1342. };
  1343. /*
  1344. * TVP5146M2/TVP5147M1 Init/Power on Sequence
  1345. */
  1346. static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
  1347. {TOK_WRITE, REG_OPERATION_MODE, 0x01},
  1348. {TOK_WRITE, REG_OPERATION_MODE, 0x00},
  1349. };
  1350. static const struct tvp514x_init_seq tvp514xm_init = {
  1351. .no_regs = ARRAY_SIZE(tvp514xm_init_reg_seq),
  1352. .init_reg_seq = tvp514xm_init_reg_seq,
  1353. };
  1354. /*
  1355. * I2C Device Table -
  1356. *
  1357. * name - Name of the actual device/chip.
  1358. * driver_data - Driver data
  1359. */
  1360. static const struct i2c_device_id tvp514x_id[] = {
  1361. {"tvp5146", (unsigned long)&tvp5146_init},
  1362. {"tvp5146m2", (unsigned long)&tvp514xm_init},
  1363. {"tvp5147", (unsigned long)&tvp5147_init},
  1364. {"tvp5147m1", (unsigned long)&tvp514xm_init},
  1365. {},
  1366. };
  1367. MODULE_DEVICE_TABLE(i2c, tvp514x_id);
  1368. static struct i2c_driver tvp514x_i2c_driver = {
  1369. .driver = {
  1370. .name = TVP514X_MODULE_NAME,
  1371. .owner = THIS_MODULE,
  1372. },
  1373. .probe = tvp514x_probe,
  1374. .remove = __exit_p(tvp514x_remove),
  1375. .id_table = tvp514x_id,
  1376. };
  1377. /**
  1378. * tvp514x_init
  1379. *
  1380. * Module init function
  1381. */
  1382. static int __init tvp514x_init(void)
  1383. {
  1384. return i2c_add_driver(&tvp514x_i2c_driver);
  1385. }
  1386. /**
  1387. * tvp514x_cleanup
  1388. *
  1389. * Module exit function
  1390. */
  1391. static void __exit tvp514x_cleanup(void)
  1392. {
  1393. i2c_del_driver(&tvp514x_i2c_driver);
  1394. }
  1395. module_init(tvp514x_init);
  1396. module_exit(tvp514x_cleanup);
  1397. MODULE_AUTHOR("Texas Instruments");
  1398. MODULE_DESCRIPTION("TVP514X linux decoder driver");
  1399. MODULE_LICENSE("GPL");