tc358743.c 53 KB

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