tda1997x.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821
  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 100ms */
  515. schedule_delayed_work(&state->delayed_work_enable_hpd, HZ / 10);
  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 = 1;
  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 = 0;
  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 = 0;
  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. u32 vper;
  971. u16 hper;
  972. u16 hsper;
  973. int i;
  974. /*
  975. * Read the FMT registers
  976. * REG_V_PER: Period of a frame (or two fields) in MCLK(27MHz) cycles
  977. * REG_H_PER: Period of a line in MCLK(27MHz) cycles
  978. * REG_HS_WIDTH: Period of horiz sync pulse in MCLK(27MHz) cycles
  979. */
  980. vper = io_read24(sd, REG_V_PER) & MASK_VPER;
  981. hper = io_read16(sd, REG_H_PER) & MASK_HPER;
  982. hsper = io_read16(sd, REG_HS_WIDTH) & MASK_HSWIDTH;
  983. v4l2_dbg(1, debug, sd, "Signal Timings: %u/%u/%u\n", vper, hper, hsper);
  984. if (!vper || !hper || !hsper)
  985. return -ENOLINK;
  986. for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
  987. const struct v4l2_bt_timings *bt;
  988. u32 lines, width, _hper, _hsper;
  989. u32 vmin, vmax, hmin, hmax, hsmin, hsmax;
  990. bool vmatch, hmatch, hsmatch;
  991. bt = &v4l2_dv_timings_presets[i].bt;
  992. width = V4L2_DV_BT_FRAME_WIDTH(bt);
  993. lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
  994. _hper = (u32)bt->pixelclock / width;
  995. if (bt->interlaced)
  996. lines /= 2;
  997. /* vper +/- 0.7% */
  998. vmin = ((27000000 / 1000) * 993) / _hper * lines;
  999. vmax = ((27000000 / 1000) * 1007) / _hper * lines;
  1000. /* hper +/- 1.0% */
  1001. hmin = ((27000000 / 100) * 99) / _hper;
  1002. hmax = ((27000000 / 100) * 101) / _hper;
  1003. /* hsper +/- 2 (take care to avoid 32bit overflow) */
  1004. _hsper = 27000 * bt->hsync / ((u32)bt->pixelclock/1000);
  1005. hsmin = _hsper - 2;
  1006. hsmax = _hsper + 2;
  1007. /* vmatch matches the framerate */
  1008. vmatch = ((vper <= vmax) && (vper >= vmin)) ? 1 : 0;
  1009. /* hmatch matches the width */
  1010. hmatch = ((hper <= hmax) && (hper >= hmin)) ? 1 : 0;
  1011. /* hsmatch matches the hswidth */
  1012. hsmatch = ((hsper <= hsmax) && (hsper >= hsmin)) ? 1 : 0;
  1013. if (hmatch && vmatch && hsmatch) {
  1014. v4l2_print_dv_timings(sd->name, "Detected format: ",
  1015. &v4l2_dv_timings_presets[i],
  1016. false);
  1017. if (timings)
  1018. *timings = v4l2_dv_timings_presets[i];
  1019. return 0;
  1020. }
  1021. }
  1022. v4l_err(state->client, "no resolution match for timings: %d/%d/%d\n",
  1023. vper, hper, hsper);
  1024. return -ERANGE;
  1025. }
  1026. /* some sort of errata workaround for chip revision 0 (N1) */
  1027. static void tda1997x_reset_n1(struct tda1997x_state *state)
  1028. {
  1029. struct v4l2_subdev *sd = &state->sd;
  1030. u8 reg;
  1031. /* clear HDMI mode flag in BCAPS */
  1032. io_write(sd, REG_CLK_CFG, CLK_CFG_SEL_ACLK_EN | CLK_CFG_SEL_ACLK);
  1033. io_write(sd, REG_PON_OVR_EN, PON_EN);
  1034. io_write(sd, REG_PON_CBIAS, PON_EN);
  1035. io_write(sd, REG_PON_PLL, PON_EN);
  1036. reg = io_read(sd, REG_MODE_REC_CFG1);
  1037. reg &= ~0x06;
  1038. reg |= 0x02;
  1039. io_write(sd, REG_MODE_REC_CFG1, reg);
  1040. io_write(sd, REG_CLK_CFG, CLK_CFG_DIS);
  1041. io_write(sd, REG_PON_OVR_EN, PON_DIS);
  1042. reg = io_read(sd, REG_MODE_REC_CFG1);
  1043. reg &= ~0x06;
  1044. io_write(sd, REG_MODE_REC_CFG1, reg);
  1045. }
  1046. /*
  1047. * Activity detection must only be notified when stable_clk_x AND active_x
  1048. * bits are set to 1. If only stable_clk_x bit is set to 1 but not
  1049. * active_x, it means that the TMDS clock is not in the defined range
  1050. * and activity detection must not be notified.
  1051. */
  1052. static u8
  1053. tda1997x_read_activity_status_regs(struct v4l2_subdev *sd)
  1054. {
  1055. u8 reg, status = 0;
  1056. /* Read CLK_A_STATUS register */
  1057. reg = io_read(sd, REG_CLK_A_STATUS);
  1058. /* ignore if not active */
  1059. if ((reg & MASK_CLK_STABLE) && !(reg & MASK_CLK_ACTIVE))
  1060. reg &= ~MASK_CLK_STABLE;
  1061. status |= ((reg & MASK_CLK_STABLE) >> 2);
  1062. /* Read CLK_B_STATUS register */
  1063. reg = io_read(sd, REG_CLK_B_STATUS);
  1064. /* ignore if not active */
  1065. if ((reg & MASK_CLK_STABLE) && !(reg & MASK_CLK_ACTIVE))
  1066. reg &= ~MASK_CLK_STABLE;
  1067. status |= ((reg & MASK_CLK_STABLE) >> 1);
  1068. /* Read the SUS_STATUS register */
  1069. reg = io_read(sd, REG_SUS_STATUS);
  1070. /* If state = 5 => TMDS is locked */
  1071. if ((reg & MASK_SUS_STATUS) == LAST_STATE_REACHED)
  1072. status |= MASK_SUS_STATE;
  1073. else
  1074. status &= ~MASK_SUS_STATE;
  1075. return status;
  1076. }
  1077. static void
  1078. set_rgb_quantization_range(struct tda1997x_state *state)
  1079. {
  1080. struct v4l2_hdmi_colorimetry *c = &state->colorimetry;
  1081. state->colorimetry = v4l2_hdmi_rx_colorimetry(&state->avi_infoframe,
  1082. NULL,
  1083. state->timings.bt.height);
  1084. /* If ycbcr_enc is V4L2_YCBCR_ENC_DEFAULT, we receive RGB */
  1085. if (c->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
  1086. switch (state->rgb_quantization_range) {
  1087. case V4L2_DV_RGB_RANGE_LIMITED:
  1088. c->quantization = V4L2_QUANTIZATION_FULL_RANGE;
  1089. break;
  1090. case V4L2_DV_RGB_RANGE_FULL:
  1091. c->quantization = V4L2_QUANTIZATION_LIM_RANGE;
  1092. break;
  1093. }
  1094. }
  1095. v4l_dbg(1, debug, state->client,
  1096. "colorspace=%d/%d colorimetry=%d range=%s content=%d\n",
  1097. state->avi_infoframe.colorspace, c->colorspace,
  1098. state->avi_infoframe.colorimetry,
  1099. v4l2_quantization_names[c->quantization],
  1100. state->avi_infoframe.content_type);
  1101. }
  1102. /* parse an infoframe and do some sanity checks on it */
  1103. static unsigned int
  1104. tda1997x_parse_infoframe(struct tda1997x_state *state, u16 addr)
  1105. {
  1106. struct v4l2_subdev *sd = &state->sd;
  1107. union hdmi_infoframe frame;
  1108. u8 buffer[40];
  1109. u8 reg;
  1110. int len, err;
  1111. /* read data */
  1112. len = io_readn(sd, addr, sizeof(buffer), buffer);
  1113. err = hdmi_infoframe_unpack(&frame, buffer);
  1114. if (err) {
  1115. v4l_err(state->client,
  1116. "failed parsing %d byte infoframe: 0x%04x/0x%02x\n",
  1117. len, addr, buffer[0]);
  1118. return err;
  1119. }
  1120. hdmi_infoframe_log(KERN_INFO, &state->client->dev, &frame);
  1121. switch (frame.any.type) {
  1122. /* Audio InfoFrame: see HDMI spec 8.2.2 */
  1123. case HDMI_INFOFRAME_TYPE_AUDIO:
  1124. /* sample rate */
  1125. switch (frame.audio.sample_frequency) {
  1126. case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
  1127. state->audio_samplerate = 32000;
  1128. break;
  1129. case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
  1130. state->audio_samplerate = 44100;
  1131. break;
  1132. case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
  1133. state->audio_samplerate = 48000;
  1134. break;
  1135. case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
  1136. state->audio_samplerate = 88200;
  1137. break;
  1138. case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
  1139. state->audio_samplerate = 96000;
  1140. break;
  1141. case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
  1142. state->audio_samplerate = 176400;
  1143. break;
  1144. case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
  1145. state->audio_samplerate = 192000;
  1146. break;
  1147. default:
  1148. case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
  1149. break;
  1150. }
  1151. /* sample size */
  1152. switch (frame.audio.sample_size) {
  1153. case HDMI_AUDIO_SAMPLE_SIZE_16:
  1154. state->audio_samplesize = 16;
  1155. break;
  1156. case HDMI_AUDIO_SAMPLE_SIZE_20:
  1157. state->audio_samplesize = 20;
  1158. break;
  1159. case HDMI_AUDIO_SAMPLE_SIZE_24:
  1160. state->audio_samplesize = 24;
  1161. break;
  1162. case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
  1163. default:
  1164. break;
  1165. }
  1166. /* Channel Count */
  1167. state->audio_channels = frame.audio.channels;
  1168. if (frame.audio.channel_allocation &&
  1169. frame.audio.channel_allocation != state->audio_ch_alloc) {
  1170. /* use the channel assignment from the infoframe */
  1171. state->audio_ch_alloc = frame.audio.channel_allocation;
  1172. tda1997x_configure_audout(sd, state->audio_ch_alloc);
  1173. /* reset the audio FIFO */
  1174. tda1997x_hdmi_info_reset(sd, RESET_AUDIO, false);
  1175. }
  1176. break;
  1177. /* Auxiliary Video information (AVI) InfoFrame: see HDMI spec 8.2.1 */
  1178. case HDMI_INFOFRAME_TYPE_AVI:
  1179. state->avi_infoframe = frame.avi;
  1180. set_rgb_quantization_range(state);
  1181. /* configure upsampler: 0=bypass 1=repeatchroma 2=interpolate */
  1182. reg = io_read(sd, REG_PIX_REPEAT);
  1183. reg &= ~PIX_REPEAT_MASK_UP_SEL;
  1184. if (frame.avi.colorspace == HDMI_COLORSPACE_YUV422)
  1185. reg |= (PIX_REPEAT_CHROMA << PIX_REPEAT_SHIFT);
  1186. io_write(sd, REG_PIX_REPEAT, reg);
  1187. /* ConfigurePixelRepeater: repeat n-times each pixel */
  1188. reg = io_read(sd, REG_PIX_REPEAT);
  1189. reg &= ~PIX_REPEAT_MASK_REP;
  1190. reg |= frame.avi.pixel_repeat;
  1191. io_write(sd, REG_PIX_REPEAT, reg);
  1192. /* configure the receiver with the new colorspace */
  1193. tda1997x_configure_csc(sd);
  1194. break;
  1195. default:
  1196. break;
  1197. }
  1198. return 0;
  1199. }
  1200. static void tda1997x_irq_sus(struct tda1997x_state *state, u8 *flags)
  1201. {
  1202. struct v4l2_subdev *sd = &state->sd;
  1203. u8 reg, source;
  1204. source = io_read(sd, REG_INT_FLG_CLR_SUS);
  1205. io_write(sd, REG_INT_FLG_CLR_SUS, source);
  1206. if (source & MASK_MPT) {
  1207. /* reset MTP in use flag if set */
  1208. if (state->mptrw_in_progress)
  1209. state->mptrw_in_progress = 0;
  1210. }
  1211. if (source & MASK_SUS_END) {
  1212. /* reset audio FIFO */
  1213. reg = io_read(sd, REG_HDMI_INFO_RST);
  1214. reg |= MASK_SR_FIFO_FIFO_CTRL;
  1215. io_write(sd, REG_HDMI_INFO_RST, reg);
  1216. reg &= ~MASK_SR_FIFO_FIFO_CTRL;
  1217. io_write(sd, REG_HDMI_INFO_RST, reg);
  1218. /* reset HDMI flags */
  1219. state->hdmi_status = 0;
  1220. }
  1221. /* filter FMT interrupt based on SUS state */
  1222. reg = io_read(sd, REG_SUS_STATUS);
  1223. if (((reg & MASK_SUS_STATUS) != LAST_STATE_REACHED)
  1224. || (source & MASK_MPT)) {
  1225. source &= ~MASK_FMT;
  1226. }
  1227. if (source & (MASK_FMT | MASK_SUS_END)) {
  1228. reg = io_read(sd, REG_SUS_STATUS);
  1229. if ((reg & MASK_SUS_STATUS) != LAST_STATE_REACHED) {
  1230. v4l_err(state->client, "BAD SUS STATUS\n");
  1231. return;
  1232. }
  1233. if (debug)
  1234. tda1997x_detect_std(state, NULL);
  1235. /* notify user of change in resolution */
  1236. v4l2_subdev_notify_event(&state->sd, &tda1997x_ev_fmt);
  1237. }
  1238. }
  1239. static void tda1997x_irq_ddc(struct tda1997x_state *state, u8 *flags)
  1240. {
  1241. struct v4l2_subdev *sd = &state->sd;
  1242. u8 source;
  1243. source = io_read(sd, REG_INT_FLG_CLR_DDC);
  1244. io_write(sd, REG_INT_FLG_CLR_DDC, source);
  1245. if (source & MASK_EDID_MTP) {
  1246. /* reset MTP in use flag if set */
  1247. if (state->mptrw_in_progress)
  1248. state->mptrw_in_progress = 0;
  1249. }
  1250. /* Detection of +5V */
  1251. if (source & MASK_DET_5V) {
  1252. v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
  1253. tda1997x_detect_tx_5v(sd));
  1254. }
  1255. }
  1256. static void tda1997x_irq_rate(struct tda1997x_state *state, u8 *flags)
  1257. {
  1258. struct v4l2_subdev *sd = &state->sd;
  1259. u8 reg, source;
  1260. u8 irq_status;
  1261. source = io_read(sd, REG_INT_FLG_CLR_RATE);
  1262. io_write(sd, REG_INT_FLG_CLR_RATE, source);
  1263. /* read status regs */
  1264. irq_status = tda1997x_read_activity_status_regs(sd);
  1265. /*
  1266. * read clock status reg until INT_FLG_CLR_RATE is still 0
  1267. * after the read to make sure its the last one
  1268. */
  1269. reg = source;
  1270. while (reg != 0) {
  1271. irq_status = tda1997x_read_activity_status_regs(sd);
  1272. reg = io_read(sd, REG_INT_FLG_CLR_RATE);
  1273. io_write(sd, REG_INT_FLG_CLR_RATE, reg);
  1274. source |= reg;
  1275. }
  1276. /* we only pay attention to stability change events */
  1277. if (source & (MASK_RATE_A_ST | MASK_RATE_B_ST)) {
  1278. int input = (source & MASK_RATE_A_ST)?0:1;
  1279. u8 mask = 1<<input;
  1280. /* state change */
  1281. if ((irq_status & mask) != (state->activity_status & mask)) {
  1282. /* activity lost */
  1283. if ((irq_status & mask) == 0) {
  1284. v4l_info(state->client,
  1285. "HDMI-%c: Digital Activity Lost\n",
  1286. input+'A');
  1287. /* bypass up/down sampler and pixel repeater */
  1288. reg = io_read(sd, REG_PIX_REPEAT);
  1289. reg &= ~PIX_REPEAT_MASK_UP_SEL;
  1290. reg &= ~PIX_REPEAT_MASK_REP;
  1291. io_write(sd, REG_PIX_REPEAT, reg);
  1292. if (state->chip_revision == 0)
  1293. tda1997x_reset_n1(state);
  1294. state->input_detect[input] = 0;
  1295. v4l2_subdev_notify_event(sd, &tda1997x_ev_fmt);
  1296. }
  1297. /* activity detected */
  1298. else {
  1299. v4l_info(state->client,
  1300. "HDMI-%c: Digital Activity Detected\n",
  1301. input+'A');
  1302. state->input_detect[input] = 1;
  1303. }
  1304. /* hold onto current state */
  1305. state->activity_status = (irq_status & mask);
  1306. }
  1307. }
  1308. }
  1309. static void tda1997x_irq_info(struct tda1997x_state *state, u8 *flags)
  1310. {
  1311. struct v4l2_subdev *sd = &state->sd;
  1312. u8 source;
  1313. source = io_read(sd, REG_INT_FLG_CLR_INFO);
  1314. io_write(sd, REG_INT_FLG_CLR_INFO, source);
  1315. /* Audio infoframe */
  1316. if (source & MASK_AUD_IF) {
  1317. tda1997x_parse_infoframe(state, AUD_IF);
  1318. source &= ~MASK_AUD_IF;
  1319. }
  1320. /* Source Product Descriptor infoframe change */
  1321. if (source & MASK_SPD_IF) {
  1322. tda1997x_parse_infoframe(state, SPD_IF);
  1323. source &= ~MASK_SPD_IF;
  1324. }
  1325. /* Auxiliary Video Information infoframe */
  1326. if (source & MASK_AVI_IF) {
  1327. tda1997x_parse_infoframe(state, AVI_IF);
  1328. source &= ~MASK_AVI_IF;
  1329. }
  1330. }
  1331. static void tda1997x_irq_audio(struct tda1997x_state *state, u8 *flags)
  1332. {
  1333. struct v4l2_subdev *sd = &state->sd;
  1334. u8 reg, source;
  1335. source = io_read(sd, REG_INT_FLG_CLR_AUDIO);
  1336. io_write(sd, REG_INT_FLG_CLR_AUDIO, source);
  1337. /* reset audio FIFO on FIFO pointer error or audio mute */
  1338. if (source & MASK_ERROR_FIFO_PT ||
  1339. source & MASK_MUTE_FLG) {
  1340. /* audio reset audio FIFO */
  1341. reg = io_read(sd, REG_SUS_STATUS);
  1342. if ((reg & MASK_SUS_STATUS) == LAST_STATE_REACHED) {
  1343. reg = io_read(sd, REG_HDMI_INFO_RST);
  1344. reg |= MASK_SR_FIFO_FIFO_CTRL;
  1345. io_write(sd, REG_HDMI_INFO_RST, reg);
  1346. reg &= ~MASK_SR_FIFO_FIFO_CTRL;
  1347. io_write(sd, REG_HDMI_INFO_RST, reg);
  1348. /* reset channel status IT if present */
  1349. source &= ~(MASK_CH_STATE);
  1350. }
  1351. }
  1352. if (source & MASK_AUDIO_FREQ_FLG) {
  1353. static const int freq[] = {
  1354. 0, 32000, 44100, 48000, 88200, 96000, 176400, 192000
  1355. };
  1356. reg = io_read(sd, REG_AUDIO_FREQ);
  1357. state->audio_samplerate = freq[reg & 7];
  1358. v4l_info(state->client, "Audio Frequency Change: %dHz\n",
  1359. state->audio_samplerate);
  1360. }
  1361. if (source & MASK_AUDIO_FLG) {
  1362. reg = io_read(sd, REG_AUDIO_FLAGS);
  1363. if (reg & BIT(AUDCFG_TYPE_DST))
  1364. state->audio_type = AUDCFG_TYPE_DST;
  1365. if (reg & BIT(AUDCFG_TYPE_OBA))
  1366. state->audio_type = AUDCFG_TYPE_OBA;
  1367. if (reg & BIT(AUDCFG_TYPE_HBR))
  1368. state->audio_type = AUDCFG_TYPE_HBR;
  1369. if (reg & BIT(AUDCFG_TYPE_PCM))
  1370. state->audio_type = AUDCFG_TYPE_PCM;
  1371. v4l_info(state->client, "Audio Type: %s\n",
  1372. audtype_names[state->audio_type]);
  1373. }
  1374. }
  1375. static void tda1997x_irq_hdcp(struct tda1997x_state *state, u8 *flags)
  1376. {
  1377. struct v4l2_subdev *sd = &state->sd;
  1378. u8 reg, source;
  1379. source = io_read(sd, REG_INT_FLG_CLR_HDCP);
  1380. io_write(sd, REG_INT_FLG_CLR_HDCP, source);
  1381. /* reset MTP in use flag if set */
  1382. if (source & MASK_HDCP_MTP)
  1383. state->mptrw_in_progress = 0;
  1384. if (source & MASK_STATE_C5) {
  1385. /* REPEATER: mask AUDIO and IF irqs to avoid IF during auth */
  1386. reg = io_read(sd, REG_INT_MASK_TOP);
  1387. reg &= ~(INTERRUPT_AUDIO | INTERRUPT_INFO);
  1388. io_write(sd, REG_INT_MASK_TOP, reg);
  1389. *flags &= (INTERRUPT_AUDIO | INTERRUPT_INFO);
  1390. }
  1391. }
  1392. static irqreturn_t tda1997x_isr_thread(int irq, void *d)
  1393. {
  1394. struct tda1997x_state *state = d;
  1395. struct v4l2_subdev *sd = &state->sd;
  1396. u8 flags;
  1397. mutex_lock(&state->lock);
  1398. do {
  1399. /* read interrupt flags */
  1400. flags = io_read(sd, REG_INT_FLG_CLR_TOP);
  1401. if (flags == 0)
  1402. break;
  1403. /* SUS interrupt source (Input activity events) */
  1404. if (flags & INTERRUPT_SUS)
  1405. tda1997x_irq_sus(state, &flags);
  1406. /* DDC interrupt source (Display Data Channel) */
  1407. else if (flags & INTERRUPT_DDC)
  1408. tda1997x_irq_ddc(state, &flags);
  1409. /* RATE interrupt source (Digital Input activity) */
  1410. else if (flags & INTERRUPT_RATE)
  1411. tda1997x_irq_rate(state, &flags);
  1412. /* Infoframe change interrupt */
  1413. else if (flags & INTERRUPT_INFO)
  1414. tda1997x_irq_info(state, &flags);
  1415. /* Audio interrupt source:
  1416. * freq change, DST,OBA,HBR,ASP flags, mute, FIFO err
  1417. */
  1418. else if (flags & INTERRUPT_AUDIO)
  1419. tda1997x_irq_audio(state, &flags);
  1420. /* HDCP interrupt source (content protection) */
  1421. if (flags & INTERRUPT_HDCP)
  1422. tda1997x_irq_hdcp(state, &flags);
  1423. } while (flags != 0);
  1424. mutex_unlock(&state->lock);
  1425. return IRQ_HANDLED;
  1426. }
  1427. /* -----------------------------------------------------------------------------
  1428. * v4l2_subdev_video_ops
  1429. */
  1430. static int
  1431. tda1997x_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1432. {
  1433. struct tda1997x_state *state = to_state(sd);
  1434. u32 vper;
  1435. u16 hper;
  1436. u16 hsper;
  1437. mutex_lock(&state->lock);
  1438. vper = io_read24(sd, REG_V_PER) & MASK_VPER;
  1439. hper = io_read16(sd, REG_H_PER) & MASK_HPER;
  1440. hsper = io_read16(sd, REG_HS_WIDTH) & MASK_HSWIDTH;
  1441. /*
  1442. * The tda1997x supports A/B inputs but only a single output.
  1443. * The irq handler monitors for timing changes on both inputs and
  1444. * sets the input_detect array to 0|1 depending on signal presence.
  1445. * I believe selection of A vs B is automatic.
  1446. *
  1447. * The vper/hper/hsper registers provide the frame period, line period
  1448. * and horiz sync period (units of MCLK clock cycles (27MHz)) and
  1449. * testing shows these values to be random if no signal is present
  1450. * or locked.
  1451. */
  1452. v4l2_dbg(1, debug, sd, "inputs:%d/%d timings:%d/%d/%d\n",
  1453. state->input_detect[0], state->input_detect[1],
  1454. vper, hper, hsper);
  1455. if (!state->input_detect[0] && !state->input_detect[1])
  1456. *status = V4L2_IN_ST_NO_SIGNAL;
  1457. else if (!vper || !hper || !hsper)
  1458. *status = V4L2_IN_ST_NO_SYNC;
  1459. else
  1460. *status = 0;
  1461. mutex_unlock(&state->lock);
  1462. return 0;
  1463. };
  1464. static int tda1997x_s_dv_timings(struct v4l2_subdev *sd,
  1465. struct v4l2_dv_timings *timings)
  1466. {
  1467. struct tda1997x_state *state = to_state(sd);
  1468. v4l_dbg(1, debug, state->client, "%s\n", __func__);
  1469. if (v4l2_match_dv_timings(&state->timings, timings, 0, false))
  1470. return 0; /* no changes */
  1471. if (!v4l2_valid_dv_timings(timings, &tda1997x_dv_timings_cap,
  1472. NULL, NULL))
  1473. return -ERANGE;
  1474. mutex_lock(&state->lock);
  1475. state->timings = *timings;
  1476. /* setup frame detection window and VHREF timing generator */
  1477. tda1997x_configure_vhref(sd);
  1478. /* configure colorspace conversion */
  1479. tda1997x_configure_csc(sd);
  1480. mutex_unlock(&state->lock);
  1481. return 0;
  1482. }
  1483. static int tda1997x_g_dv_timings(struct v4l2_subdev *sd,
  1484. struct v4l2_dv_timings *timings)
  1485. {
  1486. struct tda1997x_state *state = to_state(sd);
  1487. v4l_dbg(1, debug, state->client, "%s\n", __func__);
  1488. mutex_lock(&state->lock);
  1489. *timings = state->timings;
  1490. mutex_unlock(&state->lock);
  1491. return 0;
  1492. }
  1493. static int tda1997x_query_dv_timings(struct v4l2_subdev *sd,
  1494. struct v4l2_dv_timings *timings)
  1495. {
  1496. struct tda1997x_state *state = to_state(sd);
  1497. v4l_dbg(1, debug, state->client, "%s\n", __func__);
  1498. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1499. mutex_lock(&state->lock);
  1500. tda1997x_detect_std(state, timings);
  1501. mutex_unlock(&state->lock);
  1502. return 0;
  1503. }
  1504. static const struct v4l2_subdev_video_ops tda1997x_video_ops = {
  1505. .g_input_status = tda1997x_g_input_status,
  1506. .s_dv_timings = tda1997x_s_dv_timings,
  1507. .g_dv_timings = tda1997x_g_dv_timings,
  1508. .query_dv_timings = tda1997x_query_dv_timings,
  1509. };
  1510. /* -----------------------------------------------------------------------------
  1511. * v4l2_subdev_pad_ops
  1512. */
  1513. static int tda1997x_init_cfg(struct v4l2_subdev *sd,
  1514. struct v4l2_subdev_pad_config *cfg)
  1515. {
  1516. struct tda1997x_state *state = to_state(sd);
  1517. struct v4l2_mbus_framefmt *mf;
  1518. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  1519. mf->code = state->mbus_codes[0];
  1520. return 0;
  1521. }
  1522. static int tda1997x_enum_mbus_code(struct v4l2_subdev *sd,
  1523. struct v4l2_subdev_pad_config *cfg,
  1524. struct v4l2_subdev_mbus_code_enum *code)
  1525. {
  1526. struct tda1997x_state *state = to_state(sd);
  1527. v4l_dbg(1, debug, state->client, "%s %d\n", __func__, code->index);
  1528. if (code->index >= ARRAY_SIZE(state->mbus_codes))
  1529. return -EINVAL;
  1530. if (!state->mbus_codes[code->index])
  1531. return -EINVAL;
  1532. code->code = state->mbus_codes[code->index];
  1533. return 0;
  1534. }
  1535. static void tda1997x_fill_format(struct tda1997x_state *state,
  1536. struct v4l2_mbus_framefmt *format)
  1537. {
  1538. const struct v4l2_bt_timings *bt;
  1539. memset(format, 0, sizeof(*format));
  1540. bt = &state->timings.bt;
  1541. format->width = bt->width;
  1542. format->height = bt->height;
  1543. format->colorspace = state->colorimetry.colorspace;
  1544. format->field = (bt->interlaced) ?
  1545. V4L2_FIELD_SEQ_TB : V4L2_FIELD_NONE;
  1546. }
  1547. static int tda1997x_get_format(struct v4l2_subdev *sd,
  1548. struct v4l2_subdev_pad_config *cfg,
  1549. struct v4l2_subdev_format *format)
  1550. {
  1551. struct tda1997x_state *state = to_state(sd);
  1552. v4l_dbg(1, debug, state->client, "%s pad=%d which=%d\n",
  1553. __func__, format->pad, format->which);
  1554. tda1997x_fill_format(state, &format->format);
  1555. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1556. struct v4l2_mbus_framefmt *fmt;
  1557. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  1558. format->format.code = fmt->code;
  1559. } else
  1560. format->format.code = state->mbus_code;
  1561. return 0;
  1562. }
  1563. static int tda1997x_set_format(struct v4l2_subdev *sd,
  1564. struct v4l2_subdev_pad_config *cfg,
  1565. struct v4l2_subdev_format *format)
  1566. {
  1567. struct tda1997x_state *state = to_state(sd);
  1568. u32 code = 0;
  1569. int i;
  1570. v4l_dbg(1, debug, state->client, "%s pad=%d which=%d fmt=0x%x\n",
  1571. __func__, format->pad, format->which, format->format.code);
  1572. for (i = 0; i < ARRAY_SIZE(state->mbus_codes); i++) {
  1573. if (format->format.code == state->mbus_codes[i]) {
  1574. code = state->mbus_codes[i];
  1575. break;
  1576. }
  1577. }
  1578. if (!code)
  1579. code = state->mbus_codes[0];
  1580. tda1997x_fill_format(state, &format->format);
  1581. format->format.code = code;
  1582. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1583. struct v4l2_mbus_framefmt *fmt;
  1584. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  1585. *fmt = format->format;
  1586. } else {
  1587. int ret = tda1997x_setup_format(state, format->format.code);
  1588. if (ret)
  1589. return ret;
  1590. /* mbus_code has changed - re-configure csc/vidout */
  1591. tda1997x_configure_csc(sd);
  1592. tda1997x_configure_vidout(state);
  1593. }
  1594. return 0;
  1595. }
  1596. static int tda1997x_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1597. {
  1598. struct tda1997x_state *state = to_state(sd);
  1599. v4l_dbg(1, debug, state->client, "%s pad=%d\n", __func__, edid->pad);
  1600. memset(edid->reserved, 0, sizeof(edid->reserved));
  1601. if (edid->start_block == 0 && edid->blocks == 0) {
  1602. edid->blocks = state->edid.blocks;
  1603. return 0;
  1604. }
  1605. if (!state->edid.present)
  1606. return -ENODATA;
  1607. if (edid->start_block >= state->edid.blocks)
  1608. return -EINVAL;
  1609. if (edid->start_block + edid->blocks > state->edid.blocks)
  1610. edid->blocks = state->edid.blocks - edid->start_block;
  1611. memcpy(edid->edid, state->edid.edid + edid->start_block * 128,
  1612. edid->blocks * 128);
  1613. return 0;
  1614. }
  1615. static int tda1997x_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1616. {
  1617. struct tda1997x_state *state = to_state(sd);
  1618. int i;
  1619. v4l_dbg(1, debug, state->client, "%s pad=%d\n", __func__, edid->pad);
  1620. memset(edid->reserved, 0, sizeof(edid->reserved));
  1621. if (edid->start_block != 0)
  1622. return -EINVAL;
  1623. if (edid->blocks == 0) {
  1624. state->edid.blocks = 0;
  1625. state->edid.present = 0;
  1626. tda1997x_disable_edid(sd);
  1627. return 0;
  1628. }
  1629. if (edid->blocks > 2) {
  1630. edid->blocks = 2;
  1631. return -E2BIG;
  1632. }
  1633. tda1997x_disable_edid(sd);
  1634. /* write base EDID */
  1635. for (i = 0; i < 128; i++)
  1636. io_write(sd, REG_EDID_IN_BYTE0 + i, edid->edid[i]);
  1637. /* write CEA Extension */
  1638. for (i = 0; i < 128; i++)
  1639. io_write(sd, REG_EDID_IN_BYTE128 + i, edid->edid[i+128]);
  1640. tda1997x_enable_edid(sd);
  1641. return 0;
  1642. }
  1643. static int tda1997x_get_dv_timings_cap(struct v4l2_subdev *sd,
  1644. struct v4l2_dv_timings_cap *cap)
  1645. {
  1646. *cap = tda1997x_dv_timings_cap;
  1647. return 0;
  1648. }
  1649. static int tda1997x_enum_dv_timings(struct v4l2_subdev *sd,
  1650. struct v4l2_enum_dv_timings *timings)
  1651. {
  1652. return v4l2_enum_dv_timings_cap(timings, &tda1997x_dv_timings_cap,
  1653. NULL, NULL);
  1654. }
  1655. static const struct v4l2_subdev_pad_ops tda1997x_pad_ops = {
  1656. .init_cfg = tda1997x_init_cfg,
  1657. .enum_mbus_code = tda1997x_enum_mbus_code,
  1658. .get_fmt = tda1997x_get_format,
  1659. .set_fmt = tda1997x_set_format,
  1660. .get_edid = tda1997x_get_edid,
  1661. .set_edid = tda1997x_set_edid,
  1662. .dv_timings_cap = tda1997x_get_dv_timings_cap,
  1663. .enum_dv_timings = tda1997x_enum_dv_timings,
  1664. };
  1665. /* -----------------------------------------------------------------------------
  1666. * v4l2_subdev_core_ops
  1667. */
  1668. static int tda1997x_log_infoframe(struct v4l2_subdev *sd, int addr)
  1669. {
  1670. struct tda1997x_state *state = to_state(sd);
  1671. union hdmi_infoframe frame;
  1672. u8 buffer[40];
  1673. int len, err;
  1674. /* read data */
  1675. len = io_readn(sd, addr, sizeof(buffer), buffer);
  1676. v4l2_dbg(1, debug, sd, "infoframe: addr=%d len=%d\n", addr, len);
  1677. err = hdmi_infoframe_unpack(&frame, buffer);
  1678. if (err) {
  1679. v4l_err(state->client,
  1680. "failed parsing %d byte infoframe: 0x%04x/0x%02x\n",
  1681. len, addr, buffer[0]);
  1682. return err;
  1683. }
  1684. hdmi_infoframe_log(KERN_INFO, &state->client->dev, &frame);
  1685. return 0;
  1686. }
  1687. static int tda1997x_log_status(struct v4l2_subdev *sd)
  1688. {
  1689. struct tda1997x_state *state = to_state(sd);
  1690. struct v4l2_dv_timings timings;
  1691. struct hdmi_avi_infoframe *avi = &state->avi_infoframe;
  1692. v4l2_info(sd, "-----Chip status-----\n");
  1693. v4l2_info(sd, "Chip: %s N%d\n", state->info->name,
  1694. state->chip_revision + 1);
  1695. v4l2_info(sd, "EDID Enabled: %s\n", state->edid.present ? "yes" : "no");
  1696. v4l2_info(sd, "-----Signal status-----\n");
  1697. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  1698. tda1997x_detect_tx_5v(sd) ? "yes" : "no");
  1699. v4l2_info(sd, "HPD detected: %s\n",
  1700. tda1997x_detect_tx_hpd(sd) ? "yes" : "no");
  1701. v4l2_info(sd, "-----Video Timings-----\n");
  1702. switch (tda1997x_detect_std(state, &timings)) {
  1703. case -ENOLINK:
  1704. v4l2_info(sd, "No video detected\n");
  1705. break;
  1706. case -ERANGE:
  1707. v4l2_info(sd, "Invalid signal detected\n");
  1708. break;
  1709. }
  1710. v4l2_print_dv_timings(sd->name, "Configured format: ",
  1711. &state->timings, true);
  1712. v4l2_info(sd, "-----Color space-----\n");
  1713. v4l2_info(sd, "Input color space: %s %s %s",
  1714. hdmi_colorspace_names[avi->colorspace],
  1715. (avi->colorspace == HDMI_COLORSPACE_RGB) ? "" :
  1716. hdmi_colorimetry_names[avi->colorimetry],
  1717. v4l2_quantization_names[state->colorimetry.quantization]);
  1718. v4l2_info(sd, "Output color space: %s",
  1719. vidfmt_names[state->vid_fmt]);
  1720. v4l2_info(sd, "Color space conversion: %s", state->conv ?
  1721. state->conv->name : "None");
  1722. v4l2_info(sd, "-----Audio-----\n");
  1723. if (state->audio_channels) {
  1724. v4l2_info(sd, "audio: %dch %dHz\n", state->audio_channels,
  1725. state->audio_samplerate);
  1726. } else {
  1727. v4l2_info(sd, "audio: none\n");
  1728. }
  1729. v4l2_info(sd, "-----Infoframes-----\n");
  1730. tda1997x_log_infoframe(sd, AUD_IF);
  1731. tda1997x_log_infoframe(sd, SPD_IF);
  1732. tda1997x_log_infoframe(sd, AVI_IF);
  1733. return 0;
  1734. }
  1735. static int tda1997x_subscribe_event(struct v4l2_subdev *sd,
  1736. struct v4l2_fh *fh,
  1737. struct v4l2_event_subscription *sub)
  1738. {
  1739. switch (sub->type) {
  1740. case V4L2_EVENT_SOURCE_CHANGE:
  1741. return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
  1742. case V4L2_EVENT_CTRL:
  1743. return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
  1744. default:
  1745. return -EINVAL;
  1746. }
  1747. }
  1748. static const struct v4l2_subdev_core_ops tda1997x_core_ops = {
  1749. .log_status = tda1997x_log_status,
  1750. .subscribe_event = tda1997x_subscribe_event,
  1751. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1752. };
  1753. /* -----------------------------------------------------------------------------
  1754. * v4l2_subdev_ops
  1755. */
  1756. static const struct v4l2_subdev_ops tda1997x_subdev_ops = {
  1757. .core = &tda1997x_core_ops,
  1758. .video = &tda1997x_video_ops,
  1759. .pad = &tda1997x_pad_ops,
  1760. };
  1761. /* -----------------------------------------------------------------------------
  1762. * v4l2_controls
  1763. */
  1764. static int tda1997x_s_ctrl(struct v4l2_ctrl *ctrl)
  1765. {
  1766. struct v4l2_subdev *sd = to_sd(ctrl);
  1767. struct tda1997x_state *state = to_state(sd);
  1768. switch (ctrl->id) {
  1769. /* allow overriding the default RGB quantization range */
  1770. case V4L2_CID_DV_RX_RGB_RANGE:
  1771. state->rgb_quantization_range = ctrl->val;
  1772. set_rgb_quantization_range(state);
  1773. tda1997x_configure_csc(sd);
  1774. return 0;
  1775. }
  1776. return -EINVAL;
  1777. };
  1778. static int tda1997x_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1779. {
  1780. struct v4l2_subdev *sd = to_sd(ctrl);
  1781. struct tda1997x_state *state = to_state(sd);
  1782. if (ctrl->id == V4L2_CID_DV_RX_IT_CONTENT_TYPE) {
  1783. ctrl->val = state->avi_infoframe.content_type;
  1784. return 0;
  1785. }
  1786. return -EINVAL;
  1787. };
  1788. static const struct v4l2_ctrl_ops tda1997x_ctrl_ops = {
  1789. .s_ctrl = tda1997x_s_ctrl,
  1790. .g_volatile_ctrl = tda1997x_g_volatile_ctrl,
  1791. };
  1792. static int tda1997x_core_init(struct v4l2_subdev *sd)
  1793. {
  1794. struct tda1997x_state *state = to_state(sd);
  1795. struct tda1997x_platform_data *pdata = &state->pdata;
  1796. u8 reg;
  1797. int i;
  1798. /* disable HPD */
  1799. io_write(sd, REG_HPD_AUTO_CTRL, HPD_AUTO_HPD_UNSEL);
  1800. if (state->chip_revision == 0) {
  1801. io_write(sd, REG_MAN_SUS_HDMI_SEL, MAN_DIS_HDCP | MAN_RST_HDCP);
  1802. io_write(sd, REG_CGU_DBG_SEL, 1 << CGU_DBG_CLK_SEL_SHIFT);
  1803. }
  1804. /* reset infoframe at end of start-up-sequencer */
  1805. io_write(sd, REG_SUS_SET_RGB2, 0x06);
  1806. io_write(sd, REG_SUS_SET_RGB3, 0x06);
  1807. /* Enable TMDS pull-ups */
  1808. io_write(sd, REG_RT_MAN_CTRL, RT_MAN_CTRL_RT |
  1809. RT_MAN_CTRL_RT_B | RT_MAN_CTRL_RT_A);
  1810. /* enable sync measurement timing */
  1811. tda1997x_cec_write(sd, REG_PWR_CONTROL & 0xff, 0x04);
  1812. /* adjust CEC clock divider */
  1813. tda1997x_cec_write(sd, REG_OSC_DIVIDER & 0xff, 0x03);
  1814. tda1997x_cec_write(sd, REG_EN_OSC_PERIOD_LSB & 0xff, 0xa0);
  1815. io_write(sd, REG_TIMER_D, 0x54);
  1816. /* enable power switch */
  1817. reg = tda1997x_cec_read(sd, REG_CONTROL & 0xff);
  1818. reg |= 0x20;
  1819. tda1997x_cec_write(sd, REG_CONTROL & 0xff, reg);
  1820. mdelay(50);
  1821. /* read the chip version */
  1822. reg = io_read(sd, REG_VERSION);
  1823. /* get the chip configuration */
  1824. reg = io_read(sd, REG_CMTP_REG10);
  1825. /* enable interrupts we care about */
  1826. io_write(sd, REG_INT_MASK_TOP,
  1827. INTERRUPT_HDCP | INTERRUPT_AUDIO | INTERRUPT_INFO |
  1828. INTERRUPT_RATE | INTERRUPT_SUS);
  1829. /* config_mtp,fmt,sus_end,sus_st */
  1830. io_write(sd, REG_INT_MASK_SUS, MASK_MPT | MASK_FMT | MASK_SUS_END);
  1831. /* rate stability change for inputs A/B */
  1832. io_write(sd, REG_INT_MASK_RATE, MASK_RATE_B_ST | MASK_RATE_A_ST);
  1833. /* aud,spd,avi*/
  1834. io_write(sd, REG_INT_MASK_INFO,
  1835. MASK_AUD_IF | MASK_SPD_IF | MASK_AVI_IF);
  1836. /* audio_freq,audio_flg,mute_flg,fifo_err */
  1837. io_write(sd, REG_INT_MASK_AUDIO,
  1838. MASK_AUDIO_FREQ_FLG | MASK_AUDIO_FLG | MASK_MUTE_FLG |
  1839. MASK_ERROR_FIFO_PT);
  1840. /* HDCP C5 state reached */
  1841. io_write(sd, REG_INT_MASK_HDCP, MASK_STATE_C5);
  1842. /* 5V detect and HDP pulse end */
  1843. io_write(sd, REG_INT_MASK_DDC, MASK_DET_5V);
  1844. /* don't care about AFE/MODE */
  1845. io_write(sd, REG_INT_MASK_AFE, 0);
  1846. io_write(sd, REG_INT_MASK_MODE, 0);
  1847. /* clear all interrupts */
  1848. io_write(sd, REG_INT_FLG_CLR_TOP, 0xff);
  1849. io_write(sd, REG_INT_FLG_CLR_SUS, 0xff);
  1850. io_write(sd, REG_INT_FLG_CLR_DDC, 0xff);
  1851. io_write(sd, REG_INT_FLG_CLR_RATE, 0xff);
  1852. io_write(sd, REG_INT_FLG_CLR_MODE, 0xff);
  1853. io_write(sd, REG_INT_FLG_CLR_INFO, 0xff);
  1854. io_write(sd, REG_INT_FLG_CLR_AUDIO, 0xff);
  1855. io_write(sd, REG_INT_FLG_CLR_HDCP, 0xff);
  1856. io_write(sd, REG_INT_FLG_CLR_AFE, 0xff);
  1857. /* init TMDS equalizer */
  1858. if (state->chip_revision == 0)
  1859. io_write(sd, REG_CGU_DBG_SEL, 1 << CGU_DBG_CLK_SEL_SHIFT);
  1860. io_write24(sd, REG_CLK_MIN_RATE, CLK_MIN_RATE);
  1861. io_write24(sd, REG_CLK_MAX_RATE, CLK_MAX_RATE);
  1862. if (state->chip_revision == 0)
  1863. io_write(sd, REG_WDL_CFG, WDL_CFG_VAL);
  1864. /* DC filter */
  1865. io_write(sd, REG_DEEP_COLOR_CTRL, DC_FILTER_VAL);
  1866. /* disable test pattern */
  1867. io_write(sd, REG_SVC_MODE, 0x00);
  1868. /* update HDMI INFO CTRL */
  1869. io_write(sd, REG_INFO_CTRL, 0xff);
  1870. /* write HDMI INFO EXCEED value */
  1871. io_write(sd, REG_INFO_EXCEED, 3);
  1872. if (state->chip_revision == 0)
  1873. tda1997x_reset_n1(state);
  1874. /*
  1875. * No HDCP acknowledge when HDCP is disabled
  1876. * and reset SUS to force format detection
  1877. */
  1878. tda1997x_hdmi_info_reset(sd, NACK_HDCP, true);
  1879. /* Set HPD low */
  1880. tda1997x_manual_hpd(sd, HPD_LOW_BP);
  1881. /* Configure receiver capabilities */
  1882. io_write(sd, REG_HDCP_BCAPS, HDCP_HDMI | HDCP_FAST_REAUTH);
  1883. /* Configure HDMI: Auto HDCP mode, packet controlled mute */
  1884. reg = HDMI_CTRL_MUTE_AUTO << HDMI_CTRL_MUTE_SHIFT;
  1885. reg |= HDMI_CTRL_HDCP_AUTO << HDMI_CTRL_HDCP_SHIFT;
  1886. io_write(sd, REG_HDMI_CTRL, reg);
  1887. /* reset start-up-sequencer to force format detection */
  1888. tda1997x_hdmi_info_reset(sd, 0, true);
  1889. /* disable matrix conversion */
  1890. reg = io_read(sd, REG_VDP_CTRL);
  1891. reg |= VDP_CTRL_MATRIX_BP;
  1892. io_write(sd, REG_VDP_CTRL, reg);
  1893. /* set video output mode */
  1894. tda1997x_configure_vidout(state);
  1895. /* configure video output port */
  1896. for (i = 0; i < 9; i++) {
  1897. v4l_dbg(1, debug, state->client, "vidout_cfg[%d]=0x%02x\n", i,
  1898. pdata->vidout_port_cfg[i]);
  1899. io_write(sd, REG_VP35_32_CTRL + i, pdata->vidout_port_cfg[i]);
  1900. }
  1901. /* configure audio output port */
  1902. tda1997x_configure_audout(sd, 0);
  1903. /* configure audio clock freq */
  1904. switch (pdata->audout_mclk_fs) {
  1905. case 512:
  1906. reg = AUDIO_CLOCK_SEL_512FS;
  1907. break;
  1908. case 256:
  1909. reg = AUDIO_CLOCK_SEL_256FS;
  1910. break;
  1911. case 128:
  1912. reg = AUDIO_CLOCK_SEL_128FS;
  1913. break;
  1914. case 64:
  1915. reg = AUDIO_CLOCK_SEL_64FS;
  1916. break;
  1917. case 32:
  1918. reg = AUDIO_CLOCK_SEL_32FS;
  1919. break;
  1920. default:
  1921. reg = AUDIO_CLOCK_SEL_16FS;
  1922. break;
  1923. }
  1924. io_write(sd, REG_AUDIO_CLOCK, reg);
  1925. /* reset advanced infoframes (ISRC1/ISRC2/ACP) */
  1926. tda1997x_hdmi_info_reset(sd, RESET_AI, false);
  1927. /* reset infoframe */
  1928. tda1997x_hdmi_info_reset(sd, RESET_IF, false);
  1929. /* reset audio infoframes */
  1930. tda1997x_hdmi_info_reset(sd, RESET_AUDIO, false);
  1931. /* reset gamut */
  1932. tda1997x_hdmi_info_reset(sd, RESET_GAMUT, false);
  1933. /* get initial HDMI status */
  1934. state->hdmi_status = io_read(sd, REG_HDMI_FLAGS);
  1935. return 0;
  1936. }
  1937. static int tda1997x_set_power(struct tda1997x_state *state, bool on)
  1938. {
  1939. int ret = 0;
  1940. if (on) {
  1941. ret = regulator_bulk_enable(TDA1997X_NUM_SUPPLIES,
  1942. state->supplies);
  1943. msleep(300);
  1944. } else {
  1945. ret = regulator_bulk_disable(TDA1997X_NUM_SUPPLIES,
  1946. state->supplies);
  1947. }
  1948. return ret;
  1949. }
  1950. static const struct i2c_device_id tda1997x_i2c_id[] = {
  1951. {"tda19971", (kernel_ulong_t)&tda1997x_chip_info[TDA19971]},
  1952. {"tda19973", (kernel_ulong_t)&tda1997x_chip_info[TDA19973]},
  1953. { },
  1954. };
  1955. MODULE_DEVICE_TABLE(i2c, tda1997x_i2c_id);
  1956. static const struct of_device_id tda1997x_of_id[] __maybe_unused = {
  1957. { .compatible = "nxp,tda19971", .data = &tda1997x_chip_info[TDA19971] },
  1958. { .compatible = "nxp,tda19973", .data = &tda1997x_chip_info[TDA19973] },
  1959. { },
  1960. };
  1961. MODULE_DEVICE_TABLE(of, tda1997x_of_id);
  1962. static int tda1997x_parse_dt(struct tda1997x_state *state)
  1963. {
  1964. struct tda1997x_platform_data *pdata = &state->pdata;
  1965. struct v4l2_fwnode_endpoint bus_cfg;
  1966. struct device_node *ep;
  1967. struct device_node *np;
  1968. unsigned int flags;
  1969. const char *str;
  1970. int ret;
  1971. u32 v;
  1972. /*
  1973. * setup default values:
  1974. * - HREF: active high from start to end of row
  1975. * - VS: Vertical Sync active high at beginning of frame
  1976. * - DE: Active high when data valid
  1977. * - A_CLK: 128*Fs
  1978. */
  1979. pdata->vidout_sel_hs = HS_HREF_SEL_HREF_VHREF;
  1980. pdata->vidout_sel_vs = VS_VREF_SEL_VREF_HDMI;
  1981. pdata->vidout_sel_de = DE_FREF_SEL_DE_VHREF;
  1982. np = state->client->dev.of_node;
  1983. ep = of_graph_get_next_endpoint(np, NULL);
  1984. if (!ep)
  1985. return -EINVAL;
  1986. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
  1987. if (ret) {
  1988. of_node_put(ep);
  1989. return ret;
  1990. }
  1991. of_node_put(ep);
  1992. pdata->vidout_bus_type = bus_cfg.bus_type;
  1993. /* polarity of HS/VS/DE */
  1994. flags = bus_cfg.bus.parallel.flags;
  1995. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  1996. pdata->vidout_inv_hs = 1;
  1997. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  1998. pdata->vidout_inv_vs = 1;
  1999. if (flags & V4L2_MBUS_DATA_ACTIVE_LOW)
  2000. pdata->vidout_inv_de = 1;
  2001. pdata->vidout_bus_width = bus_cfg.bus.parallel.bus_width;
  2002. /* video output port config */
  2003. ret = of_property_count_u32_elems(np, "nxp,vidout-portcfg");
  2004. if (ret > 0) {
  2005. u32 reg, val, i;
  2006. for (i = 0; i < ret / 2 && i < 9; i++) {
  2007. of_property_read_u32_index(np, "nxp,vidout-portcfg",
  2008. i * 2, &reg);
  2009. of_property_read_u32_index(np, "nxp,vidout-portcfg",
  2010. i * 2 + 1, &val);
  2011. if (reg < 9)
  2012. pdata->vidout_port_cfg[reg] = val;
  2013. }
  2014. } else {
  2015. v4l_err(state->client, "nxp,vidout-portcfg missing\n");
  2016. return -EINVAL;
  2017. }
  2018. /* default to channel layout dictated by packet header */
  2019. pdata->audout_layoutauto = true;
  2020. pdata->audout_format = AUDFMT_TYPE_DISABLED;
  2021. if (!of_property_read_string(np, "nxp,audout-format", &str)) {
  2022. if (strcmp(str, "i2s") == 0)
  2023. pdata->audout_format = AUDFMT_TYPE_I2S;
  2024. else if (strcmp(str, "spdif") == 0)
  2025. pdata->audout_format = AUDFMT_TYPE_SPDIF;
  2026. else {
  2027. v4l_err(state->client, "nxp,audout-format invalid\n");
  2028. return -EINVAL;
  2029. }
  2030. if (!of_property_read_u32(np, "nxp,audout-layout", &v)) {
  2031. switch (v) {
  2032. case 0:
  2033. case 1:
  2034. break;
  2035. default:
  2036. v4l_err(state->client,
  2037. "nxp,audout-layout invalid\n");
  2038. return -EINVAL;
  2039. }
  2040. pdata->audout_layout = v;
  2041. }
  2042. if (!of_property_read_u32(np, "nxp,audout-width", &v)) {
  2043. switch (v) {
  2044. case 16:
  2045. case 32:
  2046. break;
  2047. default:
  2048. v4l_err(state->client,
  2049. "nxp,audout-width invalid\n");
  2050. return -EINVAL;
  2051. }
  2052. pdata->audout_width = v;
  2053. }
  2054. if (!of_property_read_u32(np, "nxp,audout-mclk-fs", &v)) {
  2055. switch (v) {
  2056. case 512:
  2057. case 256:
  2058. case 128:
  2059. case 64:
  2060. case 32:
  2061. case 16:
  2062. break;
  2063. default:
  2064. v4l_err(state->client,
  2065. "nxp,audout-mclk-fs invalid\n");
  2066. return -EINVAL;
  2067. }
  2068. pdata->audout_mclk_fs = v;
  2069. }
  2070. }
  2071. return 0;
  2072. }
  2073. static int tda1997x_get_regulators(struct tda1997x_state *state)
  2074. {
  2075. int i;
  2076. for (i = 0; i < TDA1997X_NUM_SUPPLIES; i++)
  2077. state->supplies[i].supply = tda1997x_supply_name[i];
  2078. return devm_regulator_bulk_get(&state->client->dev,
  2079. TDA1997X_NUM_SUPPLIES,
  2080. state->supplies);
  2081. }
  2082. static int tda1997x_identify_module(struct tda1997x_state *state)
  2083. {
  2084. struct v4l2_subdev *sd = &state->sd;
  2085. enum tda1997x_type type;
  2086. u8 reg;
  2087. /* Read chip configuration*/
  2088. reg = io_read(sd, REG_CMTP_REG10);
  2089. state->tmdsb_clk = (reg >> 6) & 0x01; /* use tmds clock B_inv for B */
  2090. state->tmdsb_soc = (reg >> 5) & 0x01; /* tmds of input B */
  2091. state->port_30bit = (reg >> 2) & 0x03; /* 30bit vs 24bit */
  2092. state->output_2p5 = (reg >> 1) & 0x01; /* output supply 2.5v */
  2093. switch ((reg >> 4) & 0x03) {
  2094. case 0x00:
  2095. type = TDA19971;
  2096. break;
  2097. case 0x02:
  2098. case 0x03:
  2099. type = TDA19973;
  2100. break;
  2101. default:
  2102. dev_err(&state->client->dev, "unsupported chip ID\n");
  2103. return -EIO;
  2104. }
  2105. if (state->info->type != type) {
  2106. dev_err(&state->client->dev, "chip id mismatch\n");
  2107. return -EIO;
  2108. }
  2109. /* read chip revision */
  2110. state->chip_revision = io_read(sd, REG_CMTP_REG11);
  2111. return 0;
  2112. }
  2113. static const struct media_entity_operations tda1997x_media_ops = {
  2114. .link_validate = v4l2_subdev_link_validate,
  2115. };
  2116. /* -----------------------------------------------------------------------------
  2117. * HDMI Audio Codec
  2118. */
  2119. /* refine sample-rate based on HDMI source */
  2120. static int tda1997x_pcm_startup(struct snd_pcm_substream *substream,
  2121. struct snd_soc_dai *dai)
  2122. {
  2123. struct tda1997x_state *state = snd_soc_dai_get_drvdata(dai);
  2124. struct snd_soc_component *component = dai->component;
  2125. struct snd_pcm_runtime *rtd = substream->runtime;
  2126. int rate, err;
  2127. rate = state->audio_samplerate;
  2128. err = snd_pcm_hw_constraint_minmax(rtd, SNDRV_PCM_HW_PARAM_RATE,
  2129. rate, rate);
  2130. if (err < 0) {
  2131. dev_err(component->dev, "failed to constrain samplerate to %dHz\n",
  2132. rate);
  2133. return err;
  2134. }
  2135. dev_info(component->dev, "set samplerate constraint to %dHz\n", rate);
  2136. return 0;
  2137. }
  2138. static const struct snd_soc_dai_ops tda1997x_dai_ops = {
  2139. .startup = tda1997x_pcm_startup,
  2140. };
  2141. static struct snd_soc_dai_driver tda1997x_audio_dai = {
  2142. .name = "tda1997x",
  2143. .capture = {
  2144. .stream_name = "Capture",
  2145. .channels_min = 2,
  2146. .channels_max = 8,
  2147. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  2148. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  2149. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  2150. SNDRV_PCM_RATE_192000,
  2151. },
  2152. .ops = &tda1997x_dai_ops,
  2153. };
  2154. static int tda1997x_codec_probe(struct snd_soc_component *component)
  2155. {
  2156. return 0;
  2157. }
  2158. static void tda1997x_codec_remove(struct snd_soc_component *component)
  2159. {
  2160. }
  2161. static struct snd_soc_component_driver tda1997x_codec_driver = {
  2162. .probe = tda1997x_codec_probe,
  2163. .remove = tda1997x_codec_remove,
  2164. .idle_bias_on = 1,
  2165. .use_pmdown_time = 1,
  2166. .endianness = 1,
  2167. .non_legacy_dai_naming = 1,
  2168. };
  2169. static int tda1997x_probe(struct i2c_client *client,
  2170. const struct i2c_device_id *id)
  2171. {
  2172. struct tda1997x_state *state;
  2173. struct tda1997x_platform_data *pdata;
  2174. struct v4l2_subdev *sd;
  2175. struct v4l2_ctrl_handler *hdl;
  2176. struct v4l2_ctrl *ctrl;
  2177. static const struct v4l2_dv_timings cea1920x1080 =
  2178. V4L2_DV_BT_CEA_1920X1080P60;
  2179. u32 *mbus_codes;
  2180. int i, ret;
  2181. /* Check if the adapter supports the needed features */
  2182. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2183. return -EIO;
  2184. state = kzalloc(sizeof(struct tda1997x_state), GFP_KERNEL);
  2185. if (!state)
  2186. return -ENOMEM;
  2187. state->client = client;
  2188. pdata = &state->pdata;
  2189. if (IS_ENABLED(CONFIG_OF) && client->dev.of_node) {
  2190. const struct of_device_id *oid;
  2191. oid = of_match_node(tda1997x_of_id, client->dev.of_node);
  2192. state->info = oid->data;
  2193. ret = tda1997x_parse_dt(state);
  2194. if (ret < 0) {
  2195. v4l_err(client, "DT parsing error\n");
  2196. goto err_free_state;
  2197. }
  2198. } else if (client->dev.platform_data) {
  2199. struct tda1997x_platform_data *pdata =
  2200. client->dev.platform_data;
  2201. state->info =
  2202. (const struct tda1997x_chip_info *)id->driver_data;
  2203. state->pdata = *pdata;
  2204. } else {
  2205. v4l_err(client, "No platform data\n");
  2206. ret = -ENODEV;
  2207. goto err_free_state;
  2208. }
  2209. ret = tda1997x_get_regulators(state);
  2210. if (ret)
  2211. goto err_free_state;
  2212. ret = tda1997x_set_power(state, 1);
  2213. if (ret)
  2214. goto err_free_state;
  2215. mutex_init(&state->page_lock);
  2216. mutex_init(&state->lock);
  2217. state->page = 0xff;
  2218. INIT_DELAYED_WORK(&state->delayed_work_enable_hpd,
  2219. tda1997x_delayed_work_enable_hpd);
  2220. /* set video format based on chip and bus width */
  2221. ret = tda1997x_identify_module(state);
  2222. if (ret)
  2223. goto err_free_mutex;
  2224. /* initialize subdev */
  2225. sd = &state->sd;
  2226. v4l2_i2c_subdev_init(sd, client, &tda1997x_subdev_ops);
  2227. snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
  2228. id->name, i2c_adapter_id(client->adapter),
  2229. client->addr);
  2230. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  2231. sd->entity.function = MEDIA_ENT_F_DV_DECODER;
  2232. sd->entity.ops = &tda1997x_media_ops;
  2233. /* set allowed mbus modes based on chip, bus-type, and bus-width */
  2234. i = 0;
  2235. mbus_codes = state->mbus_codes;
  2236. switch (state->info->type) {
  2237. case TDA19973:
  2238. switch (pdata->vidout_bus_type) {
  2239. case V4L2_MBUS_PARALLEL:
  2240. switch (pdata->vidout_bus_width) {
  2241. case 36:
  2242. mbus_codes[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
  2243. mbus_codes[i++] = MEDIA_BUS_FMT_YUV12_1X36;
  2244. /* fall-through */
  2245. case 24:
  2246. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
  2247. break;
  2248. }
  2249. break;
  2250. case V4L2_MBUS_BT656:
  2251. switch (pdata->vidout_bus_width) {
  2252. case 36:
  2253. case 24:
  2254. case 12:
  2255. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_2X12;
  2256. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY10_2X10;
  2257. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY8_2X8;
  2258. break;
  2259. }
  2260. break;
  2261. default:
  2262. break;
  2263. }
  2264. break;
  2265. case TDA19971:
  2266. switch (pdata->vidout_bus_type) {
  2267. case V4L2_MBUS_PARALLEL:
  2268. switch (pdata->vidout_bus_width) {
  2269. case 24:
  2270. mbus_codes[i++] = MEDIA_BUS_FMT_RGB888_1X24;
  2271. mbus_codes[i++] = MEDIA_BUS_FMT_YUV8_1X24;
  2272. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
  2273. /* fall through */
  2274. case 20:
  2275. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
  2276. /* fall through */
  2277. case 16:
  2278. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
  2279. break;
  2280. }
  2281. break;
  2282. case V4L2_MBUS_BT656:
  2283. switch (pdata->vidout_bus_width) {
  2284. case 24:
  2285. case 20:
  2286. case 16:
  2287. case 12:
  2288. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_2X12;
  2289. /* fall through */
  2290. case 10:
  2291. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY10_2X10;
  2292. /* fall through */
  2293. case 8:
  2294. mbus_codes[i++] = MEDIA_BUS_FMT_UYVY8_2X8;
  2295. break;
  2296. }
  2297. break;
  2298. default:
  2299. break;
  2300. }
  2301. break;
  2302. }
  2303. if (WARN_ON(i > ARRAY_SIZE(state->mbus_codes))) {
  2304. ret = -EINVAL;
  2305. goto err_free_mutex;
  2306. }
  2307. /* default format */
  2308. tda1997x_setup_format(state, state->mbus_codes[0]);
  2309. state->timings = cea1920x1080;
  2310. /*
  2311. * default to SRGB full range quantization
  2312. * (in case we don't get an infoframe such as DVI signal
  2313. */
  2314. state->colorimetry.colorspace = V4L2_COLORSPACE_SRGB;
  2315. state->colorimetry.quantization = V4L2_QUANTIZATION_FULL_RANGE;
  2316. /* disable/reset HDCP to get correct I2C access to Rx HDMI */
  2317. io_write(sd, REG_MAN_SUS_HDMI_SEL, MAN_RST_HDCP | MAN_DIS_HDCP);
  2318. /*
  2319. * if N2 version, reset compdel_bp as it may generate some small pixel
  2320. * shifts in case of embedded sync/or delay lower than 4
  2321. */
  2322. if (state->chip_revision != 0) {
  2323. io_write(sd, REG_MAN_SUS_HDMI_SEL, 0x00);
  2324. io_write(sd, REG_VDP_CTRL, 0x1f);
  2325. }
  2326. v4l_info(client, "NXP %s N%d detected\n", state->info->name,
  2327. state->chip_revision + 1);
  2328. v4l_info(client, "video: %dbit %s %d formats available\n",
  2329. pdata->vidout_bus_width,
  2330. (pdata->vidout_bus_type == V4L2_MBUS_PARALLEL) ?
  2331. "parallel" : "BT656",
  2332. i);
  2333. if (pdata->audout_format) {
  2334. v4l_info(client, "audio: %dch %s layout%d sysclk=%d*fs\n",
  2335. pdata->audout_layout ? 2 : 8,
  2336. audfmt_names[pdata->audout_format],
  2337. pdata->audout_layout,
  2338. pdata->audout_mclk_fs);
  2339. }
  2340. ret = 0x34 + ((io_read(sd, REG_SLAVE_ADDR)>>4) & 0x03);
  2341. state->client_cec = i2c_new_dummy(client->adapter, ret);
  2342. v4l_info(client, "CEC slave address 0x%02x\n", ret);
  2343. ret = tda1997x_core_init(sd);
  2344. if (ret)
  2345. goto err_free_mutex;
  2346. /* control handlers */
  2347. hdl = &state->hdl;
  2348. v4l2_ctrl_handler_init(hdl, 3);
  2349. ctrl = v4l2_ctrl_new_std_menu(hdl, &tda1997x_ctrl_ops,
  2350. V4L2_CID_DV_RX_IT_CONTENT_TYPE,
  2351. V4L2_DV_IT_CONTENT_TYPE_NO_ITC, 0,
  2352. V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
  2353. if (ctrl)
  2354. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  2355. /* custom controls */
  2356. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  2357. V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
  2358. state->rgb_quantization_range_ctrl = v4l2_ctrl_new_std_menu(hdl,
  2359. &tda1997x_ctrl_ops,
  2360. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL, 0,
  2361. V4L2_DV_RGB_RANGE_AUTO);
  2362. state->sd.ctrl_handler = hdl;
  2363. if (hdl->error) {
  2364. ret = hdl->error;
  2365. goto err_free_handler;
  2366. }
  2367. v4l2_ctrl_handler_setup(hdl);
  2368. /* initialize source pads */
  2369. state->pads[TDA1997X_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  2370. ret = media_entity_pads_init(&sd->entity, TDA1997X_NUM_PADS,
  2371. state->pads);
  2372. if (ret) {
  2373. v4l_err(client, "failed entity_init: %d", ret);
  2374. goto err_free_handler;
  2375. }
  2376. ret = v4l2_async_register_subdev(sd);
  2377. if (ret)
  2378. goto err_free_media;
  2379. /* register audio DAI */
  2380. if (pdata->audout_format) {
  2381. u64 formats;
  2382. if (pdata->audout_width == 32)
  2383. formats = SNDRV_PCM_FMTBIT_S32_LE;
  2384. else
  2385. formats = SNDRV_PCM_FMTBIT_S16_LE;
  2386. tda1997x_audio_dai.capture.formats = formats;
  2387. ret = devm_snd_soc_register_component(&state->client->dev,
  2388. &tda1997x_codec_driver,
  2389. &tda1997x_audio_dai, 1);
  2390. if (ret) {
  2391. dev_err(&client->dev, "register audio codec failed\n");
  2392. goto err_free_media;
  2393. }
  2394. dev_set_drvdata(&state->client->dev, state);
  2395. v4l_info(state->client, "registered audio codec\n");
  2396. }
  2397. /* request irq */
  2398. ret = devm_request_threaded_irq(&client->dev, client->irq,
  2399. NULL, tda1997x_isr_thread,
  2400. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  2401. KBUILD_MODNAME, state);
  2402. if (ret) {
  2403. v4l_err(client, "irq%d reg failed: %d\n", client->irq, ret);
  2404. goto err_free_media;
  2405. }
  2406. return 0;
  2407. err_free_media:
  2408. media_entity_cleanup(&sd->entity);
  2409. err_free_handler:
  2410. v4l2_ctrl_handler_free(&state->hdl);
  2411. err_free_mutex:
  2412. cancel_delayed_work(&state->delayed_work_enable_hpd);
  2413. mutex_destroy(&state->page_lock);
  2414. mutex_destroy(&state->lock);
  2415. err_free_state:
  2416. kfree(state);
  2417. dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
  2418. return ret;
  2419. }
  2420. static int tda1997x_remove(struct i2c_client *client)
  2421. {
  2422. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2423. struct tda1997x_state *state = to_state(sd);
  2424. struct tda1997x_platform_data *pdata = &state->pdata;
  2425. if (pdata->audout_format) {
  2426. mutex_destroy(&state->audio_lock);
  2427. }
  2428. disable_irq(state->client->irq);
  2429. tda1997x_power_mode(state, 0);
  2430. v4l2_async_unregister_subdev(sd);
  2431. media_entity_cleanup(&sd->entity);
  2432. v4l2_ctrl_handler_free(&state->hdl);
  2433. regulator_bulk_disable(TDA1997X_NUM_SUPPLIES, state->supplies);
  2434. i2c_unregister_device(state->client_cec);
  2435. cancel_delayed_work(&state->delayed_work_enable_hpd);
  2436. mutex_destroy(&state->page_lock);
  2437. mutex_destroy(&state->lock);
  2438. kfree(state);
  2439. return 0;
  2440. }
  2441. static struct i2c_driver tda1997x_i2c_driver = {
  2442. .driver = {
  2443. .name = "tda1997x",
  2444. .of_match_table = of_match_ptr(tda1997x_of_id),
  2445. },
  2446. .probe = tda1997x_probe,
  2447. .remove = tda1997x_remove,
  2448. .id_table = tda1997x_i2c_id,
  2449. };
  2450. module_i2c_driver(tda1997x_i2c_driver);
  2451. MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
  2452. MODULE_DESCRIPTION("TDA1997X HDMI Receiver driver");
  2453. MODULE_LICENSE("GPL v2");