ad9389b.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /*
  2. * Analog Devices AD9389B/AD9889B video encoder driver
  3. *
  4. * Copyright 2012 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. /*
  20. * References (c = chapter, p = page):
  21. * REF_01 - Analog Devices, Programming Guide, AD9889B/AD9389B,
  22. * HDMI Transitter, Rev. A, October 2010
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/i2c.h>
  28. #include <linux/delay.h>
  29. #include <linux/videodev2.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/v4l2-dv-timings.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-dv-timings.h>
  35. #include <media/v4l2-ctrls.h>
  36. #include <media/ad9389b.h>
  37. static int debug;
  38. module_param(debug, int, 0644);
  39. MODULE_PARM_DESC(debug, "debug level (0-2)");
  40. MODULE_DESCRIPTION("Analog Devices AD9389B/AD9889B video encoder driver");
  41. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  42. MODULE_AUTHOR("Martin Bugge <marbugge@cisco.com>");
  43. MODULE_LICENSE("GPL");
  44. #define MASK_AD9389B_EDID_RDY_INT 0x04
  45. #define MASK_AD9389B_MSEN_INT 0x40
  46. #define MASK_AD9389B_HPD_INT 0x80
  47. #define MASK_AD9389B_HPD_DETECT 0x40
  48. #define MASK_AD9389B_MSEN_DETECT 0x20
  49. #define MASK_AD9389B_EDID_RDY 0x10
  50. #define EDID_MAX_RETRIES (8)
  51. #define EDID_DELAY 250
  52. #define EDID_MAX_SEGM 8
  53. /*
  54. **********************************************************************
  55. *
  56. * Arrays with configuration parameters for the AD9389B
  57. *
  58. **********************************************************************
  59. */
  60. struct ad9389b_state_edid {
  61. /* total number of blocks */
  62. u32 blocks;
  63. /* Number of segments read */
  64. u32 segments;
  65. u8 data[EDID_MAX_SEGM * 256];
  66. /* Number of EDID read retries left */
  67. unsigned read_retries;
  68. };
  69. struct ad9389b_state {
  70. struct ad9389b_platform_data pdata;
  71. struct v4l2_subdev sd;
  72. struct media_pad pad;
  73. struct v4l2_ctrl_handler hdl;
  74. int chip_revision;
  75. /* Is the ad9389b powered on? */
  76. bool power_on;
  77. /* Did we receive hotplug and rx-sense signals? */
  78. bool have_monitor;
  79. /* timings from s_dv_timings */
  80. struct v4l2_dv_timings dv_timings;
  81. /* controls */
  82. struct v4l2_ctrl *hdmi_mode_ctrl;
  83. struct v4l2_ctrl *hotplug_ctrl;
  84. struct v4l2_ctrl *rx_sense_ctrl;
  85. struct v4l2_ctrl *have_edid0_ctrl;
  86. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  87. struct i2c_client *edid_i2c_client;
  88. struct ad9389b_state_edid edid;
  89. /* Running counter of the number of detected EDIDs (for debugging) */
  90. unsigned edid_detect_counter;
  91. struct workqueue_struct *work_queue;
  92. struct delayed_work edid_handler; /* work entry */
  93. };
  94. static void ad9389b_check_monitor_present_status(struct v4l2_subdev *sd);
  95. static bool ad9389b_check_edid_status(struct v4l2_subdev *sd);
  96. static void ad9389b_setup(struct v4l2_subdev *sd);
  97. static int ad9389b_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq);
  98. static int ad9389b_s_clock_freq(struct v4l2_subdev *sd, u32 freq);
  99. static inline struct ad9389b_state *get_ad9389b_state(struct v4l2_subdev *sd)
  100. {
  101. return container_of(sd, struct ad9389b_state, sd);
  102. }
  103. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  104. {
  105. return &container_of(ctrl->handler, struct ad9389b_state, hdl)->sd;
  106. }
  107. /* ------------------------ I2C ----------------------------------------------- */
  108. static int ad9389b_rd(struct v4l2_subdev *sd, u8 reg)
  109. {
  110. struct i2c_client *client = v4l2_get_subdevdata(sd);
  111. return i2c_smbus_read_byte_data(client, reg);
  112. }
  113. static int ad9389b_wr(struct v4l2_subdev *sd, u8 reg, u8 val)
  114. {
  115. struct i2c_client *client = v4l2_get_subdevdata(sd);
  116. int ret;
  117. int i;
  118. for (i = 0; i < 3; i++) {
  119. ret = i2c_smbus_write_byte_data(client, reg, val);
  120. if (ret == 0)
  121. return 0;
  122. }
  123. v4l2_err(sd, "%s: failed reg 0x%x, val 0x%x\n", __func__, reg, val);
  124. return ret;
  125. }
  126. /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
  127. and then the value-mask (to be OR-ed). */
  128. static inline void ad9389b_wr_and_or(struct v4l2_subdev *sd, u8 reg,
  129. u8 clr_mask, u8 val_mask)
  130. {
  131. ad9389b_wr(sd, reg, (ad9389b_rd(sd, reg) & clr_mask) | val_mask);
  132. }
  133. static void ad9389b_edid_rd(struct v4l2_subdev *sd, u16 len, u8 *buf)
  134. {
  135. struct ad9389b_state *state = get_ad9389b_state(sd);
  136. int i;
  137. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  138. for (i = 0; i < len; i++)
  139. buf[i] = i2c_smbus_read_byte_data(state->edid_i2c_client, i);
  140. }
  141. static inline bool ad9389b_have_hotplug(struct v4l2_subdev *sd)
  142. {
  143. return ad9389b_rd(sd, 0x42) & MASK_AD9389B_HPD_DETECT;
  144. }
  145. static inline bool ad9389b_have_rx_sense(struct v4l2_subdev *sd)
  146. {
  147. return ad9389b_rd(sd, 0x42) & MASK_AD9389B_MSEN_DETECT;
  148. }
  149. static void ad9389b_csc_conversion_mode(struct v4l2_subdev *sd, u8 mode)
  150. {
  151. ad9389b_wr_and_or(sd, 0x17, 0xe7, (mode & 0x3)<<3);
  152. ad9389b_wr_and_or(sd, 0x18, 0x9f, (mode & 0x3)<<5);
  153. }
  154. static void ad9389b_csc_coeff(struct v4l2_subdev *sd,
  155. u16 A1, u16 A2, u16 A3, u16 A4,
  156. u16 B1, u16 B2, u16 B3, u16 B4,
  157. u16 C1, u16 C2, u16 C3, u16 C4)
  158. {
  159. /* A */
  160. ad9389b_wr_and_or(sd, 0x18, 0xe0, A1>>8);
  161. ad9389b_wr(sd, 0x19, A1);
  162. ad9389b_wr_and_or(sd, 0x1A, 0xe0, A2>>8);
  163. ad9389b_wr(sd, 0x1B, A2);
  164. ad9389b_wr_and_or(sd, 0x1c, 0xe0, A3>>8);
  165. ad9389b_wr(sd, 0x1d, A3);
  166. ad9389b_wr_and_or(sd, 0x1e, 0xe0, A4>>8);
  167. ad9389b_wr(sd, 0x1f, A4);
  168. /* B */
  169. ad9389b_wr_and_or(sd, 0x20, 0xe0, B1>>8);
  170. ad9389b_wr(sd, 0x21, B1);
  171. ad9389b_wr_and_or(sd, 0x22, 0xe0, B2>>8);
  172. ad9389b_wr(sd, 0x23, B2);
  173. ad9389b_wr_and_or(sd, 0x24, 0xe0, B3>>8);
  174. ad9389b_wr(sd, 0x25, B3);
  175. ad9389b_wr_and_or(sd, 0x26, 0xe0, B4>>8);
  176. ad9389b_wr(sd, 0x27, B4);
  177. /* C */
  178. ad9389b_wr_and_or(sd, 0x28, 0xe0, C1>>8);
  179. ad9389b_wr(sd, 0x29, C1);
  180. ad9389b_wr_and_or(sd, 0x2A, 0xe0, C2>>8);
  181. ad9389b_wr(sd, 0x2B, C2);
  182. ad9389b_wr_and_or(sd, 0x2C, 0xe0, C3>>8);
  183. ad9389b_wr(sd, 0x2D, C3);
  184. ad9389b_wr_and_or(sd, 0x2E, 0xe0, C4>>8);
  185. ad9389b_wr(sd, 0x2F, C4);
  186. }
  187. static void ad9389b_csc_rgb_full2limit(struct v4l2_subdev *sd, bool enable)
  188. {
  189. if (enable) {
  190. u8 csc_mode = 0;
  191. ad9389b_csc_conversion_mode(sd, csc_mode);
  192. ad9389b_csc_coeff(sd,
  193. 4096-564, 0, 0, 256,
  194. 0, 4096-564, 0, 256,
  195. 0, 0, 4096-564, 256);
  196. /* enable CSC */
  197. ad9389b_wr_and_or(sd, 0x3b, 0xfe, 0x1);
  198. /* AVI infoframe: Limited range RGB (16-235) */
  199. ad9389b_wr_and_or(sd, 0xcd, 0xf9, 0x02);
  200. } else {
  201. /* disable CSC */
  202. ad9389b_wr_and_or(sd, 0x3b, 0xfe, 0x0);
  203. /* AVI infoframe: Full range RGB (0-255) */
  204. ad9389b_wr_and_or(sd, 0xcd, 0xf9, 0x04);
  205. }
  206. }
  207. static void ad9389b_set_IT_content_AVI_InfoFrame(struct v4l2_subdev *sd)
  208. {
  209. struct ad9389b_state *state = get_ad9389b_state(sd);
  210. if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
  211. /* CE format, not IT */
  212. ad9389b_wr_and_or(sd, 0xcd, 0xbf, 0x00);
  213. } else {
  214. /* IT format */
  215. ad9389b_wr_and_or(sd, 0xcd, 0xbf, 0x40);
  216. }
  217. }
  218. static int ad9389b_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2_ctrl *ctrl)
  219. {
  220. struct ad9389b_state *state = get_ad9389b_state(sd);
  221. switch (ctrl->val) {
  222. case V4L2_DV_RGB_RANGE_AUTO:
  223. /* automatic */
  224. if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
  225. /* CE format, RGB limited range (16-235) */
  226. ad9389b_csc_rgb_full2limit(sd, true);
  227. } else {
  228. /* not CE format, RGB full range (0-255) */
  229. ad9389b_csc_rgb_full2limit(sd, false);
  230. }
  231. break;
  232. case V4L2_DV_RGB_RANGE_LIMITED:
  233. /* RGB limited range (16-235) */
  234. ad9389b_csc_rgb_full2limit(sd, true);
  235. break;
  236. case V4L2_DV_RGB_RANGE_FULL:
  237. /* RGB full range (0-255) */
  238. ad9389b_csc_rgb_full2limit(sd, false);
  239. break;
  240. default:
  241. return -EINVAL;
  242. }
  243. return 0;
  244. }
  245. static void ad9389b_set_manual_pll_gear(struct v4l2_subdev *sd, u32 pixelclock)
  246. {
  247. u8 gear;
  248. /* Workaround for TMDS PLL problem
  249. * The TMDS PLL in AD9389b change gear when the chip is heated above a
  250. * certain temperature. The output is disabled when the PLL change gear
  251. * so the monitor has to lock on the signal again. A workaround for
  252. * this is to use the manual PLL gears. This is a solution from Analog
  253. * Devices that is not documented in the datasheets.
  254. * 0x98 [7] = enable manual gearing. 0x98 [6:4] = gear
  255. *
  256. * The pixel frequency ranges are based on readout of the gear the
  257. * automatic gearing selects for different pixel clocks
  258. * (read from 0x9e [3:1]).
  259. */
  260. if (pixelclock > 140000000)
  261. gear = 0xc0; /* 4th gear */
  262. else if (pixelclock > 117000000)
  263. gear = 0xb0; /* 3rd gear */
  264. else if (pixelclock > 87000000)
  265. gear = 0xa0; /* 2nd gear */
  266. else if (pixelclock > 60000000)
  267. gear = 0x90; /* 1st gear */
  268. else
  269. gear = 0x80; /* 0th gear */
  270. ad9389b_wr_and_or(sd, 0x98, 0x0f, gear);
  271. }
  272. /* ------------------------------ CTRL OPS ------------------------------ */
  273. static int ad9389b_s_ctrl(struct v4l2_ctrl *ctrl)
  274. {
  275. struct v4l2_subdev *sd = to_sd(ctrl);
  276. struct ad9389b_state *state = get_ad9389b_state(sd);
  277. v4l2_dbg(1, debug, sd,
  278. "%s: ctrl id: %d, ctrl->val %d\n", __func__, ctrl->id, ctrl->val);
  279. if (state->hdmi_mode_ctrl == ctrl) {
  280. /* Set HDMI or DVI-D */
  281. ad9389b_wr_and_or(sd, 0xaf, 0xfd,
  282. ctrl->val == V4L2_DV_TX_MODE_HDMI ? 0x02 : 0x00);
  283. return 0;
  284. }
  285. if (state->rgb_quantization_range_ctrl == ctrl)
  286. return ad9389b_set_rgb_quantization_mode(sd, ctrl);
  287. return -EINVAL;
  288. }
  289. static const struct v4l2_ctrl_ops ad9389b_ctrl_ops = {
  290. .s_ctrl = ad9389b_s_ctrl,
  291. };
  292. /* ---------------------------- CORE OPS ------------------------------------------- */
  293. #ifdef CONFIG_VIDEO_ADV_DEBUG
  294. static int ad9389b_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  295. {
  296. reg->val = ad9389b_rd(sd, reg->reg & 0xff);
  297. reg->size = 1;
  298. return 0;
  299. }
  300. static int ad9389b_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  301. {
  302. ad9389b_wr(sd, reg->reg & 0xff, reg->val & 0xff);
  303. return 0;
  304. }
  305. #endif
  306. static int ad9389b_log_status(struct v4l2_subdev *sd)
  307. {
  308. struct ad9389b_state *state = get_ad9389b_state(sd);
  309. struct ad9389b_state_edid *edid = &state->edid;
  310. static const char * const states[] = {
  311. "in reset",
  312. "reading EDID",
  313. "idle",
  314. "initializing HDCP",
  315. "HDCP enabled",
  316. "initializing HDCP repeater",
  317. "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
  318. };
  319. static const char * const errors[] = {
  320. "no error",
  321. "bad receiver BKSV",
  322. "Ri mismatch",
  323. "Pj mismatch",
  324. "i2c error",
  325. "timed out",
  326. "max repeater cascade exceeded",
  327. "hash check failed",
  328. "too many devices",
  329. "9", "A", "B", "C", "D", "E", "F"
  330. };
  331. u8 manual_gear;
  332. v4l2_info(sd, "chip revision %d\n", state->chip_revision);
  333. v4l2_info(sd, "power %s\n", state->power_on ? "on" : "off");
  334. v4l2_info(sd, "%s hotplug, %s Rx Sense, %s EDID (%d block(s))\n",
  335. (ad9389b_rd(sd, 0x42) & MASK_AD9389B_HPD_DETECT) ?
  336. "detected" : "no",
  337. (ad9389b_rd(sd, 0x42) & MASK_AD9389B_MSEN_DETECT) ?
  338. "detected" : "no",
  339. edid->segments ? "found" : "no", edid->blocks);
  340. v4l2_info(sd, "%s output %s\n",
  341. (ad9389b_rd(sd, 0xaf) & 0x02) ?
  342. "HDMI" : "DVI-D",
  343. (ad9389b_rd(sd, 0xa1) & 0x3c) ?
  344. "disabled" : "enabled");
  345. v4l2_info(sd, "ad9389b: %s\n", (ad9389b_rd(sd, 0xb8) & 0x40) ?
  346. "encrypted" : "no encryption");
  347. v4l2_info(sd, "state: %s, error: %s, detect count: %u, msk/irq: %02x/%02x\n",
  348. states[ad9389b_rd(sd, 0xc8) & 0xf],
  349. errors[ad9389b_rd(sd, 0xc8) >> 4],
  350. state->edid_detect_counter,
  351. ad9389b_rd(sd, 0x94), ad9389b_rd(sd, 0x96));
  352. manual_gear = ad9389b_rd(sd, 0x98) & 0x80;
  353. v4l2_info(sd, "ad9389b: RGB quantization: %s range\n",
  354. ad9389b_rd(sd, 0x3b) & 0x01 ? "limited" : "full");
  355. v4l2_info(sd, "ad9389b: %s gear %d\n",
  356. manual_gear ? "manual" : "automatic",
  357. manual_gear ? ((ad9389b_rd(sd, 0x98) & 0x70) >> 4) :
  358. ((ad9389b_rd(sd, 0x9e) & 0x0e) >> 1));
  359. if (ad9389b_rd(sd, 0xaf) & 0x02) {
  360. /* HDMI only */
  361. u8 manual_cts = ad9389b_rd(sd, 0x0a) & 0x80;
  362. u32 N = (ad9389b_rd(sd, 0x01) & 0xf) << 16 |
  363. ad9389b_rd(sd, 0x02) << 8 |
  364. ad9389b_rd(sd, 0x03);
  365. u8 vic_detect = ad9389b_rd(sd, 0x3e) >> 2;
  366. u8 vic_sent = ad9389b_rd(sd, 0x3d) & 0x3f;
  367. u32 CTS;
  368. if (manual_cts)
  369. CTS = (ad9389b_rd(sd, 0x07) & 0xf) << 16 |
  370. ad9389b_rd(sd, 0x08) << 8 |
  371. ad9389b_rd(sd, 0x09);
  372. else
  373. CTS = (ad9389b_rd(sd, 0x04) & 0xf) << 16 |
  374. ad9389b_rd(sd, 0x05) << 8 |
  375. ad9389b_rd(sd, 0x06);
  376. N = (ad9389b_rd(sd, 0x01) & 0xf) << 16 |
  377. ad9389b_rd(sd, 0x02) << 8 |
  378. ad9389b_rd(sd, 0x03);
  379. v4l2_info(sd, "ad9389b: CTS %s mode: N %d, CTS %d\n",
  380. manual_cts ? "manual" : "automatic", N, CTS);
  381. v4l2_info(sd, "ad9389b: VIC: detected %d, sent %d\n",
  382. vic_detect, vic_sent);
  383. }
  384. if (state->dv_timings.type == V4L2_DV_BT_656_1120)
  385. v4l2_print_dv_timings(sd->name, "timings: ",
  386. &state->dv_timings, false);
  387. else
  388. v4l2_info(sd, "no timings set\n");
  389. return 0;
  390. }
  391. /* Power up/down ad9389b */
  392. static int ad9389b_s_power(struct v4l2_subdev *sd, int on)
  393. {
  394. struct ad9389b_state *state = get_ad9389b_state(sd);
  395. struct ad9389b_platform_data *pdata = &state->pdata;
  396. const int retries = 20;
  397. int i;
  398. v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
  399. state->power_on = on;
  400. if (!on) {
  401. /* Power down */
  402. ad9389b_wr_and_or(sd, 0x41, 0xbf, 0x40);
  403. return true;
  404. }
  405. /* Power up */
  406. /* The ad9389b does not always come up immediately.
  407. Retry multiple times. */
  408. for (i = 0; i < retries; i++) {
  409. ad9389b_wr_and_or(sd, 0x41, 0xbf, 0x0);
  410. if ((ad9389b_rd(sd, 0x41) & 0x40) == 0)
  411. break;
  412. ad9389b_wr_and_or(sd, 0x41, 0xbf, 0x40);
  413. msleep(10);
  414. }
  415. if (i == retries) {
  416. v4l2_dbg(1, debug, sd, "failed to powerup the ad9389b\n");
  417. ad9389b_s_power(sd, 0);
  418. return false;
  419. }
  420. if (i > 1)
  421. v4l2_dbg(1, debug, sd,
  422. "needed %d retries to powerup the ad9389b\n", i);
  423. /* Select chip: AD9389B */
  424. ad9389b_wr_and_or(sd, 0xba, 0xef, 0x10);
  425. /* Reserved registers that must be set according to REF_01 p. 11*/
  426. ad9389b_wr_and_or(sd, 0x98, 0xf0, 0x07);
  427. ad9389b_wr(sd, 0x9c, 0x38);
  428. ad9389b_wr_and_or(sd, 0x9d, 0xfc, 0x01);
  429. /* Differential output drive strength */
  430. if (pdata->diff_data_drive_strength > 0)
  431. ad9389b_wr(sd, 0xa2, pdata->diff_data_drive_strength);
  432. else
  433. ad9389b_wr(sd, 0xa2, 0x87);
  434. if (pdata->diff_clk_drive_strength > 0)
  435. ad9389b_wr(sd, 0xa3, pdata->diff_clk_drive_strength);
  436. else
  437. ad9389b_wr(sd, 0xa3, 0x87);
  438. ad9389b_wr(sd, 0x0a, 0x01);
  439. ad9389b_wr(sd, 0xbb, 0xff);
  440. /* Set number of attempts to read the EDID */
  441. ad9389b_wr(sd, 0xc9, 0xf);
  442. return true;
  443. }
  444. /* Enable interrupts */
  445. static void ad9389b_set_isr(struct v4l2_subdev *sd, bool enable)
  446. {
  447. u8 irqs = MASK_AD9389B_HPD_INT | MASK_AD9389B_MSEN_INT;
  448. u8 irqs_rd;
  449. int retries = 100;
  450. /* The datasheet says that the EDID ready interrupt should be
  451. disabled if there is no hotplug. */
  452. if (!enable)
  453. irqs = 0;
  454. else if (ad9389b_have_hotplug(sd))
  455. irqs |= MASK_AD9389B_EDID_RDY_INT;
  456. /*
  457. * This i2c write can fail (approx. 1 in 1000 writes). But it
  458. * is essential that this register is correct, so retry it
  459. * multiple times.
  460. *
  461. * Note that the i2c write does not report an error, but the readback
  462. * clearly shows the wrong value.
  463. */
  464. do {
  465. ad9389b_wr(sd, 0x94, irqs);
  466. irqs_rd = ad9389b_rd(sd, 0x94);
  467. } while (retries-- && irqs_rd != irqs);
  468. if (irqs_rd != irqs)
  469. v4l2_err(sd, "Could not set interrupts: hw failure?\n");
  470. }
  471. /* Interrupt handler */
  472. static int ad9389b_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  473. {
  474. u8 irq_status;
  475. /* disable interrupts to prevent a race condition */
  476. ad9389b_set_isr(sd, false);
  477. irq_status = ad9389b_rd(sd, 0x96);
  478. /* clear detected interrupts */
  479. ad9389b_wr(sd, 0x96, irq_status);
  480. /* enable interrupts */
  481. ad9389b_set_isr(sd, true);
  482. v4l2_dbg(1, debug, sd, "%s: irq_status 0x%x\n", __func__, irq_status);
  483. if (irq_status & (MASK_AD9389B_HPD_INT))
  484. ad9389b_check_monitor_present_status(sd);
  485. if (irq_status & MASK_AD9389B_EDID_RDY_INT)
  486. ad9389b_check_edid_status(sd);
  487. *handled = true;
  488. return 0;
  489. }
  490. static const struct v4l2_subdev_core_ops ad9389b_core_ops = {
  491. .log_status = ad9389b_log_status,
  492. #ifdef CONFIG_VIDEO_ADV_DEBUG
  493. .g_register = ad9389b_g_register,
  494. .s_register = ad9389b_s_register,
  495. #endif
  496. .s_power = ad9389b_s_power,
  497. .interrupt_service_routine = ad9389b_isr,
  498. };
  499. /* ------------------------------ VIDEO OPS ------------------------------ */
  500. /* Enable/disable ad9389b output */
  501. static int ad9389b_s_stream(struct v4l2_subdev *sd, int enable)
  502. {
  503. v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis"));
  504. ad9389b_wr_and_or(sd, 0xa1, ~0x3c, (enable ? 0 : 0x3c));
  505. if (enable) {
  506. ad9389b_check_monitor_present_status(sd);
  507. } else {
  508. ad9389b_s_power(sd, 0);
  509. }
  510. return 0;
  511. }
  512. static const struct v4l2_dv_timings_cap ad9389b_timings_cap = {
  513. .type = V4L2_DV_BT_656_1120,
  514. /* keep this initialization for compatibility with GCC < 4.4.6 */
  515. .reserved = { 0 },
  516. V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1200, 25000000, 170000000,
  517. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  518. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  519. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  520. V4L2_DV_BT_CAP_CUSTOM)
  521. };
  522. static int ad9389b_s_dv_timings(struct v4l2_subdev *sd,
  523. struct v4l2_dv_timings *timings)
  524. {
  525. struct ad9389b_state *state = get_ad9389b_state(sd);
  526. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  527. /* quick sanity check */
  528. if (!v4l2_valid_dv_timings(timings, &ad9389b_timings_cap, NULL, NULL))
  529. return -EINVAL;
  530. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  531. if the format is one of the CEA or DMT timings. */
  532. v4l2_find_dv_timings_cap(timings, &ad9389b_timings_cap, 0, NULL, NULL);
  533. timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
  534. /* save timings */
  535. state->dv_timings = *timings;
  536. /* update quantization range based on new dv_timings */
  537. ad9389b_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl);
  538. /* update PLL gear based on new dv_timings */
  539. if (state->pdata.tmds_pll_gear == AD9389B_TMDS_PLL_GEAR_SEMI_AUTOMATIC)
  540. ad9389b_set_manual_pll_gear(sd, (u32)timings->bt.pixelclock);
  541. /* update AVI infoframe */
  542. ad9389b_set_IT_content_AVI_InfoFrame(sd);
  543. return 0;
  544. }
  545. static int ad9389b_g_dv_timings(struct v4l2_subdev *sd,
  546. struct v4l2_dv_timings *timings)
  547. {
  548. struct ad9389b_state *state = get_ad9389b_state(sd);
  549. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  550. if (!timings)
  551. return -EINVAL;
  552. *timings = state->dv_timings;
  553. return 0;
  554. }
  555. static int ad9389b_enum_dv_timings(struct v4l2_subdev *sd,
  556. struct v4l2_enum_dv_timings *timings)
  557. {
  558. if (timings->pad != 0)
  559. return -EINVAL;
  560. return v4l2_enum_dv_timings_cap(timings, &ad9389b_timings_cap,
  561. NULL, NULL);
  562. }
  563. static int ad9389b_dv_timings_cap(struct v4l2_subdev *sd,
  564. struct v4l2_dv_timings_cap *cap)
  565. {
  566. if (cap->pad != 0)
  567. return -EINVAL;
  568. *cap = ad9389b_timings_cap;
  569. return 0;
  570. }
  571. static const struct v4l2_subdev_video_ops ad9389b_video_ops = {
  572. .s_stream = ad9389b_s_stream,
  573. .s_dv_timings = ad9389b_s_dv_timings,
  574. .g_dv_timings = ad9389b_g_dv_timings,
  575. };
  576. /* ------------------------------ PAD OPS ------------------------------ */
  577. static int ad9389b_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  578. {
  579. struct ad9389b_state *state = get_ad9389b_state(sd);
  580. if (edid->pad != 0)
  581. return -EINVAL;
  582. if (edid->blocks == 0 || edid->blocks > 256)
  583. return -EINVAL;
  584. if (!state->edid.segments) {
  585. v4l2_dbg(1, debug, sd, "EDID segment 0 not found\n");
  586. return -ENODATA;
  587. }
  588. if (edid->start_block >= state->edid.segments * 2)
  589. return -E2BIG;
  590. if (edid->blocks + edid->start_block >= state->edid.segments * 2)
  591. edid->blocks = state->edid.segments * 2 - edid->start_block;
  592. memcpy(edid->edid, &state->edid.data[edid->start_block * 128],
  593. 128 * edid->blocks);
  594. return 0;
  595. }
  596. static const struct v4l2_subdev_pad_ops ad9389b_pad_ops = {
  597. .get_edid = ad9389b_get_edid,
  598. .enum_dv_timings = ad9389b_enum_dv_timings,
  599. .dv_timings_cap = ad9389b_dv_timings_cap,
  600. };
  601. /* ------------------------------ AUDIO OPS ------------------------------ */
  602. static int ad9389b_s_audio_stream(struct v4l2_subdev *sd, int enable)
  603. {
  604. v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis"));
  605. if (enable)
  606. ad9389b_wr_and_or(sd, 0x45, 0x3f, 0x80);
  607. else
  608. ad9389b_wr_and_or(sd, 0x45, 0x3f, 0x40);
  609. return 0;
  610. }
  611. static int ad9389b_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  612. {
  613. u32 N;
  614. switch (freq) {
  615. case 32000: N = 4096; break;
  616. case 44100: N = 6272; break;
  617. case 48000: N = 6144; break;
  618. case 88200: N = 12544; break;
  619. case 96000: N = 12288; break;
  620. case 176400: N = 25088; break;
  621. case 192000: N = 24576; break;
  622. default:
  623. return -EINVAL;
  624. }
  625. /* Set N (used with CTS to regenerate the audio clock) */
  626. ad9389b_wr(sd, 0x01, (N >> 16) & 0xf);
  627. ad9389b_wr(sd, 0x02, (N >> 8) & 0xff);
  628. ad9389b_wr(sd, 0x03, N & 0xff);
  629. return 0;
  630. }
  631. static int ad9389b_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  632. {
  633. u32 i2s_sf;
  634. switch (freq) {
  635. case 32000: i2s_sf = 0x30; break;
  636. case 44100: i2s_sf = 0x00; break;
  637. case 48000: i2s_sf = 0x20; break;
  638. case 88200: i2s_sf = 0x80; break;
  639. case 96000: i2s_sf = 0xa0; break;
  640. case 176400: i2s_sf = 0xc0; break;
  641. case 192000: i2s_sf = 0xe0; break;
  642. default:
  643. return -EINVAL;
  644. }
  645. /* Set sampling frequency for I2S audio to 48 kHz */
  646. ad9389b_wr_and_or(sd, 0x15, 0xf, i2s_sf);
  647. return 0;
  648. }
  649. static int ad9389b_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, u32 config)
  650. {
  651. /* TODO based on input/output/config */
  652. /* TODO See datasheet "Programmers guide" p. 39-40 */
  653. /* Only 2 channels in use for application */
  654. ad9389b_wr_and_or(sd, 0x50, 0x1f, 0x20);
  655. /* Speaker mapping */
  656. ad9389b_wr(sd, 0x51, 0x00);
  657. /* TODO Where should this be placed? */
  658. /* 16 bit audio word length */
  659. ad9389b_wr_and_or(sd, 0x14, 0xf0, 0x02);
  660. return 0;
  661. }
  662. static const struct v4l2_subdev_audio_ops ad9389b_audio_ops = {
  663. .s_stream = ad9389b_s_audio_stream,
  664. .s_clock_freq = ad9389b_s_clock_freq,
  665. .s_i2s_clock_freq = ad9389b_s_i2s_clock_freq,
  666. .s_routing = ad9389b_s_routing,
  667. };
  668. /* --------------------- SUBDEV OPS --------------------------------------- */
  669. static const struct v4l2_subdev_ops ad9389b_ops = {
  670. .core = &ad9389b_core_ops,
  671. .video = &ad9389b_video_ops,
  672. .audio = &ad9389b_audio_ops,
  673. .pad = &ad9389b_pad_ops,
  674. };
  675. /* ----------------------------------------------------------------------- */
  676. static void ad9389b_dbg_dump_edid(int lvl, int debug, struct v4l2_subdev *sd,
  677. int segment, u8 *buf)
  678. {
  679. int i, j;
  680. if (debug < lvl)
  681. return;
  682. v4l2_dbg(lvl, debug, sd, "edid segment %d\n", segment);
  683. for (i = 0; i < 256; i += 16) {
  684. u8 b[128];
  685. u8 *bp = b;
  686. if (i == 128)
  687. v4l2_dbg(lvl, debug, sd, "\n");
  688. for (j = i; j < i + 16; j++) {
  689. sprintf(bp, "0x%02x, ", buf[j]);
  690. bp += 6;
  691. }
  692. bp[0] = '\0';
  693. v4l2_dbg(lvl, debug, sd, "%s\n", b);
  694. }
  695. }
  696. static void ad9389b_edid_handler(struct work_struct *work)
  697. {
  698. struct delayed_work *dwork = to_delayed_work(work);
  699. struct ad9389b_state *state =
  700. container_of(dwork, struct ad9389b_state, edid_handler);
  701. struct v4l2_subdev *sd = &state->sd;
  702. struct ad9389b_edid_detect ed;
  703. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  704. if (ad9389b_check_edid_status(sd)) {
  705. /* Return if we received the EDID. */
  706. return;
  707. }
  708. if (ad9389b_have_hotplug(sd)) {
  709. /* We must retry reading the EDID several times, it is possible
  710. * that initially the EDID couldn't be read due to i2c errors
  711. * (DVI connectors are particularly prone to this problem). */
  712. if (state->edid.read_retries) {
  713. state->edid.read_retries--;
  714. v4l2_dbg(1, debug, sd, "%s: edid read failed\n", __func__);
  715. ad9389b_s_power(sd, false);
  716. ad9389b_s_power(sd, true);
  717. queue_delayed_work(state->work_queue,
  718. &state->edid_handler, EDID_DELAY);
  719. return;
  720. }
  721. }
  722. /* We failed to read the EDID, so send an event for this. */
  723. ed.present = false;
  724. ed.segment = ad9389b_rd(sd, 0xc4);
  725. v4l2_subdev_notify(sd, AD9389B_EDID_DETECT, (void *)&ed);
  726. v4l2_dbg(1, debug, sd, "%s: no edid found\n", __func__);
  727. }
  728. static void ad9389b_audio_setup(struct v4l2_subdev *sd)
  729. {
  730. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  731. ad9389b_s_i2s_clock_freq(sd, 48000);
  732. ad9389b_s_clock_freq(sd, 48000);
  733. ad9389b_s_routing(sd, 0, 0, 0);
  734. }
  735. /* Initial setup of AD9389b */
  736. /* Configure hdmi transmitter. */
  737. static void ad9389b_setup(struct v4l2_subdev *sd)
  738. {
  739. struct ad9389b_state *state = get_ad9389b_state(sd);
  740. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  741. /* Input format: RGB 4:4:4 */
  742. ad9389b_wr_and_or(sd, 0x15, 0xf1, 0x0);
  743. /* Output format: RGB 4:4:4 */
  744. ad9389b_wr_and_or(sd, 0x16, 0x3f, 0x0);
  745. /* 1st order interpolation 4:2:2 -> 4:4:4 up conversion,
  746. Aspect ratio: 16:9 */
  747. ad9389b_wr_and_or(sd, 0x17, 0xf9, 0x06);
  748. /* Output format: RGB 4:4:4, Active Format Information is valid. */
  749. ad9389b_wr_and_or(sd, 0x45, 0xc7, 0x08);
  750. /* Underscanned */
  751. ad9389b_wr_and_or(sd, 0x46, 0x3f, 0x80);
  752. /* Setup video format */
  753. ad9389b_wr(sd, 0x3c, 0x0);
  754. /* Active format aspect ratio: same as picure. */
  755. ad9389b_wr(sd, 0x47, 0x80);
  756. /* No encryption */
  757. ad9389b_wr_and_or(sd, 0xaf, 0xef, 0x0);
  758. /* Positive clk edge capture for input video clock */
  759. ad9389b_wr_and_or(sd, 0xba, 0x1f, 0x60);
  760. ad9389b_audio_setup(sd);
  761. v4l2_ctrl_handler_setup(&state->hdl);
  762. ad9389b_set_IT_content_AVI_InfoFrame(sd);
  763. }
  764. static void ad9389b_notify_monitor_detect(struct v4l2_subdev *sd)
  765. {
  766. struct ad9389b_monitor_detect mdt;
  767. struct ad9389b_state *state = get_ad9389b_state(sd);
  768. mdt.present = state->have_monitor;
  769. v4l2_subdev_notify(sd, AD9389B_MONITOR_DETECT, (void *)&mdt);
  770. }
  771. static void ad9389b_update_monitor_present_status(struct v4l2_subdev *sd)
  772. {
  773. struct ad9389b_state *state = get_ad9389b_state(sd);
  774. /* read hotplug and rx-sense state */
  775. u8 status = ad9389b_rd(sd, 0x42);
  776. v4l2_dbg(1, debug, sd, "%s: status: 0x%x%s%s\n",
  777. __func__,
  778. status,
  779. status & MASK_AD9389B_HPD_DETECT ? ", hotplug" : "",
  780. status & MASK_AD9389B_MSEN_DETECT ? ", rx-sense" : "");
  781. if (status & MASK_AD9389B_HPD_DETECT) {
  782. v4l2_dbg(1, debug, sd, "%s: hotplug detected\n", __func__);
  783. state->have_monitor = true;
  784. if (!ad9389b_s_power(sd, true)) {
  785. v4l2_dbg(1, debug, sd,
  786. "%s: monitor detected, powerup failed\n", __func__);
  787. return;
  788. }
  789. ad9389b_setup(sd);
  790. ad9389b_notify_monitor_detect(sd);
  791. state->edid.read_retries = EDID_MAX_RETRIES;
  792. queue_delayed_work(state->work_queue,
  793. &state->edid_handler, EDID_DELAY);
  794. } else if (!(status & MASK_AD9389B_HPD_DETECT)) {
  795. v4l2_dbg(1, debug, sd, "%s: hotplug not detected\n", __func__);
  796. state->have_monitor = false;
  797. ad9389b_notify_monitor_detect(sd);
  798. ad9389b_s_power(sd, false);
  799. memset(&state->edid, 0, sizeof(struct ad9389b_state_edid));
  800. }
  801. /* update read only ctrls */
  802. v4l2_ctrl_s_ctrl(state->hotplug_ctrl, ad9389b_have_hotplug(sd) ? 0x1 : 0x0);
  803. v4l2_ctrl_s_ctrl(state->rx_sense_ctrl, ad9389b_have_rx_sense(sd) ? 0x1 : 0x0);
  804. v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, state->edid.segments ? 0x1 : 0x0);
  805. /* update with setting from ctrls */
  806. ad9389b_s_ctrl(state->rgb_quantization_range_ctrl);
  807. ad9389b_s_ctrl(state->hdmi_mode_ctrl);
  808. }
  809. static void ad9389b_check_monitor_present_status(struct v4l2_subdev *sd)
  810. {
  811. struct ad9389b_state *state = get_ad9389b_state(sd);
  812. int retry = 0;
  813. ad9389b_update_monitor_present_status(sd);
  814. /*
  815. * Rapid toggling of the hotplug may leave the chip powered off,
  816. * even if we think it is on. In that case reset and power up again.
  817. */
  818. while (state->power_on && (ad9389b_rd(sd, 0x41) & 0x40)) {
  819. if (++retry > 5) {
  820. v4l2_err(sd, "retried %d times, give up\n", retry);
  821. return;
  822. }
  823. v4l2_dbg(1, debug, sd, "%s: reset and re-check status (%d)\n", __func__, retry);
  824. ad9389b_notify_monitor_detect(sd);
  825. cancel_delayed_work_sync(&state->edid_handler);
  826. memset(&state->edid, 0, sizeof(struct ad9389b_state_edid));
  827. ad9389b_s_power(sd, false);
  828. ad9389b_update_monitor_present_status(sd);
  829. }
  830. }
  831. static bool edid_block_verify_crc(u8 *edid_block)
  832. {
  833. u8 sum = 0;
  834. int i;
  835. for (i = 0; i < 128; i++)
  836. sum += edid_block[i];
  837. return sum == 0;
  838. }
  839. static bool edid_verify_crc(struct v4l2_subdev *sd, u32 segment)
  840. {
  841. struct ad9389b_state *state = get_ad9389b_state(sd);
  842. u32 blocks = state->edid.blocks;
  843. u8 *data = state->edid.data;
  844. if (edid_block_verify_crc(&data[segment * 256])) {
  845. if ((segment + 1) * 2 <= blocks)
  846. return edid_block_verify_crc(&data[segment * 256 + 128]);
  847. return true;
  848. }
  849. return false;
  850. }
  851. static bool edid_verify_header(struct v4l2_subdev *sd, u32 segment)
  852. {
  853. static const u8 hdmi_header[] = {
  854. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
  855. };
  856. struct ad9389b_state *state = get_ad9389b_state(sd);
  857. u8 *data = state->edid.data;
  858. int i;
  859. if (segment)
  860. return true;
  861. for (i = 0; i < ARRAY_SIZE(hdmi_header); i++)
  862. if (data[i] != hdmi_header[i])
  863. return false;
  864. return true;
  865. }
  866. static bool ad9389b_check_edid_status(struct v4l2_subdev *sd)
  867. {
  868. struct ad9389b_state *state = get_ad9389b_state(sd);
  869. struct ad9389b_edid_detect ed;
  870. int segment;
  871. u8 edidRdy = ad9389b_rd(sd, 0xc5);
  872. v4l2_dbg(1, debug, sd, "%s: edid ready (retries: %d)\n",
  873. __func__, EDID_MAX_RETRIES - state->edid.read_retries);
  874. if (!(edidRdy & MASK_AD9389B_EDID_RDY))
  875. return false;
  876. segment = ad9389b_rd(sd, 0xc4);
  877. if (segment >= EDID_MAX_SEGM) {
  878. v4l2_err(sd, "edid segment number too big\n");
  879. return false;
  880. }
  881. v4l2_dbg(1, debug, sd, "%s: got segment %d\n", __func__, segment);
  882. ad9389b_edid_rd(sd, 256, &state->edid.data[segment * 256]);
  883. ad9389b_dbg_dump_edid(2, debug, sd, segment,
  884. &state->edid.data[segment * 256]);
  885. if (segment == 0) {
  886. state->edid.blocks = state->edid.data[0x7e] + 1;
  887. v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n",
  888. __func__, state->edid.blocks);
  889. }
  890. if (!edid_verify_crc(sd, segment) ||
  891. !edid_verify_header(sd, segment)) {
  892. /* edid crc error, force reread of edid segment */
  893. v4l2_err(sd, "%s: edid crc or header error\n", __func__);
  894. ad9389b_s_power(sd, false);
  895. ad9389b_s_power(sd, true);
  896. return false;
  897. }
  898. /* one more segment read ok */
  899. state->edid.segments = segment + 1;
  900. if (((state->edid.data[0x7e] >> 1) + 1) > state->edid.segments) {
  901. /* Request next EDID segment */
  902. v4l2_dbg(1, debug, sd, "%s: request segment %d\n",
  903. __func__, state->edid.segments);
  904. ad9389b_wr(sd, 0xc9, 0xf);
  905. ad9389b_wr(sd, 0xc4, state->edid.segments);
  906. state->edid.read_retries = EDID_MAX_RETRIES;
  907. queue_delayed_work(state->work_queue,
  908. &state->edid_handler, EDID_DELAY);
  909. return false;
  910. }
  911. /* report when we have all segments but report only for segment 0 */
  912. ed.present = true;
  913. ed.segment = 0;
  914. v4l2_subdev_notify(sd, AD9389B_EDID_DETECT, (void *)&ed);
  915. state->edid_detect_counter++;
  916. v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, state->edid.segments ? 0x1 : 0x0);
  917. return ed.present;
  918. }
  919. /* ----------------------------------------------------------------------- */
  920. static void ad9389b_init_setup(struct v4l2_subdev *sd)
  921. {
  922. struct ad9389b_state *state = get_ad9389b_state(sd);
  923. struct ad9389b_state_edid *edid = &state->edid;
  924. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  925. /* clear all interrupts */
  926. ad9389b_wr(sd, 0x96, 0xff);
  927. memset(edid, 0, sizeof(struct ad9389b_state_edid));
  928. state->have_monitor = false;
  929. ad9389b_set_isr(sd, false);
  930. }
  931. static int ad9389b_probe(struct i2c_client *client, const struct i2c_device_id *id)
  932. {
  933. const struct v4l2_dv_timings dv1080p60 = V4L2_DV_BT_CEA_1920X1080P60;
  934. struct ad9389b_state *state;
  935. struct ad9389b_platform_data *pdata = client->dev.platform_data;
  936. struct v4l2_ctrl_handler *hdl;
  937. struct v4l2_subdev *sd;
  938. int err = -EIO;
  939. /* Check if the adapter supports the needed features */
  940. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  941. return -EIO;
  942. v4l_dbg(1, debug, client, "detecting ad9389b client on address 0x%x\n",
  943. client->addr << 1);
  944. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  945. if (!state)
  946. return -ENOMEM;
  947. /* Platform data */
  948. if (pdata == NULL) {
  949. v4l_err(client, "No platform data!\n");
  950. return -ENODEV;
  951. }
  952. memcpy(&state->pdata, pdata, sizeof(state->pdata));
  953. sd = &state->sd;
  954. v4l2_i2c_subdev_init(sd, client, &ad9389b_ops);
  955. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  956. hdl = &state->hdl;
  957. v4l2_ctrl_handler_init(hdl, 5);
  958. /* private controls */
  959. state->hdmi_mode_ctrl = v4l2_ctrl_new_std_menu(hdl, &ad9389b_ctrl_ops,
  960. V4L2_CID_DV_TX_MODE, V4L2_DV_TX_MODE_HDMI,
  961. 0, V4L2_DV_TX_MODE_DVI_D);
  962. state->hotplug_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  963. V4L2_CID_DV_TX_HOTPLUG, 0, 1, 0, 0);
  964. state->rx_sense_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  965. V4L2_CID_DV_TX_RXSENSE, 0, 1, 0, 0);
  966. state->have_edid0_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  967. V4L2_CID_DV_TX_EDID_PRESENT, 0, 1, 0, 0);
  968. state->rgb_quantization_range_ctrl =
  969. v4l2_ctrl_new_std_menu(hdl, &ad9389b_ctrl_ops,
  970. V4L2_CID_DV_TX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  971. 0, V4L2_DV_RGB_RANGE_AUTO);
  972. sd->ctrl_handler = hdl;
  973. if (hdl->error) {
  974. err = hdl->error;
  975. goto err_hdl;
  976. }
  977. state->hdmi_mode_ctrl->is_private = true;
  978. state->hotplug_ctrl->is_private = true;
  979. state->rx_sense_ctrl->is_private = true;
  980. state->have_edid0_ctrl->is_private = true;
  981. state->rgb_quantization_range_ctrl->is_private = true;
  982. state->pad.flags = MEDIA_PAD_FL_SINK;
  983. err = media_entity_init(&sd->entity, 1, &state->pad, 0);
  984. if (err)
  985. goto err_hdl;
  986. state->chip_revision = ad9389b_rd(sd, 0x0);
  987. if (state->chip_revision != 2) {
  988. v4l2_err(sd, "chip_revision %d != 2\n", state->chip_revision);
  989. err = -EIO;
  990. goto err_entity;
  991. }
  992. v4l2_dbg(1, debug, sd, "reg 0x41 0x%x, chip version (reg 0x00) 0x%x\n",
  993. ad9389b_rd(sd, 0x41), state->chip_revision);
  994. state->edid_i2c_client = i2c_new_dummy(client->adapter, (0x7e>>1));
  995. if (state->edid_i2c_client == NULL) {
  996. v4l2_err(sd, "failed to register edid i2c client\n");
  997. err = -ENOMEM;
  998. goto err_entity;
  999. }
  1000. state->work_queue = create_singlethread_workqueue(sd->name);
  1001. if (state->work_queue == NULL) {
  1002. v4l2_err(sd, "could not create workqueue\n");
  1003. err = -ENOMEM;
  1004. goto err_unreg;
  1005. }
  1006. INIT_DELAYED_WORK(&state->edid_handler, ad9389b_edid_handler);
  1007. state->dv_timings = dv1080p60;
  1008. ad9389b_init_setup(sd);
  1009. ad9389b_set_isr(sd, true);
  1010. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  1011. client->addr << 1, client->adapter->name);
  1012. return 0;
  1013. err_unreg:
  1014. i2c_unregister_device(state->edid_i2c_client);
  1015. err_entity:
  1016. media_entity_cleanup(&sd->entity);
  1017. err_hdl:
  1018. v4l2_ctrl_handler_free(&state->hdl);
  1019. return err;
  1020. }
  1021. /* ----------------------------------------------------------------------- */
  1022. static int ad9389b_remove(struct i2c_client *client)
  1023. {
  1024. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1025. struct ad9389b_state *state = get_ad9389b_state(sd);
  1026. state->chip_revision = -1;
  1027. v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
  1028. client->addr << 1, client->adapter->name);
  1029. ad9389b_s_stream(sd, false);
  1030. ad9389b_s_audio_stream(sd, false);
  1031. ad9389b_init_setup(sd);
  1032. cancel_delayed_work(&state->edid_handler);
  1033. i2c_unregister_device(state->edid_i2c_client);
  1034. destroy_workqueue(state->work_queue);
  1035. v4l2_device_unregister_subdev(sd);
  1036. media_entity_cleanup(&sd->entity);
  1037. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1038. return 0;
  1039. }
  1040. /* ----------------------------------------------------------------------- */
  1041. static struct i2c_device_id ad9389b_id[] = {
  1042. { "ad9389b", 0 },
  1043. { "ad9889b", 0 },
  1044. { }
  1045. };
  1046. MODULE_DEVICE_TABLE(i2c, ad9389b_id);
  1047. static struct i2c_driver ad9389b_driver = {
  1048. .driver = {
  1049. .owner = THIS_MODULE,
  1050. .name = "ad9389b",
  1051. },
  1052. .probe = ad9389b_probe,
  1053. .remove = ad9389b_remove,
  1054. .id_table = ad9389b_id,
  1055. };
  1056. module_i2c_driver(ad9389b_driver);