tc358743.c 53 KB

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