tc358743.c 53 KB

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