tc358743.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  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_handler(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. if (handled)
  790. *handled = true;
  791. }
  792. if ((intstatus & MASK_CEC_RINT) &&
  793. (cec_rxint & MASK_CECRIEND)) {
  794. struct cec_msg msg = {};
  795. unsigned int i;
  796. unsigned int v;
  797. v = i2c_rd32(sd, CECRCTR);
  798. msg.len = v & 0x1f;
  799. for (i = 0; i < msg.len; i++) {
  800. v = i2c_rd32(sd, CECRBUF1 + i * 4);
  801. msg.msg[i] = v & 0xff;
  802. }
  803. cec_received_msg(state->cec_adap, &msg);
  804. if (handled)
  805. *handled = true;
  806. }
  807. i2c_wr16(sd, INTSTATUS,
  808. intstatus & (MASK_CEC_RINT | MASK_CEC_TINT));
  809. }
  810. #endif
  811. /* --------------- IRQ --------------- */
  812. static void tc358743_format_change(struct v4l2_subdev *sd)
  813. {
  814. struct tc358743_state *state = to_state(sd);
  815. struct v4l2_dv_timings timings;
  816. const struct v4l2_event tc358743_ev_fmt = {
  817. .type = V4L2_EVENT_SOURCE_CHANGE,
  818. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  819. };
  820. if (tc358743_get_detected_timings(sd, &timings)) {
  821. enable_stream(sd, false);
  822. v4l2_dbg(1, debug, sd, "%s: No signal\n",
  823. __func__);
  824. } else {
  825. if (!v4l2_match_dv_timings(&state->timings, &timings, 0, false))
  826. enable_stream(sd, false);
  827. if (debug)
  828. v4l2_print_dv_timings(sd->name,
  829. "tc358743_format_change: New format: ",
  830. &timings, false);
  831. }
  832. if (sd->devnode)
  833. v4l2_subdev_notify_event(sd, &tc358743_ev_fmt);
  834. }
  835. static void tc358743_init_interrupts(struct v4l2_subdev *sd)
  836. {
  837. u16 i;
  838. /* clear interrupt status registers */
  839. for (i = SYS_INT; i <= KEY_INT; i++)
  840. i2c_wr8(sd, i, 0xff);
  841. i2c_wr16(sd, INTSTATUS, 0xffff);
  842. }
  843. static void tc358743_enable_interrupts(struct v4l2_subdev *sd,
  844. bool cable_connected)
  845. {
  846. v4l2_dbg(2, debug, sd, "%s: cable connected = %d\n", __func__,
  847. cable_connected);
  848. if (cable_connected) {
  849. i2c_wr8(sd, SYS_INTM, ~(MASK_M_DDC | MASK_M_DVI_DET |
  850. MASK_M_HDMI_DET) & 0xff);
  851. i2c_wr8(sd, CLK_INTM, ~MASK_M_IN_DE_CHG);
  852. i2c_wr8(sd, CBIT_INTM, ~(MASK_M_CBIT_FS | MASK_M_AF_LOCK |
  853. MASK_M_AF_UNLOCK) & 0xff);
  854. i2c_wr8(sd, AUDIO_INTM, ~MASK_M_BUFINIT_END);
  855. i2c_wr8(sd, MISC_INTM, ~MASK_M_SYNC_CHG);
  856. } else {
  857. i2c_wr8(sd, SYS_INTM, ~MASK_M_DDC & 0xff);
  858. i2c_wr8(sd, CLK_INTM, 0xff);
  859. i2c_wr8(sd, CBIT_INTM, 0xff);
  860. i2c_wr8(sd, AUDIO_INTM, 0xff);
  861. i2c_wr8(sd, MISC_INTM, 0xff);
  862. }
  863. }
  864. static void tc358743_hdmi_audio_int_handler(struct v4l2_subdev *sd,
  865. bool *handled)
  866. {
  867. u8 audio_int_mask = i2c_rd8(sd, AUDIO_INTM);
  868. u8 audio_int = i2c_rd8(sd, AUDIO_INT) & ~audio_int_mask;
  869. i2c_wr8(sd, AUDIO_INT, audio_int);
  870. v4l2_dbg(3, debug, sd, "%s: AUDIO_INT = 0x%02x\n", __func__, audio_int);
  871. tc358743_s_ctrl_audio_sampling_rate(sd);
  872. tc358743_s_ctrl_audio_present(sd);
  873. }
  874. static void tc358743_csi_err_int_handler(struct v4l2_subdev *sd, bool *handled)
  875. {
  876. v4l2_err(sd, "%s: CSI_ERR = 0x%x\n", __func__, i2c_rd32(sd, CSI_ERR));
  877. i2c_wr32(sd, CSI_INT_CLR, MASK_ICRER);
  878. }
  879. static void tc358743_hdmi_misc_int_handler(struct v4l2_subdev *sd,
  880. bool *handled)
  881. {
  882. u8 misc_int_mask = i2c_rd8(sd, MISC_INTM);
  883. u8 misc_int = i2c_rd8(sd, MISC_INT) & ~misc_int_mask;
  884. i2c_wr8(sd, MISC_INT, misc_int);
  885. v4l2_dbg(3, debug, sd, "%s: MISC_INT = 0x%02x\n", __func__, misc_int);
  886. if (misc_int & MASK_I_SYNC_CHG) {
  887. /* Reset the HDMI PHY to try to trigger proper lock on the
  888. * incoming video format. Erase BKSV to prevent that old keys
  889. * are used when a new source is connected. */
  890. if (no_sync(sd) || no_signal(sd)) {
  891. tc358743_reset_phy(sd);
  892. tc358743_erase_bksv(sd);
  893. }
  894. tc358743_format_change(sd);
  895. misc_int &= ~MASK_I_SYNC_CHG;
  896. if (handled)
  897. *handled = true;
  898. }
  899. if (misc_int) {
  900. v4l2_err(sd, "%s: Unhandled MISC_INT interrupts: 0x%02x\n",
  901. __func__, misc_int);
  902. }
  903. }
  904. static void tc358743_hdmi_cbit_int_handler(struct v4l2_subdev *sd,
  905. bool *handled)
  906. {
  907. u8 cbit_int_mask = i2c_rd8(sd, CBIT_INTM);
  908. u8 cbit_int = i2c_rd8(sd, CBIT_INT) & ~cbit_int_mask;
  909. i2c_wr8(sd, CBIT_INT, cbit_int);
  910. v4l2_dbg(3, debug, sd, "%s: CBIT_INT = 0x%02x\n", __func__, cbit_int);
  911. if (cbit_int & MASK_I_CBIT_FS) {
  912. v4l2_dbg(1, debug, sd, "%s: Audio sample rate changed\n",
  913. __func__);
  914. tc358743_s_ctrl_audio_sampling_rate(sd);
  915. cbit_int &= ~MASK_I_CBIT_FS;
  916. if (handled)
  917. *handled = true;
  918. }
  919. if (cbit_int & (MASK_I_AF_LOCK | MASK_I_AF_UNLOCK)) {
  920. v4l2_dbg(1, debug, sd, "%s: Audio present changed\n",
  921. __func__);
  922. tc358743_s_ctrl_audio_present(sd);
  923. cbit_int &= ~(MASK_I_AF_LOCK | MASK_I_AF_UNLOCK);
  924. if (handled)
  925. *handled = true;
  926. }
  927. if (cbit_int) {
  928. v4l2_err(sd, "%s: Unhandled CBIT_INT interrupts: 0x%02x\n",
  929. __func__, cbit_int);
  930. }
  931. }
  932. static void tc358743_hdmi_clk_int_handler(struct v4l2_subdev *sd, bool *handled)
  933. {
  934. u8 clk_int_mask = i2c_rd8(sd, CLK_INTM);
  935. u8 clk_int = i2c_rd8(sd, CLK_INT) & ~clk_int_mask;
  936. /* Bit 7 and bit 6 are set even when they are masked */
  937. i2c_wr8(sd, CLK_INT, clk_int | 0x80 | MASK_I_OUT_H_CHG);
  938. v4l2_dbg(3, debug, sd, "%s: CLK_INT = 0x%02x\n", __func__, clk_int);
  939. if (clk_int & (MASK_I_IN_DE_CHG)) {
  940. v4l2_dbg(1, debug, sd, "%s: DE size or position has changed\n",
  941. __func__);
  942. /* If the source switch to a new resolution with the same pixel
  943. * frequency as the existing (e.g. 1080p25 -> 720p50), the
  944. * I_SYNC_CHG interrupt is not always triggered, while the
  945. * I_IN_DE_CHG interrupt seems to work fine. Format change
  946. * notifications are only sent when the signal is stable to
  947. * reduce the number of notifications. */
  948. if (!no_signal(sd) && !no_sync(sd))
  949. tc358743_format_change(sd);
  950. clk_int &= ~(MASK_I_IN_DE_CHG);
  951. if (handled)
  952. *handled = true;
  953. }
  954. if (clk_int) {
  955. v4l2_err(sd, "%s: Unhandled CLK_INT interrupts: 0x%02x\n",
  956. __func__, clk_int);
  957. }
  958. }
  959. static void tc358743_hdmi_sys_int_handler(struct v4l2_subdev *sd, bool *handled)
  960. {
  961. struct tc358743_state *state = to_state(sd);
  962. u8 sys_int_mask = i2c_rd8(sd, SYS_INTM);
  963. u8 sys_int = i2c_rd8(sd, SYS_INT) & ~sys_int_mask;
  964. i2c_wr8(sd, SYS_INT, sys_int);
  965. v4l2_dbg(3, debug, sd, "%s: SYS_INT = 0x%02x\n", __func__, sys_int);
  966. if (sys_int & MASK_I_DDC) {
  967. bool tx_5v = tx_5v_power_present(sd);
  968. v4l2_dbg(1, debug, sd, "%s: Tx 5V power present: %s\n",
  969. __func__, tx_5v ? "yes" : "no");
  970. if (tx_5v) {
  971. tc358743_enable_edid(sd);
  972. } else {
  973. tc358743_enable_interrupts(sd, false);
  974. tc358743_disable_edid(sd);
  975. memset(&state->timings, 0, sizeof(state->timings));
  976. tc358743_erase_bksv(sd);
  977. tc358743_update_controls(sd);
  978. }
  979. sys_int &= ~MASK_I_DDC;
  980. if (handled)
  981. *handled = true;
  982. }
  983. if (sys_int & MASK_I_DVI) {
  984. v4l2_dbg(1, debug, sd, "%s: HDMI->DVI change detected\n",
  985. __func__);
  986. /* Reset the HDMI PHY to try to trigger proper lock on the
  987. * incoming video format. Erase BKSV to prevent that old keys
  988. * are used when a new source is connected. */
  989. if (no_sync(sd) || no_signal(sd)) {
  990. tc358743_reset_phy(sd);
  991. tc358743_erase_bksv(sd);
  992. }
  993. sys_int &= ~MASK_I_DVI;
  994. if (handled)
  995. *handled = true;
  996. }
  997. if (sys_int & MASK_I_HDMI) {
  998. v4l2_dbg(1, debug, sd, "%s: DVI->HDMI change detected\n",
  999. __func__);
  1000. /* Register is reset in DVI mode (REF_01, c. 6.6.41) */
  1001. i2c_wr8(sd, ANA_CTL, MASK_APPL_PCSX_NORMAL | MASK_ANALOG_ON);
  1002. sys_int &= ~MASK_I_HDMI;
  1003. if (handled)
  1004. *handled = true;
  1005. }
  1006. if (sys_int) {
  1007. v4l2_err(sd, "%s: Unhandled SYS_INT interrupts: 0x%02x\n",
  1008. __func__, sys_int);
  1009. }
  1010. }
  1011. /* --------------- CORE OPS --------------- */
  1012. static int tc358743_log_status(struct v4l2_subdev *sd)
  1013. {
  1014. struct tc358743_state *state = to_state(sd);
  1015. struct v4l2_dv_timings timings;
  1016. uint8_t hdmi_sys_status = i2c_rd8(sd, SYS_STATUS);
  1017. uint16_t sysctl = i2c_rd16(sd, SYSCTL);
  1018. u8 vi_status3 = i2c_rd8(sd, VI_STATUS3);
  1019. const int deep_color_mode[4] = { 8, 10, 12, 16 };
  1020. static const char * const input_color_space[] = {
  1021. "RGB", "YCbCr 601", "opRGB", "YCbCr 709", "NA (4)",
  1022. "xvYCC 601", "NA(6)", "xvYCC 709", "NA(8)", "sYCC601",
  1023. "NA(10)", "NA(11)", "NA(12)", "opYCC 601"};
  1024. v4l2_info(sd, "-----Chip status-----\n");
  1025. v4l2_info(sd, "Chip ID: 0x%02x\n",
  1026. (i2c_rd16(sd, CHIPID) & MASK_CHIPID) >> 8);
  1027. v4l2_info(sd, "Chip revision: 0x%02x\n",
  1028. i2c_rd16(sd, CHIPID) & MASK_REVID);
  1029. v4l2_info(sd, "Reset: IR: %d, CEC: %d, CSI TX: %d, HDMI: %d\n",
  1030. !!(sysctl & MASK_IRRST),
  1031. !!(sysctl & MASK_CECRST),
  1032. !!(sysctl & MASK_CTXRST),
  1033. !!(sysctl & MASK_HDMIRST));
  1034. v4l2_info(sd, "Sleep mode: %s\n", sysctl & MASK_SLEEP ? "on" : "off");
  1035. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  1036. hdmi_sys_status & MASK_S_DDC5V ? "yes" : "no");
  1037. v4l2_info(sd, "DDC lines enabled: %s\n",
  1038. (i2c_rd8(sd, EDID_MODE) & MASK_EDID_MODE_E_DDC) ?
  1039. "yes" : "no");
  1040. v4l2_info(sd, "Hotplug enabled: %s\n",
  1041. (i2c_rd8(sd, HPD_CTL) & MASK_HPD_OUT0) ?
  1042. "yes" : "no");
  1043. v4l2_info(sd, "CEC enabled: %s\n",
  1044. (i2c_rd16(sd, CECEN) & MASK_CECEN) ? "yes" : "no");
  1045. v4l2_info(sd, "-----Signal status-----\n");
  1046. v4l2_info(sd, "TMDS signal detected: %s\n",
  1047. hdmi_sys_status & MASK_S_TMDS ? "yes" : "no");
  1048. v4l2_info(sd, "Stable sync signal: %s\n",
  1049. hdmi_sys_status & MASK_S_SYNC ? "yes" : "no");
  1050. v4l2_info(sd, "PHY PLL locked: %s\n",
  1051. hdmi_sys_status & MASK_S_PHY_PLL ? "yes" : "no");
  1052. v4l2_info(sd, "PHY DE detected: %s\n",
  1053. hdmi_sys_status & MASK_S_PHY_SCDT ? "yes" : "no");
  1054. if (tc358743_get_detected_timings(sd, &timings)) {
  1055. v4l2_info(sd, "No video detected\n");
  1056. } else {
  1057. v4l2_print_dv_timings(sd->name, "Detected format: ", &timings,
  1058. true);
  1059. }
  1060. v4l2_print_dv_timings(sd->name, "Configured format: ", &state->timings,
  1061. true);
  1062. v4l2_info(sd, "-----CSI-TX status-----\n");
  1063. v4l2_info(sd, "Lanes needed: %d\n",
  1064. tc358743_num_csi_lanes_needed(sd));
  1065. v4l2_info(sd, "Lanes in use: %d\n",
  1066. state->csi_lanes_in_use);
  1067. v4l2_info(sd, "Waiting for particular sync signal: %s\n",
  1068. (i2c_rd16(sd, CSI_STATUS) & MASK_S_WSYNC) ?
  1069. "yes" : "no");
  1070. v4l2_info(sd, "Transmit mode: %s\n",
  1071. (i2c_rd16(sd, CSI_STATUS) & MASK_S_TXACT) ?
  1072. "yes" : "no");
  1073. v4l2_info(sd, "Receive mode: %s\n",
  1074. (i2c_rd16(sd, CSI_STATUS) & MASK_S_RXACT) ?
  1075. "yes" : "no");
  1076. v4l2_info(sd, "Stopped: %s\n",
  1077. (i2c_rd16(sd, CSI_STATUS) & MASK_S_HLT) ?
  1078. "yes" : "no");
  1079. v4l2_info(sd, "Color space: %s\n",
  1080. state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16 ?
  1081. "YCbCr 422 16-bit" :
  1082. state->mbus_fmt_code == MEDIA_BUS_FMT_RGB888_1X24 ?
  1083. "RGB 888 24-bit" : "Unsupported");
  1084. v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
  1085. v4l2_info(sd, "HDCP encrypted content: %s\n",
  1086. hdmi_sys_status & MASK_S_HDCP ? "yes" : "no");
  1087. v4l2_info(sd, "Input color space: %s %s range\n",
  1088. input_color_space[(vi_status3 & MASK_S_V_COLOR) >> 1],
  1089. (vi_status3 & MASK_LIMITED) ? "limited" : "full");
  1090. if (!is_hdmi(sd))
  1091. return 0;
  1092. v4l2_info(sd, "AV Mute: %s\n", hdmi_sys_status & MASK_S_AVMUTE ? "on" :
  1093. "off");
  1094. v4l2_info(sd, "Deep color mode: %d-bits per channel\n",
  1095. deep_color_mode[(i2c_rd8(sd, VI_STATUS1) &
  1096. MASK_S_DEEPCOLOR) >> 2]);
  1097. print_avi_infoframe(sd);
  1098. return 0;
  1099. }
  1100. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1101. static void tc358743_print_register_map(struct v4l2_subdev *sd)
  1102. {
  1103. v4l2_info(sd, "0x0000-0x00FF: Global Control Register\n");
  1104. v4l2_info(sd, "0x0100-0x01FF: CSI2-TX PHY Register\n");
  1105. v4l2_info(sd, "0x0200-0x03FF: CSI2-TX PPI Register\n");
  1106. v4l2_info(sd, "0x0400-0x05FF: Reserved\n");
  1107. v4l2_info(sd, "0x0600-0x06FF: CEC Register\n");
  1108. v4l2_info(sd, "0x0700-0x84FF: Reserved\n");
  1109. v4l2_info(sd, "0x8500-0x85FF: HDMIRX System Control Register\n");
  1110. v4l2_info(sd, "0x8600-0x86FF: HDMIRX Audio Control Register\n");
  1111. v4l2_info(sd, "0x8700-0x87FF: HDMIRX InfoFrame packet data Register\n");
  1112. v4l2_info(sd, "0x8800-0x88FF: HDMIRX HDCP Port Register\n");
  1113. v4l2_info(sd, "0x8900-0x89FF: HDMIRX Video Output Port & 3D Register\n");
  1114. v4l2_info(sd, "0x8A00-0x8BFF: Reserved\n");
  1115. v4l2_info(sd, "0x8C00-0x8FFF: HDMIRX EDID-RAM (1024bytes)\n");
  1116. v4l2_info(sd, "0x9000-0x90FF: HDMIRX GBD Extraction Control\n");
  1117. v4l2_info(sd, "0x9100-0x92FF: HDMIRX GBD RAM read\n");
  1118. v4l2_info(sd, "0x9300- : Reserved\n");
  1119. }
  1120. static int tc358743_get_reg_size(u16 address)
  1121. {
  1122. /* REF_01 p. 66-72 */
  1123. if (address <= 0x00ff)
  1124. return 2;
  1125. else if ((address >= 0x0100) && (address <= 0x06FF))
  1126. return 4;
  1127. else if ((address >= 0x0700) && (address <= 0x84ff))
  1128. return 2;
  1129. else
  1130. return 1;
  1131. }
  1132. static int tc358743_g_register(struct v4l2_subdev *sd,
  1133. struct v4l2_dbg_register *reg)
  1134. {
  1135. if (reg->reg > 0xffff) {
  1136. tc358743_print_register_map(sd);
  1137. return -EINVAL;
  1138. }
  1139. reg->size = tc358743_get_reg_size(reg->reg);
  1140. reg->val = i2c_rdreg(sd, reg->reg, reg->size);
  1141. return 0;
  1142. }
  1143. static int tc358743_s_register(struct v4l2_subdev *sd,
  1144. const struct v4l2_dbg_register *reg)
  1145. {
  1146. if (reg->reg > 0xffff) {
  1147. tc358743_print_register_map(sd);
  1148. return -EINVAL;
  1149. }
  1150. /* It should not be possible for the user to enable HDCP with a simple
  1151. * v4l2-dbg command.
  1152. *
  1153. * DO NOT REMOVE THIS unless all other issues with HDCP have been
  1154. * resolved.
  1155. */
  1156. if (reg->reg == HDCP_MODE ||
  1157. reg->reg == HDCP_REG1 ||
  1158. reg->reg == HDCP_REG2 ||
  1159. reg->reg == HDCP_REG3 ||
  1160. reg->reg == BCAPS)
  1161. return 0;
  1162. i2c_wrreg(sd, (u16)reg->reg, reg->val,
  1163. tc358743_get_reg_size(reg->reg));
  1164. return 0;
  1165. }
  1166. #endif
  1167. static int tc358743_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1168. {
  1169. u16 intstatus = i2c_rd16(sd, INTSTATUS);
  1170. v4l2_dbg(1, debug, sd, "%s: IntStatus = 0x%04x\n", __func__, intstatus);
  1171. if (intstatus & MASK_HDMI_INT) {
  1172. u8 hdmi_int0 = i2c_rd8(sd, HDMI_INT0);
  1173. u8 hdmi_int1 = i2c_rd8(sd, HDMI_INT1);
  1174. if (hdmi_int0 & MASK_I_MISC)
  1175. tc358743_hdmi_misc_int_handler(sd, handled);
  1176. if (hdmi_int1 & MASK_I_CBIT)
  1177. tc358743_hdmi_cbit_int_handler(sd, handled);
  1178. if (hdmi_int1 & MASK_I_CLK)
  1179. tc358743_hdmi_clk_int_handler(sd, handled);
  1180. if (hdmi_int1 & MASK_I_SYS)
  1181. tc358743_hdmi_sys_int_handler(sd, handled);
  1182. if (hdmi_int1 & MASK_I_AUD)
  1183. tc358743_hdmi_audio_int_handler(sd, handled);
  1184. i2c_wr16(sd, INTSTATUS, MASK_HDMI_INT);
  1185. intstatus &= ~MASK_HDMI_INT;
  1186. }
  1187. #ifdef CONFIG_VIDEO_TC358743_CEC
  1188. if (intstatus & (MASK_CEC_RINT | MASK_CEC_TINT)) {
  1189. tc358743_cec_handler(sd, intstatus, handled);
  1190. i2c_wr16(sd, INTSTATUS,
  1191. intstatus & (MASK_CEC_RINT | MASK_CEC_TINT));
  1192. intstatus &= ~(MASK_CEC_RINT | MASK_CEC_TINT);
  1193. }
  1194. #endif
  1195. if (intstatus & MASK_CSI_INT) {
  1196. u32 csi_int = i2c_rd32(sd, CSI_INT);
  1197. if (csi_int & MASK_INTER)
  1198. tc358743_csi_err_int_handler(sd, handled);
  1199. i2c_wr16(sd, INTSTATUS, MASK_CSI_INT);
  1200. }
  1201. intstatus = i2c_rd16(sd, INTSTATUS);
  1202. if (intstatus) {
  1203. v4l2_dbg(1, debug, sd,
  1204. "%s: Unhandled IntStatus interrupts: 0x%02x\n",
  1205. __func__, intstatus);
  1206. }
  1207. return 0;
  1208. }
  1209. static irqreturn_t tc358743_irq_handler(int irq, void *dev_id)
  1210. {
  1211. struct tc358743_state *state = dev_id;
  1212. bool handled = false;
  1213. tc358743_isr(&state->sd, 0, &handled);
  1214. return handled ? IRQ_HANDLED : IRQ_NONE;
  1215. }
  1216. static void tc358743_irq_poll_timer(struct timer_list *t)
  1217. {
  1218. struct tc358743_state *state = from_timer(state, t, timer);
  1219. unsigned int msecs;
  1220. schedule_work(&state->work_i2c_poll);
  1221. /*
  1222. * If CEC is present, then we need to poll more frequently,
  1223. * otherwise we will miss CEC messages.
  1224. */
  1225. msecs = state->cec_adap ? POLL_INTERVAL_CEC_MS : POLL_INTERVAL_MS;
  1226. mod_timer(&state->timer, jiffies + msecs_to_jiffies(msecs));
  1227. }
  1228. static void tc358743_work_i2c_poll(struct work_struct *work)
  1229. {
  1230. struct tc358743_state *state = container_of(work,
  1231. struct tc358743_state, work_i2c_poll);
  1232. bool handled;
  1233. tc358743_isr(&state->sd, 0, &handled);
  1234. }
  1235. static int tc358743_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  1236. struct v4l2_event_subscription *sub)
  1237. {
  1238. switch (sub->type) {
  1239. case V4L2_EVENT_SOURCE_CHANGE:
  1240. return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
  1241. case V4L2_EVENT_CTRL:
  1242. return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
  1243. default:
  1244. return -EINVAL;
  1245. }
  1246. }
  1247. /* --------------- VIDEO OPS --------------- */
  1248. static int tc358743_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1249. {
  1250. *status = 0;
  1251. *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
  1252. *status |= no_sync(sd) ? V4L2_IN_ST_NO_SYNC : 0;
  1253. v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
  1254. return 0;
  1255. }
  1256. static int tc358743_s_dv_timings(struct v4l2_subdev *sd,
  1257. struct v4l2_dv_timings *timings)
  1258. {
  1259. struct tc358743_state *state = to_state(sd);
  1260. if (!timings)
  1261. return -EINVAL;
  1262. if (debug)
  1263. v4l2_print_dv_timings(sd->name, "tc358743_s_dv_timings: ",
  1264. timings, false);
  1265. if (v4l2_match_dv_timings(&state->timings, timings, 0, false)) {
  1266. v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
  1267. return 0;
  1268. }
  1269. if (!v4l2_valid_dv_timings(timings,
  1270. &tc358743_timings_cap, NULL, NULL)) {
  1271. v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
  1272. return -ERANGE;
  1273. }
  1274. state->timings = *timings;
  1275. enable_stream(sd, false);
  1276. tc358743_set_pll(sd);
  1277. tc358743_set_csi(sd);
  1278. return 0;
  1279. }
  1280. static int tc358743_g_dv_timings(struct v4l2_subdev *sd,
  1281. struct v4l2_dv_timings *timings)
  1282. {
  1283. struct tc358743_state *state = to_state(sd);
  1284. *timings = state->timings;
  1285. return 0;
  1286. }
  1287. static int tc358743_enum_dv_timings(struct v4l2_subdev *sd,
  1288. struct v4l2_enum_dv_timings *timings)
  1289. {
  1290. if (timings->pad != 0)
  1291. return -EINVAL;
  1292. return v4l2_enum_dv_timings_cap(timings,
  1293. &tc358743_timings_cap, NULL, NULL);
  1294. }
  1295. static int tc358743_query_dv_timings(struct v4l2_subdev *sd,
  1296. struct v4l2_dv_timings *timings)
  1297. {
  1298. int ret;
  1299. ret = tc358743_get_detected_timings(sd, timings);
  1300. if (ret)
  1301. return ret;
  1302. if (debug)
  1303. v4l2_print_dv_timings(sd->name, "tc358743_query_dv_timings: ",
  1304. timings, false);
  1305. if (!v4l2_valid_dv_timings(timings,
  1306. &tc358743_timings_cap, NULL, NULL)) {
  1307. v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
  1308. return -ERANGE;
  1309. }
  1310. return 0;
  1311. }
  1312. static int tc358743_dv_timings_cap(struct v4l2_subdev *sd,
  1313. struct v4l2_dv_timings_cap *cap)
  1314. {
  1315. if (cap->pad != 0)
  1316. return -EINVAL;
  1317. *cap = tc358743_timings_cap;
  1318. return 0;
  1319. }
  1320. static int tc358743_g_mbus_config(struct v4l2_subdev *sd,
  1321. struct v4l2_mbus_config *cfg)
  1322. {
  1323. struct tc358743_state *state = to_state(sd);
  1324. cfg->type = V4L2_MBUS_CSI2;
  1325. /* Support for non-continuous CSI-2 clock is missing in the driver */
  1326. cfg->flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  1327. switch (state->csi_lanes_in_use) {
  1328. case 1:
  1329. cfg->flags |= V4L2_MBUS_CSI2_1_LANE;
  1330. break;
  1331. case 2:
  1332. cfg->flags |= V4L2_MBUS_CSI2_2_LANE;
  1333. break;
  1334. case 3:
  1335. cfg->flags |= V4L2_MBUS_CSI2_3_LANE;
  1336. break;
  1337. case 4:
  1338. cfg->flags |= V4L2_MBUS_CSI2_4_LANE;
  1339. break;
  1340. default:
  1341. return -EINVAL;
  1342. }
  1343. return 0;
  1344. }
  1345. static int tc358743_s_stream(struct v4l2_subdev *sd, int enable)
  1346. {
  1347. enable_stream(sd, enable);
  1348. if (!enable) {
  1349. /* Put all lanes in LP-11 state (STOPSTATE) */
  1350. tc358743_set_csi(sd);
  1351. }
  1352. return 0;
  1353. }
  1354. /* --------------- PAD OPS --------------- */
  1355. static int tc358743_enum_mbus_code(struct v4l2_subdev *sd,
  1356. struct v4l2_subdev_pad_config *cfg,
  1357. struct v4l2_subdev_mbus_code_enum *code)
  1358. {
  1359. switch (code->index) {
  1360. case 0:
  1361. code->code = MEDIA_BUS_FMT_RGB888_1X24;
  1362. break;
  1363. case 1:
  1364. code->code = MEDIA_BUS_FMT_UYVY8_1X16;
  1365. break;
  1366. default:
  1367. return -EINVAL;
  1368. }
  1369. return 0;
  1370. }
  1371. static int tc358743_get_fmt(struct v4l2_subdev *sd,
  1372. struct v4l2_subdev_pad_config *cfg,
  1373. struct v4l2_subdev_format *format)
  1374. {
  1375. struct tc358743_state *state = to_state(sd);
  1376. u8 vi_rep = i2c_rd8(sd, VI_REP);
  1377. if (format->pad != 0)
  1378. return -EINVAL;
  1379. format->format.code = state->mbus_fmt_code;
  1380. format->format.width = state->timings.bt.width;
  1381. format->format.height = state->timings.bt.height;
  1382. format->format.field = V4L2_FIELD_NONE;
  1383. switch (vi_rep & MASK_VOUT_COLOR_SEL) {
  1384. case MASK_VOUT_COLOR_RGB_FULL:
  1385. case MASK_VOUT_COLOR_RGB_LIMITED:
  1386. format->format.colorspace = V4L2_COLORSPACE_SRGB;
  1387. break;
  1388. case MASK_VOUT_COLOR_601_YCBCR_LIMITED:
  1389. case MASK_VOUT_COLOR_601_YCBCR_FULL:
  1390. format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1391. break;
  1392. case MASK_VOUT_COLOR_709_YCBCR_FULL:
  1393. case MASK_VOUT_COLOR_709_YCBCR_LIMITED:
  1394. format->format.colorspace = V4L2_COLORSPACE_REC709;
  1395. break;
  1396. default:
  1397. format->format.colorspace = 0;
  1398. break;
  1399. }
  1400. return 0;
  1401. }
  1402. static int tc358743_set_fmt(struct v4l2_subdev *sd,
  1403. struct v4l2_subdev_pad_config *cfg,
  1404. struct v4l2_subdev_format *format)
  1405. {
  1406. struct tc358743_state *state = to_state(sd);
  1407. u32 code = format->format.code; /* is overwritten by get_fmt */
  1408. int ret = tc358743_get_fmt(sd, cfg, format);
  1409. format->format.code = code;
  1410. if (ret)
  1411. return ret;
  1412. switch (code) {
  1413. case MEDIA_BUS_FMT_RGB888_1X24:
  1414. case MEDIA_BUS_FMT_UYVY8_1X16:
  1415. break;
  1416. default:
  1417. return -EINVAL;
  1418. }
  1419. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  1420. return 0;
  1421. state->mbus_fmt_code = format->format.code;
  1422. enable_stream(sd, false);
  1423. tc358743_set_pll(sd);
  1424. tc358743_set_csi(sd);
  1425. tc358743_set_csi_color_space(sd);
  1426. return 0;
  1427. }
  1428. static int tc358743_g_edid(struct v4l2_subdev *sd,
  1429. struct v4l2_subdev_edid *edid)
  1430. {
  1431. struct tc358743_state *state = to_state(sd);
  1432. memset(edid->reserved, 0, sizeof(edid->reserved));
  1433. if (edid->pad != 0)
  1434. return -EINVAL;
  1435. if (edid->start_block == 0 && edid->blocks == 0) {
  1436. edid->blocks = state->edid_blocks_written;
  1437. return 0;
  1438. }
  1439. if (state->edid_blocks_written == 0)
  1440. return -ENODATA;
  1441. if (edid->start_block >= state->edid_blocks_written ||
  1442. edid->blocks == 0)
  1443. return -EINVAL;
  1444. if (edid->start_block + edid->blocks > state->edid_blocks_written)
  1445. edid->blocks = state->edid_blocks_written - edid->start_block;
  1446. i2c_rd(sd, EDID_RAM + (edid->start_block * EDID_BLOCK_SIZE), edid->edid,
  1447. edid->blocks * EDID_BLOCK_SIZE);
  1448. return 0;
  1449. }
  1450. static int tc358743_s_edid(struct v4l2_subdev *sd,
  1451. struct v4l2_subdev_edid *edid)
  1452. {
  1453. struct tc358743_state *state = to_state(sd);
  1454. u16 edid_len = edid->blocks * EDID_BLOCK_SIZE;
  1455. u16 pa;
  1456. int err;
  1457. int i;
  1458. v4l2_dbg(2, debug, sd, "%s, pad %d, start block %d, blocks %d\n",
  1459. __func__, edid->pad, edid->start_block, edid->blocks);
  1460. memset(edid->reserved, 0, sizeof(edid->reserved));
  1461. if (edid->pad != 0)
  1462. return -EINVAL;
  1463. if (edid->start_block != 0)
  1464. return -EINVAL;
  1465. if (edid->blocks > EDID_NUM_BLOCKS_MAX) {
  1466. edid->blocks = EDID_NUM_BLOCKS_MAX;
  1467. return -E2BIG;
  1468. }
  1469. pa = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
  1470. err = v4l2_phys_addr_validate(pa, &pa, NULL);
  1471. if (err)
  1472. return err;
  1473. cec_phys_addr_invalidate(state->cec_adap);
  1474. tc358743_disable_edid(sd);
  1475. i2c_wr8(sd, EDID_LEN1, edid_len & 0xff);
  1476. i2c_wr8(sd, EDID_LEN2, edid_len >> 8);
  1477. if (edid->blocks == 0) {
  1478. state->edid_blocks_written = 0;
  1479. return 0;
  1480. }
  1481. for (i = 0; i < edid_len; i += EDID_BLOCK_SIZE)
  1482. i2c_wr(sd, EDID_RAM + i, edid->edid + i, EDID_BLOCK_SIZE);
  1483. state->edid_blocks_written = edid->blocks;
  1484. cec_s_phys_addr(state->cec_adap, pa, false);
  1485. if (tx_5v_power_present(sd))
  1486. tc358743_enable_edid(sd);
  1487. return 0;
  1488. }
  1489. /* -------------------------------------------------------------------------- */
  1490. static const struct v4l2_subdev_core_ops tc358743_core_ops = {
  1491. .log_status = tc358743_log_status,
  1492. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1493. .g_register = tc358743_g_register,
  1494. .s_register = tc358743_s_register,
  1495. #endif
  1496. .interrupt_service_routine = tc358743_isr,
  1497. .subscribe_event = tc358743_subscribe_event,
  1498. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1499. };
  1500. static const struct v4l2_subdev_video_ops tc358743_video_ops = {
  1501. .g_input_status = tc358743_g_input_status,
  1502. .s_dv_timings = tc358743_s_dv_timings,
  1503. .g_dv_timings = tc358743_g_dv_timings,
  1504. .query_dv_timings = tc358743_query_dv_timings,
  1505. .g_mbus_config = tc358743_g_mbus_config,
  1506. .s_stream = tc358743_s_stream,
  1507. };
  1508. static const struct v4l2_subdev_pad_ops tc358743_pad_ops = {
  1509. .enum_mbus_code = tc358743_enum_mbus_code,
  1510. .set_fmt = tc358743_set_fmt,
  1511. .get_fmt = tc358743_get_fmt,
  1512. .get_edid = tc358743_g_edid,
  1513. .set_edid = tc358743_s_edid,
  1514. .enum_dv_timings = tc358743_enum_dv_timings,
  1515. .dv_timings_cap = tc358743_dv_timings_cap,
  1516. };
  1517. static const struct v4l2_subdev_ops tc358743_ops = {
  1518. .core = &tc358743_core_ops,
  1519. .video = &tc358743_video_ops,
  1520. .pad = &tc358743_pad_ops,
  1521. };
  1522. /* --------------- CUSTOM CTRLS --------------- */
  1523. static const struct v4l2_ctrl_config tc358743_ctrl_audio_sampling_rate = {
  1524. .id = TC358743_CID_AUDIO_SAMPLING_RATE,
  1525. .name = "Audio sampling rate",
  1526. .type = V4L2_CTRL_TYPE_INTEGER,
  1527. .min = 0,
  1528. .max = 768000,
  1529. .step = 1,
  1530. .def = 0,
  1531. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  1532. };
  1533. static const struct v4l2_ctrl_config tc358743_ctrl_audio_present = {
  1534. .id = TC358743_CID_AUDIO_PRESENT,
  1535. .name = "Audio present",
  1536. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1537. .min = 0,
  1538. .max = 1,
  1539. .step = 1,
  1540. .def = 0,
  1541. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  1542. };
  1543. /* --------------- PROBE / REMOVE --------------- */
  1544. #ifdef CONFIG_OF
  1545. static void tc358743_gpio_reset(struct tc358743_state *state)
  1546. {
  1547. usleep_range(5000, 10000);
  1548. gpiod_set_value(state->reset_gpio, 1);
  1549. usleep_range(1000, 2000);
  1550. gpiod_set_value(state->reset_gpio, 0);
  1551. msleep(20);
  1552. }
  1553. static int tc358743_probe_of(struct tc358743_state *state)
  1554. {
  1555. struct device *dev = &state->i2c_client->dev;
  1556. struct v4l2_fwnode_endpoint *endpoint;
  1557. struct device_node *ep;
  1558. struct clk *refclk;
  1559. u32 bps_pr_lane;
  1560. int ret = -EINVAL;
  1561. refclk = devm_clk_get(dev, "refclk");
  1562. if (IS_ERR(refclk)) {
  1563. if (PTR_ERR(refclk) != -EPROBE_DEFER)
  1564. dev_err(dev, "failed to get refclk: %ld\n",
  1565. PTR_ERR(refclk));
  1566. return PTR_ERR(refclk);
  1567. }
  1568. ep = of_graph_get_next_endpoint(dev->of_node, NULL);
  1569. if (!ep) {
  1570. dev_err(dev, "missing endpoint node\n");
  1571. return -EINVAL;
  1572. }
  1573. endpoint = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep));
  1574. if (IS_ERR(endpoint)) {
  1575. dev_err(dev, "failed to parse endpoint\n");
  1576. ret = PTR_ERR(endpoint);
  1577. goto put_node;
  1578. }
  1579. if (endpoint->bus_type != V4L2_MBUS_CSI2 ||
  1580. endpoint->bus.mipi_csi2.num_data_lanes == 0 ||
  1581. endpoint->nr_of_link_frequencies == 0) {
  1582. dev_err(dev, "missing CSI-2 properties in endpoint\n");
  1583. goto free_endpoint;
  1584. }
  1585. if (endpoint->bus.mipi_csi2.num_data_lanes > 4) {
  1586. dev_err(dev, "invalid number of lanes\n");
  1587. goto free_endpoint;
  1588. }
  1589. state->bus = endpoint->bus.mipi_csi2;
  1590. ret = clk_prepare_enable(refclk);
  1591. if (ret) {
  1592. dev_err(dev, "Failed! to enable clock\n");
  1593. goto free_endpoint;
  1594. }
  1595. state->pdata.refclk_hz = clk_get_rate(refclk);
  1596. state->pdata.ddc5v_delay = DDC5V_DELAY_100_MS;
  1597. state->pdata.enable_hdcp = false;
  1598. /* A FIFO level of 16 should be enough for 2-lane 720p60 at 594 MHz. */
  1599. state->pdata.fifo_level = 16;
  1600. /*
  1601. * The PLL input clock is obtained by dividing refclk by pll_prd.
  1602. * It must be between 6 MHz and 40 MHz, lower frequency is better.
  1603. */
  1604. switch (state->pdata.refclk_hz) {
  1605. case 26000000:
  1606. case 27000000:
  1607. case 42000000:
  1608. state->pdata.pll_prd = state->pdata.refclk_hz / 6000000;
  1609. break;
  1610. default:
  1611. dev_err(dev, "unsupported refclk rate: %u Hz\n",
  1612. state->pdata.refclk_hz);
  1613. goto disable_clk;
  1614. }
  1615. /*
  1616. * The CSI bps per lane must be between 62.5 Mbps and 1 Gbps.
  1617. * The default is 594 Mbps for 4-lane 1080p60 or 2-lane 720p60.
  1618. */
  1619. bps_pr_lane = 2 * endpoint->link_frequencies[0];
  1620. if (bps_pr_lane < 62500000U || bps_pr_lane > 1000000000U) {
  1621. dev_err(dev, "unsupported bps per lane: %u bps\n", bps_pr_lane);
  1622. goto disable_clk;
  1623. }
  1624. /* The CSI speed per lane is refclk / pll_prd * pll_fbd */
  1625. state->pdata.pll_fbd = bps_pr_lane /
  1626. state->pdata.refclk_hz * state->pdata.pll_prd;
  1627. /*
  1628. * FIXME: These timings are from REF_02 for 594 Mbps per lane (297 MHz
  1629. * link frequency). In principle it should be possible to calculate
  1630. * them based on link frequency and resolution.
  1631. */
  1632. if (bps_pr_lane != 594000000U)
  1633. dev_warn(dev, "untested bps per lane: %u bps\n", bps_pr_lane);
  1634. state->pdata.lineinitcnt = 0xe80;
  1635. state->pdata.lptxtimecnt = 0x003;
  1636. /* tclk-preparecnt: 3, tclk-zerocnt: 20 */
  1637. state->pdata.tclk_headercnt = 0x1403;
  1638. state->pdata.tclk_trailcnt = 0x00;
  1639. /* ths-preparecnt: 3, ths-zerocnt: 1 */
  1640. state->pdata.ths_headercnt = 0x0103;
  1641. state->pdata.twakeup = 0x4882;
  1642. state->pdata.tclk_postcnt = 0x008;
  1643. state->pdata.ths_trailcnt = 0x2;
  1644. state->pdata.hstxvregcnt = 0;
  1645. state->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1646. GPIOD_OUT_LOW);
  1647. if (IS_ERR(state->reset_gpio)) {
  1648. dev_err(dev, "failed to get reset gpio\n");
  1649. ret = PTR_ERR(state->reset_gpio);
  1650. goto disable_clk;
  1651. }
  1652. if (state->reset_gpio)
  1653. tc358743_gpio_reset(state);
  1654. ret = 0;
  1655. goto free_endpoint;
  1656. disable_clk:
  1657. clk_disable_unprepare(refclk);
  1658. free_endpoint:
  1659. v4l2_fwnode_endpoint_free(endpoint);
  1660. put_node:
  1661. of_node_put(ep);
  1662. return ret;
  1663. }
  1664. #else
  1665. static inline int tc358743_probe_of(struct tc358743_state *state)
  1666. {
  1667. return -ENODEV;
  1668. }
  1669. #endif
  1670. static int tc358743_probe(struct i2c_client *client,
  1671. const struct i2c_device_id *id)
  1672. {
  1673. static struct v4l2_dv_timings default_timing =
  1674. V4L2_DV_BT_CEA_640X480P59_94;
  1675. struct tc358743_state *state;
  1676. struct tc358743_platform_data *pdata = client->dev.platform_data;
  1677. struct v4l2_subdev *sd;
  1678. u16 irq_mask = MASK_HDMI_MSK | MASK_CSI_MSK;
  1679. int err;
  1680. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1681. return -EIO;
  1682. v4l_dbg(1, debug, client, "chip found @ 0x%x (%s)\n",
  1683. client->addr << 1, client->adapter->name);
  1684. state = devm_kzalloc(&client->dev, sizeof(struct tc358743_state),
  1685. GFP_KERNEL);
  1686. if (!state)
  1687. return -ENOMEM;
  1688. state->i2c_client = client;
  1689. /* platform data */
  1690. if (pdata) {
  1691. state->pdata = *pdata;
  1692. state->bus.flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  1693. } else {
  1694. err = tc358743_probe_of(state);
  1695. if (err == -ENODEV)
  1696. v4l_err(client, "No platform data!\n");
  1697. if (err)
  1698. return err;
  1699. }
  1700. sd = &state->sd;
  1701. v4l2_i2c_subdev_init(sd, client, &tc358743_ops);
  1702. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1703. /* i2c access */
  1704. if ((i2c_rd16(sd, CHIPID) & MASK_CHIPID) != 0) {
  1705. v4l2_info(sd, "not a TC358743 on address 0x%x\n",
  1706. client->addr << 1);
  1707. return -ENODEV;
  1708. }
  1709. /* control handlers */
  1710. v4l2_ctrl_handler_init(&state->hdl, 3);
  1711. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(&state->hdl, NULL,
  1712. V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
  1713. /* custom controls */
  1714. state->audio_sampling_rate_ctrl = v4l2_ctrl_new_custom(&state->hdl,
  1715. &tc358743_ctrl_audio_sampling_rate, NULL);
  1716. state->audio_present_ctrl = v4l2_ctrl_new_custom(&state->hdl,
  1717. &tc358743_ctrl_audio_present, NULL);
  1718. sd->ctrl_handler = &state->hdl;
  1719. if (state->hdl.error) {
  1720. err = state->hdl.error;
  1721. goto err_hdl;
  1722. }
  1723. if (tc358743_update_controls(sd)) {
  1724. err = -ENODEV;
  1725. goto err_hdl;
  1726. }
  1727. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  1728. sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  1729. err = media_entity_pads_init(&sd->entity, 1, &state->pad);
  1730. if (err < 0)
  1731. goto err_hdl;
  1732. state->mbus_fmt_code = MEDIA_BUS_FMT_RGB888_1X24;
  1733. sd->dev = &client->dev;
  1734. err = v4l2_async_register_subdev(sd);
  1735. if (err < 0)
  1736. goto err_hdl;
  1737. mutex_init(&state->confctl_mutex);
  1738. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  1739. tc358743_delayed_work_enable_hotplug);
  1740. #ifdef CONFIG_VIDEO_TC358743_CEC
  1741. state->cec_adap = cec_allocate_adapter(&tc358743_cec_adap_ops,
  1742. state, dev_name(&client->dev),
  1743. CEC_CAP_DEFAULTS | CEC_CAP_MONITOR_ALL, CEC_MAX_LOG_ADDRS);
  1744. if (IS_ERR(state->cec_adap)) {
  1745. err = PTR_ERR(state->cec_adap);
  1746. goto err_hdl;
  1747. }
  1748. irq_mask |= MASK_CEC_RMSK | MASK_CEC_TMSK;
  1749. #endif
  1750. tc358743_initial_setup(sd);
  1751. tc358743_s_dv_timings(sd, &default_timing);
  1752. tc358743_set_csi_color_space(sd);
  1753. tc358743_init_interrupts(sd);
  1754. if (state->i2c_client->irq) {
  1755. err = devm_request_threaded_irq(&client->dev,
  1756. state->i2c_client->irq,
  1757. NULL, tc358743_irq_handler,
  1758. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1759. "tc358743", state);
  1760. if (err)
  1761. goto err_work_queues;
  1762. } else {
  1763. INIT_WORK(&state->work_i2c_poll,
  1764. tc358743_work_i2c_poll);
  1765. timer_setup(&state->timer, tc358743_irq_poll_timer, 0);
  1766. state->timer.expires = jiffies +
  1767. msecs_to_jiffies(POLL_INTERVAL_MS);
  1768. add_timer(&state->timer);
  1769. }
  1770. err = cec_register_adapter(state->cec_adap, &client->dev);
  1771. if (err < 0) {
  1772. pr_err("%s: failed to register the cec device\n", __func__);
  1773. cec_delete_adapter(state->cec_adap);
  1774. state->cec_adap = NULL;
  1775. goto err_work_queues;
  1776. }
  1777. tc358743_enable_interrupts(sd, tx_5v_power_present(sd));
  1778. i2c_wr16(sd, INTMASK, ~irq_mask);
  1779. err = v4l2_ctrl_handler_setup(sd->ctrl_handler);
  1780. if (err)
  1781. goto err_work_queues;
  1782. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  1783. client->addr << 1, client->adapter->name);
  1784. return 0;
  1785. err_work_queues:
  1786. cec_unregister_adapter(state->cec_adap);
  1787. if (!state->i2c_client->irq)
  1788. flush_work(&state->work_i2c_poll);
  1789. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  1790. mutex_destroy(&state->confctl_mutex);
  1791. err_hdl:
  1792. media_entity_cleanup(&sd->entity);
  1793. v4l2_ctrl_handler_free(&state->hdl);
  1794. return err;
  1795. }
  1796. static int tc358743_remove(struct i2c_client *client)
  1797. {
  1798. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1799. struct tc358743_state *state = to_state(sd);
  1800. if (!state->i2c_client->irq) {
  1801. del_timer_sync(&state->timer);
  1802. flush_work(&state->work_i2c_poll);
  1803. }
  1804. cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
  1805. cec_unregister_adapter(state->cec_adap);
  1806. v4l2_async_unregister_subdev(sd);
  1807. v4l2_device_unregister_subdev(sd);
  1808. mutex_destroy(&state->confctl_mutex);
  1809. media_entity_cleanup(&sd->entity);
  1810. v4l2_ctrl_handler_free(&state->hdl);
  1811. return 0;
  1812. }
  1813. static const struct i2c_device_id tc358743_id[] = {
  1814. {"tc358743", 0},
  1815. {}
  1816. };
  1817. MODULE_DEVICE_TABLE(i2c, tc358743_id);
  1818. #if IS_ENABLED(CONFIG_OF)
  1819. static const struct of_device_id tc358743_of_match[] = {
  1820. { .compatible = "toshiba,tc358743" },
  1821. {},
  1822. };
  1823. MODULE_DEVICE_TABLE(of, tc358743_of_match);
  1824. #endif
  1825. static struct i2c_driver tc358743_driver = {
  1826. .driver = {
  1827. .name = "tc358743",
  1828. .of_match_table = of_match_ptr(tc358743_of_match),
  1829. },
  1830. .probe = tc358743_probe,
  1831. .remove = tc358743_remove,
  1832. .id_table = tc358743_id,
  1833. };
  1834. module_i2c_driver(tc358743_driver);