tc358743.c 53 KB

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