tc358743.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tc358743 - Toshiba HDMI to CSI-2 bridge
  4. *
  5. * Copyright 2015 Cisco Systems, Inc. and/or its affiliates. All rights
  6. * reserved.
  7. */
  8. /*
  9. * References (c = chapter, p = page):
  10. * REF_01 - Toshiba, TC358743XBG (H2C), Functional Specification, Rev 0.60
  11. * REF_02 - Toshiba, TC358743XBG_HDMI-CSI_Tv11p_nm.xls
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/i2c.h>
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/timer.h>
  22. #include <linux/of_graph.h>
  23. #include <linux/videodev2.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/v4l2-dv-timings.h>
  26. #include <linux/hdmi.h>
  27. #include <media/cec.h>
  28. #include <media/v4l2-dv-timings.h>
  29. #include <media/v4l2-device.h>
  30. #include <media/v4l2-ctrls.h>
  31. #include <media/v4l2-event.h>
  32. #include <media/v4l2-fwnode.h>
  33. #include <media/i2c/tc358743.h>
  34. #include "tc358743_regs.h"
  35. static int debug;
  36. module_param(debug, int, 0644);
  37. MODULE_PARM_DESC(debug, "debug level (0-3)");
  38. MODULE_DESCRIPTION("Toshiba TC358743 HDMI to CSI-2 bridge driver");
  39. MODULE_AUTHOR("Ramakrishnan Muthukrishnan <ram@rkrishnan.org>");
  40. MODULE_AUTHOR("Mikhail Khelik <mkhelik@cisco.com>");
  41. MODULE_AUTHOR("Mats Randgaard <matrandg@cisco.com>");
  42. MODULE_LICENSE("GPL");
  43. #define EDID_NUM_BLOCKS_MAX 8
  44. #define EDID_BLOCK_SIZE 128
  45. #define I2C_MAX_XFER_SIZE (EDID_BLOCK_SIZE + 2)
  46. #define POLL_INTERVAL_CEC_MS 10
  47. #define POLL_INTERVAL_MS 1000
  48. static const struct v4l2_dv_timings_cap tc358743_timings_cap = {
  49. .type = V4L2_DV_BT_656_1120,
  50. /* keep this initialization for compatibility with GCC < 4.4.6 */
  51. .reserved = { 0 },
  52. /* Pixel clock from REF_01 p. 20. Min/max height/width are unknown */
  53. V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1200, 13000000, 165000000,
  54. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  55. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  56. V4L2_DV_BT_CAP_PROGRESSIVE |
  57. V4L2_DV_BT_CAP_REDUCED_BLANKING |
  58. V4L2_DV_BT_CAP_CUSTOM)
  59. };
  60. struct tc358743_state {
  61. struct tc358743_platform_data pdata;
  62. struct v4l2_fwnode_bus_mipi_csi2 bus;
  63. struct v4l2_subdev sd;
  64. struct media_pad pad;
  65. struct v4l2_ctrl_handler hdl;
  66. struct i2c_client *i2c_client;
  67. /* CONFCTL is modified in ops and tc358743_hdmi_sys_int_handler */
  68. struct mutex confctl_mutex;
  69. /* controls */
  70. struct v4l2_ctrl *detect_tx_5v_ctrl;
  71. struct v4l2_ctrl *audio_sampling_rate_ctrl;
  72. struct v4l2_ctrl *audio_present_ctrl;
  73. struct delayed_work delayed_work_enable_hotplug;
  74. struct timer_list timer;
  75. struct work_struct work_i2c_poll;
  76. /* edid */
  77. u8 edid_blocks_written;
  78. struct v4l2_dv_timings timings;
  79. u32 mbus_fmt_code;
  80. u8 csi_lanes_in_use;
  81. struct gpio_desc *reset_gpio;
  82. struct cec_adapter *cec_adap;
  83. };
  84. static void tc358743_enable_interrupts(struct v4l2_subdev *sd,
  85. bool cable_connected);
  86. static int tc358743_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd);
  87. static inline struct tc358743_state *to_state(struct v4l2_subdev *sd)
  88. {
  89. return container_of(sd, struct tc358743_state, sd);
  90. }
  91. /* --------------- I2C --------------- */
  92. static void i2c_rd(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
  93. {
  94. struct tc358743_state *state = to_state(sd);
  95. struct i2c_client *client = state->i2c_client;
  96. int err;
  97. u8 buf[2] = { reg >> 8, reg & 0xff };
  98. struct i2c_msg msgs[] = {
  99. {
  100. .addr = client->addr,
  101. .flags = 0,
  102. .len = 2,
  103. .buf = buf,
  104. },
  105. {
  106. .addr = client->addr,
  107. .flags = I2C_M_RD,
  108. .len = n,
  109. .buf = values,
  110. },
  111. };
  112. err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  113. if (err != ARRAY_SIZE(msgs)) {
  114. v4l2_err(sd, "%s: reading register 0x%x from 0x%x failed\n",
  115. __func__, reg, client->addr);
  116. }
  117. }
  118. static void i2c_wr(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
  119. {
  120. struct tc358743_state *state = to_state(sd);
  121. struct i2c_client *client = state->i2c_client;
  122. int err, i;
  123. struct i2c_msg msg;
  124. u8 data[I2C_MAX_XFER_SIZE];
  125. if ((2 + n) > I2C_MAX_XFER_SIZE) {
  126. n = I2C_MAX_XFER_SIZE - 2;
  127. v4l2_warn(sd, "i2c wr reg=%04x: len=%d is too big!\n",
  128. reg, 2 + n);
  129. }
  130. msg.addr = client->addr;
  131. msg.buf = data;
  132. msg.len = 2 + n;
  133. msg.flags = 0;
  134. data[0] = reg >> 8;
  135. data[1] = reg & 0xff;
  136. for (i = 0; i < n; i++)
  137. data[2 + i] = values[i];
  138. err = i2c_transfer(client->adapter, &msg, 1);
  139. if (err != 1) {
  140. v4l2_err(sd, "%s: writing register 0x%x from 0x%x failed\n",
  141. __func__, reg, client->addr);
  142. return;
  143. }
  144. if (debug < 3)
  145. return;
  146. switch (n) {
  147. case 1:
  148. v4l2_info(sd, "I2C write 0x%04x = 0x%02x",
  149. reg, data[2]);
  150. break;
  151. case 2:
  152. v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x",
  153. reg, data[3], data[2]);
  154. break;
  155. case 4:
  156. v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x%02x%02x",
  157. reg, data[5], data[4], data[3], data[2]);
  158. break;
  159. default:
  160. v4l2_info(sd, "I2C write %d bytes from address 0x%04x\n",
  161. n, reg);
  162. }
  163. }
  164. static noinline u32 i2c_rdreg(struct v4l2_subdev *sd, u16 reg, u32 n)
  165. {
  166. __le32 val = 0;
  167. i2c_rd(sd, reg, (u8 __force *)&val, n);
  168. return le32_to_cpu(val);
  169. }
  170. static noinline void i2c_wrreg(struct v4l2_subdev *sd, u16 reg, u32 val, u32 n)
  171. {
  172. __le32 raw = cpu_to_le32(val);
  173. i2c_wr(sd, reg, (u8 __force *)&raw, n);
  174. }
  175. static u8 i2c_rd8(struct v4l2_subdev *sd, u16 reg)
  176. {
  177. return i2c_rdreg(sd, reg, 1);
  178. }
  179. static void i2c_wr8(struct v4l2_subdev *sd, u16 reg, u8 val)
  180. {
  181. i2c_wrreg(sd, reg, val, 1);
  182. }
  183. static void i2c_wr8_and_or(struct v4l2_subdev *sd, u16 reg,
  184. u8 mask, u8 val)
  185. {
  186. i2c_wrreg(sd, reg, (i2c_rdreg(sd, reg, 1) & mask) | val, 1);
  187. }
  188. static u16 i2c_rd16(struct v4l2_subdev *sd, u16 reg)
  189. {
  190. return i2c_rdreg(sd, reg, 2);
  191. }
  192. static void i2c_wr16(struct v4l2_subdev *sd, u16 reg, u16 val)
  193. {
  194. i2c_wrreg(sd, reg, val, 2);
  195. }
  196. static void i2c_wr16_and_or(struct v4l2_subdev *sd, u16 reg, u16 mask, u16 val)
  197. {
  198. i2c_wrreg(sd, reg, (i2c_rdreg(sd, reg, 2) & mask) | val, 2);
  199. }
  200. static u32 i2c_rd32(struct v4l2_subdev *sd, u16 reg)
  201. {
  202. return i2c_rdreg(sd, reg, 4);
  203. }
  204. static void i2c_wr32(struct v4l2_subdev *sd, u16 reg, u32 val)
  205. {
  206. i2c_wrreg(sd, reg, val, 4);
  207. }
  208. /* --------------- STATUS --------------- */
  209. static inline bool is_hdmi(struct v4l2_subdev *sd)
  210. {
  211. return i2c_rd8(sd, SYS_STATUS) & MASK_S_HDMI;
  212. }
  213. static inline bool tx_5v_power_present(struct v4l2_subdev *sd)
  214. {
  215. return i2c_rd8(sd, SYS_STATUS) & MASK_S_DDC5V;
  216. }
  217. static inline bool no_signal(struct v4l2_subdev *sd)
  218. {
  219. return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_TMDS);
  220. }
  221. static inline bool no_sync(struct v4l2_subdev *sd)
  222. {
  223. return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_SYNC);
  224. }
  225. static inline bool audio_present(struct v4l2_subdev *sd)
  226. {
  227. return i2c_rd8(sd, AU_STATUS0) & MASK_S_A_SAMPLE;
  228. }
  229. static int get_audio_sampling_rate(struct v4l2_subdev *sd)
  230. {
  231. static const int code_to_rate[] = {
  232. 44100, 0, 48000, 32000, 22050, 384000, 24000, 352800,
  233. 88200, 768000, 96000, 705600, 176400, 0, 192000, 0
  234. };
  235. /* Register FS_SET is not cleared when the cable is disconnected */
  236. if (no_signal(sd))
  237. return 0;
  238. return code_to_rate[i2c_rd8(sd, FS_SET) & MASK_FS];
  239. }
  240. /* --------------- TIMINGS --------------- */
  241. static inline unsigned fps(const struct v4l2_bt_timings *t)
  242. {
  243. if (!V4L2_DV_BT_FRAME_HEIGHT(t) || !V4L2_DV_BT_FRAME_WIDTH(t))
  244. return 0;
  245. return DIV_ROUND_CLOSEST((unsigned)t->pixelclock,
  246. V4L2_DV_BT_FRAME_HEIGHT(t) * V4L2_DV_BT_FRAME_WIDTH(t));
  247. }
  248. static int tc358743_get_detected_timings(struct v4l2_subdev *sd,
  249. struct v4l2_dv_timings *timings)
  250. {
  251. struct v4l2_bt_timings *bt = &timings->bt;
  252. unsigned width, height, frame_width, frame_height, frame_interval, fps;
  253. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  254. if (no_signal(sd)) {
  255. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  256. return -ENOLINK;
  257. }
  258. if (no_sync(sd)) {
  259. v4l2_dbg(1, debug, sd, "%s: no sync on signal\n", __func__);
  260. return -ENOLCK;
  261. }
  262. timings->type = V4L2_DV_BT_656_1120;
  263. bt->interlaced = i2c_rd8(sd, VI_STATUS1) & MASK_S_V_INTERLACE ?
  264. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  265. width = ((i2c_rd8(sd, DE_WIDTH_H_HI) & 0x1f) << 8) +
  266. i2c_rd8(sd, DE_WIDTH_H_LO);
  267. height = ((i2c_rd8(sd, DE_WIDTH_V_HI) & 0x1f) << 8) +
  268. i2c_rd8(sd, DE_WIDTH_V_LO);
  269. frame_width = ((i2c_rd8(sd, H_SIZE_HI) & 0x1f) << 8) +
  270. i2c_rd8(sd, H_SIZE_LO);
  271. frame_height = (((i2c_rd8(sd, V_SIZE_HI) & 0x3f) << 8) +
  272. i2c_rd8(sd, V_SIZE_LO)) / 2;
  273. /* frame interval in milliseconds * 10
  274. * Require SYS_FREQ0 and SYS_FREQ1 are precisely set */
  275. frame_interval = ((i2c_rd8(sd, FV_CNT_HI) & 0x3) << 8) +
  276. i2c_rd8(sd, FV_CNT_LO);
  277. fps = (frame_interval > 0) ?
  278. DIV_ROUND_CLOSEST(10000, frame_interval) : 0;
  279. bt->width = width;
  280. bt->height = height;
  281. bt->vsync = frame_height - height;
  282. bt->hsync = frame_width - width;
  283. bt->pixelclock = frame_width * frame_height * fps;
  284. if (bt->interlaced == V4L2_DV_INTERLACED) {
  285. bt->height *= 2;
  286. bt->il_vsync = bt->vsync + 1;
  287. bt->pixelclock /= 2;
  288. }
  289. return 0;
  290. }
  291. /* --------------- HOTPLUG / HDCP / EDID --------------- */
  292. static void tc358743_delayed_work_enable_hotplug(struct work_struct *work)
  293. {
  294. struct delayed_work *dwork = to_delayed_work(work);
  295. struct tc358743_state *state = container_of(dwork,
  296. struct tc358743_state, delayed_work_enable_hotplug);
  297. struct v4l2_subdev *sd = &state->sd;
  298. v4l2_dbg(2, debug, sd, "%s:\n", __func__);
  299. i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, MASK_HPD_OUT0);
  300. }
  301. static void tc358743_set_hdmi_hdcp(struct v4l2_subdev *sd, bool enable)
  302. {
  303. v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ?
  304. "enable" : "disable");
  305. if (enable) {
  306. i2c_wr8_and_or(sd, HDCP_REG3, ~KEY_RD_CMD, KEY_RD_CMD);
  307. i2c_wr8_and_or(sd, HDCP_MODE, ~MASK_MANUAL_AUTHENTICATION, 0);
  308. i2c_wr8_and_or(sd, HDCP_REG1, 0xff,
  309. MASK_AUTH_UNAUTH_SEL_16_FRAMES |
  310. MASK_AUTH_UNAUTH_AUTO);
  311. i2c_wr8_and_or(sd, HDCP_REG2, ~MASK_AUTO_P3_RESET,
  312. SET_AUTO_P3_RESET_FRAMES(0x0f));
  313. } else {
  314. i2c_wr8_and_or(sd, HDCP_MODE, ~MASK_MANUAL_AUTHENTICATION,
  315. MASK_MANUAL_AUTHENTICATION);
  316. }
  317. }
  318. static void tc358743_disable_edid(struct v4l2_subdev *sd)
  319. {
  320. struct tc358743_state *state = to_state(sd);
  321. v4l2_dbg(2, debug, sd, "%s:\n", __func__);
  322. cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
  323. /* DDC access to EDID is also disabled when hotplug is disabled. See
  324. * register DDC_CTL */
  325. i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, 0x0);
  326. }
  327. static void tc358743_enable_edid(struct v4l2_subdev *sd)
  328. {
  329. struct tc358743_state *state = to_state(sd);
  330. if (state->edid_blocks_written == 0) {
  331. v4l2_dbg(2, debug, sd, "%s: no EDID -> no hotplug\n", __func__);
  332. tc358743_s_ctrl_detect_tx_5v(sd);
  333. return;
  334. }
  335. v4l2_dbg(2, debug, sd, "%s:\n", __func__);
  336. /* Enable hotplug after 100 ms. DDC access to EDID is also enabled when
  337. * hotplug is enabled. See register DDC_CTL */
  338. schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 10);
  339. tc358743_enable_interrupts(sd, true);
  340. tc358743_s_ctrl_detect_tx_5v(sd);
  341. }
  342. static void tc358743_erase_bksv(struct v4l2_subdev *sd)
  343. {
  344. int i;
  345. for (i = 0; i < 5; i++)
  346. i2c_wr8(sd, BKSV + i, 0);
  347. }
  348. /* --------------- AVI infoframe --------------- */
  349. static void print_avi_infoframe(struct v4l2_subdev *sd)
  350. {
  351. struct i2c_client *client = v4l2_get_subdevdata(sd);
  352. struct device *dev = &client->dev;
  353. union hdmi_infoframe frame;
  354. u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
  355. if (!is_hdmi(sd)) {
  356. v4l2_info(sd, "DVI-D signal - AVI infoframe not supported\n");
  357. return;
  358. }
  359. i2c_rd(sd, PK_AVI_0HEAD, buffer, HDMI_INFOFRAME_SIZE(AVI));
  360. if (hdmi_infoframe_unpack(&frame, buffer) < 0) {
  361. v4l2_err(sd, "%s: unpack of AVI infoframe failed\n", __func__);
  362. return;
  363. }
  364. hdmi_infoframe_log(KERN_INFO, dev, &frame);
  365. }
  366. /* --------------- CTRLS --------------- */
  367. static int tc358743_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd)
  368. {
  369. struct tc358743_state *state = to_state(sd);
  370. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
  371. tx_5v_power_present(sd));
  372. }
  373. static int tc358743_s_ctrl_audio_sampling_rate(struct v4l2_subdev *sd)
  374. {
  375. struct tc358743_state *state = to_state(sd);
  376. return v4l2_ctrl_s_ctrl(state->audio_sampling_rate_ctrl,
  377. get_audio_sampling_rate(sd));
  378. }
  379. static int tc358743_s_ctrl_audio_present(struct v4l2_subdev *sd)
  380. {
  381. struct tc358743_state *state = to_state(sd);
  382. return v4l2_ctrl_s_ctrl(state->audio_present_ctrl,
  383. audio_present(sd));
  384. }
  385. static int tc358743_update_controls(struct v4l2_subdev *sd)
  386. {
  387. int ret = 0;
  388. ret |= tc358743_s_ctrl_detect_tx_5v(sd);
  389. ret |= tc358743_s_ctrl_audio_sampling_rate(sd);
  390. ret |= tc358743_s_ctrl_audio_present(sd);
  391. return ret;
  392. }
  393. /* --------------- INIT --------------- */
  394. static void tc358743_reset_phy(struct v4l2_subdev *sd)
  395. {
  396. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  397. i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, 0);
  398. i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, MASK_RESET_CTRL);
  399. }
  400. static void tc358743_reset(struct v4l2_subdev *sd, uint16_t mask)
  401. {
  402. u16 sysctl = i2c_rd16(sd, SYSCTL);
  403. i2c_wr16(sd, SYSCTL, sysctl | mask);
  404. i2c_wr16(sd, SYSCTL, sysctl & ~mask);
  405. }
  406. static inline void tc358743_sleep_mode(struct v4l2_subdev *sd, bool enable)
  407. {
  408. i2c_wr16_and_or(sd, SYSCTL, ~MASK_SLEEP,
  409. enable ? MASK_SLEEP : 0);
  410. }
  411. static inline void enable_stream(struct v4l2_subdev *sd, bool enable)
  412. {
  413. struct tc358743_state *state = to_state(sd);
  414. v4l2_dbg(3, debug, sd, "%s: %sable\n",
  415. __func__, enable ? "en" : "dis");
  416. if (enable) {
  417. /* It is critical for CSI receiver to see lane transition
  418. * LP11->HS. Set to non-continuous mode to enable clock lane
  419. * LP11 state. */
  420. i2c_wr32(sd, TXOPTIONCNTRL, 0);
  421. /* Set to continuous mode to trigger LP11->HS transition */
  422. i2c_wr32(sd, TXOPTIONCNTRL, MASK_CONTCLKMODE);
  423. /* Unmute video */
  424. i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE);
  425. } else {
  426. /* Mute video so that all data lanes go to LSP11 state.
  427. * No data is output to CSI Tx block. */
  428. i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE | MASK_VI_MUTE);
  429. }
  430. mutex_lock(&state->confctl_mutex);
  431. i2c_wr16_and_or(sd, CONFCTL, ~(MASK_VBUFEN | MASK_ABUFEN),
  432. enable ? (MASK_VBUFEN | MASK_ABUFEN) : 0x0);
  433. mutex_unlock(&state->confctl_mutex);
  434. }
  435. static void tc358743_set_pll(struct v4l2_subdev *sd)
  436. {
  437. struct tc358743_state *state = to_state(sd);
  438. struct tc358743_platform_data *pdata = &state->pdata;
  439. u16 pllctl0 = i2c_rd16(sd, PLLCTL0);
  440. u16 pllctl1 = i2c_rd16(sd, PLLCTL1);
  441. u16 pllctl0_new = SET_PLL_PRD(pdata->pll_prd) |
  442. SET_PLL_FBD(pdata->pll_fbd);
  443. u32 hsck = (pdata->refclk_hz / pdata->pll_prd) * pdata->pll_fbd;
  444. v4l2_dbg(2, debug, sd, "%s:\n", __func__);
  445. /* Only rewrite when needed (new value or disabled), since rewriting
  446. * triggers another format change event. */
  447. if ((pllctl0 != pllctl0_new) || ((pllctl1 & MASK_PLL_EN) == 0)) {
  448. u16 pll_frs;
  449. if (hsck > 500000000)
  450. pll_frs = 0x0;
  451. else if (hsck > 250000000)
  452. pll_frs = 0x1;
  453. else if (hsck > 125000000)
  454. pll_frs = 0x2;
  455. else
  456. pll_frs = 0x3;
  457. v4l2_dbg(1, debug, sd, "%s: updating PLL clock\n", __func__);
  458. tc358743_sleep_mode(sd, true);
  459. i2c_wr16(sd, PLLCTL0, pllctl0_new);
  460. i2c_wr16_and_or(sd, PLLCTL1,
  461. ~(MASK_PLL_FRS | MASK_RESETB | MASK_PLL_EN),
  462. (SET_PLL_FRS(pll_frs) | MASK_RESETB |
  463. MASK_PLL_EN));
  464. udelay(10); /* REF_02, Sheet "Source HDMI" */
  465. i2c_wr16_and_or(sd, PLLCTL1, ~MASK_CKEN, MASK_CKEN);
  466. tc358743_sleep_mode(sd, false);
  467. }
  468. }
  469. static void tc358743_set_ref_clk(struct v4l2_subdev *sd)
  470. {
  471. struct tc358743_state *state = to_state(sd);
  472. struct tc358743_platform_data *pdata = &state->pdata;
  473. u32 sys_freq;
  474. u32 lockdet_ref;
  475. u32 cec_freq;
  476. u16 fh_min;
  477. u16 fh_max;
  478. BUG_ON(!(pdata->refclk_hz == 26000000 ||
  479. pdata->refclk_hz == 27000000 ||
  480. pdata->refclk_hz == 42000000));
  481. sys_freq = pdata->refclk_hz / 10000;
  482. i2c_wr8(sd, SYS_FREQ0, sys_freq & 0x00ff);
  483. i2c_wr8(sd, SYS_FREQ1, (sys_freq & 0xff00) >> 8);
  484. i2c_wr8_and_or(sd, PHY_CTL0, ~MASK_PHY_SYSCLK_IND,
  485. (pdata->refclk_hz == 42000000) ?
  486. MASK_PHY_SYSCLK_IND : 0x0);
  487. fh_min = pdata->refclk_hz / 100000;
  488. i2c_wr8(sd, FH_MIN0, fh_min & 0x00ff);
  489. i2c_wr8(sd, FH_MIN1, (fh_min & 0xff00) >> 8);
  490. fh_max = (fh_min * 66) / 10;
  491. i2c_wr8(sd, FH_MAX0, fh_max & 0x00ff);
  492. i2c_wr8(sd, FH_MAX1, (fh_max & 0xff00) >> 8);
  493. lockdet_ref = pdata->refclk_hz / 100;
  494. i2c_wr8(sd, LOCKDET_REF0, lockdet_ref & 0x0000ff);
  495. i2c_wr8(sd, LOCKDET_REF1, (lockdet_ref & 0x00ff00) >> 8);
  496. i2c_wr8(sd, LOCKDET_REF2, (lockdet_ref & 0x0f0000) >> 16);
  497. i2c_wr8_and_or(sd, NCO_F0_MOD, ~MASK_NCO_F0_MOD,
  498. (pdata->refclk_hz == 27000000) ?
  499. MASK_NCO_F0_MOD_27MHZ : 0x0);
  500. /*
  501. * Trial and error suggests that the default register value
  502. * of 656 is for a 42 MHz reference clock. Use that to derive
  503. * a new value based on the actual reference clock.
  504. */
  505. cec_freq = (656 * sys_freq) / 4200;
  506. i2c_wr16(sd, CECHCLK, cec_freq);
  507. i2c_wr16(sd, CECLCLK, cec_freq);
  508. }
  509. static void tc358743_set_csi_color_space(struct v4l2_subdev *sd)
  510. {
  511. struct tc358743_state *state = to_state(sd);
  512. switch (state->mbus_fmt_code) {
  513. case MEDIA_BUS_FMT_UYVY8_1X16:
  514. v4l2_dbg(2, debug, sd, "%s: YCbCr 422 16-bit\n", __func__);
  515. i2c_wr8_and_or(sd, VOUT_SET2,
  516. ~(MASK_SEL422 | MASK_VOUT_422FIL_100) & 0xff,
  517. MASK_SEL422 | MASK_VOUT_422FIL_100);
  518. i2c_wr8_and_or(sd, VI_REP, ~MASK_VOUT_COLOR_SEL & 0xff,
  519. MASK_VOUT_COLOR_601_YCBCR_LIMITED);
  520. mutex_lock(&state->confctl_mutex);
  521. i2c_wr16_and_or(sd, CONFCTL, ~MASK_YCBCRFMT,
  522. MASK_YCBCRFMT_422_8_BIT);
  523. mutex_unlock(&state->confctl_mutex);
  524. break;
  525. case MEDIA_BUS_FMT_RGB888_1X24:
  526. v4l2_dbg(2, debug, sd, "%s: RGB 888 24-bit\n", __func__);
  527. i2c_wr8_and_or(sd, VOUT_SET2,
  528. ~(MASK_SEL422 | MASK_VOUT_422FIL_100) & 0xff,
  529. 0x00);
  530. i2c_wr8_and_or(sd, VI_REP, ~MASK_VOUT_COLOR_SEL & 0xff,
  531. MASK_VOUT_COLOR_RGB_FULL);
  532. mutex_lock(&state->confctl_mutex);
  533. i2c_wr16_and_or(sd, CONFCTL, ~MASK_YCBCRFMT, 0);
  534. mutex_unlock(&state->confctl_mutex);
  535. break;
  536. default:
  537. v4l2_dbg(2, debug, sd, "%s: Unsupported format code 0x%x\n",
  538. __func__, state->mbus_fmt_code);
  539. }
  540. }
  541. static unsigned tc358743_num_csi_lanes_needed(struct v4l2_subdev *sd)
  542. {
  543. struct tc358743_state *state = to_state(sd);
  544. struct v4l2_bt_timings *bt = &state->timings.bt;
  545. struct tc358743_platform_data *pdata = &state->pdata;
  546. u32 bits_pr_pixel =
  547. (state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16) ? 16 : 24;
  548. u32 bps = bt->width * bt->height * fps(bt) * bits_pr_pixel;
  549. u32 bps_pr_lane = (pdata->refclk_hz / pdata->pll_prd) * pdata->pll_fbd;
  550. return DIV_ROUND_UP(bps, bps_pr_lane);
  551. }
  552. static void tc358743_set_csi(struct v4l2_subdev *sd)
  553. {
  554. struct tc358743_state *state = to_state(sd);
  555. struct tc358743_platform_data *pdata = &state->pdata;
  556. unsigned lanes = tc358743_num_csi_lanes_needed(sd);
  557. v4l2_dbg(3, debug, sd, "%s:\n", __func__);
  558. state->csi_lanes_in_use = lanes;
  559. tc358743_reset(sd, MASK_CTXRST);
  560. if (lanes < 1)
  561. i2c_wr32(sd, CLW_CNTRL, MASK_CLW_LANEDISABLE);
  562. if (lanes < 1)
  563. i2c_wr32(sd, D0W_CNTRL, MASK_D0W_LANEDISABLE);
  564. if (lanes < 2)
  565. i2c_wr32(sd, D1W_CNTRL, MASK_D1W_LANEDISABLE);
  566. if (lanes < 3)
  567. i2c_wr32(sd, D2W_CNTRL, MASK_D2W_LANEDISABLE);
  568. if (lanes < 4)
  569. i2c_wr32(sd, D3W_CNTRL, MASK_D3W_LANEDISABLE);
  570. i2c_wr32(sd, LINEINITCNT, pdata->lineinitcnt);
  571. i2c_wr32(sd, LPTXTIMECNT, pdata->lptxtimecnt);
  572. i2c_wr32(sd, TCLK_HEADERCNT, pdata->tclk_headercnt);
  573. i2c_wr32(sd, TCLK_TRAILCNT, pdata->tclk_trailcnt);
  574. i2c_wr32(sd, THS_HEADERCNT, pdata->ths_headercnt);
  575. i2c_wr32(sd, TWAKEUP, pdata->twakeup);
  576. i2c_wr32(sd, TCLK_POSTCNT, pdata->tclk_postcnt);
  577. i2c_wr32(sd, THS_TRAILCNT, pdata->ths_trailcnt);
  578. i2c_wr32(sd, HSTXVREGCNT, pdata->hstxvregcnt);
  579. i2c_wr32(sd, HSTXVREGEN,
  580. ((lanes > 0) ? MASK_CLM_HSTXVREGEN : 0x0) |
  581. ((lanes > 0) ? MASK_D0M_HSTXVREGEN : 0x0) |
  582. ((lanes > 1) ? MASK_D1M_HSTXVREGEN : 0x0) |
  583. ((lanes > 2) ? MASK_D2M_HSTXVREGEN : 0x0) |
  584. ((lanes > 3) ? MASK_D3M_HSTXVREGEN : 0x0));
  585. i2c_wr32(sd, TXOPTIONCNTRL, (state->bus.flags &
  586. V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) ? MASK_CONTCLKMODE : 0);
  587. i2c_wr32(sd, STARTCNTRL, MASK_START);
  588. i2c_wr32(sd, CSI_START, MASK_STRT);
  589. i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
  590. MASK_ADDRESS_CSI_CONTROL |
  591. MASK_CSI_MODE |
  592. MASK_TXHSMD |
  593. ((lanes == 4) ? MASK_NOL_4 :
  594. (lanes == 3) ? MASK_NOL_3 :
  595. (lanes == 2) ? MASK_NOL_2 : MASK_NOL_1));
  596. i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
  597. MASK_ADDRESS_CSI_ERR_INTENA | MASK_TXBRK | MASK_QUNK |
  598. MASK_WCER | MASK_INER);
  599. i2c_wr32(sd, CSI_CONFW, MASK_MODE_CLEAR |
  600. MASK_ADDRESS_CSI_ERR_HALT | MASK_TXBRK | MASK_QUNK);
  601. i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
  602. MASK_ADDRESS_CSI_INT_ENA | MASK_INTER);
  603. }
  604. static void tc358743_set_hdmi_phy(struct v4l2_subdev *sd)
  605. {
  606. struct tc358743_state *state = to_state(sd);
  607. struct tc358743_platform_data *pdata = &state->pdata;
  608. /* Default settings from REF_02, sheet "Source HDMI"
  609. * and custom settings as platform data */
  610. i2c_wr8_and_or(sd, PHY_EN, ~MASK_ENABLE_PHY, 0x0);
  611. i2c_wr8(sd, PHY_CTL1, SET_PHY_AUTO_RST1_US(1600) |
  612. SET_FREQ_RANGE_MODE_CYCLES(1));
  613. i2c_wr8_and_or(sd, PHY_CTL2, ~MASK_PHY_AUTO_RSTn,
  614. (pdata->hdmi_phy_auto_reset_tmds_detected ?
  615. MASK_PHY_AUTO_RST2 : 0) |
  616. (pdata->hdmi_phy_auto_reset_tmds_in_range ?
  617. MASK_PHY_AUTO_RST3 : 0) |
  618. (pdata->hdmi_phy_auto_reset_tmds_valid ?
  619. MASK_PHY_AUTO_RST4 : 0));
  620. i2c_wr8(sd, PHY_BIAS, 0x40);
  621. i2c_wr8(sd, PHY_CSQ, SET_CSQ_CNT_LEVEL(0x0a));
  622. i2c_wr8(sd, AVM_CTL, 45);
  623. i2c_wr8_and_or(sd, HDMI_DET, ~MASK_HDMI_DET_V,
  624. pdata->hdmi_detection_delay << 4);
  625. i2c_wr8_and_or(sd, HV_RST, ~(MASK_H_PI_RST | MASK_V_PI_RST),
  626. (pdata->hdmi_phy_auto_reset_hsync_out_of_range ?
  627. MASK_H_PI_RST : 0) |
  628. (pdata->hdmi_phy_auto_reset_vsync_out_of_range ?
  629. MASK_V_PI_RST : 0));
  630. i2c_wr8_and_or(sd, PHY_EN, ~MASK_ENABLE_PHY, MASK_ENABLE_PHY);
  631. }
  632. static void tc358743_set_hdmi_audio(struct v4l2_subdev *sd)
  633. {
  634. struct tc358743_state *state = to_state(sd);
  635. /* Default settings from REF_02, sheet "Source HDMI" */
  636. i2c_wr8(sd, FORCE_MUTE, 0x00);
  637. i2c_wr8(sd, AUTO_CMD0, MASK_AUTO_MUTE7 | MASK_AUTO_MUTE6 |
  638. MASK_AUTO_MUTE5 | MASK_AUTO_MUTE4 |
  639. MASK_AUTO_MUTE1 | MASK_AUTO_MUTE0);
  640. i2c_wr8(sd, AUTO_CMD1, MASK_AUTO_MUTE9);
  641. i2c_wr8(sd, AUTO_CMD2, MASK_AUTO_PLAY3 | MASK_AUTO_PLAY2);
  642. i2c_wr8(sd, BUFINIT_START, SET_BUFINIT_START_MS(500));
  643. i2c_wr8(sd, FS_MUTE, 0x00);
  644. i2c_wr8(sd, FS_IMODE, MASK_NLPCM_SMODE | MASK_FS_SMODE);
  645. i2c_wr8(sd, ACR_MODE, MASK_CTS_MODE);
  646. i2c_wr8(sd, ACR_MDF0, MASK_ACR_L2MDF_1976_PPM | MASK_ACR_L1MDF_976_PPM);
  647. i2c_wr8(sd, ACR_MDF1, MASK_ACR_L3MDF_3906_PPM);
  648. i2c_wr8(sd, SDO_MODE1, MASK_SDO_FMT_I2S);
  649. i2c_wr8(sd, DIV_MODE, SET_DIV_DLY_MS(100));
  650. mutex_lock(&state->confctl_mutex);
  651. i2c_wr16_and_or(sd, CONFCTL, 0xffff, MASK_AUDCHNUM_2 |
  652. MASK_AUDOUTSEL_I2S | MASK_AUTOINDEX);
  653. mutex_unlock(&state->confctl_mutex);
  654. }
  655. static void tc358743_set_hdmi_info_frame_mode(struct v4l2_subdev *sd)
  656. {
  657. /* Default settings from REF_02, sheet "Source HDMI" */
  658. i2c_wr8(sd, PK_INT_MODE, MASK_ISRC2_INT_MODE | MASK_ISRC_INT_MODE |
  659. MASK_ACP_INT_MODE | MASK_VS_INT_MODE |
  660. MASK_SPD_INT_MODE | MASK_MS_INT_MODE |
  661. MASK_AUD_INT_MODE | MASK_AVI_INT_MODE);
  662. i2c_wr8(sd, NO_PKT_LIMIT, 0x2c);
  663. i2c_wr8(sd, NO_PKT_CLR, 0x53);
  664. i2c_wr8(sd, ERR_PK_LIMIT, 0x01);
  665. i2c_wr8(sd, NO_PKT_LIMIT2, 0x30);
  666. i2c_wr8(sd, NO_GDB_LIMIT, 0x10);
  667. }
  668. static void tc358743_initial_setup(struct v4l2_subdev *sd)
  669. {
  670. struct tc358743_state *state = to_state(sd);
  671. struct tc358743_platform_data *pdata = &state->pdata;
  672. /*
  673. * IR is not supported by this driver.
  674. * CEC is only enabled if needed.
  675. */
  676. i2c_wr16_and_or(sd, SYSCTL, ~(MASK_IRRST | MASK_CECRST),
  677. (MASK_IRRST | MASK_CECRST));
  678. tc358743_reset(sd, MASK_CTXRST | MASK_HDMIRST);
  679. #ifdef CONFIG_VIDEO_TC358743_CEC
  680. tc358743_reset(sd, MASK_CECRST);
  681. #endif
  682. tc358743_sleep_mode(sd, false);
  683. i2c_wr16(sd, FIFOCTL, pdata->fifo_level);
  684. tc358743_set_ref_clk(sd);
  685. i2c_wr8_and_or(sd, DDC_CTL, ~MASK_DDC5V_MODE,
  686. pdata->ddc5v_delay & MASK_DDC5V_MODE);
  687. i2c_wr8_and_or(sd, EDID_MODE, ~MASK_EDID_MODE, MASK_EDID_MODE_E_DDC);
  688. tc358743_set_hdmi_phy(sd);
  689. tc358743_set_hdmi_hdcp(sd, pdata->enable_hdcp);
  690. tc358743_set_hdmi_audio(sd);
  691. tc358743_set_hdmi_info_frame_mode(sd);
  692. /* All CE and IT formats are detected as RGB full range in DVI mode */
  693. i2c_wr8_and_or(sd, VI_MODE, ~MASK_RGB_DVI, 0);
  694. i2c_wr8_and_or(sd, VOUT_SET2, ~MASK_VOUTCOLORMODE,
  695. MASK_VOUTCOLORMODE_AUTO);
  696. i2c_wr8(sd, VOUT_SET3, MASK_VOUT_EXTCNT);
  697. }
  698. /* --------------- CEC --------------- */
  699. #ifdef CONFIG_VIDEO_TC358743_CEC
  700. static int tc358743_cec_adap_enable(struct cec_adapter *adap, bool enable)
  701. {
  702. struct tc358743_state *state = adap->priv;
  703. struct v4l2_subdev *sd = &state->sd;
  704. i2c_wr32(sd, CECIMSK, enable ? MASK_CECTIM | MASK_CECRIM : 0);
  705. i2c_wr32(sd, CECICLR, MASK_CECTICLR | MASK_CECRICLR);
  706. i2c_wr32(sd, CECEN, enable);
  707. if (enable)
  708. i2c_wr32(sd, CECREN, MASK_CECREN);
  709. return 0;
  710. }
  711. static int tc358743_cec_adap_monitor_all_enable(struct cec_adapter *adap,
  712. bool enable)
  713. {
  714. struct tc358743_state *state = adap->priv;
  715. struct v4l2_subdev *sd = &state->sd;
  716. u32 reg;
  717. reg = i2c_rd32(sd, CECRCTL1);
  718. if (enable)
  719. reg |= MASK_CECOTH;
  720. else
  721. reg &= ~MASK_CECOTH;
  722. i2c_wr32(sd, CECRCTL1, reg);
  723. return 0;
  724. }
  725. static int tc358743_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
  726. {
  727. struct tc358743_state *state = adap->priv;
  728. struct v4l2_subdev *sd = &state->sd;
  729. unsigned int la = 0;
  730. if (log_addr != CEC_LOG_ADDR_INVALID) {
  731. la = i2c_rd32(sd, CECADD);
  732. la |= 1 << log_addr;
  733. }
  734. i2c_wr32(sd, CECADD, la);
  735. return 0;
  736. }
  737. static int tc358743_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  738. u32 signal_free_time, struct cec_msg *msg)
  739. {
  740. struct tc358743_state *state = adap->priv;
  741. struct v4l2_subdev *sd = &state->sd;
  742. unsigned int i;
  743. i2c_wr32(sd, CECTCTL,
  744. (cec_msg_is_broadcast(msg) ? MASK_CECBRD : 0) |
  745. (signal_free_time - 1));
  746. for (i = 0; i < msg->len; i++)
  747. i2c_wr32(sd, CECTBUF1 + i * 4,
  748. msg->msg[i] | ((i == msg->len - 1) ? MASK_CECTEOM : 0));
  749. i2c_wr32(sd, CECTEN, MASK_CECTEN);
  750. return 0;
  751. }
  752. static const struct cec_adap_ops tc358743_cec_adap_ops = {
  753. .adap_enable = tc358743_cec_adap_enable,
  754. .adap_log_addr = tc358743_cec_adap_log_addr,
  755. .adap_transmit = tc358743_cec_adap_transmit,
  756. .adap_monitor_all_enable = tc358743_cec_adap_monitor_all_enable,
  757. };
  758. static void tc358743_cec_isr(struct v4l2_subdev *sd, u16 intstatus,
  759. bool *handled)
  760. {
  761. struct tc358743_state *state = to_state(sd);
  762. unsigned int cec_rxint, cec_txint;
  763. unsigned int clr = 0;
  764. cec_rxint = i2c_rd32(sd, CECRSTAT);
  765. cec_txint = i2c_rd32(sd, CECTSTAT);
  766. if (intstatus & MASK_CEC_RINT)
  767. clr |= MASK_CECRICLR;
  768. if (intstatus & MASK_CEC_TINT)
  769. clr |= MASK_CECTICLR;
  770. i2c_wr32(sd, CECICLR, clr);
  771. if ((intstatus & MASK_CEC_TINT) && cec_txint) {
  772. if (cec_txint & MASK_CECTIEND)
  773. cec_transmit_attempt_done(state->cec_adap,
  774. CEC_TX_STATUS_OK);
  775. else if (cec_txint & MASK_CECTIAL)
  776. cec_transmit_attempt_done(state->cec_adap,
  777. CEC_TX_STATUS_ARB_LOST);
  778. else if (cec_txint & MASK_CECTIACK)
  779. cec_transmit_attempt_done(state->cec_adap,
  780. CEC_TX_STATUS_NACK);
  781. else if (cec_txint & MASK_CECTIUR) {
  782. /*
  783. * Not sure when this bit is set. Treat
  784. * it as an error for now.
  785. */
  786. cec_transmit_attempt_done(state->cec_adap,
  787. CEC_TX_STATUS_ERROR);
  788. }
  789. *handled = true;
  790. }
  791. if ((intstatus & MASK_CEC_RINT) &&
  792. (cec_rxint & MASK_CECRIEND)) {
  793. struct cec_msg msg = {};
  794. unsigned int i;
  795. unsigned int v;
  796. v = i2c_rd32(sd, CECRCTR);
  797. msg.len = v & 0x1f;
  798. for (i = 0; i < msg.len; i++) {
  799. v = i2c_rd32(sd, CECRBUF1 + i * 4);
  800. msg.msg[i] = v & 0xff;
  801. }
  802. cec_received_msg(state->cec_adap, &msg);
  803. *handled = true;
  804. }
  805. i2c_wr16(sd, INTSTATUS,
  806. intstatus & (MASK_CEC_RINT | MASK_CEC_TINT));
  807. }
  808. #endif
  809. /* --------------- IRQ --------------- */
  810. static void tc358743_format_change(struct v4l2_subdev *sd)
  811. {
  812. struct tc358743_state *state = to_state(sd);
  813. struct v4l2_dv_timings timings;
  814. const struct v4l2_event tc358743_ev_fmt = {
  815. .type = V4L2_EVENT_SOURCE_CHANGE,
  816. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  817. };
  818. if (tc358743_get_detected_timings(sd, &timings)) {
  819. enable_stream(sd, false);
  820. v4l2_dbg(1, debug, sd, "%s: No signal\n",
  821. __func__);
  822. } else {
  823. if (!v4l2_match_dv_timings(&state->timings, &timings, 0, false))
  824. enable_stream(sd, false);
  825. if (debug)
  826. v4l2_print_dv_timings(sd->name,
  827. "tc358743_format_change: New format: ",
  828. &timings, false);
  829. }
  830. if (sd->devnode)
  831. v4l2_subdev_notify_event(sd, &tc358743_ev_fmt);
  832. }
  833. static void tc358743_init_interrupts(struct v4l2_subdev *sd)
  834. {
  835. u16 i;
  836. /* clear interrupt status registers */
  837. for (i = SYS_INT; i <= KEY_INT; i++)
  838. i2c_wr8(sd, i, 0xff);
  839. i2c_wr16(sd, INTSTATUS, 0xffff);
  840. }
  841. static void tc358743_enable_interrupts(struct v4l2_subdev *sd,
  842. bool cable_connected)
  843. {
  844. v4l2_dbg(2, debug, sd, "%s: cable connected = %d\n", __func__,
  845. cable_connected);
  846. if (cable_connected) {
  847. i2c_wr8(sd, SYS_INTM, ~(MASK_M_DDC | MASK_M_DVI_DET |
  848. MASK_M_HDMI_DET) & 0xff);
  849. i2c_wr8(sd, CLK_INTM, ~MASK_M_IN_DE_CHG);
  850. i2c_wr8(sd, CBIT_INTM, ~(MASK_M_CBIT_FS | MASK_M_AF_LOCK |
  851. MASK_M_AF_UNLOCK) & 0xff);
  852. i2c_wr8(sd, AUDIO_INTM, ~MASK_M_BUFINIT_END);
  853. i2c_wr8(sd, MISC_INTM, ~MASK_M_SYNC_CHG);
  854. } else {
  855. i2c_wr8(sd, SYS_INTM, ~MASK_M_DDC & 0xff);
  856. i2c_wr8(sd, CLK_INTM, 0xff);
  857. i2c_wr8(sd, CBIT_INTM, 0xff);
  858. i2c_wr8(sd, AUDIO_INTM, 0xff);
  859. i2c_wr8(sd, MISC_INTM, 0xff);
  860. }
  861. }
  862. static void tc358743_hdmi_audio_int_handler(struct v4l2_subdev *sd,
  863. bool *handled)
  864. {
  865. u8 audio_int_mask = i2c_rd8(sd, AUDIO_INTM);
  866. u8 audio_int = i2c_rd8(sd, AUDIO_INT) & ~audio_int_mask;
  867. i2c_wr8(sd, AUDIO_INT, audio_int);
  868. v4l2_dbg(3, debug, sd, "%s: AUDIO_INT = 0x%02x\n", __func__, audio_int);
  869. tc358743_s_ctrl_audio_sampling_rate(sd);
  870. tc358743_s_ctrl_audio_present(sd);
  871. }
  872. static void tc358743_csi_err_int_handler(struct v4l2_subdev *sd, bool *handled)
  873. {
  874. v4l2_err(sd, "%s: CSI_ERR = 0x%x\n", __func__, i2c_rd32(sd, CSI_ERR));
  875. i2c_wr32(sd, CSI_INT_CLR, MASK_ICRER);
  876. }
  877. static void tc358743_hdmi_misc_int_handler(struct v4l2_subdev *sd,
  878. bool *handled)
  879. {
  880. u8 misc_int_mask = i2c_rd8(sd, MISC_INTM);
  881. u8 misc_int = i2c_rd8(sd, MISC_INT) & ~misc_int_mask;
  882. i2c_wr8(sd, MISC_INT, misc_int);
  883. v4l2_dbg(3, debug, sd, "%s: MISC_INT = 0x%02x\n", __func__, misc_int);
  884. if (misc_int & MASK_I_SYNC_CHG) {
  885. /* Reset the HDMI PHY to try to trigger proper lock on the
  886. * incoming video format. Erase BKSV to prevent that old keys
  887. * are used when a new source is connected. */
  888. if (no_sync(sd) || no_signal(sd)) {
  889. tc358743_reset_phy(sd);
  890. tc358743_erase_bksv(sd);
  891. }
  892. tc358743_format_change(sd);
  893. misc_int &= ~MASK_I_SYNC_CHG;
  894. if (handled)
  895. *handled = true;
  896. }
  897. if (misc_int) {
  898. v4l2_err(sd, "%s: Unhandled MISC_INT interrupts: 0x%02x\n",
  899. __func__, misc_int);
  900. }
  901. }
  902. static void tc358743_hdmi_cbit_int_handler(struct v4l2_subdev *sd,
  903. bool *handled)
  904. {
  905. u8 cbit_int_mask = i2c_rd8(sd, CBIT_INTM);
  906. u8 cbit_int = i2c_rd8(sd, CBIT_INT) & ~cbit_int_mask;
  907. i2c_wr8(sd, CBIT_INT, cbit_int);
  908. v4l2_dbg(3, debug, sd, "%s: CBIT_INT = 0x%02x\n", __func__, cbit_int);
  909. if (cbit_int & MASK_I_CBIT_FS) {
  910. v4l2_dbg(1, debug, sd, "%s: Audio sample rate changed\n",
  911. __func__);
  912. tc358743_s_ctrl_audio_sampling_rate(sd);
  913. cbit_int &= ~MASK_I_CBIT_FS;
  914. if (handled)
  915. *handled = true;
  916. }
  917. if (cbit_int & (MASK_I_AF_LOCK | MASK_I_AF_UNLOCK)) {
  918. v4l2_dbg(1, debug, sd, "%s: Audio present changed\n",
  919. __func__);
  920. tc358743_s_ctrl_audio_present(sd);
  921. cbit_int &= ~(MASK_I_AF_LOCK | MASK_I_AF_UNLOCK);
  922. if (handled)
  923. *handled = true;
  924. }
  925. if (cbit_int) {
  926. v4l2_err(sd, "%s: Unhandled CBIT_INT interrupts: 0x%02x\n",
  927. __func__, cbit_int);
  928. }
  929. }
  930. static void tc358743_hdmi_clk_int_handler(struct v4l2_subdev *sd, bool *handled)
  931. {
  932. u8 clk_int_mask = i2c_rd8(sd, CLK_INTM);
  933. u8 clk_int = i2c_rd8(sd, CLK_INT) & ~clk_int_mask;
  934. /* Bit 7 and bit 6 are set even when they are masked */
  935. i2c_wr8(sd, CLK_INT, clk_int | 0x80 | MASK_I_OUT_H_CHG);
  936. v4l2_dbg(3, debug, sd, "%s: CLK_INT = 0x%02x\n", __func__, clk_int);
  937. if (clk_int & (MASK_I_IN_DE_CHG)) {
  938. v4l2_dbg(1, debug, sd, "%s: DE size or position has changed\n",
  939. __func__);
  940. /* If the source switch to a new resolution with the same pixel
  941. * frequency as the existing (e.g. 1080p25 -> 720p50), the
  942. * I_SYNC_CHG interrupt is not always triggered, while the
  943. * I_IN_DE_CHG interrupt seems to work fine. Format change
  944. * notifications are only sent when the signal is stable to
  945. * reduce the number of notifications. */
  946. if (!no_signal(sd) && !no_sync(sd))
  947. tc358743_format_change(sd);
  948. clk_int &= ~(MASK_I_IN_DE_CHG);
  949. if (handled)
  950. *handled = true;
  951. }
  952. if (clk_int) {
  953. v4l2_err(sd, "%s: Unhandled CLK_INT interrupts: 0x%02x\n",
  954. __func__, clk_int);
  955. }
  956. }
  957. static void tc358743_hdmi_sys_int_handler(struct v4l2_subdev *sd, bool *handled)
  958. {
  959. struct tc358743_state *state = to_state(sd);
  960. u8 sys_int_mask = i2c_rd8(sd, SYS_INTM);
  961. u8 sys_int = i2c_rd8(sd, SYS_INT) & ~sys_int_mask;
  962. i2c_wr8(sd, SYS_INT, sys_int);
  963. v4l2_dbg(3, debug, sd, "%s: SYS_INT = 0x%02x\n", __func__, sys_int);
  964. if (sys_int & MASK_I_DDC) {
  965. bool tx_5v = tx_5v_power_present(sd);
  966. v4l2_dbg(1, debug, sd, "%s: Tx 5V power present: %s\n",
  967. __func__, tx_5v ? "yes" : "no");
  968. if (tx_5v) {
  969. tc358743_enable_edid(sd);
  970. } else {
  971. tc358743_enable_interrupts(sd, false);
  972. tc358743_disable_edid(sd);
  973. memset(&state->timings, 0, sizeof(state->timings));
  974. tc358743_erase_bksv(sd);
  975. tc358743_update_controls(sd);
  976. }
  977. sys_int &= ~MASK_I_DDC;
  978. if (handled)
  979. *handled = true;
  980. }
  981. if (sys_int & MASK_I_DVI) {
  982. v4l2_dbg(1, debug, sd, "%s: HDMI->DVI change detected\n",
  983. __func__);
  984. /* Reset the HDMI PHY to try to trigger proper lock on the
  985. * incoming video format. Erase BKSV to prevent that old keys
  986. * are used when a new source is connected. */
  987. if (no_sync(sd) || no_signal(sd)) {
  988. tc358743_reset_phy(sd);
  989. tc358743_erase_bksv(sd);
  990. }
  991. sys_int &= ~MASK_I_DVI;
  992. if (handled)
  993. *handled = true;
  994. }
  995. if (sys_int & MASK_I_HDMI) {
  996. v4l2_dbg(1, debug, sd, "%s: DVI->HDMI change detected\n",
  997. __func__);
  998. /* Register is reset in DVI mode (REF_01, c. 6.6.41) */
  999. i2c_wr8(sd, ANA_CTL, MASK_APPL_PCSX_NORMAL | MASK_ANALOG_ON);
  1000. sys_int &= ~MASK_I_HDMI;
  1001. if (handled)
  1002. *handled = true;
  1003. }
  1004. if (sys_int) {
  1005. v4l2_err(sd, "%s: Unhandled SYS_INT interrupts: 0x%02x\n",
  1006. __func__, sys_int);
  1007. }
  1008. }
  1009. /* --------------- CORE OPS --------------- */
  1010. static int tc358743_log_status(struct v4l2_subdev *sd)
  1011. {
  1012. struct tc358743_state *state = to_state(sd);
  1013. struct v4l2_dv_timings timings;
  1014. uint8_t hdmi_sys_status = i2c_rd8(sd, SYS_STATUS);
  1015. uint16_t sysctl = i2c_rd16(sd, SYSCTL);
  1016. u8 vi_status3 = i2c_rd8(sd, VI_STATUS3);
  1017. const int deep_color_mode[4] = { 8, 10, 12, 16 };
  1018. static const char * const input_color_space[] = {
  1019. "RGB", "YCbCr 601", "opRGB", "YCbCr 709", "NA (4)",
  1020. "xvYCC 601", "NA(6)", "xvYCC 709", "NA(8)", "sYCC601",
  1021. "NA(10)", "NA(11)", "NA(12)", "opYCC 601"};
  1022. v4l2_info(sd, "-----Chip status-----\n");
  1023. v4l2_info(sd, "Chip ID: 0x%02x\n",
  1024. (i2c_rd16(sd, CHIPID) & MASK_CHIPID) >> 8);
  1025. v4l2_info(sd, "Chip revision: 0x%02x\n",
  1026. i2c_rd16(sd, CHIPID) & MASK_REVID);
  1027. v4l2_info(sd, "Reset: IR: %d, CEC: %d, CSI TX: %d, HDMI: %d\n",
  1028. !!(sysctl & MASK_IRRST),
  1029. !!(sysctl & MASK_CECRST),
  1030. !!(sysctl & MASK_CTXRST),
  1031. !!(sysctl & MASK_HDMIRST));
  1032. v4l2_info(sd, "Sleep mode: %s\n", sysctl & MASK_SLEEP ? "on" : "off");
  1033. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  1034. hdmi_sys_status & MASK_S_DDC5V ? "yes" : "no");
  1035. v4l2_info(sd, "DDC lines enabled: %s\n",
  1036. (i2c_rd8(sd, EDID_MODE) & MASK_EDID_MODE_E_DDC) ?
  1037. "yes" : "no");
  1038. v4l2_info(sd, "Hotplug enabled: %s\n",
  1039. (i2c_rd8(sd, HPD_CTL) & MASK_HPD_OUT0) ?
  1040. "yes" : "no");
  1041. v4l2_info(sd, "CEC enabled: %s\n",
  1042. (i2c_rd16(sd, CECEN) & MASK_CECEN) ? "yes" : "no");
  1043. v4l2_info(sd, "-----Signal status-----\n");
  1044. v4l2_info(sd, "TMDS signal detected: %s\n",
  1045. hdmi_sys_status & MASK_S_TMDS ? "yes" : "no");
  1046. v4l2_info(sd, "Stable sync signal: %s\n",
  1047. hdmi_sys_status & MASK_S_SYNC ? "yes" : "no");
  1048. v4l2_info(sd, "PHY PLL locked: %s\n",
  1049. hdmi_sys_status & MASK_S_PHY_PLL ? "yes" : "no");
  1050. v4l2_info(sd, "PHY DE detected: %s\n",
  1051. hdmi_sys_status & MASK_S_PHY_SCDT ? "yes" : "no");
  1052. if (tc358743_get_detected_timings(sd, &timings)) {
  1053. v4l2_info(sd, "No video detected\n");
  1054. } else {
  1055. v4l2_print_dv_timings(sd->name, "Detected format: ", &timings,
  1056. true);
  1057. }
  1058. v4l2_print_dv_timings(sd->name, "Configured format: ", &state->timings,
  1059. true);
  1060. v4l2_info(sd, "-----CSI-TX status-----\n");
  1061. v4l2_info(sd, "Lanes needed: %d\n",
  1062. tc358743_num_csi_lanes_needed(sd));
  1063. v4l2_info(sd, "Lanes in use: %d\n",
  1064. state->csi_lanes_in_use);
  1065. v4l2_info(sd, "Waiting for particular sync signal: %s\n",
  1066. (i2c_rd16(sd, CSI_STATUS) & MASK_S_WSYNC) ?
  1067. "yes" : "no");
  1068. v4l2_info(sd, "Transmit mode: %s\n",
  1069. (i2c_rd16(sd, CSI_STATUS) & MASK_S_TXACT) ?
  1070. "yes" : "no");
  1071. v4l2_info(sd, "Receive mode: %s\n",
  1072. (i2c_rd16(sd, CSI_STATUS) & MASK_S_RXACT) ?
  1073. "yes" : "no");
  1074. v4l2_info(sd, "Stopped: %s\n",
  1075. (i2c_rd16(sd, CSI_STATUS) & MASK_S_HLT) ?
  1076. "yes" : "no");
  1077. v4l2_info(sd, "Color space: %s\n",
  1078. state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16 ?
  1079. "YCbCr 422 16-bit" :
  1080. state->mbus_fmt_code == MEDIA_BUS_FMT_RGB888_1X24 ?
  1081. "RGB 888 24-bit" : "Unsupported");
  1082. v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
  1083. v4l2_info(sd, "HDCP encrypted content: %s\n",
  1084. hdmi_sys_status & MASK_S_HDCP ? "yes" : "no");
  1085. v4l2_info(sd, "Input color space: %s %s range\n",
  1086. input_color_space[(vi_status3 & MASK_S_V_COLOR) >> 1],
  1087. (vi_status3 & MASK_LIMITED) ? "limited" : "full");
  1088. if (!is_hdmi(sd))
  1089. return 0;
  1090. v4l2_info(sd, "AV Mute: %s\n", hdmi_sys_status & MASK_S_AVMUTE ? "on" :
  1091. "off");
  1092. v4l2_info(sd, "Deep color mode: %d-bits per channel\n",
  1093. deep_color_mode[(i2c_rd8(sd, VI_STATUS1) &
  1094. MASK_S_DEEPCOLOR) >> 2]);
  1095. print_avi_infoframe(sd);
  1096. return 0;
  1097. }
  1098. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1099. static void tc358743_print_register_map(struct v4l2_subdev *sd)
  1100. {
  1101. v4l2_info(sd, "0x0000-0x00FF: Global Control Register\n");
  1102. v4l2_info(sd, "0x0100-0x01FF: CSI2-TX PHY Register\n");
  1103. v4l2_info(sd, "0x0200-0x03FF: CSI2-TX PPI Register\n");
  1104. v4l2_info(sd, "0x0400-0x05FF: Reserved\n");
  1105. v4l2_info(sd, "0x0600-0x06FF: CEC Register\n");
  1106. v4l2_info(sd, "0x0700-0x84FF: Reserved\n");
  1107. v4l2_info(sd, "0x8500-0x85FF: HDMIRX System Control Register\n");
  1108. v4l2_info(sd, "0x8600-0x86FF: HDMIRX Audio Control Register\n");
  1109. v4l2_info(sd, "0x8700-0x87FF: HDMIRX InfoFrame packet data Register\n");
  1110. v4l2_info(sd, "0x8800-0x88FF: HDMIRX HDCP Port Register\n");
  1111. v4l2_info(sd, "0x8900-0x89FF: HDMIRX Video Output Port & 3D Register\n");
  1112. v4l2_info(sd, "0x8A00-0x8BFF: Reserved\n");
  1113. v4l2_info(sd, "0x8C00-0x8FFF: HDMIRX EDID-RAM (1024bytes)\n");
  1114. v4l2_info(sd, "0x9000-0x90FF: HDMIRX GBD Extraction Control\n");
  1115. v4l2_info(sd, "0x9100-0x92FF: HDMIRX GBD RAM read\n");
  1116. v4l2_info(sd, "0x9300- : Reserved\n");
  1117. }
  1118. static int tc358743_get_reg_size(u16 address)
  1119. {
  1120. /* REF_01 p. 66-72 */
  1121. if (address <= 0x00ff)
  1122. return 2;
  1123. else if ((address >= 0x0100) && (address <= 0x06FF))
  1124. return 4;
  1125. else if ((address >= 0x0700) && (address <= 0x84ff))
  1126. return 2;
  1127. else
  1128. return 1;
  1129. }
  1130. static int tc358743_g_register(struct v4l2_subdev *sd,
  1131. struct v4l2_dbg_register *reg)
  1132. {
  1133. if (reg->reg > 0xffff) {
  1134. tc358743_print_register_map(sd);
  1135. return -EINVAL;
  1136. }
  1137. reg->size = tc358743_get_reg_size(reg->reg);
  1138. reg->val = i2c_rdreg(sd, reg->reg, reg->size);
  1139. return 0;
  1140. }
  1141. static int tc358743_s_register(struct v4l2_subdev *sd,
  1142. const struct v4l2_dbg_register *reg)
  1143. {
  1144. if (reg->reg > 0xffff) {
  1145. tc358743_print_register_map(sd);
  1146. return -EINVAL;
  1147. }
  1148. /* It should not be possible for the user to enable HDCP with a simple
  1149. * v4l2-dbg command.
  1150. *
  1151. * DO NOT REMOVE THIS unless all other issues with HDCP have been
  1152. * resolved.
  1153. */
  1154. if (reg->reg == HDCP_MODE ||
  1155. reg->reg == HDCP_REG1 ||
  1156. reg->reg == HDCP_REG2 ||
  1157. reg->reg == HDCP_REG3 ||
  1158. reg->reg == BCAPS)
  1159. return 0;
  1160. i2c_wrreg(sd, (u16)reg->reg, reg->val,
  1161. tc358743_get_reg_size(reg->reg));
  1162. return 0;
  1163. }
  1164. #endif
  1165. static int tc358743_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1166. {
  1167. u16 intstatus = i2c_rd16(sd, INTSTATUS);
  1168. v4l2_dbg(1, debug, sd, "%s: IntStatus = 0x%04x\n", __func__, intstatus);
  1169. if (intstatus & MASK_HDMI_INT) {
  1170. u8 hdmi_int0 = i2c_rd8(sd, HDMI_INT0);
  1171. u8 hdmi_int1 = i2c_rd8(sd, HDMI_INT1);
  1172. if (hdmi_int0 & MASK_I_MISC)
  1173. tc358743_hdmi_misc_int_handler(sd, handled);
  1174. if (hdmi_int1 & MASK_I_CBIT)
  1175. tc358743_hdmi_cbit_int_handler(sd, handled);
  1176. if (hdmi_int1 & MASK_I_CLK)
  1177. tc358743_hdmi_clk_int_handler(sd, handled);
  1178. if (hdmi_int1 & MASK_I_SYS)
  1179. tc358743_hdmi_sys_int_handler(sd, handled);
  1180. if (hdmi_int1 & MASK_I_AUD)
  1181. tc358743_hdmi_audio_int_handler(sd, handled);
  1182. i2c_wr16(sd, INTSTATUS, MASK_HDMI_INT);
  1183. intstatus &= ~MASK_HDMI_INT;
  1184. }
  1185. #ifdef CONFIG_VIDEO_TC358743_CEC
  1186. if (intstatus & (MASK_CEC_RINT | MASK_CEC_TINT)) {
  1187. tc358743_cec_isr(sd, intstatus, handled);
  1188. i2c_wr16(sd, INTSTATUS,
  1189. intstatus & (MASK_CEC_RINT | MASK_CEC_TINT));
  1190. intstatus &= ~(MASK_CEC_RINT | MASK_CEC_TINT);
  1191. }
  1192. #endif
  1193. if (intstatus & MASK_CSI_INT) {
  1194. u32 csi_int = i2c_rd32(sd, CSI_INT);
  1195. if (csi_int & MASK_INTER)
  1196. tc358743_csi_err_int_handler(sd, handled);
  1197. i2c_wr16(sd, INTSTATUS, MASK_CSI_INT);
  1198. }
  1199. intstatus = i2c_rd16(sd, INTSTATUS);
  1200. if (intstatus) {
  1201. v4l2_dbg(1, debug, sd,
  1202. "%s: Unhandled IntStatus interrupts: 0x%02x\n",
  1203. __func__, intstatus);
  1204. }
  1205. return 0;
  1206. }
  1207. static irqreturn_t tc358743_irq_handler(int irq, void *dev_id)
  1208. {
  1209. struct tc358743_state *state = dev_id;
  1210. bool handled;
  1211. tc358743_isr(&state->sd, 0, &handled);
  1212. return handled ? IRQ_HANDLED : IRQ_NONE;
  1213. }
  1214. static void tc358743_irq_poll_timer(struct timer_list *t)
  1215. {
  1216. struct tc358743_state *state = from_timer(state, t, timer);
  1217. unsigned int msecs;
  1218. schedule_work(&state->work_i2c_poll);
  1219. /*
  1220. * If CEC is present, then we need to poll more frequently,
  1221. * otherwise we will miss CEC messages.
  1222. */
  1223. msecs = state->cec_adap ? POLL_INTERVAL_CEC_MS : POLL_INTERVAL_MS;
  1224. mod_timer(&state->timer, jiffies + msecs_to_jiffies(msecs));
  1225. }
  1226. static void tc358743_work_i2c_poll(struct work_struct *work)
  1227. {
  1228. struct tc358743_state *state = container_of(work,
  1229. struct tc358743_state, work_i2c_poll);
  1230. bool handled;
  1231. tc358743_isr(&state->sd, 0, &handled);
  1232. }
  1233. static int tc358743_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  1234. struct v4l2_event_subscription *sub)
  1235. {
  1236. switch (sub->type) {
  1237. case V4L2_EVENT_SOURCE_CHANGE:
  1238. return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
  1239. case V4L2_EVENT_CTRL:
  1240. return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
  1241. default:
  1242. return -EINVAL;
  1243. }
  1244. }
  1245. /* --------------- VIDEO OPS --------------- */
  1246. static int tc358743_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1247. {
  1248. *status = 0;
  1249. *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
  1250. *status |= no_sync(sd) ? V4L2_IN_ST_NO_SYNC : 0;
  1251. v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
  1252. return 0;
  1253. }
  1254. static int tc358743_s_dv_timings(struct v4l2_subdev *sd,
  1255. struct v4l2_dv_timings *timings)
  1256. {
  1257. struct tc358743_state *state = to_state(sd);
  1258. if (!timings)
  1259. return -EINVAL;
  1260. if (debug)
  1261. v4l2_print_dv_timings(sd->name, "tc358743_s_dv_timings: ",
  1262. timings, false);
  1263. if (v4l2_match_dv_timings(&state->timings, timings, 0, false)) {
  1264. v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
  1265. return 0;
  1266. }
  1267. if (!v4l2_valid_dv_timings(timings,
  1268. &tc358743_timings_cap, NULL, NULL)) {
  1269. v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
  1270. return -ERANGE;
  1271. }
  1272. state->timings = *timings;
  1273. enable_stream(sd, false);
  1274. tc358743_set_pll(sd);
  1275. tc358743_set_csi(sd);
  1276. return 0;
  1277. }
  1278. static int tc358743_g_dv_timings(struct v4l2_subdev *sd,
  1279. struct v4l2_dv_timings *timings)
  1280. {
  1281. struct tc358743_state *state = to_state(sd);
  1282. *timings = state->timings;
  1283. return 0;
  1284. }
  1285. static int tc358743_enum_dv_timings(struct v4l2_subdev *sd,
  1286. struct v4l2_enum_dv_timings *timings)
  1287. {
  1288. if (timings->pad != 0)
  1289. return -EINVAL;
  1290. return v4l2_enum_dv_timings_cap(timings,
  1291. &tc358743_timings_cap, NULL, NULL);
  1292. }
  1293. static int tc358743_query_dv_timings(struct v4l2_subdev *sd,
  1294. struct v4l2_dv_timings *timings)
  1295. {
  1296. int ret;
  1297. ret = tc358743_get_detected_timings(sd, timings);
  1298. if (ret)
  1299. return ret;
  1300. if (debug)
  1301. v4l2_print_dv_timings(sd->name, "tc358743_query_dv_timings: ",
  1302. timings, false);
  1303. if (!v4l2_valid_dv_timings(timings,
  1304. &tc358743_timings_cap, NULL, NULL)) {
  1305. v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
  1306. return -ERANGE;
  1307. }
  1308. return 0;
  1309. }
  1310. static int tc358743_dv_timings_cap(struct v4l2_subdev *sd,
  1311. struct v4l2_dv_timings_cap *cap)
  1312. {
  1313. if (cap->pad != 0)
  1314. return -EINVAL;
  1315. *cap = tc358743_timings_cap;
  1316. return 0;
  1317. }
  1318. static int tc358743_g_mbus_config(struct v4l2_subdev *sd,
  1319. struct v4l2_mbus_config *cfg)
  1320. {
  1321. struct tc358743_state *state = to_state(sd);
  1322. cfg->type = V4L2_MBUS_CSI2;
  1323. /* Support for non-continuous CSI-2 clock is missing in the driver */
  1324. cfg->flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  1325. switch (state->csi_lanes_in_use) {
  1326. case 1:
  1327. cfg->flags |= V4L2_MBUS_CSI2_1_LANE;
  1328. break;
  1329. case 2:
  1330. cfg->flags |= V4L2_MBUS_CSI2_2_LANE;
  1331. break;
  1332. case 3:
  1333. cfg->flags |= V4L2_MBUS_CSI2_3_LANE;
  1334. break;
  1335. case 4:
  1336. cfg->flags |= V4L2_MBUS_CSI2_4_LANE;
  1337. break;
  1338. default:
  1339. return -EINVAL;
  1340. }
  1341. return 0;
  1342. }
  1343. static int tc358743_s_stream(struct v4l2_subdev *sd, int enable)
  1344. {
  1345. enable_stream(sd, enable);
  1346. if (!enable) {
  1347. /* Put all lanes in LP-11 state (STOPSTATE) */
  1348. tc358743_set_csi(sd);
  1349. }
  1350. return 0;
  1351. }
  1352. /* --------------- PAD OPS --------------- */
  1353. static int tc358743_enum_mbus_code(struct v4l2_subdev *sd,
  1354. struct v4l2_subdev_pad_config *cfg,
  1355. struct v4l2_subdev_mbus_code_enum *code)
  1356. {
  1357. switch (code->index) {
  1358. case 0:
  1359. code->code = MEDIA_BUS_FMT_RGB888_1X24;
  1360. break;
  1361. case 1:
  1362. code->code = MEDIA_BUS_FMT_UYVY8_1X16;
  1363. break;
  1364. default:
  1365. return -EINVAL;
  1366. }
  1367. return 0;
  1368. }
  1369. static int tc358743_get_fmt(struct v4l2_subdev *sd,
  1370. struct v4l2_subdev_pad_config *cfg,
  1371. struct v4l2_subdev_format *format)
  1372. {
  1373. struct tc358743_state *state = to_state(sd);
  1374. u8 vi_rep = i2c_rd8(sd, VI_REP);
  1375. if (format->pad != 0)
  1376. return -EINVAL;
  1377. format->format.code = state->mbus_fmt_code;
  1378. format->format.width = state->timings.bt.width;
  1379. format->format.height = state->timings.bt.height;
  1380. format->format.field = V4L2_FIELD_NONE;
  1381. switch (vi_rep & MASK_VOUT_COLOR_SEL) {
  1382. case MASK_VOUT_COLOR_RGB_FULL:
  1383. case MASK_VOUT_COLOR_RGB_LIMITED:
  1384. format->format.colorspace = V4L2_COLORSPACE_SRGB;
  1385. break;
  1386. case MASK_VOUT_COLOR_601_YCBCR_LIMITED:
  1387. case MASK_VOUT_COLOR_601_YCBCR_FULL:
  1388. format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1389. break;
  1390. case MASK_VOUT_COLOR_709_YCBCR_FULL:
  1391. case MASK_VOUT_COLOR_709_YCBCR_LIMITED:
  1392. format->format.colorspace = V4L2_COLORSPACE_REC709;
  1393. break;
  1394. default:
  1395. format->format.colorspace = 0;
  1396. break;
  1397. }
  1398. return 0;
  1399. }
  1400. static int tc358743_set_fmt(struct v4l2_subdev *sd,
  1401. struct v4l2_subdev_pad_config *cfg,
  1402. struct v4l2_subdev_format *format)
  1403. {
  1404. struct tc358743_state *state = to_state(sd);
  1405. u32 code = format->format.code; /* is overwritten by get_fmt */
  1406. int ret = tc358743_get_fmt(sd, cfg, format);
  1407. format->format.code = code;
  1408. if (ret)
  1409. return ret;
  1410. switch (code) {
  1411. case MEDIA_BUS_FMT_RGB888_1X24:
  1412. case MEDIA_BUS_FMT_UYVY8_1X16:
  1413. break;
  1414. default:
  1415. return -EINVAL;
  1416. }
  1417. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  1418. return 0;
  1419. state->mbus_fmt_code = format->format.code;
  1420. enable_stream(sd, false);
  1421. tc358743_set_pll(sd);
  1422. tc358743_set_csi(sd);
  1423. tc358743_set_csi_color_space(sd);
  1424. return 0;
  1425. }
  1426. static int tc358743_g_edid(struct v4l2_subdev *sd,
  1427. struct v4l2_subdev_edid *edid)
  1428. {
  1429. struct tc358743_state *state = to_state(sd);
  1430. memset(edid->reserved, 0, sizeof(edid->reserved));
  1431. if (edid->pad != 0)
  1432. return -EINVAL;
  1433. if (edid->start_block == 0 && edid->blocks == 0) {
  1434. edid->blocks = state->edid_blocks_written;
  1435. return 0;
  1436. }
  1437. if (state->edid_blocks_written == 0)
  1438. return -ENODATA;
  1439. if (edid->start_block >= state->edid_blocks_written ||
  1440. edid->blocks == 0)
  1441. return -EINVAL;
  1442. if (edid->start_block + edid->blocks > state->edid_blocks_written)
  1443. edid->blocks = state->edid_blocks_written - edid->start_block;
  1444. i2c_rd(sd, EDID_RAM + (edid->start_block * EDID_BLOCK_SIZE), edid->edid,
  1445. edid->blocks * EDID_BLOCK_SIZE);
  1446. return 0;
  1447. }
  1448. static int tc358743_s_edid(struct v4l2_subdev *sd,
  1449. struct v4l2_subdev_edid *edid)
  1450. {
  1451. struct tc358743_state *state = to_state(sd);
  1452. u16 edid_len = edid->blocks * EDID_BLOCK_SIZE;
  1453. u16 pa;
  1454. int err;
  1455. int i;
  1456. v4l2_dbg(2, debug, sd, "%s, pad %d, start block %d, blocks %d\n",
  1457. __func__, edid->pad, edid->start_block, edid->blocks);
  1458. memset(edid->reserved, 0, sizeof(edid->reserved));
  1459. if (edid->pad != 0)
  1460. return -EINVAL;
  1461. if (edid->start_block != 0)
  1462. return -EINVAL;
  1463. if (edid->blocks > EDID_NUM_BLOCKS_MAX) {
  1464. edid->blocks = EDID_NUM_BLOCKS_MAX;
  1465. return -E2BIG;
  1466. }
  1467. pa = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
  1468. err = v4l2_phys_addr_validate(pa, &pa, NULL);
  1469. if (err)
  1470. return err;
  1471. cec_phys_addr_invalidate(state->cec_adap);
  1472. tc358743_disable_edid(sd);
  1473. i2c_wr8(sd, EDID_LEN1, edid_len & 0xff);
  1474. i2c_wr8(sd, EDID_LEN2, edid_len >> 8);
  1475. if (edid->blocks == 0) {
  1476. state->edid_blocks_written = 0;
  1477. return 0;
  1478. }
  1479. for (i = 0; i < edid_len; i += EDID_BLOCK_SIZE)
  1480. i2c_wr(sd, EDID_RAM + i, edid->edid + i, EDID_BLOCK_SIZE);
  1481. state->edid_blocks_written = edid->blocks;
  1482. cec_s_phys_addr(state->cec_adap, pa, false);
  1483. if (tx_5v_power_present(sd))
  1484. tc358743_enable_edid(sd);
  1485. return 0;
  1486. }
  1487. /* -------------------------------------------------------------------------- */
  1488. static const struct v4l2_subdev_core_ops tc358743_core_ops = {
  1489. .log_status = tc358743_log_status,
  1490. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1491. .g_register = tc358743_g_register,
  1492. .s_register = tc358743_s_register,
  1493. #endif
  1494. .interrupt_service_routine = tc358743_isr,
  1495. .subscribe_event = tc358743_subscribe_event,
  1496. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1497. };
  1498. static const struct v4l2_subdev_video_ops tc358743_video_ops = {
  1499. .g_input_status = tc358743_g_input_status,
  1500. .s_dv_timings = tc358743_s_dv_timings,
  1501. .g_dv_timings = tc358743_g_dv_timings,
  1502. .query_dv_timings = tc358743_query_dv_timings,
  1503. .g_mbus_config = tc358743_g_mbus_config,
  1504. .s_stream = tc358743_s_stream,
  1505. };
  1506. static const struct v4l2_subdev_pad_ops tc358743_pad_ops = {
  1507. .enum_mbus_code = tc358743_enum_mbus_code,
  1508. .set_fmt = tc358743_set_fmt,
  1509. .get_fmt = tc358743_get_fmt,
  1510. .get_edid = tc358743_g_edid,
  1511. .set_edid = tc358743_s_edid,
  1512. .enum_dv_timings = tc358743_enum_dv_timings,
  1513. .dv_timings_cap = tc358743_dv_timings_cap,
  1514. };
  1515. static const struct v4l2_subdev_ops tc358743_ops = {
  1516. .core = &tc358743_core_ops,
  1517. .video = &tc358743_video_ops,
  1518. .pad = &tc358743_pad_ops,
  1519. };
  1520. /* --------------- CUSTOM CTRLS --------------- */
  1521. static const struct v4l2_ctrl_config tc358743_ctrl_audio_sampling_rate = {
  1522. .id = TC358743_CID_AUDIO_SAMPLING_RATE,
  1523. .name = "Audio sampling rate",
  1524. .type = V4L2_CTRL_TYPE_INTEGER,
  1525. .min = 0,
  1526. .max = 768000,
  1527. .step = 1,
  1528. .def = 0,
  1529. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  1530. };
  1531. static const struct v4l2_ctrl_config tc358743_ctrl_audio_present = {
  1532. .id = TC358743_CID_AUDIO_PRESENT,
  1533. .name = "Audio present",
  1534. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1535. .min = 0,
  1536. .max = 1,
  1537. .step = 1,
  1538. .def = 0,
  1539. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  1540. };
  1541. /* --------------- PROBE / REMOVE --------------- */
  1542. #ifdef CONFIG_OF
  1543. static void tc358743_gpio_reset(struct tc358743_state *state)
  1544. {
  1545. usleep_range(5000, 10000);
  1546. gpiod_set_value(state->reset_gpio, 1);
  1547. usleep_range(1000, 2000);
  1548. gpiod_set_value(state->reset_gpio, 0);
  1549. msleep(20);
  1550. }
  1551. static int tc358743_probe_of(struct tc358743_state *state)
  1552. {
  1553. struct device *dev = &state->i2c_client->dev;
  1554. struct v4l2_fwnode_endpoint *endpoint;
  1555. struct device_node *ep;
  1556. struct clk *refclk;
  1557. u32 bps_pr_lane;
  1558. int ret = -EINVAL;
  1559. refclk = devm_clk_get(dev, "refclk");
  1560. if (IS_ERR(refclk)) {
  1561. if (PTR_ERR(refclk) != -EPROBE_DEFER)
  1562. dev_err(dev, "failed to get refclk: %ld\n",
  1563. PTR_ERR(refclk));
  1564. return PTR_ERR(refclk);
  1565. }
  1566. ep = of_graph_get_next_endpoint(dev->of_node, NULL);
  1567. if (!ep) {
  1568. dev_err(dev, "missing endpoint node\n");
  1569. return -EINVAL;
  1570. }
  1571. endpoint = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep));
  1572. if (IS_ERR(endpoint)) {
  1573. dev_err(dev, "failed to parse endpoint\n");
  1574. ret = PTR_ERR(endpoint);
  1575. goto put_node;
  1576. }
  1577. if (endpoint->bus_type != V4L2_MBUS_CSI2 ||
  1578. endpoint->bus.mipi_csi2.num_data_lanes == 0 ||
  1579. endpoint->nr_of_link_frequencies == 0) {
  1580. dev_err(dev, "missing CSI-2 properties in endpoint\n");
  1581. goto free_endpoint;
  1582. }
  1583. if (endpoint->bus.mipi_csi2.num_data_lanes > 4) {
  1584. dev_err(dev, "invalid number of lanes\n");
  1585. goto free_endpoint;
  1586. }
  1587. state->bus = endpoint->bus.mipi_csi2;
  1588. ret = clk_prepare_enable(refclk);
  1589. if (ret) {
  1590. dev_err(dev, "Failed! to enable clock\n");
  1591. goto free_endpoint;
  1592. }
  1593. state->pdata.refclk_hz = clk_get_rate(refclk);
  1594. state->pdata.ddc5v_delay = DDC5V_DELAY_100_MS;
  1595. state->pdata.enable_hdcp = false;
  1596. /* A FIFO level of 16 should be enough for 2-lane 720p60 at 594 MHz. */
  1597. state->pdata.fifo_level = 16;
  1598. /*
  1599. * The PLL input clock is obtained by dividing refclk by pll_prd.
  1600. * It must be between 6 MHz and 40 MHz, lower frequency is better.
  1601. */
  1602. switch (state->pdata.refclk_hz) {
  1603. case 26000000:
  1604. case 27000000:
  1605. case 42000000:
  1606. state->pdata.pll_prd = state->pdata.refclk_hz / 6000000;
  1607. break;
  1608. default:
  1609. dev_err(dev, "unsupported refclk rate: %u Hz\n",
  1610. state->pdata.refclk_hz);
  1611. goto disable_clk;
  1612. }
  1613. /*
  1614. * The CSI bps per lane must be between 62.5 Mbps and 1 Gbps.
  1615. * The default is 594 Mbps for 4-lane 1080p60 or 2-lane 720p60.
  1616. */
  1617. bps_pr_lane = 2 * endpoint->link_frequencies[0];
  1618. if (bps_pr_lane < 62500000U || bps_pr_lane > 1000000000U) {
  1619. dev_err(dev, "unsupported bps per lane: %u bps\n", bps_pr_lane);
  1620. goto disable_clk;
  1621. }
  1622. /* The CSI speed per lane is refclk / pll_prd * pll_fbd */
  1623. state->pdata.pll_fbd = bps_pr_lane /
  1624. state->pdata.refclk_hz * state->pdata.pll_prd;
  1625. /*
  1626. * FIXME: These timings are from REF_02 for 594 Mbps per lane (297 MHz
  1627. * link frequency). In principle it should be possible to calculate
  1628. * them based on link frequency and resolution.
  1629. */
  1630. if (bps_pr_lane != 594000000U)
  1631. dev_warn(dev, "untested bps per lane: %u bps\n", bps_pr_lane);
  1632. state->pdata.lineinitcnt = 0xe80;
  1633. state->pdata.lptxtimecnt = 0x003;
  1634. /* tclk-preparecnt: 3, tclk-zerocnt: 20 */
  1635. state->pdata.tclk_headercnt = 0x1403;
  1636. state->pdata.tclk_trailcnt = 0x00;
  1637. /* ths-preparecnt: 3, ths-zerocnt: 1 */
  1638. state->pdata.ths_headercnt = 0x0103;
  1639. state->pdata.twakeup = 0x4882;
  1640. state->pdata.tclk_postcnt = 0x008;
  1641. state->pdata.ths_trailcnt = 0x2;
  1642. state->pdata.hstxvregcnt = 0;
  1643. state->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1644. GPIOD_OUT_LOW);
  1645. if (IS_ERR(state->reset_gpio)) {
  1646. dev_err(dev, "failed to get reset gpio\n");
  1647. ret = PTR_ERR(state->reset_gpio);
  1648. goto disable_clk;
  1649. }
  1650. if (state->reset_gpio)
  1651. tc358743_gpio_reset(state);
  1652. ret = 0;
  1653. goto free_endpoint;
  1654. disable_clk:
  1655. clk_disable_unprepare(refclk);
  1656. free_endpoint:
  1657. v4l2_fwnode_endpoint_free(endpoint);
  1658. put_node:
  1659. of_node_put(ep);
  1660. return ret;
  1661. }
  1662. #else
  1663. static inline int tc358743_probe_of(struct tc358743_state *state)
  1664. {
  1665. return -ENODEV;
  1666. }
  1667. #endif
  1668. static int tc358743_probe(struct i2c_client *client,
  1669. const struct i2c_device_id *id)
  1670. {
  1671. static struct v4l2_dv_timings default_timing =
  1672. V4L2_DV_BT_CEA_640X480P59_94;
  1673. struct tc358743_state *state;
  1674. struct tc358743_platform_data *pdata = client->dev.platform_data;
  1675. struct v4l2_subdev *sd;
  1676. u16 irq_mask = MASK_HDMI_MSK | MASK_CSI_MSK;
  1677. int err;
  1678. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1679. return -EIO;
  1680. v4l_dbg(1, debug, client, "chip found @ 0x%x (%s)\n",
  1681. client->addr << 1, client->adapter->name);
  1682. state = devm_kzalloc(&client->dev, sizeof(struct tc358743_state),
  1683. GFP_KERNEL);
  1684. if (!state)
  1685. return -ENOMEM;
  1686. state->i2c_client = client;
  1687. /* platform data */
  1688. if (pdata) {
  1689. state->pdata = *pdata;
  1690. state->bus.flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  1691. } else {
  1692. err = tc358743_probe_of(state);
  1693. if (err == -ENODEV)
  1694. v4l_err(client, "No platform data!\n");
  1695. if (err)
  1696. return err;
  1697. }
  1698. sd = &state->sd;
  1699. v4l2_i2c_subdev_init(sd, client, &tc358743_ops);
  1700. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1701. /* i2c access */
  1702. if ((i2c_rd16(sd, CHIPID) & MASK_CHIPID) != 0) {
  1703. v4l2_info(sd, "not a TC358743 on address 0x%x\n",
  1704. client->addr << 1);
  1705. return -ENODEV;
  1706. }
  1707. /* control handlers */
  1708. v4l2_ctrl_handler_init(&state->hdl, 3);
  1709. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(&state->hdl, NULL,
  1710. V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
  1711. /* custom controls */
  1712. state->audio_sampling_rate_ctrl = v4l2_ctrl_new_custom(&state->hdl,
  1713. &tc358743_ctrl_audio_sampling_rate, NULL);
  1714. state->audio_present_ctrl = v4l2_ctrl_new_custom(&state->hdl,
  1715. &tc358743_ctrl_audio_present, NULL);
  1716. sd->ctrl_handler = &state->hdl;
  1717. if (state->hdl.error) {
  1718. err = state->hdl.error;
  1719. goto err_hdl;
  1720. }
  1721. if (tc358743_update_controls(sd)) {
  1722. err = -ENODEV;
  1723. goto err_hdl;
  1724. }
  1725. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  1726. sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  1727. err = media_entity_pads_init(&sd->entity, 1, &state->pad);
  1728. if (err < 0)
  1729. goto err_hdl;
  1730. state->mbus_fmt_code = MEDIA_BUS_FMT_RGB888_1X24;
  1731. sd->dev = &client->dev;
  1732. err = v4l2_async_register_subdev(sd);
  1733. if (err < 0)
  1734. goto err_hdl;
  1735. mutex_init(&state->confctl_mutex);
  1736. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  1737. tc358743_delayed_work_enable_hotplug);
  1738. #ifdef CONFIG_VIDEO_TC358743_CEC
  1739. state->cec_adap = cec_allocate_adapter(&tc358743_cec_adap_ops,
  1740. state, dev_name(&client->dev),
  1741. CEC_CAP_DEFAULTS | CEC_CAP_MONITOR_ALL, CEC_MAX_LOG_ADDRS);
  1742. if (IS_ERR(state->cec_adap)) {
  1743. err = PTR_ERR(state->cec_adap);
  1744. goto err_hdl;
  1745. }
  1746. irq_mask |= MASK_CEC_RMSK | MASK_CEC_TMSK;
  1747. #endif
  1748. tc358743_initial_setup(sd);
  1749. tc358743_s_dv_timings(sd, &default_timing);
  1750. tc358743_set_csi_color_space(sd);
  1751. tc358743_init_interrupts(sd);
  1752. if (state->i2c_client->irq) {
  1753. err = devm_request_threaded_irq(&client->dev,
  1754. state->i2c_client->irq,
  1755. NULL, tc358743_irq_handler,
  1756. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1757. "tc358743", state);
  1758. if (err)
  1759. goto err_work_queues;
  1760. } else {
  1761. INIT_WORK(&state->work_i2c_poll,
  1762. tc358743_work_i2c_poll);
  1763. timer_setup(&state->timer, tc358743_irq_poll_timer, 0);
  1764. state->timer.expires = jiffies +
  1765. msecs_to_jiffies(POLL_INTERVAL_MS);
  1766. add_timer(&state->timer);
  1767. }
  1768. err = cec_register_adapter(state->cec_adap, &client->dev);
  1769. if (err < 0) {
  1770. pr_err("%s: failed to register the cec device\n", __func__);
  1771. cec_delete_adapter(state->cec_adap);
  1772. state->cec_adap = NULL;
  1773. goto err_work_queues;
  1774. }
  1775. tc358743_enable_interrupts(sd, tx_5v_power_present(sd));
  1776. i2c_wr16(sd, INTMASK, ~irq_mask);
  1777. err = v4l2_ctrl_handler_setup(sd->ctrl_handler);
  1778. if (err)
  1779. goto err_work_queues;
  1780. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  1781. client->addr << 1, client->adapter->name);
  1782. return 0;
  1783. err_work_queues:
  1784. cec_unregister_adapter(state->cec_adap);
  1785. if (!state->i2c_client->irq)
  1786. flush_work(&state->work_i2c_poll);
  1787. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  1788. mutex_destroy(&state->confctl_mutex);
  1789. err_hdl:
  1790. media_entity_cleanup(&sd->entity);
  1791. v4l2_ctrl_handler_free(&state->hdl);
  1792. return err;
  1793. }
  1794. static int tc358743_remove(struct i2c_client *client)
  1795. {
  1796. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1797. struct tc358743_state *state = to_state(sd);
  1798. if (!state->i2c_client->irq) {
  1799. del_timer_sync(&state->timer);
  1800. flush_work(&state->work_i2c_poll);
  1801. }
  1802. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  1803. cec_unregister_adapter(state->cec_adap);
  1804. v4l2_async_unregister_subdev(sd);
  1805. v4l2_device_unregister_subdev(sd);
  1806. mutex_destroy(&state->confctl_mutex);
  1807. media_entity_cleanup(&sd->entity);
  1808. v4l2_ctrl_handler_free(&state->hdl);
  1809. return 0;
  1810. }
  1811. static const struct i2c_device_id tc358743_id[] = {
  1812. {"tc358743", 0},
  1813. {}
  1814. };
  1815. MODULE_DEVICE_TABLE(i2c, tc358743_id);
  1816. #if IS_ENABLED(CONFIG_OF)
  1817. static const struct of_device_id tc358743_of_match[] = {
  1818. { .compatible = "toshiba,tc358743" },
  1819. {},
  1820. };
  1821. MODULE_DEVICE_TABLE(of, tc358743_of_match);
  1822. #endif
  1823. static struct i2c_driver tc358743_driver = {
  1824. .driver = {
  1825. .name = "tc358743",
  1826. .of_match_table = of_match_ptr(tc358743_of_match),
  1827. },
  1828. .probe = tc358743_probe,
  1829. .remove = tc358743_remove,
  1830. .id_table = tc358743_id,
  1831. };
  1832. module_i2c_driver(tc358743_driver);