tda1997x.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Gateworks Corporation
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/hdmi.h>
  7. #include <linux/i2c.h>
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/types.h>
  16. #include <linux/v4l2-dv-timings.h>
  17. #include <linux/videodev2.h>
  18. #include <media/v4l2-ctrls.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-dv-timings.h>
  21. #include <media/v4l2-event.h>
  22. #include <media/v4l2-fwnode.h>
  23. #include <media/i2c/tda1997x.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <dt-bindings/media/tda1997x.h>
  29. #include "tda1997x_regs.h"
  30. #define TDA1997X_MBUS_CODES 5
  31. /* debug level */
  32. static int debug;
  33. module_param(debug, int, 0644);
  34. MODULE_PARM_DESC(debug, "debug level (0-2)");
  35. /* Audio formats */
  36. static const char * const audtype_names[] = {
  37. "PCM", /* PCM Samples */
  38. "HBR", /* High Bit Rate Audio */
  39. "OBA", /* One-Bit Audio */
  40. "DST" /* Direct Stream Transfer */
  41. };
  42. /* Audio output port formats */
  43. enum audfmt_types {
  44. AUDFMT_TYPE_DISABLED = 0,
  45. AUDFMT_TYPE_I2S,
  46. AUDFMT_TYPE_SPDIF,
  47. };
  48. static const char * const audfmt_names[] = {
  49. "Disabled",
  50. "I2S",
  51. "SPDIF",
  52. };
  53. /* Video input formats */
  54. static const char * const hdmi_colorspace_names[] = {
  55. "RGB", "YUV422", "YUV444", "YUV420", "", "", "", "",
  56. };
  57. static const char * const hdmi_colorimetry_names[] = {
  58. "", "ITU601", "ITU709", "Extended",
  59. };
  60. static const char * const v4l2_quantization_names[] = {
  61. "Default",
  62. "Full Range (0-255)",
  63. "Limited Range (16-235)",
  64. };
  65. /* Video output port formats */
  66. static const char * const vidfmt_names[] = {
  67. "RGB444/YUV444", /* RGB/YUV444 16bit data bus, 8bpp */
  68. "YUV422 semi-planar", /* YUV422 16bit data base, 8bpp */
  69. "YUV422 CCIR656", /* BT656 (YUV 8bpp 2 clock per pixel) */
  70. "Invalid",
  71. };
  72. /*
  73. * Colorspace conversion matrices
  74. */
  75. struct color_matrix_coefs {
  76. const char *name;
  77. /* Input offsets */
  78. s16 offint1;
  79. s16 offint2;
  80. s16 offint3;
  81. /* Coeficients */
  82. s16 p11coef;
  83. s16 p12coef;
  84. s16 p13coef;
  85. s16 p21coef;
  86. s16 p22coef;
  87. s16 p23coef;
  88. s16 p31coef;
  89. s16 p32coef;
  90. s16 p33coef;
  91. /* Output offsets */
  92. s16 offout1;
  93. s16 offout2;
  94. s16 offout3;
  95. };
  96. enum {
  97. ITU709_RGBFULL,
  98. ITU601_RGBFULL,
  99. RGBLIMITED_RGBFULL,
  100. RGBLIMITED_ITU601,
  101. RGBLIMITED_ITU709,
  102. RGBFULL_ITU601,
  103. RGBFULL_ITU709,
  104. };
  105. /* NB: 4096 is 1.0 using fixed point numbers */
  106. static const struct color_matrix_coefs conv_matrix[] = {
  107. {
  108. "YUV709 -> RGB full",
  109. -256, -2048, -2048,
  110. 4769, -2183, -873,
  111. 4769, 7343, 0,
  112. 4769, 0, 8652,
  113. 0, 0, 0,
  114. },
  115. {
  116. "YUV601 -> RGB full",
  117. -256, -2048, -2048,
  118. 4769, -3330, -1602,
  119. 4769, 6538, 0,
  120. 4769, 0, 8264,
  121. 256, 256, 256,
  122. },
  123. {
  124. "RGB limited -> RGB full",
  125. -256, -256, -256,
  126. 0, 4769, 0,
  127. 0, 0, 4769,
  128. 4769, 0, 0,
  129. 0, 0, 0,
  130. },
  131. {
  132. "RGB limited -> ITU601",
  133. -256, -256, -256,
  134. 2404, 1225, 467,
  135. -1754, 2095, -341,
  136. -1388, -707, 2095,
  137. 256, 2048, 2048,
  138. },
  139. {
  140. "RGB limited -> ITU709",
  141. -256, -256, -256,
  142. 2918, 867, 295,
  143. -1894, 2087, -190,
  144. -1607, -477, 2087,
  145. 256, 2048, 2048,
  146. },
  147. {
  148. "RGB full -> ITU601",
  149. 0, 0, 0,
  150. 2065, 1052, 401,
  151. -1506, 1799, -293,
  152. -1192, -607, 1799,
  153. 256, 2048, 2048,
  154. },
  155. {
  156. "RGB full -> ITU709",
  157. 0, 0, 0,
  158. 2506, 745, 253,
  159. -1627, 1792, -163,
  160. -1380, -410, 1792,
  161. 256, 2048, 2048,
  162. },
  163. };
  164. static const struct v4l2_dv_timings_cap tda1997x_dv_timings_cap = {
  165. .type = V4L2_DV_BT_656_1120,
  166. /* keep this initialization for compatibility with GCC < 4.4.6 */
  167. .reserved = { 0 },
  168. V4L2_INIT_BT_TIMINGS(
  169. 640, 1920, /* min/max width */
  170. 350, 1200, /* min/max height */
  171. 13000000, 165000000, /* min/max pixelclock */
  172. /* standards */
  173. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  174. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  175. /* capabilities */
  176. V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE |
  177. V4L2_DV_BT_CAP_REDUCED_BLANKING |
  178. V4L2_DV_BT_CAP_CUSTOM
  179. )
  180. };
  181. /* regulator supplies */
  182. static const char * const tda1997x_supply_name[] = {
  183. "DOVDD", /* Digital I/O supply */
  184. "DVDD", /* Digital Core supply */
  185. "AVDD", /* Analog supply */
  186. };
  187. #define TDA1997X_NUM_SUPPLIES ARRAY_SIZE(tda1997x_supply_name)
  188. enum tda1997x_type {
  189. TDA19971,
  190. TDA19973,
  191. };
  192. enum tda1997x_hdmi_pads {
  193. TDA1997X_PAD_SOURCE,
  194. TDA1997X_NUM_PADS,
  195. };
  196. struct tda1997x_chip_info {
  197. enum tda1997x_type type;
  198. const char *name;
  199. };
  200. struct tda1997x_state {
  201. const struct tda1997x_chip_info *info;
  202. struct tda1997x_platform_data pdata;
  203. struct i2c_client *client;
  204. struct i2c_client *client_cec;
  205. struct v4l2_subdev sd;
  206. struct regulator_bulk_data supplies[TDA1997X_NUM_SUPPLIES];
  207. struct media_pad pads[TDA1997X_NUM_PADS];
  208. struct mutex lock;
  209. struct mutex page_lock;
  210. char page;
  211. /* detected info from chip */
  212. int chip_revision;
  213. char port_30bit;
  214. char output_2p5;
  215. char tmdsb_clk;
  216. char tmdsb_soc;
  217. /* status info */
  218. char hdmi_status;
  219. char mptrw_in_progress;
  220. char activity_status;
  221. char input_detect[2];
  222. /* video */
  223. struct hdmi_avi_infoframe avi_infoframe;
  224. struct v4l2_hdmi_colorimetry colorimetry;
  225. u32 rgb_quantization_range;
  226. struct v4l2_dv_timings timings;
  227. int fps;
  228. const struct color_matrix_coefs *conv;
  229. u32 mbus_codes[TDA1997X_MBUS_CODES]; /* available modes */
  230. u32 mbus_code; /* current mode */
  231. u8 vid_fmt;
  232. /* controls */
  233. struct v4l2_ctrl_handler hdl;
  234. struct v4l2_ctrl *detect_tx_5v_ctrl;
  235. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  236. /* audio */
  237. u8 audio_ch_alloc;
  238. int audio_samplerate;
  239. int audio_channels;
  240. int audio_samplesize;
  241. int audio_type;
  242. struct mutex audio_lock;
  243. struct snd_pcm_substream *audio_stream;
  244. /* EDID */
  245. struct {
  246. u8 edid[256];
  247. u32 present;
  248. unsigned int blocks;
  249. } edid;
  250. struct delayed_work delayed_work_enable_hpd;
  251. };
  252. static const struct v4l2_event tda1997x_ev_fmt = {
  253. .type = V4L2_EVENT_SOURCE_CHANGE,
  254. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  255. };
  256. static const struct tda1997x_chip_info tda1997x_chip_info[] = {
  257. [TDA19971] = {
  258. .type = TDA19971,
  259. .name = "tda19971",
  260. },
  261. [TDA19973] = {
  262. .type = TDA19973,
  263. .name = "tda19973",
  264. },
  265. };
  266. static inline struct tda1997x_state *to_state(struct v4l2_subdev *sd)
  267. {
  268. return container_of(sd, struct tda1997x_state, sd);
  269. }
  270. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  271. {
  272. return &container_of(ctrl->handler, struct tda1997x_state, hdl)->sd;
  273. }
  274. static int tda1997x_cec_read(struct v4l2_subdev *sd, u8 reg)
  275. {
  276. struct tda1997x_state *state = to_state(sd);
  277. int val;
  278. val = i2c_smbus_read_byte_data(state->client_cec, reg);
  279. if (val < 0) {
  280. v4l_err(state->client, "read reg error: reg=%2x\n", reg);
  281. val = -1;
  282. }
  283. return val;
  284. }
  285. static int tda1997x_cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  286. {
  287. struct tda1997x_state *state = to_state(sd);
  288. int ret = 0;
  289. ret = i2c_smbus_write_byte_data(state->client_cec, reg, val);
  290. if (ret < 0) {
  291. v4l_err(state->client, "write reg error:reg=%2x,val=%2x\n",
  292. reg, val);
  293. ret = -1;
  294. }
  295. return ret;
  296. }
  297. /* -----------------------------------------------------------------------------
  298. * I2C transfer
  299. */
  300. static int tda1997x_setpage(struct v4l2_subdev *sd, u8 page)
  301. {
  302. struct tda1997x_state *state = to_state(sd);
  303. int ret;
  304. if (state->page != page) {
  305. ret = i2c_smbus_write_byte_data(state->client,
  306. REG_CURPAGE_00H, page);
  307. if (ret < 0) {
  308. v4l_err(state->client,
  309. "write reg error:reg=%2x,val=%2x\n",
  310. REG_CURPAGE_00H, page);
  311. return ret;
  312. }
  313. state->page = page;
  314. }
  315. return 0;
  316. }
  317. static inline int io_read(struct v4l2_subdev *sd, u16 reg)
  318. {
  319. struct tda1997x_state *state = to_state(sd);
  320. int val;
  321. mutex_lock(&state->page_lock);
  322. if (tda1997x_setpage(sd, reg >> 8)) {
  323. val = -1;
  324. goto out;
  325. }
  326. val = i2c_smbus_read_byte_data(state->client, reg&0xff);
  327. if (val < 0) {
  328. v4l_err(state->client, "read reg error: reg=%2x\n", reg & 0xff);
  329. val = -1;
  330. goto out;
  331. }
  332. out:
  333. mutex_unlock(&state->page_lock);
  334. return val;
  335. }
  336. static inline long io_read16(struct v4l2_subdev *sd, u16 reg)
  337. {
  338. int val;
  339. long lval = 0;
  340. val = io_read(sd, reg);
  341. if (val < 0)
  342. return val;
  343. lval |= (val << 8);
  344. val = io_read(sd, reg + 1);
  345. if (val < 0)
  346. return val;
  347. lval |= val;
  348. return lval;
  349. }
  350. static inline long io_read24(struct v4l2_subdev *sd, u16 reg)
  351. {
  352. int val;
  353. long lval = 0;
  354. val = io_read(sd, reg);
  355. if (val < 0)
  356. return val;
  357. lval |= (val << 16);
  358. val = io_read(sd, reg + 1);
  359. if (val < 0)
  360. return val;
  361. lval |= (val << 8);
  362. val = io_read(sd, reg + 2);
  363. if (val < 0)
  364. return val;
  365. lval |= val;
  366. return lval;
  367. }
  368. static unsigned int io_readn(struct v4l2_subdev *sd, u16 reg, u8 len, u8 *data)
  369. {
  370. int i;
  371. int sz = 0;
  372. int val;
  373. for (i = 0; i < len; i++) {
  374. val = io_read(sd, reg + i);
  375. if (val < 0)
  376. break;
  377. data[i] = val;
  378. sz++;
  379. }
  380. return sz;
  381. }
  382. static int io_write(struct v4l2_subdev *sd, u16 reg, u8 val)
  383. {
  384. struct tda1997x_state *state = to_state(sd);
  385. s32 ret = 0;
  386. mutex_lock(&state->page_lock);
  387. if (tda1997x_setpage(sd, reg >> 8)) {
  388. ret = -1;
  389. goto out;
  390. }
  391. ret = i2c_smbus_write_byte_data(state->client, reg & 0xff, val);
  392. if (ret < 0) {
  393. v4l_err(state->client, "write reg error:reg=%2x,val=%2x\n",
  394. reg&0xff, val);
  395. ret = -1;
  396. goto out;
  397. }
  398. out:
  399. mutex_unlock(&state->page_lock);
  400. return ret;
  401. }
  402. static int io_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
  403. {
  404. int ret;
  405. ret = io_write(sd, reg, (val >> 8) & 0xff);
  406. if (ret < 0)
  407. return ret;
  408. ret = io_write(sd, reg + 1, val & 0xff);
  409. if (ret < 0)
  410. return ret;
  411. return 0;
  412. }
  413. static int io_write24(struct v4l2_subdev *sd, u16 reg, u32 val)
  414. {
  415. int ret;
  416. ret = io_write(sd, reg, (val >> 16) & 0xff);
  417. if (ret < 0)
  418. return ret;
  419. ret = io_write(sd, reg + 1, (val >> 8) & 0xff);
  420. if (ret < 0)
  421. return ret;
  422. ret = io_write(sd, reg + 2, val & 0xff);
  423. if (ret < 0)
  424. return ret;
  425. return 0;
  426. }
  427. /* -----------------------------------------------------------------------------
  428. * Hotplug
  429. */
  430. enum hpd_mode {
  431. HPD_LOW_BP, /* HPD low and pulse of at least 100ms */
  432. HPD_LOW_OTHER, /* HPD low and pulse of at least 100ms */
  433. HPD_HIGH_BP, /* HIGH */
  434. HPD_HIGH_OTHER,
  435. HPD_PULSE, /* HPD low pulse */
  436. };
  437. /* manual HPD (Hot Plug Detect) control */
  438. static int tda1997x_manual_hpd(struct v4l2_subdev *sd, enum hpd_mode mode)
  439. {
  440. u8 hpd_auto, hpd_pwr, hpd_man;
  441. hpd_auto = io_read(sd, REG_HPD_AUTO_CTRL);
  442. hpd_pwr = io_read(sd, REG_HPD_POWER);
  443. hpd_man = io_read(sd, REG_HPD_MAN_CTRL);
  444. /* mask out unused bits */
  445. hpd_man &= (HPD_MAN_CTRL_HPD_PULSE |
  446. HPD_MAN_CTRL_5VEN |
  447. HPD_MAN_CTRL_HPD_B |
  448. HPD_MAN_CTRL_HPD_A);
  449. switch (mode) {
  450. /* HPD low and pulse of at least 100ms */
  451. case HPD_LOW_BP:
  452. /* hpd_bp=0 */
  453. hpd_pwr &= ~HPD_POWER_BP_MASK;
  454. /* disable HPD_A and HPD_B */
  455. hpd_man &= ~(HPD_MAN_CTRL_HPD_A | HPD_MAN_CTRL_HPD_B);
  456. io_write(sd, REG_HPD_POWER, hpd_pwr);
  457. io_write(sd, REG_HPD_MAN_CTRL, hpd_man);
  458. break;
  459. /* HPD high */
  460. case HPD_HIGH_BP:
  461. /* hpd_bp=1 */
  462. hpd_pwr &= ~HPD_POWER_BP_MASK;
  463. hpd_pwr |= 1 << HPD_POWER_BP_SHIFT;
  464. io_write(sd, REG_HPD_POWER, hpd_pwr);
  465. break;
  466. /* HPD low and pulse of at least 100ms */
  467. case HPD_LOW_OTHER:
  468. /* disable HPD_A and HPD_B */
  469. hpd_man &= ~(HPD_MAN_CTRL_HPD_A | HPD_MAN_CTRL_HPD_B);
  470. /* hp_other=0 */
  471. hpd_auto &= ~HPD_AUTO_HP_OTHER;
  472. io_write(sd, REG_HPD_AUTO_CTRL, hpd_auto);
  473. io_write(sd, REG_HPD_MAN_CTRL, hpd_man);
  474. break;
  475. /* HPD high */
  476. case HPD_HIGH_OTHER:
  477. hpd_auto |= HPD_AUTO_HP_OTHER;
  478. io_write(sd, REG_HPD_AUTO_CTRL, hpd_auto);
  479. break;
  480. /* HPD low pulse */
  481. case HPD_PULSE:
  482. /* disable HPD_A and HPD_B */
  483. hpd_man &= ~(HPD_MAN_CTRL_HPD_A | HPD_MAN_CTRL_HPD_B);
  484. io_write(sd, REG_HPD_MAN_CTRL, hpd_man);
  485. break;
  486. }
  487. return 0;
  488. }
  489. static void tda1997x_delayed_work_enable_hpd(struct work_struct *work)
  490. {
  491. struct delayed_work *dwork = to_delayed_work(work);
  492. struct tda1997x_state *state = container_of(dwork,
  493. struct tda1997x_state,
  494. delayed_work_enable_hpd);
  495. struct v4l2_subdev *sd = &state->sd;
  496. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  497. /* Set HPD high */
  498. tda1997x_manual_hpd(sd, HPD_HIGH_OTHER);
  499. tda1997x_manual_hpd(sd, HPD_HIGH_BP);
  500. state->edid.present = 1;
  501. }
  502. static void tda1997x_disable_edid(struct v4l2_subdev *sd)
  503. {
  504. struct tda1997x_state *state = to_state(sd);
  505. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  506. cancel_delayed_work_sync(&state->delayed_work_enable_hpd);
  507. /* Set HPD low */
  508. tda1997x_manual_hpd(sd, HPD_LOW_BP);
  509. }
  510. static void tda1997x_enable_edid(struct v4l2_subdev *sd)
  511. {
  512. struct tda1997x_state *state = to_state(sd);
  513. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  514. /* Enable hotplug after 143ms */
  515. schedule_delayed_work(&state->delayed_work_enable_hpd, HZ / 7);
  516. }
  517. /* -----------------------------------------------------------------------------
  518. * Signal Control
  519. */
  520. /*
  521. * configure vid_fmt based on mbus_code
  522. */
  523. static int
  524. tda1997x_setup_format(struct tda1997x_state *state, u32 code)
  525. {
  526. v4l_dbg(1, debug, state->client, "%s code=0x%x\n", __func__, code);
  527. switch (code) {
  528. case MEDIA_BUS_FMT_RGB121212_1X36:
  529. case MEDIA_BUS_FMT_RGB888_1X24:
  530. case MEDIA_BUS_FMT_YUV12_1X36:
  531. case MEDIA_BUS_FMT_YUV8_1X24:
  532. state->vid_fmt = OF_FMT_444;
  533. break;
  534. case MEDIA_BUS_FMT_UYVY12_1X24:
  535. case MEDIA_BUS_FMT_UYVY10_1X20:
  536. case MEDIA_BUS_FMT_UYVY8_1X16:
  537. state->vid_fmt = OF_FMT_422_SMPT;
  538. break;
  539. case MEDIA_BUS_FMT_UYVY12_2X12:
  540. case MEDIA_BUS_FMT_UYVY10_2X10:
  541. case MEDIA_BUS_FMT_UYVY8_2X8:
  542. state->vid_fmt = OF_FMT_422_CCIR;
  543. break;
  544. default:
  545. v4l_err(state->client, "incompatible format (0x%x)\n", code);
  546. return -EINVAL;
  547. }
  548. v4l_dbg(1, debug, state->client, "%s code=0x%x fmt=%s\n", __func__,
  549. code, vidfmt_names[state->vid_fmt]);
  550. state->mbus_code = code;
  551. return 0;
  552. }
  553. /*
  554. * The color conversion matrix will convert between the colorimetry of the
  555. * HDMI input to the desired output format RGB|YUV. RGB output is to be
  556. * full-range and YUV is to be limited range.
  557. *
  558. * RGB full-range uses values from 0 to 255 which is recommended on a monitor
  559. * and RGB Limited uses values from 16 to 236 (16=black, 235=white) which is
  560. * typically recommended on a TV.
  561. */
  562. static void
  563. tda1997x_configure_csc(struct v4l2_subdev *sd)
  564. {
  565. struct tda1997x_state *state = to_state(sd);
  566. struct hdmi_avi_infoframe *avi = &state->avi_infoframe;
  567. struct v4l2_hdmi_colorimetry *c = &state->colorimetry;
  568. /* Blanking code values depend on output colorspace (RGB or YUV) */
  569. struct blanking_codes {
  570. s16 code_gy;
  571. s16 code_bu;
  572. s16 code_rv;
  573. };
  574. static const struct blanking_codes rgb_blanking = { 64, 64, 64 };
  575. static const struct blanking_codes yuv_blanking = { 64, 512, 512 };
  576. const struct blanking_codes *blanking_codes = NULL;
  577. u8 reg;
  578. v4l_dbg(1, debug, state->client, "input:%s quant:%s output:%s\n",
  579. hdmi_colorspace_names[avi->colorspace],
  580. v4l2_quantization_names[c->quantization],
  581. vidfmt_names[state->vid_fmt]);
  582. state->conv = NULL;
  583. switch (state->vid_fmt) {
  584. /* RGB output */
  585. case OF_FMT_444:
  586. blanking_codes = &rgb_blanking;
  587. if (c->colorspace == V4L2_COLORSPACE_SRGB) {
  588. if (c->quantization == V4L2_QUANTIZATION_LIM_RANGE)
  589. state->conv = &conv_matrix[RGBLIMITED_RGBFULL];
  590. } else {
  591. if (c->colorspace == V4L2_COLORSPACE_REC709)
  592. state->conv = &conv_matrix[ITU709_RGBFULL];
  593. else if (c->colorspace == V4L2_COLORSPACE_SMPTE170M)
  594. state->conv = &conv_matrix[ITU601_RGBFULL];
  595. }
  596. break;
  597. /* YUV output */
  598. case OF_FMT_422_SMPT: /* semi-planar */
  599. case OF_FMT_422_CCIR: /* CCIR656 */
  600. blanking_codes = &yuv_blanking;
  601. if ((c->colorspace == V4L2_COLORSPACE_SRGB) &&
  602. (c->quantization == V4L2_QUANTIZATION_FULL_RANGE)) {
  603. if (state->timings.bt.height <= 576)
  604. state->conv = &conv_matrix[RGBFULL_ITU601];
  605. else
  606. state->conv = &conv_matrix[RGBFULL_ITU709];
  607. } else if ((c->colorspace == V4L2_COLORSPACE_SRGB) &&
  608. (c->quantization == V4L2_QUANTIZATION_LIM_RANGE)) {
  609. if (state->timings.bt.height <= 576)
  610. state->conv = &conv_matrix[RGBLIMITED_ITU601];
  611. else
  612. state->conv = &conv_matrix[RGBLIMITED_ITU709];
  613. }
  614. break;
  615. }
  616. if (state->conv) {
  617. v4l_dbg(1, debug, state->client, "%s\n",
  618. state->conv->name);
  619. /* enable matrix conversion */
  620. reg = io_read(sd, REG_VDP_CTRL);
  621. reg &= ~VDP_CTRL_MATRIX_BP;
  622. io_write(sd, REG_VDP_CTRL, reg);
  623. /* offset inputs */
  624. io_write16(sd, REG_VDP_MATRIX + 0, state->conv->offint1);
  625. io_write16(sd, REG_VDP_MATRIX + 2, state->conv->offint2);
  626. io_write16(sd, REG_VDP_MATRIX + 4, state->conv->offint3);
  627. /* coefficients */
  628. io_write16(sd, REG_VDP_MATRIX + 6, state->conv->p11coef);
  629. io_write16(sd, REG_VDP_MATRIX + 8, state->conv->p12coef);
  630. io_write16(sd, REG_VDP_MATRIX + 10, state->conv->p13coef);
  631. io_write16(sd, REG_VDP_MATRIX + 12, state->conv->p21coef);
  632. io_write16(sd, REG_VDP_MATRIX + 14, state->conv->p22coef);
  633. io_write16(sd, REG_VDP_MATRIX + 16, state->conv->p23coef);
  634. io_write16(sd, REG_VDP_MATRIX + 18, state->conv->p31coef);
  635. io_write16(sd, REG_VDP_MATRIX + 20, state->conv->p32coef);
  636. io_write16(sd, REG_VDP_MATRIX + 22, state->conv->p33coef);
  637. /* offset outputs */
  638. io_write16(sd, REG_VDP_MATRIX + 24, state->conv->offout1);
  639. io_write16(sd, REG_VDP_MATRIX + 26, state->conv->offout2);
  640. io_write16(sd, REG_VDP_MATRIX + 28, state->conv->offout3);
  641. } else {
  642. /* disable matrix conversion */
  643. reg = io_read(sd, REG_VDP_CTRL);
  644. reg |= VDP_CTRL_MATRIX_BP;
  645. io_write(sd, REG_VDP_CTRL, reg);
  646. }
  647. /* SetBlankingCodes */
  648. if (blanking_codes) {
  649. io_write16(sd, REG_BLK_GY, blanking_codes->code_gy);
  650. io_write16(sd, REG_BLK_BU, blanking_codes->code_bu);
  651. io_write16(sd, REG_BLK_RV, blanking_codes->code_rv);
  652. }
  653. }
  654. /* Configure frame detection window and VHREF timing generator */
  655. static void
  656. tda1997x_configure_vhref(struct v4l2_subdev *sd)
  657. {
  658. struct tda1997x_state *state = to_state(sd);
  659. const struct v4l2_bt_timings *bt = &state->timings.bt;
  660. int width, lines;
  661. u16 href_start, href_end;
  662. u16 vref_f1_start, vref_f2_start;
  663. u8 vref_f1_width, vref_f2_width;
  664. u8 field_polarity;
  665. u16 fieldref_f1_start, fieldref_f2_start;
  666. u8 reg;
  667. href_start = bt->hbackporch + bt->hsync + 1;
  668. href_end = href_start + bt->width;
  669. vref_f1_start = bt->height + bt->vbackporch + bt->vsync +
  670. bt->il_vbackporch + bt->il_vsync +
  671. bt->il_vfrontporch;
  672. vref_f1_width = bt->vbackporch + bt->vsync + bt->vfrontporch;
  673. vref_f2_start = 0;
  674. vref_f2_width = 0;
  675. fieldref_f1_start = 0;
  676. fieldref_f2_start = 0;
  677. if (bt->interlaced) {
  678. vref_f2_start = (bt->height / 2) +
  679. (bt->il_vbackporch + bt->il_vsync - 1);
  680. vref_f2_width = bt->il_vbackporch + bt->il_vsync +
  681. bt->il_vfrontporch;
  682. fieldref_f2_start = vref_f2_start + bt->il_vfrontporch +
  683. fieldref_f1_start;
  684. }
  685. field_polarity = 0;
  686. width = V4L2_DV_BT_FRAME_WIDTH(bt);
  687. lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
  688. /*
  689. * Configure Frame Detection Window:
  690. * horiz area where the VHREF module consider a VSYNC a new frame
  691. */
  692. io_write16(sd, REG_FDW_S, 0x2ef); /* start position */
  693. io_write16(sd, REG_FDW_E, 0x141); /* end position */
  694. /* Set Pixel And Line Counters */
  695. if (state->chip_revision == 0)
  696. io_write16(sd, REG_PXCNT_PR, 4);
  697. else
  698. io_write16(sd, REG_PXCNT_PR, 1);
  699. io_write16(sd, REG_PXCNT_NPIX, width & MASK_VHREF);
  700. io_write16(sd, REG_LCNT_PR, 1);
  701. io_write16(sd, REG_LCNT_NLIN, lines & MASK_VHREF);
  702. /*
  703. * Configure the VHRef timing generator responsible for rebuilding all
  704. * horiz and vert synch and ref signals from its input allowing auto
  705. * detection algorithms and forcing predefined modes (480i & 576i)
  706. */
  707. reg = VHREF_STD_DET_OFF << VHREF_STD_DET_SHIFT;
  708. io_write(sd, REG_VHREF_CTRL, reg);
  709. /*
  710. * Configure the VHRef timing values. In case the VHREF generator has
  711. * been configured in manual mode, this will allow to manually set all
  712. * horiz and vert ref values (non-active pixel areas) of the generator
  713. * and allows setting the frame reference params.
  714. */
  715. /* horizontal reference start/end */
  716. io_write16(sd, REG_HREF_S, href_start & MASK_VHREF);
  717. io_write16(sd, REG_HREF_E, href_end & MASK_VHREF);
  718. /* vertical reference f1 start/end */
  719. io_write16(sd, REG_VREF_F1_S, vref_f1_start & MASK_VHREF);
  720. io_write(sd, REG_VREF_F1_WIDTH, vref_f1_width);
  721. /* vertical reference f2 start/end */
  722. io_write16(sd, REG_VREF_F2_S, vref_f2_start & MASK_VHREF);
  723. io_write(sd, REG_VREF_F2_WIDTH, vref_f2_width);
  724. /* F1/F2 FREF, field polarity */
  725. reg = fieldref_f1_start & MASK_VHREF;
  726. reg |= field_polarity << 8;
  727. io_write16(sd, REG_FREF_F1_S, reg);
  728. reg = fieldref_f2_start & MASK_VHREF;
  729. io_write16(sd, REG_FREF_F2_S, reg);
  730. }
  731. /* Configure Video Output port signals */
  732. static int
  733. tda1997x_configure_vidout(struct tda1997x_state *state)
  734. {
  735. struct v4l2_subdev *sd = &state->sd;
  736. struct tda1997x_platform_data *pdata = &state->pdata;
  737. u8 prefilter;
  738. u8 reg;
  739. /* Configure pixel clock generator: delay, polarity, rate */
  740. reg = (state->vid_fmt == OF_FMT_422_CCIR) ?
  741. PCLK_SEL_X2 : PCLK_SEL_X1;
  742. reg |= pdata->vidout_delay_pclk << PCLK_DELAY_SHIFT;
  743. reg |= pdata->vidout_inv_pclk << PCLK_INV_SHIFT;
  744. io_write(sd, REG_PCLK, reg);
  745. /* Configure pre-filter */
  746. prefilter = 0; /* filters off */
  747. /* YUV422 mode requires conversion */
  748. if ((state->vid_fmt == OF_FMT_422_SMPT) ||
  749. (state->vid_fmt == OF_FMT_422_CCIR)) {
  750. /* 2/7 taps for Rv and Bu */
  751. prefilter = FILTERS_CTRL_2_7TAP << FILTERS_CTRL_BU_SHIFT |
  752. FILTERS_CTRL_2_7TAP << FILTERS_CTRL_RV_SHIFT;
  753. }
  754. io_write(sd, REG_FILTERS_CTRL, prefilter);
  755. /* Configure video port */
  756. reg = state->vid_fmt & OF_FMT_MASK;
  757. if (state->vid_fmt == OF_FMT_422_CCIR)
  758. reg |= (OF_BLK | OF_TRC);
  759. reg |= OF_VP_ENABLE;
  760. io_write(sd, REG_OF, reg);
  761. /* Configure formatter and conversions */
  762. reg = io_read(sd, REG_VDP_CTRL);
  763. /* pre-filter is needed unless (REG_FILTERS_CTRL == 0) */
  764. if (!prefilter)
  765. reg |= VDP_CTRL_PREFILTER_BP;
  766. else
  767. reg &= ~VDP_CTRL_PREFILTER_BP;
  768. /* formatter is needed for YUV422 and for trc/blc codes */
  769. if (state->vid_fmt == OF_FMT_444)
  770. reg |= VDP_CTRL_FORMATTER_BP;
  771. /* formatter and compdel needed for timing/blanking codes */
  772. else
  773. reg &= ~(VDP_CTRL_FORMATTER_BP | VDP_CTRL_COMPDEL_BP);
  774. /* activate compdel for small sync delays */
  775. if ((pdata->vidout_delay_vs < 4) || (pdata->vidout_delay_hs < 4))
  776. reg &= ~VDP_CTRL_COMPDEL_BP;
  777. io_write(sd, REG_VDP_CTRL, reg);
  778. /* Configure DE output signal: delay, polarity, and source */
  779. reg = pdata->vidout_delay_de << DE_FREF_DELAY_SHIFT |
  780. pdata->vidout_inv_de << DE_FREF_INV_SHIFT |
  781. pdata->vidout_sel_de << DE_FREF_SEL_SHIFT;
  782. io_write(sd, REG_DE_FREF, reg);
  783. /* Configure HS/HREF output signal: delay, polarity, and source */
  784. if (state->vid_fmt != OF_FMT_422_CCIR) {
  785. reg = pdata->vidout_delay_hs << HS_HREF_DELAY_SHIFT |
  786. pdata->vidout_inv_hs << HS_HREF_INV_SHIFT |
  787. pdata->vidout_sel_hs << HS_HREF_SEL_SHIFT;
  788. } else
  789. reg = HS_HREF_SEL_NONE << HS_HREF_SEL_SHIFT;
  790. io_write(sd, REG_HS_HREF, reg);
  791. /* Configure VS/VREF output signal: delay, polarity, and source */
  792. if (state->vid_fmt != OF_FMT_422_CCIR) {
  793. reg = pdata->vidout_delay_vs << VS_VREF_DELAY_SHIFT |
  794. pdata->vidout_inv_vs << VS_VREF_INV_SHIFT |
  795. pdata->vidout_sel_vs << VS_VREF_SEL_SHIFT;
  796. } else
  797. reg = VS_VREF_SEL_NONE << VS_VREF_SEL_SHIFT;
  798. io_write(sd, REG_VS_VREF, reg);
  799. return 0;
  800. }
  801. /* Configure Audio output port signals */
  802. static int
  803. tda1997x_configure_audout(struct v4l2_subdev *sd, u8 channel_assignment)
  804. {
  805. struct tda1997x_state *state = to_state(sd);
  806. struct tda1997x_platform_data *pdata = &state->pdata;
  807. bool sp_used_by_fifo = true;
  808. u8 reg;
  809. if (!pdata->audout_format)
  810. return 0;
  811. /* channel assignment (CEA-861-D Table 20) */
  812. io_write(sd, REG_AUDIO_PATH, channel_assignment);
  813. /* Audio output configuration */
  814. reg = 0;
  815. switch (pdata->audout_format) {
  816. case AUDFMT_TYPE_I2S:
  817. reg |= AUDCFG_BUS_I2S << AUDCFG_BUS_SHIFT;
  818. break;
  819. case AUDFMT_TYPE_SPDIF:
  820. reg |= AUDCFG_BUS_SPDIF << AUDCFG_BUS_SHIFT;
  821. break;
  822. }
  823. switch (state->audio_type) {
  824. case AUDCFG_TYPE_PCM:
  825. reg |= AUDCFG_TYPE_PCM << AUDCFG_TYPE_SHIFT;
  826. break;
  827. case AUDCFG_TYPE_OBA:
  828. reg |= AUDCFG_TYPE_OBA << AUDCFG_TYPE_SHIFT;
  829. break;
  830. case AUDCFG_TYPE_DST:
  831. reg |= AUDCFG_TYPE_DST << AUDCFG_TYPE_SHIFT;
  832. sp_used_by_fifo = false;
  833. break;
  834. case AUDCFG_TYPE_HBR:
  835. reg |= AUDCFG_TYPE_HBR << AUDCFG_TYPE_SHIFT;
  836. if (pdata->audout_layout == 1) {
  837. /* demuxed via AP0:AP3 */
  838. reg |= AUDCFG_HBR_DEMUX << AUDCFG_HBR_SHIFT;
  839. if (pdata->audout_format == AUDFMT_TYPE_SPDIF)
  840. sp_used_by_fifo = false;
  841. } else {
  842. /* straight via AP0 */
  843. reg |= AUDCFG_HBR_STRAIGHT << AUDCFG_HBR_SHIFT;
  844. }
  845. break;
  846. }
  847. if (pdata->audout_width == 32)
  848. reg |= AUDCFG_I2SW_32 << AUDCFG_I2SW_SHIFT;
  849. else
  850. reg |= AUDCFG_I2SW_16 << AUDCFG_I2SW_SHIFT;
  851. /* automatic hardware mute */
  852. if (pdata->audio_auto_mute)
  853. reg |= AUDCFG_AUTO_MUTE_EN;
  854. /* clock polarity */
  855. if (pdata->audout_invert_clk)
  856. reg |= AUDCFG_CLK_INVERT;
  857. io_write(sd, REG_AUDCFG, reg);
  858. /* audio layout */
  859. reg = (pdata->audout_layout) ? AUDIO_LAYOUT_LAYOUT1 : 0;
  860. if (!pdata->audout_layoutauto)
  861. reg |= AUDIO_LAYOUT_MANUAL;
  862. if (sp_used_by_fifo)
  863. reg |= AUDIO_LAYOUT_SP_FLAG;
  864. io_write(sd, REG_AUDIO_LAYOUT, reg);
  865. /* FIFO Latency value */
  866. io_write(sd, REG_FIFO_LATENCY_VAL, 0x80);
  867. /* Audio output port config */
  868. if (sp_used_by_fifo) {
  869. reg = AUDIO_OUT_ENABLE_AP0;
  870. if (channel_assignment >= 0x01)
  871. reg |= AUDIO_OUT_ENABLE_AP1;
  872. if (channel_assignment >= 0x04)
  873. reg |= AUDIO_OUT_ENABLE_AP2;
  874. if (channel_assignment >= 0x0c)
  875. reg |= AUDIO_OUT_ENABLE_AP3;
  876. /* specific cases where AP1 is not used */
  877. if ((channel_assignment == 0x04)
  878. || (channel_assignment == 0x08)
  879. || (channel_assignment == 0x0c)
  880. || (channel_assignment == 0x10)
  881. || (channel_assignment == 0x14)
  882. || (channel_assignment == 0x18)
  883. || (channel_assignment == 0x1c))
  884. reg &= ~AUDIO_OUT_ENABLE_AP1;
  885. /* specific cases where AP2 is not used */
  886. if ((channel_assignment >= 0x14)
  887. && (channel_assignment <= 0x17))
  888. reg &= ~AUDIO_OUT_ENABLE_AP2;
  889. } else {
  890. reg = AUDIO_OUT_ENABLE_AP3 |
  891. AUDIO_OUT_ENABLE_AP2 |
  892. AUDIO_OUT_ENABLE_AP1 |
  893. AUDIO_OUT_ENABLE_AP0;
  894. }
  895. if (pdata->audout_format == AUDFMT_TYPE_I2S)
  896. reg |= (AUDIO_OUT_ENABLE_ACLK | AUDIO_OUT_ENABLE_WS);
  897. io_write(sd, REG_AUDIO_OUT_ENABLE, reg);
  898. /* reset test mode to normal audio freq auto selection */
  899. io_write(sd, REG_TEST_MODE, 0x00);
  900. return 0;
  901. }
  902. /* Soft Reset of specific hdmi info */
  903. static int
  904. tda1997x_hdmi_info_reset(struct v4l2_subdev *sd, u8 info_rst, bool reset_sus)
  905. {
  906. u8 reg;
  907. /* reset infoframe engine packets */
  908. reg = io_read(sd, REG_HDMI_INFO_RST);
  909. io_write(sd, REG_HDMI_INFO_RST, info_rst);
  910. /* if infoframe engine has been reset clear INT_FLG_MODE */
  911. if (reg & RESET_IF) {
  912. reg = io_read(sd, REG_INT_FLG_CLR_MODE);
  913. io_write(sd, REG_INT_FLG_CLR_MODE, reg);
  914. }
  915. /* Disable REFTIM to restart start-up-sequencer (SUS) */
  916. reg = io_read(sd, REG_RATE_CTRL);
  917. reg &= ~RATE_REFTIM_ENABLE;
  918. if (!reset_sus)
  919. reg |= RATE_REFTIM_ENABLE;
  920. reg = io_write(sd, REG_RATE_CTRL, reg);
  921. return 0;
  922. }
  923. static void
  924. tda1997x_power_mode(struct tda1997x_state *state, bool enable)
  925. {
  926. struct v4l2_subdev *sd = &state->sd;
  927. u8 reg;
  928. if (enable) {
  929. /* Automatic control of TMDS */
  930. io_write(sd, REG_PON_OVR_EN, PON_DIS);
  931. /* Enable current bias unit */
  932. io_write(sd, REG_CFG1, PON_EN);
  933. /* Enable deep color PLL */
  934. io_write(sd, REG_DEEP_PLL7_BYP, PON_DIS);
  935. /* Output buffers active */
  936. reg = io_read(sd, REG_OF);
  937. reg &= ~OF_VP_ENABLE;
  938. io_write(sd, REG_OF, reg);
  939. } else {
  940. /* Power down EDID mode sequence */
  941. /* Output buffers in HiZ */
  942. reg = io_read(sd, REG_OF);
  943. reg |= OF_VP_ENABLE;
  944. io_write(sd, REG_OF, reg);
  945. /* Disable deep color PLL */
  946. io_write(sd, REG_DEEP_PLL7_BYP, PON_EN);
  947. /* Disable current bias unit */
  948. io_write(sd, REG_CFG1, PON_DIS);
  949. /* Manual control of TMDS */
  950. io_write(sd, REG_PON_OVR_EN, PON_EN);
  951. }
  952. }
  953. static bool
  954. tda1997x_detect_tx_5v(struct v4l2_subdev *sd)
  955. {
  956. u8 reg = io_read(sd, REG_DETECT_5V);
  957. return ((reg & DETECT_5V_SEL) ? 1 : 0);
  958. }
  959. static bool
  960. tda1997x_detect_tx_hpd(struct v4l2_subdev *sd)
  961. {
  962. u8 reg = io_read(sd, REG_DETECT_5V);
  963. return ((reg & DETECT_HPD) ? 1 : 0);
  964. }
  965. static int
  966. tda1997x_detect_std(struct tda1997x_state *state,
  967. struct v4l2_dv_timings *timings)
  968. {
  969. struct v4l2_subdev *sd = &state->sd;
  970. /*
  971. * Read the FMT registers
  972. * REG_V_PER: Period of a frame (or field) in MCLK (27MHz) cycles
  973. * REG_H_PER: Period of a line in MCLK (27MHz) cycles
  974. * REG_HS_WIDTH: Period of horiz sync pulse in MCLK (27MHz) cycles
  975. */
  976. u32 vper, vsync_pos;
  977. u16 hper, hsync_pos, hsper, interlaced;
  978. u16 htot, hact, hfront, hsync, hback;
  979. u16 vtot, vact, vfront1, vfront2, vsync, vback1, vback2;
  980. if (!state->input_detect[0] && !state->input_detect[1])
  981. return -ENOLINK;
  982. vper = io_read24(sd, REG_V_PER);
  983. hper = io_read16(sd, REG_H_PER);
  984. hsper = io_read16(sd, REG_HS_WIDTH);
  985. vsync_pos = vper & MASK_VPER_SYNC_POS;
  986. hsync_pos = hper & MASK_HPER_SYNC_POS;
  987. interlaced = hsper & MASK_HSWIDTH_INTERLACED;
  988. vper &= MASK_VPER;
  989. hper &= MASK_HPER;
  990. hsper &= MASK_HSWIDTH;
  991. v4l2_dbg(1, debug, sd, "Signal Timings: %u/%u/%u\n", vper, hper, hsper);
  992. htot = io_read16(sd, REG_FMT_H_TOT);
  993. hact = io_read16(sd, REG_FMT_H_ACT);
  994. hfront = io_read16(sd, REG_FMT_H_FRONT);
  995. hsync = io_read16(sd, REG_FMT_H_SYNC);
  996. hback = io_read16(sd, REG_FMT_H_BACK);
  997. vtot = io_read16(sd, REG_FMT_V_TOT);
  998. vact = io_read16(sd, REG_FMT_V_ACT);
  999. vfront1 = io_read(sd, REG_FMT_V_FRONT_F1);
  1000. vfront2 = io_read(sd, REG_FMT_V_FRONT_F2);
  1001. vsync = io_read(sd, REG_FMT_V_SYNC);
  1002. vback1 = io_read(sd, REG_FMT_V_BACK_F1);
  1003. vback2 = io_read(sd, REG_FMT_V_BACK_F2);
  1004. v4l2_dbg(1, debug, sd, "Geometry: H %u %u %u %u %u Sync%c V %u %u %u %u %u %u %u Sync%c\n",
  1005. htot, hact, hfront, hsync, hback, hsync_pos ? '+' : '-',
  1006. vtot, vact, vfront1, vfront2, vsync, vback1, vback2, vsync_pos ? '+' : '-');
  1007. if (!timings)
  1008. return 0;
  1009. timings->type = V4L2_DV_BT_656_1120;
  1010. timings->bt.width = hact;
  1011. timings->bt.hfrontporch = hfront;
  1012. timings->bt.hsync = hsync;
  1013. timings->bt.hbackporch = hback;
  1014. timings->bt.height = vact;
  1015. timings->bt.vfrontporch = vfront1;
  1016. timings->bt.vsync = vsync;
  1017. timings->bt.vbackporch = vback1;
  1018. timings->bt.interlaced = interlaced ? V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1019. timings->bt.polarities = vsync_pos ? V4L2_DV_VSYNC_POS_POL : 0;
  1020. timings->bt.polarities |= hsync_pos ? V4L2_DV_HSYNC_POS_POL : 0;
  1021. timings->bt.pixelclock = (u64)htot * vtot * 27000000;
  1022. if (interlaced) {
  1023. timings->bt.il_vfrontporch = vfront2;
  1024. timings->bt.il_vsync = timings->bt.vsync;
  1025. timings->bt.il_vbackporch = vback2;
  1026. do_div(timings->bt.pixelclock, vper * 2 /* full frame */);
  1027. } else {
  1028. timings->bt.il_vfrontporch = 0;
  1029. timings->bt.il_vsync = 0;
  1030. timings->bt.il_vbackporch = 0;
  1031. do_div(timings->bt.pixelclock, vper);
  1032. }
  1033. v4l2_find_dv_timings_cap(timings, &tda1997x_dv_timings_cap,
  1034. (u32)timings->bt.pixelclock / 500, NULL, NULL);
  1035. v4l2_print_dv_timings(sd->name, "Detected format: ", timings, false);
  1036. return 0;
  1037. }
  1038. /* some sort of errata workaround for chip revision 0 (N1) */
  1039. static void tda1997x_reset_n1(struct tda1997x_state *state)
  1040. {
  1041. struct v4l2_subdev *sd = &state->sd;
  1042. u8 reg;
  1043. /* clear HDMI mode flag in BCAPS */
  1044. io_write(sd, REG_CLK_CFG, CLK_CFG_SEL_ACLK_EN | CLK_CFG_SEL_ACLK);
  1045. io_write(sd, REG_PON_OVR_EN, PON_EN);
  1046. io_write(sd, REG_PON_CBIAS, PON_EN);
  1047. io_write(sd, REG_PON_PLL, PON_EN);
  1048. reg = io_read(sd, REG_MODE_REC_CFG1);
  1049. reg &= ~0x06;
  1050. reg |= 0x02;
  1051. io_write(sd, REG_MODE_REC_CFG1, reg);
  1052. io_write(sd, REG_CLK_CFG, CLK_CFG_DIS);
  1053. io_write(sd, REG_PON_OVR_EN, PON_DIS);
  1054. reg = io_read(sd, REG_MODE_REC_CFG1);
  1055. reg &= ~0x06;
  1056. io_write(sd, REG_MODE_REC_CFG1, reg);
  1057. }
  1058. /*
  1059. * Activity detection must only be notified when stable_clk_x AND active_x
  1060. * bits are set to 1. If only stable_clk_x bit is set to 1 but not
  1061. * active_x, it means that the TMDS clock is not in the defined range
  1062. * and activity detection must not be notified.
  1063. */
  1064. static u8
  1065. tda1997x_read_activity_status_regs(struct v4l2_subdev *sd)
  1066. {
  1067. u8 reg, status = 0;
  1068. /* Read CLK_A_STATUS register */
  1069. reg = io_read(sd, REG_CLK_A_STATUS);
  1070. /* ignore if not active */
  1071. if ((reg & MASK_CLK_STABLE) && !(reg & MASK_CLK_ACTIVE))
  1072. reg &= ~MASK_CLK_STABLE;
  1073. status |= ((reg & MASK_CLK_STABLE) >> 2);
  1074. /* Read CLK_B_STATUS register */
  1075. reg = io_read(sd, REG_CLK_B_STATUS);
  1076. /* ignore if not active */
  1077. if ((reg & MASK_CLK_STABLE) && !(reg & MASK_CLK_ACTIVE))
  1078. reg &= ~MASK_CLK_STABLE;
  1079. status |= ((reg & MASK_CLK_STABLE) >> 1);
  1080. /* Read the SUS_STATUS register */
  1081. reg = io_read(sd, REG_SUS_STATUS);
  1082. /* If state = 5 => TMDS is locked */
  1083. if ((reg & MASK_SUS_STATUS) == LAST_STATE_REACHED)
  1084. status |= MASK_SUS_STATE;
  1085. else
  1086. status &= ~MASK_SUS_STATE;
  1087. return status;
  1088. }
  1089. static void
  1090. set_rgb_quantization_range(struct tda1997x_state *state)
  1091. {
  1092. struct v4l2_hdmi_colorimetry *c = &state->colorimetry;
  1093. state->colorimetry = v4l2_hdmi_rx_colorimetry(&state->avi_infoframe,
  1094. NULL,
  1095. state->timings.bt.height);
  1096. /* If ycbcr_enc is V4L2_YCBCR_ENC_DEFAULT, we receive RGB */
  1097. if (c->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
  1098. switch (state->rgb_quantization_range) {
  1099. case V4L2_DV_RGB_RANGE_LIMITED:
  1100. c->quantization = V4L2_QUANTIZATION_FULL_RANGE;
  1101. break;
  1102. case V4L2_DV_RGB_RANGE_FULL:
  1103. c->quantization = V4L2_QUANTIZATION_LIM_RANGE;
  1104. break;
  1105. }
  1106. }
  1107. v4l_dbg(1, debug, state->client,
  1108. "colorspace=%d/%d colorimetry=%d range=%s content=%d\n",
  1109. state->avi_infoframe.colorspace, c->colorspace,
  1110. state->avi_infoframe.colorimetry,
  1111. v4l2_quantization_names[c->quantization],
  1112. state->avi_infoframe.content_type);
  1113. }
  1114. /* parse an infoframe and do some sanity checks on it */
  1115. static unsigned int
  1116. tda1997x_parse_infoframe(struct tda1997x_state *state, u16 addr)
  1117. {
  1118. struct v4l2_subdev *sd = &state->sd;
  1119. union hdmi_infoframe frame;
  1120. u8 buffer[40] = { 0 };
  1121. u8 reg;
  1122. int len, err;
  1123. /* read data */
  1124. len = io_readn(sd, addr, sizeof(buffer), buffer);
  1125. err = hdmi_infoframe_unpack(&frame, buffer, len);
  1126. if (err) {
  1127. v4l_err(state->client,
  1128. "failed parsing %d byte infoframe: 0x%04x/0x%02x\n",
  1129. len, addr, buffer[0]);
  1130. return err;
  1131. }
  1132. hdmi_infoframe_log(KERN_INFO, &state->client->dev, &frame);
  1133. switch (frame.any.type) {
  1134. /* Audio InfoFrame: see HDMI spec 8.2.2 */
  1135. case HDMI_INFOFRAME_TYPE_AUDIO:
  1136. /* sample rate */
  1137. switch (frame.audio.sample_frequency) {
  1138. case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
  1139. state->audio_samplerate = 32000;
  1140. break;
  1141. case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
  1142. state->audio_samplerate = 44100;
  1143. break;
  1144. case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
  1145. state->audio_samplerate = 48000;
  1146. break;
  1147. case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
  1148. state->audio_samplerate = 88200;
  1149. break;
  1150. case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
  1151. state->audio_samplerate = 96000;
  1152. break;
  1153. case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
  1154. state->audio_samplerate = 176400;
  1155. break;
  1156. case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
  1157. state->audio_samplerate = 192000;
  1158. break;
  1159. default:
  1160. case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
  1161. break;
  1162. }
  1163. /* sample size */
  1164. switch (frame.audio.sample_size) {
  1165. case HDMI_AUDIO_SAMPLE_SIZE_16:
  1166. state->audio_samplesize = 16;
  1167. break;
  1168. case HDMI_AUDIO_SAMPLE_SIZE_20:
  1169. state->audio_samplesize = 20;
  1170. break;
  1171. case HDMI_AUDIO_SAMPLE_SIZE_24:
  1172. state->audio_samplesize = 24;
  1173. break;
  1174. case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
  1175. default:
  1176. break;
  1177. }
  1178. /* Channel Count */
  1179. state->audio_channels = frame.audio.channels;
  1180. if (frame.audio.channel_allocation &&
  1181. frame.audio.channel_allocation != state->audio_ch_alloc) {
  1182. /* use the channel assignment from the infoframe */
  1183. state->audio_ch_alloc = frame.audio.channel_allocation;
  1184. tda1997x_configure_audout(sd, state->audio_ch_alloc);
  1185. /* reset the audio FIFO */
  1186. tda1997x_hdmi_info_reset(sd, RESET_AUDIO, false);
  1187. }
  1188. break;
  1189. /* Auxiliary Video information (AVI) InfoFrame: see HDMI spec 8.2.1 */
  1190. case HDMI_INFOFRAME_TYPE_AVI:
  1191. state->avi_infoframe = frame.avi;
  1192. set_rgb_quantization_range(state);
  1193. /* configure upsampler: 0=bypass 1=repeatchroma 2=interpolate */
  1194. reg = io_read(sd, REG_PIX_REPEAT);
  1195. reg &= ~PIX_REPEAT_MASK_UP_SEL;
  1196. if (frame.avi.colorspace == HDMI_COLORSPACE_YUV422)
  1197. reg |= (PIX_REPEAT_CHROMA << PIX_REPEAT_SHIFT);
  1198. io_write(sd, REG_PIX_REPEAT, reg);
  1199. /* ConfigurePixelRepeater: repeat n-times each pixel */
  1200. reg = io_read(sd, REG_PIX_REPEAT);
  1201. reg &= ~PIX_REPEAT_MASK_REP;
  1202. reg |= frame.avi.pixel_repeat;
  1203. io_write(sd, REG_PIX_REPEAT, reg);
  1204. /* configure the receiver with the new colorspace */
  1205. tda1997x_configure_csc(sd);
  1206. break;
  1207. default:
  1208. break;
  1209. }
  1210. return 0;
  1211. }
  1212. static void tda1997x_irq_sus(struct tda1997x_state *state, u8 *flags)
  1213. {
  1214. struct v4l2_subdev *sd = &state->sd;
  1215. u8 reg, source;
  1216. source = io_read(sd, REG_INT_FLG_CLR_SUS);
  1217. io_write(sd, REG_INT_FLG_CLR_SUS, source);
  1218. if (source & MASK_MPT) {
  1219. /* reset MTP in use flag if set */
  1220. if (state->mptrw_in_progress)
  1221. state->mptrw_in_progress = 0;
  1222. }
  1223. if (source & MASK_SUS_END) {
  1224. /* reset audio FIFO */
  1225. reg = io_read(sd, REG_HDMI_INFO_RST);
  1226. reg |= MASK_SR_FIFO_FIFO_CTRL;
  1227. io_write(sd, REG_HDMI_INFO_RST, reg);
  1228. reg &= ~MASK_SR_FIFO_FIFO_CTRL;
  1229. io_write(sd, REG_HDMI_INFO_RST, reg);
  1230. /* reset HDMI flags */
  1231. state->hdmi_status = 0;
  1232. }
  1233. /* filter FMT interrupt based on SUS state */
  1234. reg = io_read(sd, REG_SUS_STATUS);
  1235. if (((reg & MASK_SUS_STATUS) != LAST_STATE_REACHED)
  1236. || (source & MASK_MPT)) {
  1237. source &= ~MASK_FMT;
  1238. }
  1239. if (source & (MASK_FMT | MASK_SUS_END)) {
  1240. reg = io_read(sd, REG_SUS_STATUS);
  1241. if ((reg & MASK_SUS_STATUS) != LAST_STATE_REACHED) {
  1242. v4l_err(state->client, "BAD SUS STATUS\n");
  1243. return;
  1244. }
  1245. if (debug)
  1246. tda1997x_detect_std(state, NULL);
  1247. /* notify user of change in resolution */
  1248. v4l2_subdev_notify_event(&state->sd, &tda1997x_ev_fmt);
  1249. }
  1250. }
  1251. static void tda1997x_irq_ddc(struct tda1997x_state *state, u8 *flags)
  1252. {
  1253. struct v4l2_subdev *sd = &state->sd;
  1254. u8 source;
  1255. source = io_read(sd, REG_INT_FLG_CLR_DDC);
  1256. io_write(sd, REG_INT_FLG_CLR_DDC, source);
  1257. if (source & MASK_EDID_MTP) {
  1258. /* reset MTP in use flag if set */
  1259. if (state->mptrw_in_progress)
  1260. state->mptrw_in_progress = 0;
  1261. }
  1262. /* Detection of +5V */
  1263. if (source & MASK_DET_5V) {
  1264. v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
  1265. tda1997x_detect_tx_5v(sd));
  1266. }
  1267. }
  1268. static void tda1997x_irq_rate(struct tda1997x_state *state, u8 *flags)
  1269. {
  1270. struct v4l2_subdev *sd = &state->sd;
  1271. u8 reg, source;
  1272. u8 irq_status;
  1273. source = io_read(sd, REG_INT_FLG_CLR_RATE);
  1274. io_write(sd, REG_INT_FLG_CLR_RATE, source);
  1275. /* read status regs */
  1276. irq_status = tda1997x_read_activity_status_regs(sd);
  1277. /*
  1278. * read clock status reg until INT_FLG_CLR_RATE is still 0
  1279. * after the read to make sure its the last one
  1280. */
  1281. reg = source;
  1282. while (reg != 0) {
  1283. irq_status = tda1997x_read_activity_status_regs(sd);
  1284. reg = io_read(sd, REG_INT_FLG_CLR_RATE);
  1285. io_write(sd, REG_INT_FLG_CLR_RATE, reg);
  1286. source |= reg;
  1287. }
  1288. /* we only pay attention to stability change events */
  1289. if (source & (MASK_RATE_A_ST | MASK_RATE_B_ST)) {
  1290. int input = (source & MASK_RATE_A_ST)?0:1;
  1291. u8 mask = 1<<input;
  1292. /* state change */
  1293. if ((irq_status & mask) != (state->activity_status & mask)) {
  1294. /* activity lost */
  1295. if ((irq_status & mask) == 0) {
  1296. v4l_info(state->client,
  1297. "HDMI-%c: Digital Activity Lost\n",
  1298. input+'A');
  1299. /* bypass up/down sampler and pixel repeater */
  1300. reg = io_read(sd, REG_PIX_REPEAT);
  1301. reg &= ~PIX_REPEAT_MASK_UP_SEL;
  1302. reg &= ~PIX_REPEAT_MASK_REP;
  1303. io_write(sd, REG_PIX_REPEAT, reg);
  1304. if (state->chip_revision == 0)
  1305. tda1997x_reset_n1(state);
  1306. state->input_detect[input] = 0;
  1307. v4l2_subdev_notify_event(sd, &tda1997x_ev_fmt);
  1308. }
  1309. /* activity detected */
  1310. else {
  1311. v4l_info(state->client,
  1312. "HDMI-%c: Digital Activity Detected\n",
  1313. input+'A');
  1314. state->input_detect[input] = 1;
  1315. }
  1316. /* hold onto current state */
  1317. state->activity_status = (irq_status & mask);
  1318. }
  1319. }
  1320. }
  1321. static void tda1997x_irq_info(struct tda1997x_state *state, u8 *flags)
  1322. {
  1323. struct v4l2_subdev *sd = &state->sd;
  1324. u8 source;
  1325. source = io_read(sd, REG_INT_FLG_CLR_INFO);
  1326. io_write(sd, REG_INT_FLG_CLR_INFO, source);
  1327. /* Audio infoframe */
  1328. if (source & MASK_AUD_IF) {
  1329. tda1997x_parse_infoframe(state, AUD_IF);
  1330. source &= ~MASK_AUD_IF;
  1331. }
  1332. /* Source Product Descriptor infoframe change */
  1333. if (source & MASK_SPD_IF) {
  1334. tda1997x_parse_infoframe(state, SPD_IF);
  1335. source &= ~MASK_SPD_IF;
  1336. }
  1337. /* Auxiliary Video Information infoframe */
  1338. if (source & MASK_AVI_IF) {
  1339. tda1997x_parse_infoframe(state, AVI_IF);
  1340. source &= ~MASK_AVI_IF;
  1341. }
  1342. }
  1343. static void tda1997x_irq_audio(struct tda1997x_state *state, u8 *flags)
  1344. {
  1345. struct v4l2_subdev *sd = &state->sd;
  1346. u8 reg, source;
  1347. source = io_read(sd, REG_INT_FLG_CLR_AUDIO);
  1348. io_write(sd, REG_INT_FLG_CLR_AUDIO, source);
  1349. /* reset audio FIFO on FIFO pointer error or audio mute */
  1350. if (source & MASK_ERROR_FIFO_PT ||
  1351. source & MASK_MUTE_FLG) {
  1352. /* audio reset audio FIFO */
  1353. reg = io_read(sd, REG_SUS_STATUS);
  1354. if ((reg & MASK_SUS_STATUS) == LAST_STATE_REACHED) {
  1355. reg = io_read(sd, REG_HDMI_INFO_RST);
  1356. reg |= MASK_SR_FIFO_FIFO_CTRL;
  1357. io_write(sd, REG_HDMI_INFO_RST, reg);
  1358. reg &= ~MASK_SR_FIFO_FIFO_CTRL;
  1359. io_write(sd, REG_HDMI_INFO_RST, reg);
  1360. /* reset channel status IT if present */
  1361. source &= ~(MASK_CH_STATE);
  1362. }
  1363. }
  1364. if (source & MASK_AUDIO_FREQ_FLG) {
  1365. static const int freq[] = {
  1366. 0, 32000, 44100, 48000, 88200, 96000, 176400, 192000
  1367. };
  1368. reg = io_read(sd, REG_AUDIO_FREQ);
  1369. state->audio_samplerate = freq[reg & 7];
  1370. v4l_info(state->client, "Audio Frequency Change: %dHz\n",
  1371. state->audio_samplerate);
  1372. }
  1373. if (source & MASK_AUDIO_FLG) {
  1374. reg = io_read(sd, REG_AUDIO_FLAGS);
  1375. if (reg & BIT(AUDCFG_TYPE_DST))
  1376. state->audio_type = AUDCFG_TYPE_DST;
  1377. if (reg & BIT(AUDCFG_TYPE_OBA))
  1378. state->audio_type = AUDCFG_TYPE_OBA;
  1379. if (reg & BIT(AUDCFG_TYPE_HBR))
  1380. state->audio_type = AUDCFG_TYPE_HBR;
  1381. if (reg & BIT(AUDCFG_TYPE_PCM))
  1382. state->audio_type = AUDCFG_TYPE_PCM;
  1383. v4l_info(state->client, "Audio Type: %s\n",
  1384. audtype_names[state->audio_type]);
  1385. }
  1386. }
  1387. static void tda1997x_irq_hdcp(struct tda1997x_state *state, u8 *flags)
  1388. {
  1389. struct v4l2_subdev *sd = &state->sd;
  1390. u8 reg, source;
  1391. source = io_read(sd, REG_INT_FLG_CLR_HDCP);
  1392. io_write(sd, REG_INT_FLG_CLR_HDCP, source);
  1393. /* reset MTP in use flag if set */
  1394. if (source & MASK_HDCP_MTP)
  1395. state->mptrw_in_progress = 0;
  1396. if (source & MASK_STATE_C5) {
  1397. /* REPEATER: mask AUDIO and IF irqs to avoid IF during auth */
  1398. reg = io_read(sd, REG_INT_MASK_TOP);
  1399. reg &= ~(INTERRUPT_AUDIO | INTERRUPT_INFO);
  1400. io_write(sd, REG_INT_MASK_TOP, reg);
  1401. *flags &= (INTERRUPT_AUDIO | INTERRUPT_INFO);
  1402. }
  1403. }
  1404. static irqreturn_t tda1997x_isr_thread(int irq, void *d)
  1405. {
  1406. struct tda1997x_state *state = d;
  1407. struct v4l2_subdev *sd = &state->sd;
  1408. u8 flags;
  1409. mutex_lock(&state->lock);
  1410. do {
  1411. /* read interrupt flags */
  1412. flags = io_read(sd, REG_INT_FLG_CLR_TOP);
  1413. if (flags == 0)
  1414. break;
  1415. /* SUS interrupt source (Input activity events) */
  1416. if (flags & INTERRUPT_SUS)
  1417. tda1997x_irq_sus(state, &flags);
  1418. /* DDC interrupt source (Display Data Channel) */
  1419. else if (flags & INTERRUPT_DDC)
  1420. tda1997x_irq_ddc(state, &flags);
  1421. /* RATE interrupt source (Digital Input activity) */
  1422. else if (flags & INTERRUPT_RATE)
  1423. tda1997x_irq_rate(state, &flags);
  1424. /* Infoframe change interrupt */
  1425. else if (flags & INTERRUPT_INFO)
  1426. tda1997x_irq_info(state, &flags);
  1427. /* Audio interrupt source:
  1428. * freq change, DST,OBA,HBR,ASP flags, mute, FIFO err
  1429. */
  1430. else if (flags & INTERRUPT_AUDIO)
  1431. tda1997x_irq_audio(state, &flags);
  1432. /* HDCP interrupt source (content protection) */
  1433. if (flags & INTERRUPT_HDCP)
  1434. tda1997x_irq_hdcp(state, &flags);
  1435. } while (flags != 0);
  1436. mutex_unlock(&state->lock);
  1437. return IRQ_HANDLED;
  1438. }
  1439. /* -----------------------------------------------------------------------------
  1440. * v4l2_subdev_video_ops
  1441. */
  1442. static int
  1443. tda1997x_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1444. {
  1445. struct tda1997x_state *state = to_state(sd);
  1446. u32 vper;
  1447. u16 hper;
  1448. u16 hsper;
  1449. mutex_lock(&state->lock);
  1450. vper = io_read24(sd, REG_V_PER) & MASK_VPER;
  1451. hper = io_read16(sd, REG_H_PER) & MASK_HPER;
  1452. hsper = io_read16(sd, REG_HS_WIDTH) & MASK_HSWIDTH;
  1453. /*
  1454. * The tda1997x supports A/B inputs but only a single output.
  1455. * The irq handler monitors for timing changes on both inputs and
  1456. * sets the input_detect array to 0|1 depending on signal presence.
  1457. * I believe selection of A vs B is automatic.
  1458. *
  1459. * The vper/hper/hsper registers provide the frame period, line period
  1460. * and horiz sync period (units of MCLK clock cycles (27MHz)) and
  1461. * testing shows these values to be random if no signal is present
  1462. * or locked.
  1463. */
  1464. v4l2_dbg(1, debug, sd, "inputs:%d/%d timings:%d/%d/%d\n",
  1465. state->input_detect[0], state->input_detect[1],
  1466. vper, hper, hsper);
  1467. if (!state->input_detect[0] && !state->input_detect[1])
  1468. *status = V4L2_IN_ST_NO_SIGNAL;
  1469. else if (!vper || !hper || !hsper)
  1470. *status = V4L2_IN_ST_NO_SYNC;
  1471. else
  1472. *status = 0;
  1473. mutex_unlock(&state->lock);
  1474. return 0;
  1475. };
  1476. static int tda1997x_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  1477. struct v4l2_dv_timings *timings)
  1478. {
  1479. struct tda1997x_state *state = to_state(sd);
  1480. v4l_dbg(1, debug, state->client, "%s\n", __func__);
  1481. if (v4l2_match_dv_timings(&state->timings, timings, 0, false))
  1482. return 0; /* no changes */
  1483. if (!v4l2_valid_dv_timings(timings, &tda1997x_dv_timings_cap,
  1484. NULL, NULL))
  1485. return -ERANGE;
  1486. mutex_lock(&state->lock);
  1487. state->timings = *timings;
  1488. /* setup frame detection window and VHREF timing generator */
  1489. tda1997x_configure_vhref(sd);
  1490. /* configure colorspace conversion */
  1491. tda1997x_configure_csc(sd);
  1492. mutex_unlock(&state->lock);
  1493. return 0;
  1494. }
  1495. static int tda1997x_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  1496. struct v4l2_dv_timings *timings)
  1497. {
  1498. struct tda1997x_state *state = to_state(sd);
  1499. v4l_dbg(1, debug, state->client, "%s\n", __func__);
  1500. mutex_lock(&state->lock);
  1501. *timings = state->timings;
  1502. mutex_unlock(&state->lock);
  1503. return 0;
  1504. }
  1505. static int tda1997x_query_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  1506. struct v4l2_dv_timings *timings)
  1507. {
  1508. struct tda1997x_state *state = to_state(sd);
  1509. int ret;
  1510. v4l_dbg(1, debug, state->client, "%s\n", __func__);
  1511. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1512. mutex_lock(&state->lock);
  1513. ret = tda1997x_detect_std(state, timings);
  1514. mutex_unlock(&state->lock);
  1515. return ret;
  1516. }
  1517. static const struct v4l2_subdev_video_ops tda1997x_video_ops = {
  1518. .g_input_status = tda1997x_g_input_status,
  1519. };
  1520. /* -----------------------------------------------------------------------------
  1521. * v4l2_subdev_pad_ops
  1522. */
  1523. static int tda1997x_init_state(struct v4l2_subdev *sd,
  1524. struct v4l2_subdev_state *sd_state)
  1525. {
  1526. struct tda1997x_state *state = to_state(sd);
  1527. struct v4l2_mbus_framefmt *mf;
  1528. mf = v4l2_subdev_state_get_format(sd_state, 0);
  1529. mf->code = state->mbus_codes[0];
  1530. return 0;
  1531. }
  1532. static int tda1997x_enum_mbus_code(struct v4l2_subdev *sd,
  1533. struct v4l2_subdev_state *sd_state,
  1534. struct v4l2_subdev_mbus_code_enum *code)
  1535. {
  1536. struct tda1997x_state *state = to_state(sd);
  1537. v4l_dbg(1, debug, state->client, "%s %d\n", __func__, code->index);
  1538. if (code->index >= ARRAY_SIZE(state->mbus_codes))
  1539. return -EINVAL;
  1540. if (!state->mbus_codes[code->index])
  1541. return -EINVAL;
  1542. code->code = state->mbus_codes[code->index];
  1543. return 0;
  1544. }
  1545. static void tda1997x_fill_format(struct tda1997x_state *state,
  1546. struct v4l2_mbus_framefmt *format)
  1547. {
  1548. const struct v4l2_bt_timings *bt;
  1549. memset(format, 0, sizeof(*format));
  1550. bt = &state->timings.bt;
  1551. format->width = bt->width;
  1552. format->height = bt->height;
  1553. format->colorspace = state->colorimetry.colorspace;
  1554. format->field = (bt->interlaced) ?
  1555. V4L2_FIELD_SEQ_TB : V4L2_FIELD_NONE;
  1556. }
  1557. static int tda1997x_get_format(struct v4l2_subdev *sd,
  1558. struct v4l2_subdev_state *sd_state,
  1559. struct v4l2_subdev_format *format)
  1560. {
  1561. struct tda1997x_state *state = to_state(sd);
  1562. v4l_dbg(1, debug, state->client, "%s pad=%d which=%d\n",
  1563. __func__, format->pad, format->which);
  1564. tda1997x_fill_format(state, &format->format);
  1565. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1566. struct v4l2_mbus_framefmt *fmt;
  1567. fmt = v4l2_subdev_state_get_format(sd_state, format->pad);
  1568. format->format.code = fmt->code;
  1569. } else
  1570. format->format.code = state->mbus_code;
  1571. return 0;
  1572. }
  1573. static int tda1997x_set_format(struct v4l2_subdev *sd,
  1574. struct v4l2_subdev_state *sd_state,
  1575. struct v4l2_subdev_format *format)
  1576. {
  1577. struct tda1997x_state *state = to_state(sd);
  1578. u32 code = 0;
  1579. int i;
  1580. v4l_dbg(1, debug, state->client, "%s pad=%d which=%d fmt=0x%x\n",
  1581. __func__, format->pad, format->which, format->format.code);
  1582. for (i = 0; i < ARRAY_SIZE(state->mbus_codes); i++) {
  1583. if (format->format.code == state->mbus_codes[i]) {
  1584. code = state->mbus_codes[i];
  1585. break;
  1586. }
  1587. }
  1588. if (!code)
  1589. code = state->mbus_codes[0];
  1590. tda1997x_fill_format(state, &format->format);
  1591. format->format.code = code;
  1592. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1593. struct v4l2_mbus_framefmt *fmt;
  1594. fmt = v4l2_subdev_state_get_format(sd_state, format->pad);
  1595. *fmt = format->format;
  1596. } else {
  1597. int ret = tda1997x_setup_format(state, format->format.code);
  1598. if (ret)
  1599. return ret;
  1600. /* mbus_code has changed - re-configure csc/vidout */
  1601. tda1997x_configure_csc(sd);
  1602. tda1997x_configure_vidout(state);
  1603. }
  1604. return 0;
  1605. }
  1606. static int tda1997x_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1607. {
  1608. struct tda1997x_state *state = to_state(sd);
  1609. v4l_dbg(1, debug, state->client, "%s pad=%d\n", __func__, edid->pad);
  1610. memset(edid->reserved, 0, sizeof(edid->reserved));
  1611. if (edid->start_block == 0 && edid->blocks == 0) {
  1612. edid->blocks = state->edid.blocks;
  1613. return 0;
  1614. }
  1615. if (!state->edid.present)
  1616. return -ENODATA;
  1617. if (edid->start_block >= state->edid.blocks)
  1618. return -EINVAL;
  1619. if (edid->start_block + edid->blocks > state->edid.blocks)
  1620. edid->blocks = state->edid.blocks - edid->start_block;
  1621. memcpy(edid->edid, state->edid.edid + edid->start_block * 128,
  1622. edid->blocks * 128);
  1623. return 0;
  1624. }
  1625. static int tda1997x_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1626. {
  1627. struct tda1997x_state *state = to_state(sd);
  1628. int i;
  1629. v4l_dbg(1, debug, state->client, "%s pad=%d\n", __func__, edid->pad);
  1630. memset(edid->reserved, 0, sizeof(edid->reserved));
  1631. if (edid->start_block != 0)
  1632. return -EINVAL;
  1633. if (edid->blocks == 0) {
  1634. state->edid.blocks = 0;
  1635. state->edid.present = 0;
  1636. tda1997x_disable_edid(sd);
  1637. return 0;
  1638. }
  1639. if (edid->blocks > 2) {
  1640. edid->blocks = 2;
  1641. return -E2BIG;
  1642. }
  1643. tda1997x_disable_edid(sd);
  1644. /* write base EDID */
  1645. for (i = 0; i < 128; i++)
  1646. io_write(sd, REG_EDID_IN_BYTE0 + i, edid->edid[i]);
  1647. /* write CEA Extension */
  1648. for (i = 0; i < 128; i++)
  1649. io_write(sd, REG_EDID_IN_BYTE128 + i, edid->edid[i+128]);
  1650. /* store state */
  1651. memcpy(state->edid.edid, edid->edid, 256);
  1652. state->edid.blocks = edid->blocks;
  1653. tda1997x_enable_edid(sd);
  1654. return 0;
  1655. }
  1656. static int tda1997x_get_dv_timings_cap(struct v4l2_subdev *sd,
  1657. struct v4l2_dv_timings_cap *cap)
  1658. {
  1659. *cap = tda1997x_dv_timings_cap;
  1660. return 0;
  1661. }
  1662. static int tda1997x_enum_dv_timings(struct v4l2_subdev *sd,
  1663. struct v4l2_enum_dv_timings *timings)
  1664. {
  1665. return v4l2_enum_dv_timings_cap(timings, &tda1997x_dv_timings_cap,
  1666. NULL, NULL);
  1667. }
  1668. static const struct v4l2_subdev_pad_ops tda1997x_pad_ops = {
  1669. .enum_mbus_code = tda1997x_enum_mbus_code,
  1670. .get_fmt = tda1997x_get_format,
  1671. .set_fmt = tda1997x_set_format,
  1672. .get_edid = tda1997x_get_edid,
  1673. .set_edid = tda1997x_set_edid,
  1674. .s_dv_timings = tda1997x_s_dv_timings,
  1675. .g_dv_timings = tda1997x_g_dv_timings,
  1676. .query_dv_timings = tda1997x_query_dv_timings,
  1677. .dv_timings_cap = tda1997x_get_dv_timings_cap,
  1678. .enum_dv_timings = tda1997x_enum_dv_timings,
  1679. };
  1680. /* -----------------------------------------------------------------------------
  1681. * v4l2_subdev_core_ops
  1682. */
  1683. static int tda1997x_log_infoframe(struct v4l2_subdev *sd, int addr)
  1684. {
  1685. struct tda1997x_state *state = to_state(sd);
  1686. union hdmi_infoframe frame;
  1687. u8 buffer[40] = { 0 };
  1688. int len, err;
  1689. /* read data */
  1690. len = io_readn(sd, addr, sizeof(buffer), buffer);
  1691. v4l2_dbg(1, debug, sd, "infoframe: addr=%d len=%d\n", addr, len);
  1692. err = hdmi_infoframe_unpack(&frame, buffer, len);
  1693. if (err) {
  1694. v4l_err(state->client,
  1695. "failed parsing %d byte infoframe: 0x%04x/0x%02x\n",
  1696. len, addr, buffer[0]);
  1697. return err;
  1698. }
  1699. hdmi_infoframe_log(KERN_INFO, &state->client->dev, &frame);
  1700. return 0;
  1701. }
  1702. static int tda1997x_log_status(struct v4l2_subdev *sd)
  1703. {
  1704. struct tda1997x_state *state = to_state(sd);
  1705. struct v4l2_dv_timings timings;
  1706. struct hdmi_avi_infoframe *avi = &state->avi_infoframe;
  1707. v4l2_info(sd, "-----Chip status-----\n");
  1708. v4l2_info(sd, "Chip: %s N%d\n", state->info->name,
  1709. state->chip_revision + 1);
  1710. v4l2_info(sd, "EDID Enabled: %s\n", state->edid.present ? "yes" : "no");
  1711. v4l2_info(sd, "-----Signal status-----\n");
  1712. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  1713. tda1997x_detect_tx_5v(sd) ? "yes" : "no");
  1714. v4l2_info(sd, "HPD detected: %s\n",
  1715. tda1997x_detect_tx_hpd(sd) ? "yes" : "no");
  1716. v4l2_info(sd, "-----Video Timings-----\n");
  1717. switch (tda1997x_detect_std(state, &timings)) {
  1718. case -ENOLINK:
  1719. v4l2_info(sd, "No video detected\n");
  1720. break;
  1721. case -ERANGE:
  1722. v4l2_info(sd, "Invalid signal detected\n");
  1723. break;
  1724. }
  1725. v4l2_print_dv_timings(sd->name, "Configured format: ",
  1726. &state->timings, true);
  1727. v4l2_info(sd, "-----Color space-----\n");
  1728. v4l2_info(sd, "Input color space: %s %s %s",
  1729. hdmi_colorspace_names[avi->colorspace],
  1730. (avi->colorspace == HDMI_COLORSPACE_RGB) ? "" :
  1731. hdmi_colorimetry_names[avi->colorimetry],
  1732. v4l2_quantization_names[state->colorimetry.quantization]);
  1733. v4l2_info(sd, "Output color space: %s",
  1734. vidfmt_names[state->vid_fmt]);
  1735. v4l2_info(sd, "Color space conversion: %s", state->conv ?
  1736. state->conv->name : "None");
  1737. v4l2_info(sd, "-----Audio-----\n");
  1738. if (state->audio_channels) {
  1739. v4l2_info(sd, "audio: %dch %dHz\n", state->audio_channels,
  1740. state->audio_samplerate);
  1741. } else {
  1742. v4l2_info(sd, "audio: none\n");
  1743. }
  1744. v4l2_info(sd, "-----Infoframes-----\n");
  1745. tda1997x_log_infoframe(sd, AUD_IF);
  1746. tda1997x_log_infoframe(sd, SPD_IF);
  1747. tda1997x_log_infoframe(sd, AVI_IF);
  1748. return 0;
  1749. }
  1750. static int tda1997x_subscribe_event(struct v4l2_subdev *sd,
  1751. struct v4l2_fh *fh,
  1752. struct v4l2_event_subscription *sub)
  1753. {
  1754. switch (sub->type) {
  1755. case V4L2_EVENT_SOURCE_CHANGE:
  1756. return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
  1757. case V4L2_EVENT_CTRL:
  1758. return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
  1759. default:
  1760. return -EINVAL;
  1761. }
  1762. }
  1763. static const struct v4l2_subdev_core_ops tda1997x_core_ops = {
  1764. .log_status = tda1997x_log_status,
  1765. .subscribe_event = tda1997x_subscribe_event,
  1766. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1767. };
  1768. /* -----------------------------------------------------------------------------
  1769. * v4l2_subdev_ops
  1770. */
  1771. static const struct v4l2_subdev_ops tda1997x_subdev_ops = {
  1772. .core = &tda1997x_core_ops,
  1773. .video = &tda1997x_video_ops,
  1774. .pad = &tda1997x_pad_ops,
  1775. };
  1776. static const struct v4l2_subdev_internal_ops tda1997x_internal_ops = {
  1777. .init_state = tda1997x_init_state,
  1778. };
  1779. /* -----------------------------------------------------------------------------
  1780. * v4l2_controls
  1781. */
  1782. static int tda1997x_s_ctrl(struct v4l2_ctrl *ctrl)
  1783. {
  1784. struct v4l2_subdev *sd = to_sd(ctrl);
  1785. struct tda1997x_state *state = to_state(sd);
  1786. switch (ctrl->id) {
  1787. /* allow overriding the default RGB quantization range */
  1788. case V4L2_CID_DV_RX_RGB_RANGE:
  1789. state->rgb_quantization_range = ctrl->val;
  1790. set_rgb_quantization_range(state);
  1791. tda1997x_configure_csc(sd);
  1792. return 0;
  1793. }
  1794. return -EINVAL;
  1795. };
  1796. static int tda1997x_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1797. {
  1798. struct v4l2_subdev *sd = to_sd(ctrl);
  1799. struct tda1997x_state *state = to_state(sd);
  1800. if (ctrl->id == V4L2_CID_DV_RX_IT_CONTENT_TYPE) {
  1801. ctrl->val = state->avi_infoframe.content_type;
  1802. return 0;
  1803. }
  1804. return -EINVAL;
  1805. };
  1806. static const struct v4l2_ctrl_ops tda1997x_ctrl_ops = {
  1807. .s_ctrl = tda1997x_s_ctrl,
  1808. .g_volatile_ctrl = tda1997x_g_volatile_ctrl,
  1809. };
  1810. static int tda1997x_core_init(struct v4l2_subdev *sd)
  1811. {
  1812. struct tda1997x_state *state = to_state(sd);
  1813. struct tda1997x_platform_data *pdata = &state->pdata;
  1814. u8 reg;
  1815. int i;
  1816. /* disable HPD */
  1817. io_write(sd, REG_HPD_AUTO_CTRL, HPD_AUTO_HPD_UNSEL);
  1818. if (state->chip_revision == 0) {
  1819. io_write(sd, REG_MAN_SUS_HDMI_SEL, MAN_DIS_HDCP | MAN_RST_HDCP);
  1820. io_write(sd, REG_CGU_DBG_SEL, 1 << CGU_DBG_CLK_SEL_SHIFT);
  1821. }
  1822. /* reset infoframe at end of start-up-sequencer */
  1823. io_write(sd, REG_SUS_SET_RGB2, 0x06);
  1824. io_write(sd, REG_SUS_SET_RGB3, 0x06);
  1825. /* Enable TMDS pull-ups */
  1826. io_write(sd, REG_RT_MAN_CTRL, RT_MAN_CTRL_RT |
  1827. RT_MAN_CTRL_RT_B | RT_MAN_CTRL_RT_A);
  1828. /* enable sync measurement timing */
  1829. tda1997x_cec_write(sd, REG_PWR_CONTROL & 0xff, 0x04);
  1830. /* adjust CEC clock divider */
  1831. tda1997x_cec_write(sd, REG_OSC_DIVIDER & 0xff, 0x03);
  1832. tda1997x_cec_write(sd, REG_EN_OSC_PERIOD_LSB & 0xff, 0xa0);
  1833. io_write(sd, REG_TIMER_D, 0x54);
  1834. /* enable power switch */
  1835. reg = tda1997x_cec_read(sd, REG_CONTROL & 0xff);
  1836. reg |= 0x20;
  1837. tda1997x_cec_write(sd, REG_CONTROL & 0xff, reg);
  1838. mdelay(50);
  1839. /* read the chip version */
  1840. reg = io_read(sd, REG_VERSION);
  1841. /* get the chip configuration */
  1842. reg = io_read(sd, REG_CMTP_REG10);
  1843. /* enable interrupts we care about */
  1844. io_write(sd, REG_INT_MASK_TOP,
  1845. INTERRUPT_HDCP | INTERRUPT_AUDIO | INTERRUPT_INFO |
  1846. INTERRUPT_RATE | INTERRUPT_SUS);
  1847. /* config_mtp,fmt,sus_end,sus_st */
  1848. io_write(sd, REG_INT_MASK_SUS, MASK_MPT | MASK_FMT | MASK_SUS_END);
  1849. /* rate stability change for inputs A/B */
  1850. io_write(sd, REG_INT_MASK_RATE, MASK_RATE_B_ST | MASK_RATE_A_ST);
  1851. /* aud,spd,avi*/
  1852. io_write(sd, REG_INT_MASK_INFO,
  1853. MASK_AUD_IF | MASK_SPD_IF | MASK_AVI_IF);
  1854. /* audio_freq,audio_flg,mute_flg,fifo_err */
  1855. io_write(sd, REG_INT_MASK_AUDIO,
  1856. MASK_AUDIO_FREQ_FLG | MASK_AUDIO_FLG | MASK_MUTE_FLG |
  1857. MASK_ERROR_FIFO_PT);
  1858. /* HDCP C5 state reached */
  1859. io_write(sd, REG_INT_MASK_HDCP, MASK_STATE_C5);
  1860. /* 5V detect and HDP pulse end */
  1861. io_write(sd, REG_INT_MASK_DDC, MASK_DET_5V);
  1862. /* don't care about AFE/MODE */
  1863. io_write(sd, REG_INT_MASK_AFE, 0);
  1864. io_write(sd, REG_INT_MASK_MODE, 0);
  1865. /* clear all interrupts */
  1866. io_write(sd, REG_INT_FLG_CLR_TOP, 0xff);
  1867. io_write(sd, REG_INT_FLG_CLR_SUS, 0xff);
  1868. io_write(sd, REG_INT_FLG_CLR_DDC, 0xff);
  1869. io_write(sd, REG_INT_FLG_CLR_RATE, 0xff);
  1870. io_write(sd, REG_INT_FLG_CLR_MODE, 0xff);
  1871. io_write(sd, REG_INT_FLG_CLR_INFO, 0xff);
  1872. io_write(sd, REG_INT_FLG_CLR_AUDIO, 0xff);
  1873. io_write(sd, REG_INT_FLG_CLR_HDCP, 0xff);
  1874. io_write(sd, REG_INT_FLG_CLR_AFE, 0xff);
  1875. /* init TMDS equalizer */
  1876. if (state->chip_revision == 0)
  1877. io_write(sd, REG_CGU_DBG_SEL, 1 << CGU_DBG_CLK_SEL_SHIFT);
  1878. io_write24(sd, REG_CLK_MIN_RATE, CLK_MIN_RATE);
  1879. io_write24(sd, REG_CLK_MAX_RATE, CLK_MAX_RATE);
  1880. if (state->chip_revision == 0)
  1881. io_write(sd, REG_WDL_CFG, WDL_CFG_VAL);
  1882. /* DC filter */
  1883. io_write(sd, REG_DEEP_COLOR_CTRL, DC_FILTER_VAL);
  1884. /* disable test pattern */
  1885. io_write(sd, REG_SVC_MODE, 0x00);
  1886. /* update HDMI INFO CTRL */
  1887. io_write(sd, REG_INFO_CTRL, 0xff);
  1888. /* write HDMI INFO EXCEED value */
  1889. io_write(sd, REG_INFO_EXCEED, 3);
  1890. if (state->chip_revision == 0)
  1891. tda1997x_reset_n1(state);
  1892. /*
  1893. * No HDCP acknowledge when HDCP is disabled
  1894. * and reset SUS to force format detection
  1895. */
  1896. tda1997x_hdmi_info_reset(sd, NACK_HDCP, true);
  1897. /* Set HPD low */
  1898. tda1997x_manual_hpd(sd, HPD_LOW_BP);
  1899. /* Configure receiver capabilities */
  1900. io_write(sd, REG_HDCP_BCAPS, HDCP_HDMI | HDCP_FAST_REAUTH);
  1901. /* Configure HDMI: Auto HDCP mode, packet controlled mute */
  1902. reg = HDMI_CTRL_MUTE_AUTO << HDMI_CTRL_MUTE_SHIFT;
  1903. reg |= HDMI_CTRL_HDCP_AUTO << HDMI_CTRL_HDCP_SHIFT;
  1904. io_write(sd, REG_HDMI_CTRL, reg);
  1905. /* reset start-up-sequencer to force format detection */
  1906. tda1997x_hdmi_info_reset(sd, 0, true);
  1907. /* disable matrix conversion */
  1908. reg = io_read(sd, REG_VDP_CTRL);
  1909. reg |= VDP_CTRL_MATRIX_BP;
  1910. io_write(sd, REG_VDP_CTRL, reg);
  1911. /* set video output mode */
  1912. tda1997x_configure_vidout(state);
  1913. /* configure video output port */
  1914. for (i = 0; i < 9; i++) {
  1915. v4l_dbg(1, debug, state->client, "vidout_cfg[%d]=0x%02x\n", i,
  1916. pdata->vidout_port_cfg[i]);
  1917. io_write(sd, REG_VP35_32_CTRL + i, pdata->vidout_port_cfg[i]);
  1918. }
  1919. /* configure audio output port */
  1920. tda1997x_configure_audout(sd, 0);
  1921. /* configure audio clock freq */
  1922. switch (pdata->audout_mclk_fs) {
  1923. case 512:
  1924. reg = AUDIO_CLOCK_SEL_512FS;
  1925. break;
  1926. case 256:
  1927. reg = AUDIO_CLOCK_SEL_256FS;
  1928. break;
  1929. case 128:
  1930. reg = AUDIO_CLOCK_SEL_128FS;
  1931. break;
  1932. case 64:
  1933. reg = AUDIO_CLOCK_SEL_64FS;
  1934. break;
  1935. case 32:
  1936. reg = AUDIO_CLOCK_SEL_32FS;
  1937. break;
  1938. default:
  1939. reg = AUDIO_CLOCK_SEL_16FS;
  1940. break;
  1941. }
  1942. io_write(sd, REG_AUDIO_CLOCK, reg);
  1943. /* reset advanced infoframes (ISRC1/ISRC2/ACP) */
  1944. tda1997x_hdmi_info_reset(sd, RESET_AI, false);
  1945. /* reset infoframe */
  1946. tda1997x_hdmi_info_reset(sd, RESET_IF, false);
  1947. /* reset audio infoframes */
  1948. tda1997x_hdmi_info_reset(sd, RESET_AUDIO, false);
  1949. /* reset gamut */
  1950. tda1997x_hdmi_info_reset(sd, RESET_GAMUT, false);
  1951. /* get initial HDMI status */
  1952. state->hdmi_status = io_read(sd, REG_HDMI_FLAGS);
  1953. io_write(sd, REG_EDID_ENABLE, EDID_ENABLE_A_EN | EDID_ENABLE_B_EN);
  1954. return 0;
  1955. }
  1956. static int tda1997x_set_power(struct tda1997x_state *state, bool on)
  1957. {
  1958. int ret = 0;
  1959. if (on) {
  1960. ret = regulator_bulk_enable(TDA1997X_NUM_SUPPLIES,
  1961. state->supplies);
  1962. msleep(300);
  1963. } else {
  1964. ret = regulator_bulk_disable(TDA1997X_NUM_SUPPLIES,
  1965. state->supplies);
  1966. }
  1967. return ret;
  1968. }
  1969. static const struct i2c_device_id tda1997x_i2c_id[] = {
  1970. {"tda19971", (kernel_ulong_t)&tda1997x_chip_info[TDA19971]},
  1971. {"tda19973", (kernel_ulong_t)&tda1997x_chip_info[TDA19973]},
  1972. { },
  1973. };
  1974. MODULE_DEVICE_TABLE(i2c, tda1997x_i2c_id);
  1975. static const struct of_device_id tda1997x_of_id[] __maybe_unused = {
  1976. { .compatible = "nxp,tda19971", .data = &tda1997x_chip_info[TDA19971] },
  1977. { .compatible = "nxp,tda19973", .data = &tda1997x_chip_info[TDA19973] },
  1978. { },
  1979. };
  1980. MODULE_DEVICE_TABLE(of, tda1997x_of_id);
  1981. static int tda1997x_parse_dt(struct tda1997x_state *state)
  1982. {
  1983. struct tda1997x_platform_data *pdata = &state->pdata;
  1984. struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
  1985. struct device_node *ep;
  1986. struct device_node *np;
  1987. unsigned int flags;
  1988. const char *str;
  1989. int ret;
  1990. u32 v;
  1991. /*
  1992. * setup default values:
  1993. * - HREF: active high from start to end of row
  1994. * - VS: Vertical Sync active high at beginning of frame
  1995. * - DE: Active high when data valid
  1996. * - A_CLK: 128*Fs
  1997. */
  1998. pdata->vidout_sel_hs = HS_HREF_SEL_HREF_VHREF;
  1999. pdata->vidout_sel_vs = VS_VREF_SEL_VREF_HDMI;
  2000. pdata->vidout_sel_de = DE_FREF_SEL_DE_VHREF;
  2001. np = state->client->dev.of_node;
  2002. ep = of_graph_get_endpoint_by_regs(np, 0, -1);
  2003. if (!ep)
  2004. return -EINVAL;
  2005. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
  2006. of_node_put(ep);
  2007. if (ret)
  2008. return ret;
  2009. pdata->vidout_bus_type = bus_cfg.bus_type;
  2010. /* polarity of HS/VS/DE */
  2011. flags = bus_cfg.bus.parallel.flags;
  2012. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  2013. pdata->vidout_inv_hs = 1;
  2014. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  2015. pdata->vidout_inv_vs = 1;
  2016. if (flags & V4L2_MBUS_DATA_ACTIVE_LOW)
  2017. pdata->vidout_inv_de = 1;
  2018. pdata->vidout_bus_width = bus_cfg.bus.parallel.bus_width;
  2019. /* video output port config */
  2020. ret = of_property_count_u32_elems(np, "nxp,vidout-portcfg");
  2021. if (ret > 0) {
  2022. u32 reg, val, i;
  2023. for (i = 0; i < ret / 2 && i < 9; i++) {
  2024. of_property_read_u32_index(np, "nxp,vidout-portcfg",
  2025. i * 2, &reg);
  2026. of_property_read_u32_index(np, "nxp,vidout-portcfg",
  2027. i * 2 + 1, &val);
  2028. if (reg < 9)
  2029. pdata->vidout_port_cfg[reg] = val;
  2030. }
  2031. } else {
  2032. v4l_err(state->client, "nxp,vidout-portcfg missing\n");
  2033. return -EINVAL;
  2034. }
  2035. /* default to channel layout dictated by packet header */
  2036. pdata->audout_layoutauto = true;
  2037. pdata->audout_format = AUDFMT_TYPE_DISABLED;
  2038. if (!of_property_read_string(np, "nxp,audout-format", &str)) {
  2039. if (strcmp(str, "i2s") == 0)
  2040. pdata->audout_format = AUDFMT_TYPE_I2S;
  2041. else if (strcmp(str, "spdif") == 0)
  2042. pdata->audout_format = AUDFMT_TYPE_SPDIF;
  2043. else {
  2044. v4l_err(state->client, "nxp,audout-format invalid\n");
  2045. return -EINVAL;
  2046. }
  2047. if (!of_property_read_u32(np, "nxp,audout-layout", &v)) {
  2048. switch (v) {
  2049. case 0:
  2050. case 1:
  2051. break;
  2052. default:
  2053. v4l_err(state->client,
  2054. "nxp,audout-layout invalid\n");
  2055. return -EINVAL;
  2056. }
  2057. pdata->audout_layout = v;
  2058. }
  2059. if (!of_property_read_u32(np, "nxp,audout-width", &v)) {
  2060. switch (v) {
  2061. case 16:
  2062. case 32:
  2063. break;
  2064. default:
  2065. v4l_err(state->client,
  2066. "nxp,audout-width invalid\n");
  2067. return -EINVAL;
  2068. }
  2069. pdata->audout_width = v;
  2070. }
  2071. if (!of_property_read_u32(np, "nxp,audout-mclk-fs", &v)) {
  2072. switch (v) {
  2073. case 512:
  2074. case 256:
  2075. case 128:
  2076. case 64:
  2077. case 32:
  2078. case 16:
  2079. break;
  2080. default:
  2081. v4l_err(state->client,
  2082. "nxp,audout-mclk-fs invalid\n");
  2083. return -EINVAL;
  2084. }
  2085. pdata->audout_mclk_fs = v;
  2086. }
  2087. }
  2088. return 0;
  2089. }
  2090. static int tda1997x_get_regulators(struct tda1997x_state *state)
  2091. {
  2092. int i;
  2093. for (i = 0; i < TDA1997X_NUM_SUPPLIES; i++)
  2094. state->supplies[i].supply = tda1997x_supply_name[i];
  2095. return devm_regulator_bulk_get(&state->client->dev,
  2096. TDA1997X_NUM_SUPPLIES,
  2097. state->supplies);
  2098. }
  2099. static int tda1997x_identify_module(struct tda1997x_state *state)
  2100. {
  2101. struct v4l2_subdev *sd = &state->sd;
  2102. enum tda1997x_type type;
  2103. u8 reg;
  2104. /* Read chip configuration*/
  2105. reg = io_read(sd, REG_CMTP_REG10);
  2106. state->tmdsb_clk = (reg >> 6) & 0x01; /* use tmds clock B_inv for B */
  2107. state->tmdsb_soc = (reg >> 5) & 0x01; /* tmds of input B */
  2108. state->port_30bit = (reg >> 2) & 0x03; /* 30bit vs 24bit */
  2109. state->output_2p5 = (reg >> 1) & 0x01; /* output supply 2.5v */
  2110. switch ((reg >> 4) & 0x03) {
  2111. case 0x00:
  2112. type = TDA19971;
  2113. break;
  2114. case 0x02:
  2115. case 0x03:
  2116. type = TDA19973;
  2117. break;
  2118. default:
  2119. dev_err(&state->client->dev, "unsupported chip ID\n");
  2120. return -EIO;
  2121. }
  2122. if (state->info->type != type) {
  2123. dev_err(&state->client->dev, "chip id mismatch\n");
  2124. return -EIO;
  2125. }
  2126. /* read chip revision */
  2127. state->chip_revision = io_read(sd, REG_CMTP_REG11);
  2128. return 0;
  2129. }
  2130. static const struct media_entity_operations tda1997x_media_ops = {
  2131. .link_validate = v4l2_subdev_link_validate,
  2132. };
  2133. /* -----------------------------------------------------------------------------
  2134. * HDMI Audio Codec
  2135. */
  2136. /* refine sample-rate based on HDMI source */
  2137. static int tda1997x_pcm_startup(struct snd_pcm_substream *substream,
  2138. struct snd_soc_dai *dai)
  2139. {
  2140. struct v4l2_subdev *sd = snd_soc_dai_get_drvdata(dai);
  2141. struct tda1997x_state *state = to_state(sd);
  2142. struct snd_soc_component *component = dai->component;
  2143. struct snd_pcm_runtime *rtd = substream->runtime;
  2144. int rate, err;
  2145. rate = state->audio_samplerate;
  2146. err = snd_pcm_hw_constraint_minmax(rtd, SNDRV_PCM_HW_PARAM_RATE,
  2147. rate, rate);
  2148. if (err < 0) {
  2149. dev_err(component->dev, "failed to constrain samplerate to %dHz\n",
  2150. rate);
  2151. return err;
  2152. }
  2153. dev_info(component->dev, "set samplerate constraint to %dHz\n", rate);
  2154. return 0;
  2155. }
  2156. static const struct snd_soc_dai_ops tda1997x_dai_ops = {
  2157. .startup = tda1997x_pcm_startup,
  2158. };
  2159. static struct snd_soc_dai_driver tda1997x_audio_dai = {
  2160. .name = "tda1997x",
  2161. .capture = {
  2162. .stream_name = "Capture",
  2163. .channels_min = 2,
  2164. .channels_max = 8,
  2165. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  2166. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  2167. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  2168. SNDRV_PCM_RATE_192000,
  2169. },
  2170. .ops = &tda1997x_dai_ops,
  2171. };
  2172. static int tda1997x_codec_probe(struct snd_soc_component *component)
  2173. {
  2174. return 0;
  2175. }
  2176. static void tda1997x_codec_remove(struct snd_soc_component *component)
  2177. {
  2178. }
  2179. static const struct snd_soc_component_driver tda1997x_codec_driver = {
  2180. .probe = tda1997x_codec_probe,
  2181. .remove = tda1997x_codec_remove,
  2182. .idle_bias_on = 1,
  2183. .use_pmdown_time = 1,
  2184. .endianness = 1,
  2185. };
  2186. static int tda1997x_probe(struct i2c_client *client)
  2187. {
  2188. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  2189. struct tda1997x_state *state;
  2190. struct tda1997x_platform_data *pdata;
  2191. struct v4l2_subdev *sd;
  2192. struct v4l2_ctrl_handler *hdl;
  2193. struct v4l2_ctrl *ctrl;
  2194. static const struct v4l2_dv_timings cea1920x1080 =
  2195. V4L2_DV_BT_CEA_1920X1080P60;
  2196. u32 *mbus_codes;
  2197. int i, ret;
  2198. /* Check if the adapter supports the needed features */
  2199. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2200. return -EIO;
  2201. state = kzalloc_obj(struct tda1997x_state);
  2202. if (!state)
  2203. return -ENOMEM;
  2204. state->client = client;
  2205. pdata = &state->pdata;
  2206. if (IS_ENABLED(CONFIG_OF) && client->dev.of_node) {
  2207. const struct of_device_id *oid;
  2208. oid = of_match_node(tda1997x_of_id, client->dev.of_node);
  2209. state->info = oid->data;
  2210. ret = tda1997x_parse_dt(state);
  2211. if (ret < 0) {
  2212. v4l_err(client, "DT parsing error\n");
  2213. goto err_free_state;
  2214. }
  2215. } else if (client->dev.platform_data) {
  2216. struct tda1997x_platform_data *pdata =
  2217. client->dev.platform_data;
  2218. state->info =
  2219. (const struct tda1997x_chip_info *)id->driver_data;
  2220. state->pdata = *pdata;
  2221. } else {
  2222. v4l_err(client, "No platform data\n");
  2223. ret = -ENODEV;
  2224. goto err_free_state;
  2225. }
  2226. ret = tda1997x_get_regulators(state);
  2227. if (ret)
  2228. goto err_free_state;
  2229. ret = tda1997x_set_power(state, 1);
  2230. if (ret)
  2231. goto err_free_state;
  2232. mutex_init(&state->page_lock);
  2233. mutex_init(&state->lock);
  2234. state->page = 0xff;
  2235. INIT_DELAYED_WORK(&state->delayed_work_enable_hpd,
  2236. tda1997x_delayed_work_enable_hpd);
  2237. /* set video format based on chip and bus width */
  2238. ret = tda1997x_identify_module(state);
  2239. if (ret)
  2240. goto err_free_mutex;
  2241. /* initialize subdev */
  2242. sd = &state->sd;
  2243. v4l2_i2c_subdev_init(sd, client, &tda1997x_subdev_ops);
  2244. sd->internal_ops = &tda1997x_internal_ops;
  2245. snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
  2246. id->name, i2c_adapter_id(client->adapter),
  2247. client->addr);
  2248. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  2249. sd->entity.function = MEDIA_ENT_F_DV_DECODER;
  2250. sd->entity.ops = &tda1997x_media_ops;
  2251. /* set allowed mbus modes based on chip, bus-type, and bus-width */
  2252. i = 0;
  2253. mbus_codes = state->mbus_codes;
  2254. switch (state->info->type) {
  2255. case TDA19973:
  2256. switch (pdata->vidout_bus_type) {
  2257. case V4L2_MBUS_PARALLEL:
  2258. switch (pdata->vidout_bus_width) {
  2259. case 36:
  2260. mbus_codes[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
  2261. mbus_codes[i++] = MEDIA_BUS_FMT_YUV12_1X36;
  2262. fallthrough;
  2263. case 24:
  2264. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
  2265. break;
  2266. }
  2267. break;
  2268. case V4L2_MBUS_BT656:
  2269. switch (pdata->vidout_bus_width) {
  2270. case 36:
  2271. case 24:
  2272. case 12:
  2273. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_2X12;
  2274. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY10_2X10;
  2275. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY8_2X8;
  2276. break;
  2277. }
  2278. break;
  2279. default:
  2280. break;
  2281. }
  2282. break;
  2283. case TDA19971:
  2284. switch (pdata->vidout_bus_type) {
  2285. case V4L2_MBUS_PARALLEL:
  2286. switch (pdata->vidout_bus_width) {
  2287. case 24:
  2288. mbus_codes[i++] = MEDIA_BUS_FMT_RGB888_1X24;
  2289. mbus_codes[i++] = MEDIA_BUS_FMT_YUV8_1X24;
  2290. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
  2291. fallthrough;
  2292. case 20:
  2293. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
  2294. fallthrough;
  2295. case 16:
  2296. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
  2297. break;
  2298. }
  2299. break;
  2300. case V4L2_MBUS_BT656:
  2301. switch (pdata->vidout_bus_width) {
  2302. case 24:
  2303. case 20:
  2304. case 16:
  2305. case 12:
  2306. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_2X12;
  2307. fallthrough;
  2308. case 10:
  2309. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY10_2X10;
  2310. fallthrough;
  2311. case 8:
  2312. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY8_2X8;
  2313. break;
  2314. }
  2315. break;
  2316. default:
  2317. break;
  2318. }
  2319. break;
  2320. }
  2321. if (WARN_ON(i > ARRAY_SIZE(state->mbus_codes))) {
  2322. ret = -EINVAL;
  2323. goto err_free_mutex;
  2324. }
  2325. /* default format */
  2326. tda1997x_setup_format(state, state->mbus_codes[0]);
  2327. state->timings = cea1920x1080;
  2328. /*
  2329. * default to SRGB full range quantization
  2330. * (in case we don't get an infoframe such as DVI signal
  2331. */
  2332. state->colorimetry.colorspace = V4L2_COLORSPACE_SRGB;
  2333. state->colorimetry.quantization = V4L2_QUANTIZATION_FULL_RANGE;
  2334. /* disable/reset HDCP to get correct I2C access to Rx HDMI */
  2335. io_write(sd, REG_MAN_SUS_HDMI_SEL, MAN_RST_HDCP | MAN_DIS_HDCP);
  2336. /*
  2337. * if N2 version, reset compdel_bp as it may generate some small pixel
  2338. * shifts in case of embedded sync/or delay lower than 4
  2339. */
  2340. if (state->chip_revision != 0) {
  2341. io_write(sd, REG_MAN_SUS_HDMI_SEL, 0x00);
  2342. io_write(sd, REG_VDP_CTRL, 0x1f);
  2343. }
  2344. v4l_info(client, "NXP %s N%d detected\n", state->info->name,
  2345. state->chip_revision + 1);
  2346. v4l_info(client, "video: %dbit %s %d formats available\n",
  2347. pdata->vidout_bus_width,
  2348. (pdata->vidout_bus_type == V4L2_MBUS_PARALLEL) ?
  2349. "parallel" : "BT656",
  2350. i);
  2351. if (pdata->audout_format) {
  2352. v4l_info(client, "audio: %dch %s layout%d sysclk=%d*fs\n",
  2353. pdata->audout_layout ? 2 : 8,
  2354. audfmt_names[pdata->audout_format],
  2355. pdata->audout_layout,
  2356. pdata->audout_mclk_fs);
  2357. }
  2358. ret = 0x34 + ((io_read(sd, REG_SLAVE_ADDR)>>4) & 0x03);
  2359. state->client_cec = devm_i2c_new_dummy_device(&client->dev,
  2360. client->adapter, ret);
  2361. if (IS_ERR(state->client_cec)) {
  2362. ret = PTR_ERR(state->client_cec);
  2363. goto err_free_mutex;
  2364. }
  2365. v4l_info(client, "CEC slave address 0x%02x\n", ret);
  2366. ret = tda1997x_core_init(sd);
  2367. if (ret)
  2368. goto err_free_mutex;
  2369. /* control handlers */
  2370. hdl = &state->hdl;
  2371. v4l2_ctrl_handler_init(hdl, 3);
  2372. ctrl = v4l2_ctrl_new_std_menu(hdl, &tda1997x_ctrl_ops,
  2373. V4L2_CID_DV_RX_IT_CONTENT_TYPE,
  2374. V4L2_DV_IT_CONTENT_TYPE_NO_ITC, 0,
  2375. V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
  2376. if (ctrl)
  2377. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  2378. /* custom controls */
  2379. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  2380. V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
  2381. state->rgb_quantization_range_ctrl = v4l2_ctrl_new_std_menu(hdl,
  2382. &tda1997x_ctrl_ops,
  2383. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL, 0,
  2384. V4L2_DV_RGB_RANGE_AUTO);
  2385. state->sd.ctrl_handler = hdl;
  2386. if (hdl->error) {
  2387. ret = hdl->error;
  2388. goto err_free_handler;
  2389. }
  2390. v4l2_ctrl_handler_setup(hdl);
  2391. /* initialize source pads */
  2392. state->pads[TDA1997X_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  2393. ret = media_entity_pads_init(&sd->entity, TDA1997X_NUM_PADS,
  2394. state->pads);
  2395. if (ret) {
  2396. v4l_err(client, "failed entity_init: %d", ret);
  2397. goto err_free_handler;
  2398. }
  2399. ret = v4l2_async_register_subdev(sd);
  2400. if (ret)
  2401. goto err_free_media;
  2402. /* register audio DAI */
  2403. if (pdata->audout_format) {
  2404. u64 formats;
  2405. if (pdata->audout_width == 32)
  2406. formats = SNDRV_PCM_FMTBIT_S32_LE;
  2407. else
  2408. formats = SNDRV_PCM_FMTBIT_S16_LE;
  2409. tda1997x_audio_dai.capture.formats = formats;
  2410. ret = devm_snd_soc_register_component(&state->client->dev,
  2411. &tda1997x_codec_driver,
  2412. &tda1997x_audio_dai, 1);
  2413. if (ret) {
  2414. dev_err(&client->dev, "register audio codec failed\n");
  2415. goto err_free_media;
  2416. }
  2417. v4l_info(state->client, "registered audio codec\n");
  2418. }
  2419. /* request irq */
  2420. ret = devm_request_threaded_irq(&client->dev, client->irq,
  2421. NULL, tda1997x_isr_thread,
  2422. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  2423. KBUILD_MODNAME, state);
  2424. if (ret) {
  2425. v4l_err(client, "irq%d reg failed: %d\n", client->irq, ret);
  2426. goto err_free_media;
  2427. }
  2428. return 0;
  2429. err_free_media:
  2430. media_entity_cleanup(&sd->entity);
  2431. err_free_handler:
  2432. v4l2_ctrl_handler_free(&state->hdl);
  2433. err_free_mutex:
  2434. mutex_destroy(&state->page_lock);
  2435. mutex_destroy(&state->lock);
  2436. tda1997x_set_power(state, 0);
  2437. err_free_state:
  2438. kfree(state);
  2439. dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
  2440. return ret;
  2441. }
  2442. static void tda1997x_remove(struct i2c_client *client)
  2443. {
  2444. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2445. struct tda1997x_state *state = to_state(sd);
  2446. struct tda1997x_platform_data *pdata = &state->pdata;
  2447. if (pdata->audout_format) {
  2448. mutex_destroy(&state->audio_lock);
  2449. }
  2450. disable_irq(state->client->irq);
  2451. tda1997x_power_mode(state, 0);
  2452. v4l2_async_unregister_subdev(sd);
  2453. media_entity_cleanup(&sd->entity);
  2454. v4l2_ctrl_handler_free(&state->hdl);
  2455. regulator_bulk_disable(TDA1997X_NUM_SUPPLIES, state->supplies);
  2456. cancel_delayed_work_sync(&state->delayed_work_enable_hpd);
  2457. mutex_destroy(&state->page_lock);
  2458. mutex_destroy(&state->lock);
  2459. kfree(state);
  2460. }
  2461. static struct i2c_driver tda1997x_i2c_driver = {
  2462. .driver = {
  2463. .name = "tda1997x",
  2464. .of_match_table = of_match_ptr(tda1997x_of_id),
  2465. },
  2466. .probe = tda1997x_probe,
  2467. .remove = tda1997x_remove,
  2468. .id_table = tda1997x_i2c_id,
  2469. };
  2470. module_i2c_driver(tda1997x_i2c_driver);
  2471. MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
  2472. MODULE_DESCRIPTION("TDA1997X HDMI Receiver driver");
  2473. MODULE_LICENSE("GPL v2");