tc358743.c 53 KB

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