adv7511.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. /*
  2. * Analog Devices ADV7511 HDMI Transmitter Device Driver
  3. *
  4. * Copyright 2013 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. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include <linux/delay.h>
  24. #include <linux/videodev2.h>
  25. #include <linux/gpio.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/hdmi.h>
  28. #include <linux/v4l2-dv-timings.h>
  29. #include <media/v4l2-device.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-ctrls.h>
  32. #include <media/v4l2-dv-timings.h>
  33. #include <media/adv7511.h>
  34. static int debug;
  35. module_param(debug, int, 0644);
  36. MODULE_PARM_DESC(debug, "debug level (0-2)");
  37. MODULE_DESCRIPTION("Analog Devices ADV7511 HDMI Transmitter Device Driver");
  38. MODULE_AUTHOR("Hans Verkuil");
  39. MODULE_LICENSE("GPL v2");
  40. #define MASK_ADV7511_EDID_RDY_INT 0x04
  41. #define MASK_ADV7511_MSEN_INT 0x40
  42. #define MASK_ADV7511_HPD_INT 0x80
  43. #define MASK_ADV7511_HPD_DETECT 0x40
  44. #define MASK_ADV7511_MSEN_DETECT 0x20
  45. #define MASK_ADV7511_EDID_RDY 0x10
  46. #define EDID_MAX_RETRIES (8)
  47. #define EDID_DELAY 250
  48. #define EDID_MAX_SEGM 8
  49. #define ADV7511_MAX_WIDTH 1920
  50. #define ADV7511_MAX_HEIGHT 1200
  51. #define ADV7511_MIN_PIXELCLOCK 20000000
  52. #define ADV7511_MAX_PIXELCLOCK 225000000
  53. /*
  54. **********************************************************************
  55. *
  56. * Arrays with configuration parameters for the ADV7511
  57. *
  58. **********************************************************************
  59. */
  60. struct i2c_reg_value {
  61. unsigned char reg;
  62. unsigned char value;
  63. };
  64. struct adv7511_state_edid {
  65. /* total number of blocks */
  66. u32 blocks;
  67. /* Number of segments read */
  68. u32 segments;
  69. u8 data[EDID_MAX_SEGM * 256];
  70. /* Number of EDID read retries left */
  71. unsigned read_retries;
  72. bool complete;
  73. };
  74. struct adv7511_state {
  75. struct adv7511_platform_data pdata;
  76. struct v4l2_subdev sd;
  77. struct media_pad pad;
  78. struct v4l2_ctrl_handler hdl;
  79. int chip_revision;
  80. u8 i2c_edid_addr;
  81. u8 i2c_cec_addr;
  82. u8 i2c_pktmem_addr;
  83. /* Is the adv7511 powered on? */
  84. bool power_on;
  85. /* Did we receive hotplug and rx-sense signals? */
  86. bool have_monitor;
  87. /* timings from s_dv_timings */
  88. struct v4l2_dv_timings dv_timings;
  89. u32 fmt_code;
  90. u32 colorspace;
  91. u32 ycbcr_enc;
  92. u32 quantization;
  93. u32 xfer_func;
  94. /* controls */
  95. struct v4l2_ctrl *hdmi_mode_ctrl;
  96. struct v4l2_ctrl *hotplug_ctrl;
  97. struct v4l2_ctrl *rx_sense_ctrl;
  98. struct v4l2_ctrl *have_edid0_ctrl;
  99. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  100. struct i2c_client *i2c_edid;
  101. struct i2c_client *i2c_pktmem;
  102. struct adv7511_state_edid edid;
  103. /* Running counter of the number of detected EDIDs (for debugging) */
  104. unsigned edid_detect_counter;
  105. struct workqueue_struct *work_queue;
  106. struct delayed_work edid_handler; /* work entry */
  107. };
  108. static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd);
  109. static bool adv7511_check_edid_status(struct v4l2_subdev *sd);
  110. static void adv7511_setup(struct v4l2_subdev *sd);
  111. static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq);
  112. static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq);
  113. static const struct v4l2_dv_timings_cap adv7511_timings_cap = {
  114. .type = V4L2_DV_BT_656_1120,
  115. /* keep this initialization for compatibility with GCC < 4.4.6 */
  116. .reserved = { 0 },
  117. V4L2_INIT_BT_TIMINGS(0, ADV7511_MAX_WIDTH, 0, ADV7511_MAX_HEIGHT,
  118. ADV7511_MIN_PIXELCLOCK, ADV7511_MAX_PIXELCLOCK,
  119. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  120. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  121. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  122. V4L2_DV_BT_CAP_CUSTOM)
  123. };
  124. static inline struct adv7511_state *get_adv7511_state(struct v4l2_subdev *sd)
  125. {
  126. return container_of(sd, struct adv7511_state, sd);
  127. }
  128. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  129. {
  130. return &container_of(ctrl->handler, struct adv7511_state, hdl)->sd;
  131. }
  132. /* ------------------------ I2C ----------------------------------------------- */
  133. static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
  134. u8 command, bool check)
  135. {
  136. union i2c_smbus_data data;
  137. if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  138. I2C_SMBUS_READ, command,
  139. I2C_SMBUS_BYTE_DATA, &data))
  140. return data.byte;
  141. if (check)
  142. v4l_err(client, "error reading %02x, %02x\n",
  143. client->addr, command);
  144. return -1;
  145. }
  146. static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
  147. {
  148. int i;
  149. for (i = 0; i < 3; i++) {
  150. int ret = adv_smbus_read_byte_data_check(client, command, true);
  151. if (ret >= 0) {
  152. if (i)
  153. v4l_err(client, "read ok after %d retries\n", i);
  154. return ret;
  155. }
  156. }
  157. v4l_err(client, "read failed\n");
  158. return -1;
  159. }
  160. static int adv7511_rd(struct v4l2_subdev *sd, u8 reg)
  161. {
  162. struct i2c_client *client = v4l2_get_subdevdata(sd);
  163. return adv_smbus_read_byte_data(client, reg);
  164. }
  165. static int adv7511_wr(struct v4l2_subdev *sd, u8 reg, u8 val)
  166. {
  167. struct i2c_client *client = v4l2_get_subdevdata(sd);
  168. int ret;
  169. int i;
  170. for (i = 0; i < 3; i++) {
  171. ret = i2c_smbus_write_byte_data(client, reg, val);
  172. if (ret == 0)
  173. return 0;
  174. }
  175. v4l2_err(sd, "%s: i2c write error\n", __func__);
  176. return ret;
  177. }
  178. /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
  179. and then the value-mask (to be OR-ed). */
  180. static inline void adv7511_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask, u8 val_mask)
  181. {
  182. adv7511_wr(sd, reg, (adv7511_rd(sd, reg) & clr_mask) | val_mask);
  183. }
  184. static int adv_smbus_read_i2c_block_data(struct i2c_client *client,
  185. u8 command, unsigned length, u8 *values)
  186. {
  187. union i2c_smbus_data data;
  188. int ret;
  189. if (length > I2C_SMBUS_BLOCK_MAX)
  190. length = I2C_SMBUS_BLOCK_MAX;
  191. data.block[0] = length;
  192. ret = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  193. I2C_SMBUS_READ, command,
  194. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  195. memcpy(values, data.block + 1, length);
  196. return ret;
  197. }
  198. static inline void adv7511_edid_rd(struct v4l2_subdev *sd, u16 len, u8 *buf)
  199. {
  200. struct adv7511_state *state = get_adv7511_state(sd);
  201. int i;
  202. int err = 0;
  203. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  204. for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
  205. err = adv_smbus_read_i2c_block_data(state->i2c_edid, i,
  206. I2C_SMBUS_BLOCK_MAX, buf + i);
  207. if (err)
  208. v4l2_err(sd, "%s: i2c read error\n", __func__);
  209. }
  210. static int adv7511_pktmem_rd(struct v4l2_subdev *sd, u8 reg)
  211. {
  212. struct adv7511_state *state = get_adv7511_state(sd);
  213. return adv_smbus_read_byte_data(state->i2c_pktmem, reg);
  214. }
  215. static int adv7511_pktmem_wr(struct v4l2_subdev *sd, u8 reg, u8 val)
  216. {
  217. struct adv7511_state *state = get_adv7511_state(sd);
  218. int ret;
  219. int i;
  220. for (i = 0; i < 3; i++) {
  221. ret = i2c_smbus_write_byte_data(state->i2c_pktmem, reg, val);
  222. if (ret == 0)
  223. return 0;
  224. }
  225. v4l2_err(sd, "%s: i2c write error\n", __func__);
  226. return ret;
  227. }
  228. /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
  229. and then the value-mask (to be OR-ed). */
  230. static inline void adv7511_pktmem_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask, u8 val_mask)
  231. {
  232. adv7511_pktmem_wr(sd, reg, (adv7511_pktmem_rd(sd, reg) & clr_mask) | val_mask);
  233. }
  234. static inline bool adv7511_have_hotplug(struct v4l2_subdev *sd)
  235. {
  236. return adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT;
  237. }
  238. static inline bool adv7511_have_rx_sense(struct v4l2_subdev *sd)
  239. {
  240. return adv7511_rd(sd, 0x42) & MASK_ADV7511_MSEN_DETECT;
  241. }
  242. static void adv7511_csc_conversion_mode(struct v4l2_subdev *sd, u8 mode)
  243. {
  244. adv7511_wr_and_or(sd, 0x18, 0x9f, (mode & 0x3)<<5);
  245. }
  246. static void adv7511_csc_coeff(struct v4l2_subdev *sd,
  247. u16 A1, u16 A2, u16 A3, u16 A4,
  248. u16 B1, u16 B2, u16 B3, u16 B4,
  249. u16 C1, u16 C2, u16 C3, u16 C4)
  250. {
  251. /* A */
  252. adv7511_wr_and_or(sd, 0x18, 0xe0, A1>>8);
  253. adv7511_wr(sd, 0x19, A1);
  254. adv7511_wr_and_or(sd, 0x1A, 0xe0, A2>>8);
  255. adv7511_wr(sd, 0x1B, A2);
  256. adv7511_wr_and_or(sd, 0x1c, 0xe0, A3>>8);
  257. adv7511_wr(sd, 0x1d, A3);
  258. adv7511_wr_and_or(sd, 0x1e, 0xe0, A4>>8);
  259. adv7511_wr(sd, 0x1f, A4);
  260. /* B */
  261. adv7511_wr_and_or(sd, 0x20, 0xe0, B1>>8);
  262. adv7511_wr(sd, 0x21, B1);
  263. adv7511_wr_and_or(sd, 0x22, 0xe0, B2>>8);
  264. adv7511_wr(sd, 0x23, B2);
  265. adv7511_wr_and_or(sd, 0x24, 0xe0, B3>>8);
  266. adv7511_wr(sd, 0x25, B3);
  267. adv7511_wr_and_or(sd, 0x26, 0xe0, B4>>8);
  268. adv7511_wr(sd, 0x27, B4);
  269. /* C */
  270. adv7511_wr_and_or(sd, 0x28, 0xe0, C1>>8);
  271. adv7511_wr(sd, 0x29, C1);
  272. adv7511_wr_and_or(sd, 0x2A, 0xe0, C2>>8);
  273. adv7511_wr(sd, 0x2B, C2);
  274. adv7511_wr_and_or(sd, 0x2C, 0xe0, C3>>8);
  275. adv7511_wr(sd, 0x2D, C3);
  276. adv7511_wr_and_or(sd, 0x2E, 0xe0, C4>>8);
  277. adv7511_wr(sd, 0x2F, C4);
  278. }
  279. static void adv7511_csc_rgb_full2limit(struct v4l2_subdev *sd, bool enable)
  280. {
  281. if (enable) {
  282. u8 csc_mode = 0;
  283. adv7511_csc_conversion_mode(sd, csc_mode);
  284. adv7511_csc_coeff(sd,
  285. 4096-564, 0, 0, 256,
  286. 0, 4096-564, 0, 256,
  287. 0, 0, 4096-564, 256);
  288. /* enable CSC */
  289. adv7511_wr_and_or(sd, 0x18, 0x7f, 0x80);
  290. /* AVI infoframe: Limited range RGB (16-235) */
  291. adv7511_wr_and_or(sd, 0x57, 0xf3, 0x04);
  292. } else {
  293. /* disable CSC */
  294. adv7511_wr_and_or(sd, 0x18, 0x7f, 0x0);
  295. /* AVI infoframe: Full range RGB (0-255) */
  296. adv7511_wr_and_or(sd, 0x57, 0xf3, 0x08);
  297. }
  298. }
  299. static void adv7511_set_IT_content_AVI_InfoFrame(struct v4l2_subdev *sd)
  300. {
  301. struct adv7511_state *state = get_adv7511_state(sd);
  302. if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
  303. /* CE format, not IT */
  304. adv7511_wr_and_or(sd, 0x57, 0x7f, 0x00);
  305. } else {
  306. /* IT format */
  307. adv7511_wr_and_or(sd, 0x57, 0x7f, 0x80);
  308. }
  309. }
  310. static int adv7511_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2_ctrl *ctrl)
  311. {
  312. switch (ctrl->val) {
  313. default:
  314. return -EINVAL;
  315. break;
  316. case V4L2_DV_RGB_RANGE_AUTO: {
  317. /* automatic */
  318. struct adv7511_state *state = get_adv7511_state(sd);
  319. if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
  320. /* CE format, RGB limited range (16-235) */
  321. adv7511_csc_rgb_full2limit(sd, true);
  322. } else {
  323. /* not CE format, RGB full range (0-255) */
  324. adv7511_csc_rgb_full2limit(sd, false);
  325. }
  326. }
  327. break;
  328. case V4L2_DV_RGB_RANGE_LIMITED:
  329. /* RGB limited range (16-235) */
  330. adv7511_csc_rgb_full2limit(sd, true);
  331. break;
  332. case V4L2_DV_RGB_RANGE_FULL:
  333. /* RGB full range (0-255) */
  334. adv7511_csc_rgb_full2limit(sd, false);
  335. break;
  336. }
  337. return 0;
  338. }
  339. /* ------------------------------ CTRL OPS ------------------------------ */
  340. static int adv7511_s_ctrl(struct v4l2_ctrl *ctrl)
  341. {
  342. struct v4l2_subdev *sd = to_sd(ctrl);
  343. struct adv7511_state *state = get_adv7511_state(sd);
  344. v4l2_dbg(1, debug, sd, "%s: ctrl id: %d, ctrl->val %d\n", __func__, ctrl->id, ctrl->val);
  345. if (state->hdmi_mode_ctrl == ctrl) {
  346. /* Set HDMI or DVI-D */
  347. adv7511_wr_and_or(sd, 0xaf, 0xfd, ctrl->val == V4L2_DV_TX_MODE_HDMI ? 0x02 : 0x00);
  348. return 0;
  349. }
  350. if (state->rgb_quantization_range_ctrl == ctrl)
  351. return adv7511_set_rgb_quantization_mode(sd, ctrl);
  352. return -EINVAL;
  353. }
  354. static const struct v4l2_ctrl_ops adv7511_ctrl_ops = {
  355. .s_ctrl = adv7511_s_ctrl,
  356. };
  357. /* ---------------------------- CORE OPS ------------------------------------------- */
  358. #ifdef CONFIG_VIDEO_ADV_DEBUG
  359. static void adv7511_inv_register(struct v4l2_subdev *sd)
  360. {
  361. v4l2_info(sd, "0x000-0x0ff: Main Map\n");
  362. }
  363. static int adv7511_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  364. {
  365. reg->size = 1;
  366. switch (reg->reg >> 8) {
  367. case 0:
  368. reg->val = adv7511_rd(sd, reg->reg & 0xff);
  369. break;
  370. default:
  371. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  372. adv7511_inv_register(sd);
  373. break;
  374. }
  375. return 0;
  376. }
  377. static int adv7511_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  378. {
  379. switch (reg->reg >> 8) {
  380. case 0:
  381. adv7511_wr(sd, reg->reg & 0xff, reg->val & 0xff);
  382. break;
  383. default:
  384. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  385. adv7511_inv_register(sd);
  386. break;
  387. }
  388. return 0;
  389. }
  390. #endif
  391. struct adv7511_cfg_read_infoframe {
  392. const char *desc;
  393. u8 present_reg;
  394. u8 present_mask;
  395. u8 header[3];
  396. u16 payload_addr;
  397. };
  398. static u8 hdmi_infoframe_checksum(u8 *ptr, size_t size)
  399. {
  400. u8 csum = 0;
  401. size_t i;
  402. /* compute checksum */
  403. for (i = 0; i < size; i++)
  404. csum += ptr[i];
  405. return 256 - csum;
  406. }
  407. static void log_infoframe(struct v4l2_subdev *sd, const struct adv7511_cfg_read_infoframe *cri)
  408. {
  409. struct i2c_client *client = v4l2_get_subdevdata(sd);
  410. struct device *dev = &client->dev;
  411. union hdmi_infoframe frame;
  412. u8 buffer[32];
  413. u8 len;
  414. int i;
  415. if (!(adv7511_rd(sd, cri->present_reg) & cri->present_mask)) {
  416. v4l2_info(sd, "%s infoframe not transmitted\n", cri->desc);
  417. return;
  418. }
  419. memcpy(buffer, cri->header, sizeof(cri->header));
  420. len = buffer[2];
  421. if (len + 4 > sizeof(buffer)) {
  422. v4l2_err(sd, "%s: invalid %s infoframe length %d\n", __func__, cri->desc, len);
  423. return;
  424. }
  425. if (cri->payload_addr >= 0x100) {
  426. for (i = 0; i < len; i++)
  427. buffer[i + 4] = adv7511_pktmem_rd(sd, cri->payload_addr + i - 0x100);
  428. } else {
  429. for (i = 0; i < len; i++)
  430. buffer[i + 4] = adv7511_rd(sd, cri->payload_addr + i);
  431. }
  432. buffer[3] = 0;
  433. buffer[3] = hdmi_infoframe_checksum(buffer, len + 4);
  434. if (hdmi_infoframe_unpack(&frame, buffer) < 0) {
  435. v4l2_err(sd, "%s: unpack of %s infoframe failed\n", __func__, cri->desc);
  436. return;
  437. }
  438. hdmi_infoframe_log(KERN_INFO, dev, &frame);
  439. }
  440. static void adv7511_log_infoframes(struct v4l2_subdev *sd)
  441. {
  442. static const struct adv7511_cfg_read_infoframe cri[] = {
  443. { "AVI", 0x44, 0x10, { 0x82, 2, 13 }, 0x55 },
  444. { "Audio", 0x44, 0x08, { 0x84, 1, 10 }, 0x73 },
  445. { "SDP", 0x40, 0x40, { 0x83, 1, 25 }, 0x103 },
  446. };
  447. int i;
  448. for (i = 0; i < ARRAY_SIZE(cri); i++)
  449. log_infoframe(sd, &cri[i]);
  450. }
  451. static int adv7511_log_status(struct v4l2_subdev *sd)
  452. {
  453. struct adv7511_state *state = get_adv7511_state(sd);
  454. struct adv7511_state_edid *edid = &state->edid;
  455. static const char * const states[] = {
  456. "in reset",
  457. "reading EDID",
  458. "idle",
  459. "initializing HDCP",
  460. "HDCP enabled",
  461. "initializing HDCP repeater",
  462. "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
  463. };
  464. static const char * const errors[] = {
  465. "no error",
  466. "bad receiver BKSV",
  467. "Ri mismatch",
  468. "Pj mismatch",
  469. "i2c error",
  470. "timed out",
  471. "max repeater cascade exceeded",
  472. "hash check failed",
  473. "too many devices",
  474. "9", "A", "B", "C", "D", "E", "F"
  475. };
  476. v4l2_info(sd, "power %s\n", state->power_on ? "on" : "off");
  477. v4l2_info(sd, "%s hotplug, %s Rx Sense, %s EDID (%d block(s))\n",
  478. (adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT) ? "detected" : "no",
  479. (adv7511_rd(sd, 0x42) & MASK_ADV7511_MSEN_DETECT) ? "detected" : "no",
  480. edid->segments ? "found" : "no",
  481. edid->blocks);
  482. v4l2_info(sd, "%s output %s\n",
  483. (adv7511_rd(sd, 0xaf) & 0x02) ?
  484. "HDMI" : "DVI-D",
  485. (adv7511_rd(sd, 0xa1) & 0x3c) ?
  486. "disabled" : "enabled");
  487. v4l2_info(sd, "state: %s, error: %s, detect count: %u, msk/irq: %02x/%02x\n",
  488. states[adv7511_rd(sd, 0xc8) & 0xf],
  489. errors[adv7511_rd(sd, 0xc8) >> 4], state->edid_detect_counter,
  490. adv7511_rd(sd, 0x94), adv7511_rd(sd, 0x96));
  491. v4l2_info(sd, "RGB quantization: %s range\n", adv7511_rd(sd, 0x18) & 0x80 ? "limited" : "full");
  492. if (adv7511_rd(sd, 0xaf) & 0x02) {
  493. /* HDMI only */
  494. u8 manual_cts = adv7511_rd(sd, 0x0a) & 0x80;
  495. u32 N = (adv7511_rd(sd, 0x01) & 0xf) << 16 |
  496. adv7511_rd(sd, 0x02) << 8 |
  497. adv7511_rd(sd, 0x03);
  498. u8 vic_detect = adv7511_rd(sd, 0x3e) >> 2;
  499. u8 vic_sent = adv7511_rd(sd, 0x3d) & 0x3f;
  500. u32 CTS;
  501. if (manual_cts)
  502. CTS = (adv7511_rd(sd, 0x07) & 0xf) << 16 |
  503. adv7511_rd(sd, 0x08) << 8 |
  504. adv7511_rd(sd, 0x09);
  505. else
  506. CTS = (adv7511_rd(sd, 0x04) & 0xf) << 16 |
  507. adv7511_rd(sd, 0x05) << 8 |
  508. adv7511_rd(sd, 0x06);
  509. v4l2_info(sd, "CTS %s mode: N %d, CTS %d\n",
  510. manual_cts ? "manual" : "automatic", N, CTS);
  511. v4l2_info(sd, "VIC: detected %d, sent %d\n",
  512. vic_detect, vic_sent);
  513. adv7511_log_infoframes(sd);
  514. }
  515. if (state->dv_timings.type == V4L2_DV_BT_656_1120)
  516. v4l2_print_dv_timings(sd->name, "timings: ",
  517. &state->dv_timings, false);
  518. else
  519. v4l2_info(sd, "no timings set\n");
  520. v4l2_info(sd, "i2c edid addr: 0x%x\n", state->i2c_edid_addr);
  521. v4l2_info(sd, "i2c cec addr: 0x%x\n", state->i2c_cec_addr);
  522. v4l2_info(sd, "i2c pktmem addr: 0x%x\n", state->i2c_pktmem_addr);
  523. return 0;
  524. }
  525. /* Power up/down adv7511 */
  526. static int adv7511_s_power(struct v4l2_subdev *sd, int on)
  527. {
  528. struct adv7511_state *state = get_adv7511_state(sd);
  529. const int retries = 20;
  530. int i;
  531. v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
  532. state->power_on = on;
  533. if (!on) {
  534. /* Power down */
  535. adv7511_wr_and_or(sd, 0x41, 0xbf, 0x40);
  536. return true;
  537. }
  538. /* Power up */
  539. /* The adv7511 does not always come up immediately.
  540. Retry multiple times. */
  541. for (i = 0; i < retries; i++) {
  542. adv7511_wr_and_or(sd, 0x41, 0xbf, 0x0);
  543. if ((adv7511_rd(sd, 0x41) & 0x40) == 0)
  544. break;
  545. adv7511_wr_and_or(sd, 0x41, 0xbf, 0x40);
  546. msleep(10);
  547. }
  548. if (i == retries) {
  549. v4l2_dbg(1, debug, sd, "%s: failed to powerup the adv7511!\n", __func__);
  550. adv7511_s_power(sd, 0);
  551. return false;
  552. }
  553. if (i > 1)
  554. v4l2_dbg(1, debug, sd, "%s: needed %d retries to powerup the adv7511\n", __func__, i);
  555. /* Reserved registers that must be set */
  556. adv7511_wr(sd, 0x98, 0x03);
  557. adv7511_wr_and_or(sd, 0x9a, 0xfe, 0x70);
  558. adv7511_wr(sd, 0x9c, 0x30);
  559. adv7511_wr_and_or(sd, 0x9d, 0xfc, 0x01);
  560. adv7511_wr(sd, 0xa2, 0xa4);
  561. adv7511_wr(sd, 0xa3, 0xa4);
  562. adv7511_wr(sd, 0xe0, 0xd0);
  563. adv7511_wr(sd, 0xf9, 0x00);
  564. adv7511_wr(sd, 0x43, state->i2c_edid_addr);
  565. adv7511_wr(sd, 0x45, state->i2c_pktmem_addr);
  566. /* Set number of attempts to read the EDID */
  567. adv7511_wr(sd, 0xc9, 0xf);
  568. return true;
  569. }
  570. /* Enable interrupts */
  571. static void adv7511_set_isr(struct v4l2_subdev *sd, bool enable)
  572. {
  573. u8 irqs = MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT;
  574. u8 irqs_rd;
  575. int retries = 100;
  576. v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ? "enable" : "disable");
  577. /* The datasheet says that the EDID ready interrupt should be
  578. disabled if there is no hotplug. */
  579. if (!enable)
  580. irqs = 0;
  581. else if (adv7511_have_hotplug(sd))
  582. irqs |= MASK_ADV7511_EDID_RDY_INT;
  583. /*
  584. * This i2c write can fail (approx. 1 in 1000 writes). But it
  585. * is essential that this register is correct, so retry it
  586. * multiple times.
  587. *
  588. * Note that the i2c write does not report an error, but the readback
  589. * clearly shows the wrong value.
  590. */
  591. do {
  592. adv7511_wr(sd, 0x94, irqs);
  593. irqs_rd = adv7511_rd(sd, 0x94);
  594. } while (retries-- && irqs_rd != irqs);
  595. if (irqs_rd == irqs)
  596. return;
  597. v4l2_err(sd, "Could not set interrupts: hw failure?\n");
  598. }
  599. /* Interrupt handler */
  600. static int adv7511_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  601. {
  602. u8 irq_status;
  603. /* disable interrupts to prevent a race condition */
  604. adv7511_set_isr(sd, false);
  605. irq_status = adv7511_rd(sd, 0x96);
  606. /* clear detected interrupts */
  607. adv7511_wr(sd, 0x96, irq_status);
  608. v4l2_dbg(1, debug, sd, "%s: irq 0x%x\n", __func__, irq_status);
  609. if (irq_status & (MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT))
  610. adv7511_check_monitor_present_status(sd);
  611. if (irq_status & MASK_ADV7511_EDID_RDY_INT)
  612. adv7511_check_edid_status(sd);
  613. /* enable interrupts */
  614. adv7511_set_isr(sd, true);
  615. if (handled)
  616. *handled = true;
  617. return 0;
  618. }
  619. static const struct v4l2_subdev_core_ops adv7511_core_ops = {
  620. .log_status = adv7511_log_status,
  621. #ifdef CONFIG_VIDEO_ADV_DEBUG
  622. .g_register = adv7511_g_register,
  623. .s_register = adv7511_s_register,
  624. #endif
  625. .s_power = adv7511_s_power,
  626. .interrupt_service_routine = adv7511_isr,
  627. };
  628. /* ------------------------------ VIDEO OPS ------------------------------ */
  629. /* Enable/disable adv7511 output */
  630. static int adv7511_s_stream(struct v4l2_subdev *sd, int enable)
  631. {
  632. struct adv7511_state *state = get_adv7511_state(sd);
  633. v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis"));
  634. adv7511_wr_and_or(sd, 0xa1, ~0x3c, (enable ? 0 : 0x3c));
  635. if (enable) {
  636. adv7511_check_monitor_present_status(sd);
  637. } else {
  638. adv7511_s_power(sd, 0);
  639. state->have_monitor = false;
  640. }
  641. return 0;
  642. }
  643. static int adv7511_s_dv_timings(struct v4l2_subdev *sd,
  644. struct v4l2_dv_timings *timings)
  645. {
  646. struct adv7511_state *state = get_adv7511_state(sd);
  647. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  648. /* quick sanity check */
  649. if (!v4l2_valid_dv_timings(timings, &adv7511_timings_cap, NULL, NULL))
  650. return -EINVAL;
  651. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  652. if the format is one of the CEA or DMT timings. */
  653. v4l2_find_dv_timings_cap(timings, &adv7511_timings_cap, 0, NULL, NULL);
  654. timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
  655. /* save timings */
  656. state->dv_timings = *timings;
  657. /* update quantization range based on new dv_timings */
  658. adv7511_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl);
  659. /* update AVI infoframe */
  660. adv7511_set_IT_content_AVI_InfoFrame(sd);
  661. return 0;
  662. }
  663. static int adv7511_g_dv_timings(struct v4l2_subdev *sd,
  664. struct v4l2_dv_timings *timings)
  665. {
  666. struct adv7511_state *state = get_adv7511_state(sd);
  667. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  668. if (!timings)
  669. return -EINVAL;
  670. *timings = state->dv_timings;
  671. return 0;
  672. }
  673. static int adv7511_enum_dv_timings(struct v4l2_subdev *sd,
  674. struct v4l2_enum_dv_timings *timings)
  675. {
  676. if (timings->pad != 0)
  677. return -EINVAL;
  678. return v4l2_enum_dv_timings_cap(timings, &adv7511_timings_cap, NULL, NULL);
  679. }
  680. static int adv7511_dv_timings_cap(struct v4l2_subdev *sd,
  681. struct v4l2_dv_timings_cap *cap)
  682. {
  683. if (cap->pad != 0)
  684. return -EINVAL;
  685. *cap = adv7511_timings_cap;
  686. return 0;
  687. }
  688. static const struct v4l2_subdev_video_ops adv7511_video_ops = {
  689. .s_stream = adv7511_s_stream,
  690. .s_dv_timings = adv7511_s_dv_timings,
  691. .g_dv_timings = adv7511_g_dv_timings,
  692. };
  693. /* ------------------------------ AUDIO OPS ------------------------------ */
  694. static int adv7511_s_audio_stream(struct v4l2_subdev *sd, int enable)
  695. {
  696. v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis"));
  697. if (enable)
  698. adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x80);
  699. else
  700. adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x40);
  701. return 0;
  702. }
  703. static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  704. {
  705. u32 N;
  706. switch (freq) {
  707. case 32000: N = 4096; break;
  708. case 44100: N = 6272; break;
  709. case 48000: N = 6144; break;
  710. case 88200: N = 12544; break;
  711. case 96000: N = 12288; break;
  712. case 176400: N = 25088; break;
  713. case 192000: N = 24576; break;
  714. default:
  715. return -EINVAL;
  716. }
  717. /* Set N (used with CTS to regenerate the audio clock) */
  718. adv7511_wr(sd, 0x01, (N >> 16) & 0xf);
  719. adv7511_wr(sd, 0x02, (N >> 8) & 0xff);
  720. adv7511_wr(sd, 0x03, N & 0xff);
  721. return 0;
  722. }
  723. static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  724. {
  725. u32 i2s_sf;
  726. switch (freq) {
  727. case 32000: i2s_sf = 0x30; break;
  728. case 44100: i2s_sf = 0x00; break;
  729. case 48000: i2s_sf = 0x20; break;
  730. case 88200: i2s_sf = 0x80; break;
  731. case 96000: i2s_sf = 0xa0; break;
  732. case 176400: i2s_sf = 0xc0; break;
  733. case 192000: i2s_sf = 0xe0; break;
  734. default:
  735. return -EINVAL;
  736. }
  737. /* Set sampling frequency for I2S audio to 48 kHz */
  738. adv7511_wr_and_or(sd, 0x15, 0xf, i2s_sf);
  739. return 0;
  740. }
  741. static int adv7511_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, u32 config)
  742. {
  743. /* Only 2 channels in use for application */
  744. adv7511_wr_and_or(sd, 0x73, 0xf8, 0x1);
  745. /* Speaker mapping */
  746. adv7511_wr(sd, 0x76, 0x00);
  747. /* 16 bit audio word length */
  748. adv7511_wr_and_or(sd, 0x14, 0xf0, 0x02);
  749. return 0;
  750. }
  751. static const struct v4l2_subdev_audio_ops adv7511_audio_ops = {
  752. .s_stream = adv7511_s_audio_stream,
  753. .s_clock_freq = adv7511_s_clock_freq,
  754. .s_i2s_clock_freq = adv7511_s_i2s_clock_freq,
  755. .s_routing = adv7511_s_routing,
  756. };
  757. /* ---------------------------- PAD OPS ------------------------------------- */
  758. static int adv7511_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  759. {
  760. struct adv7511_state *state = get_adv7511_state(sd);
  761. memset(edid->reserved, 0, sizeof(edid->reserved));
  762. if (edid->pad != 0)
  763. return -EINVAL;
  764. if (edid->start_block == 0 && edid->blocks == 0) {
  765. edid->blocks = state->edid.segments * 2;
  766. return 0;
  767. }
  768. if (state->edid.segments == 0)
  769. return -ENODATA;
  770. if (edid->start_block >= state->edid.segments * 2)
  771. return -EINVAL;
  772. if (edid->start_block + edid->blocks > state->edid.segments * 2)
  773. edid->blocks = state->edid.segments * 2 - edid->start_block;
  774. memcpy(edid->edid, &state->edid.data[edid->start_block * 128],
  775. 128 * edid->blocks);
  776. return 0;
  777. }
  778. static int adv7511_enum_mbus_code(struct v4l2_subdev *sd,
  779. struct v4l2_subdev_pad_config *cfg,
  780. struct v4l2_subdev_mbus_code_enum *code)
  781. {
  782. if (code->pad != 0)
  783. return -EINVAL;
  784. switch (code->index) {
  785. case 0:
  786. code->code = MEDIA_BUS_FMT_RGB888_1X24;
  787. break;
  788. case 1:
  789. code->code = MEDIA_BUS_FMT_YUYV8_1X16;
  790. break;
  791. case 2:
  792. code->code = MEDIA_BUS_FMT_UYVY8_1X16;
  793. break;
  794. default:
  795. return -EINVAL;
  796. }
  797. return 0;
  798. }
  799. static void adv7511_fill_format(struct adv7511_state *state,
  800. struct v4l2_mbus_framefmt *format)
  801. {
  802. memset(format, 0, sizeof(*format));
  803. format->width = state->dv_timings.bt.width;
  804. format->height = state->dv_timings.bt.height;
  805. format->field = V4L2_FIELD_NONE;
  806. }
  807. static int adv7511_get_fmt(struct v4l2_subdev *sd,
  808. struct v4l2_subdev_pad_config *cfg,
  809. struct v4l2_subdev_format *format)
  810. {
  811. struct adv7511_state *state = get_adv7511_state(sd);
  812. if (format->pad != 0)
  813. return -EINVAL;
  814. adv7511_fill_format(state, &format->format);
  815. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  816. struct v4l2_mbus_framefmt *fmt;
  817. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  818. format->format.code = fmt->code;
  819. format->format.colorspace = fmt->colorspace;
  820. format->format.ycbcr_enc = fmt->ycbcr_enc;
  821. format->format.quantization = fmt->quantization;
  822. format->format.xfer_func = fmt->xfer_func;
  823. } else {
  824. format->format.code = state->fmt_code;
  825. format->format.colorspace = state->colorspace;
  826. format->format.ycbcr_enc = state->ycbcr_enc;
  827. format->format.quantization = state->quantization;
  828. format->format.xfer_func = state->xfer_func;
  829. }
  830. return 0;
  831. }
  832. static int adv7511_set_fmt(struct v4l2_subdev *sd,
  833. struct v4l2_subdev_pad_config *cfg,
  834. struct v4l2_subdev_format *format)
  835. {
  836. struct adv7511_state *state = get_adv7511_state(sd);
  837. /*
  838. * Bitfield namings come the CEA-861-F standard, table 8 "Auxiliary
  839. * Video Information (AVI) InfoFrame Format"
  840. *
  841. * c = Colorimetry
  842. * ec = Extended Colorimetry
  843. * y = RGB or YCbCr
  844. * q = RGB Quantization Range
  845. * yq = YCC Quantization Range
  846. */
  847. u8 c = HDMI_COLORIMETRY_NONE;
  848. u8 ec = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  849. u8 y = HDMI_COLORSPACE_RGB;
  850. u8 q = HDMI_QUANTIZATION_RANGE_DEFAULT;
  851. u8 yq = HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
  852. if (format->pad != 0)
  853. return -EINVAL;
  854. switch (format->format.code) {
  855. case MEDIA_BUS_FMT_UYVY8_1X16:
  856. case MEDIA_BUS_FMT_YUYV8_1X16:
  857. case MEDIA_BUS_FMT_RGB888_1X24:
  858. break;
  859. default:
  860. return -EINVAL;
  861. }
  862. adv7511_fill_format(state, &format->format);
  863. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  864. struct v4l2_mbus_framefmt *fmt;
  865. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  866. fmt->code = format->format.code;
  867. fmt->colorspace = format->format.colorspace;
  868. fmt->ycbcr_enc = format->format.ycbcr_enc;
  869. fmt->quantization = format->format.quantization;
  870. fmt->xfer_func = format->format.xfer_func;
  871. return 0;
  872. }
  873. switch (format->format.code) {
  874. case MEDIA_BUS_FMT_UYVY8_1X16:
  875. adv7511_wr_and_or(sd, 0x15, 0xf0, 0x01);
  876. adv7511_wr_and_or(sd, 0x16, 0x03, 0xb8);
  877. y = HDMI_COLORSPACE_YUV422;
  878. break;
  879. case MEDIA_BUS_FMT_YUYV8_1X16:
  880. adv7511_wr_and_or(sd, 0x15, 0xf0, 0x01);
  881. adv7511_wr_and_or(sd, 0x16, 0x03, 0xbc);
  882. y = HDMI_COLORSPACE_YUV422;
  883. break;
  884. case MEDIA_BUS_FMT_RGB888_1X24:
  885. default:
  886. adv7511_wr_and_or(sd, 0x15, 0xf0, 0x00);
  887. adv7511_wr_and_or(sd, 0x16, 0x03, 0x00);
  888. break;
  889. }
  890. state->fmt_code = format->format.code;
  891. state->colorspace = format->format.colorspace;
  892. state->ycbcr_enc = format->format.ycbcr_enc;
  893. state->quantization = format->format.quantization;
  894. state->xfer_func = format->format.xfer_func;
  895. switch (format->format.colorspace) {
  896. case V4L2_COLORSPACE_ADOBERGB:
  897. c = HDMI_COLORIMETRY_EXTENDED;
  898. ec = y ? HDMI_EXTENDED_COLORIMETRY_ADOBE_YCC_601 :
  899. HDMI_EXTENDED_COLORIMETRY_ADOBE_RGB;
  900. break;
  901. case V4L2_COLORSPACE_SMPTE170M:
  902. c = y ? HDMI_COLORIMETRY_ITU_601 : HDMI_COLORIMETRY_NONE;
  903. if (y && format->format.ycbcr_enc == V4L2_YCBCR_ENC_XV601) {
  904. c = HDMI_COLORIMETRY_EXTENDED;
  905. ec = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  906. }
  907. break;
  908. case V4L2_COLORSPACE_REC709:
  909. c = y ? HDMI_COLORIMETRY_ITU_709 : HDMI_COLORIMETRY_NONE;
  910. if (y && format->format.ycbcr_enc == V4L2_YCBCR_ENC_XV709) {
  911. c = HDMI_COLORIMETRY_EXTENDED;
  912. ec = HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
  913. }
  914. break;
  915. case V4L2_COLORSPACE_SRGB:
  916. c = y ? HDMI_COLORIMETRY_EXTENDED : HDMI_COLORIMETRY_NONE;
  917. ec = y ? HDMI_EXTENDED_COLORIMETRY_S_YCC_601 :
  918. HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  919. break;
  920. case V4L2_COLORSPACE_BT2020:
  921. c = HDMI_COLORIMETRY_EXTENDED;
  922. if (y && format->format.ycbcr_enc == V4L2_YCBCR_ENC_BT2020_CONST_LUM)
  923. ec = 5; /* Not yet available in hdmi.h */
  924. else
  925. ec = 6; /* Not yet available in hdmi.h */
  926. break;
  927. default:
  928. break;
  929. }
  930. /*
  931. * CEA-861-F says that for RGB formats the YCC range must match the
  932. * RGB range, although sources should ignore the YCC range.
  933. *
  934. * The RGB quantization range shouldn't be non-zero if the EDID doesn't
  935. * have the Q bit set in the Video Capabilities Data Block, however this
  936. * isn't checked at the moment. The assumption is that the application
  937. * knows the EDID and can detect this.
  938. *
  939. * The same is true for the YCC quantization range: non-standard YCC
  940. * quantization ranges should only be sent if the EDID has the YQ bit
  941. * set in the Video Capabilities Data Block.
  942. */
  943. switch (format->format.quantization) {
  944. case V4L2_QUANTIZATION_FULL_RANGE:
  945. q = y ? HDMI_QUANTIZATION_RANGE_DEFAULT :
  946. HDMI_QUANTIZATION_RANGE_FULL;
  947. yq = q ? q - 1 : HDMI_YCC_QUANTIZATION_RANGE_FULL;
  948. break;
  949. case V4L2_QUANTIZATION_LIM_RANGE:
  950. q = y ? HDMI_QUANTIZATION_RANGE_DEFAULT :
  951. HDMI_QUANTIZATION_RANGE_LIMITED;
  952. yq = q ? q - 1 : HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
  953. break;
  954. }
  955. adv7511_wr_and_or(sd, 0x4a, 0xbf, 0);
  956. adv7511_wr_and_or(sd, 0x55, 0x9f, y << 5);
  957. adv7511_wr_and_or(sd, 0x56, 0x3f, c << 6);
  958. adv7511_wr_and_or(sd, 0x57, 0x83, (ec << 4) | (q << 2));
  959. adv7511_wr_and_or(sd, 0x59, 0x0f, yq << 4);
  960. adv7511_wr_and_or(sd, 0x4a, 0xff, 1);
  961. return 0;
  962. }
  963. static const struct v4l2_subdev_pad_ops adv7511_pad_ops = {
  964. .get_edid = adv7511_get_edid,
  965. .enum_mbus_code = adv7511_enum_mbus_code,
  966. .get_fmt = adv7511_get_fmt,
  967. .set_fmt = adv7511_set_fmt,
  968. .enum_dv_timings = adv7511_enum_dv_timings,
  969. .dv_timings_cap = adv7511_dv_timings_cap,
  970. };
  971. /* --------------------- SUBDEV OPS --------------------------------------- */
  972. static const struct v4l2_subdev_ops adv7511_ops = {
  973. .core = &adv7511_core_ops,
  974. .pad = &adv7511_pad_ops,
  975. .video = &adv7511_video_ops,
  976. .audio = &adv7511_audio_ops,
  977. };
  978. /* ----------------------------------------------------------------------- */
  979. static void adv7511_dbg_dump_edid(int lvl, int debug, struct v4l2_subdev *sd, int segment, u8 *buf)
  980. {
  981. if (debug >= lvl) {
  982. int i, j;
  983. v4l2_dbg(lvl, debug, sd, "edid segment %d\n", segment);
  984. for (i = 0; i < 256; i += 16) {
  985. u8 b[128];
  986. u8 *bp = b;
  987. if (i == 128)
  988. v4l2_dbg(lvl, debug, sd, "\n");
  989. for (j = i; j < i + 16; j++) {
  990. sprintf(bp, "0x%02x, ", buf[j]);
  991. bp += 6;
  992. }
  993. bp[0] = '\0';
  994. v4l2_dbg(lvl, debug, sd, "%s\n", b);
  995. }
  996. }
  997. }
  998. static void adv7511_notify_no_edid(struct v4l2_subdev *sd)
  999. {
  1000. struct adv7511_state *state = get_adv7511_state(sd);
  1001. struct adv7511_edid_detect ed;
  1002. /* We failed to read the EDID, so send an event for this. */
  1003. ed.present = false;
  1004. ed.segment = adv7511_rd(sd, 0xc4);
  1005. v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed);
  1006. v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, 0x0);
  1007. }
  1008. static void adv7511_edid_handler(struct work_struct *work)
  1009. {
  1010. struct delayed_work *dwork = to_delayed_work(work);
  1011. struct adv7511_state *state = container_of(dwork, struct adv7511_state, edid_handler);
  1012. struct v4l2_subdev *sd = &state->sd;
  1013. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  1014. if (adv7511_check_edid_status(sd)) {
  1015. /* Return if we received the EDID. */
  1016. return;
  1017. }
  1018. if (adv7511_have_hotplug(sd)) {
  1019. /* We must retry reading the EDID several times, it is possible
  1020. * that initially the EDID couldn't be read due to i2c errors
  1021. * (DVI connectors are particularly prone to this problem). */
  1022. if (state->edid.read_retries) {
  1023. state->edid.read_retries--;
  1024. v4l2_dbg(1, debug, sd, "%s: edid read failed\n", __func__);
  1025. state->have_monitor = false;
  1026. adv7511_s_power(sd, false);
  1027. adv7511_s_power(sd, true);
  1028. queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
  1029. return;
  1030. }
  1031. }
  1032. /* We failed to read the EDID, so send an event for this. */
  1033. adv7511_notify_no_edid(sd);
  1034. v4l2_dbg(1, debug, sd, "%s: no edid found\n", __func__);
  1035. }
  1036. static void adv7511_audio_setup(struct v4l2_subdev *sd)
  1037. {
  1038. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  1039. adv7511_s_i2s_clock_freq(sd, 48000);
  1040. adv7511_s_clock_freq(sd, 48000);
  1041. adv7511_s_routing(sd, 0, 0, 0);
  1042. }
  1043. /* Configure hdmi transmitter. */
  1044. static void adv7511_setup(struct v4l2_subdev *sd)
  1045. {
  1046. struct adv7511_state *state = get_adv7511_state(sd);
  1047. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  1048. /* Input format: RGB 4:4:4 */
  1049. adv7511_wr_and_or(sd, 0x15, 0xf0, 0x0);
  1050. /* Output format: RGB 4:4:4 */
  1051. adv7511_wr_and_or(sd, 0x16, 0x7f, 0x0);
  1052. /* 1st order interpolation 4:2:2 -> 4:4:4 up conversion, Aspect ratio: 16:9 */
  1053. adv7511_wr_and_or(sd, 0x17, 0xf9, 0x06);
  1054. /* Disable pixel repetition */
  1055. adv7511_wr_and_or(sd, 0x3b, 0x9f, 0x0);
  1056. /* Disable CSC */
  1057. adv7511_wr_and_or(sd, 0x18, 0x7f, 0x0);
  1058. /* Output format: RGB 4:4:4, Active Format Information is valid,
  1059. * underscanned */
  1060. adv7511_wr_and_or(sd, 0x55, 0x9c, 0x12);
  1061. /* AVI Info frame packet enable, Audio Info frame disable */
  1062. adv7511_wr_and_or(sd, 0x44, 0xe7, 0x10);
  1063. /* Colorimetry, Active format aspect ratio: same as picure. */
  1064. adv7511_wr(sd, 0x56, 0xa8);
  1065. /* No encryption */
  1066. adv7511_wr_and_or(sd, 0xaf, 0xed, 0x0);
  1067. /* Positive clk edge capture for input video clock */
  1068. adv7511_wr_and_or(sd, 0xba, 0x1f, 0x60);
  1069. adv7511_audio_setup(sd);
  1070. v4l2_ctrl_handler_setup(&state->hdl);
  1071. }
  1072. static void adv7511_notify_monitor_detect(struct v4l2_subdev *sd)
  1073. {
  1074. struct adv7511_monitor_detect mdt;
  1075. struct adv7511_state *state = get_adv7511_state(sd);
  1076. mdt.present = state->have_monitor;
  1077. v4l2_subdev_notify(sd, ADV7511_MONITOR_DETECT, (void *)&mdt);
  1078. }
  1079. static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd)
  1080. {
  1081. struct adv7511_state *state = get_adv7511_state(sd);
  1082. /* read hotplug and rx-sense state */
  1083. u8 status = adv7511_rd(sd, 0x42);
  1084. v4l2_dbg(1, debug, sd, "%s: status: 0x%x%s%s\n",
  1085. __func__,
  1086. status,
  1087. status & MASK_ADV7511_HPD_DETECT ? ", hotplug" : "",
  1088. status & MASK_ADV7511_MSEN_DETECT ? ", rx-sense" : "");
  1089. /* update read only ctrls */
  1090. v4l2_ctrl_s_ctrl(state->hotplug_ctrl, adv7511_have_hotplug(sd) ? 0x1 : 0x0);
  1091. v4l2_ctrl_s_ctrl(state->rx_sense_ctrl, adv7511_have_rx_sense(sd) ? 0x1 : 0x0);
  1092. if ((status & MASK_ADV7511_HPD_DETECT) && ((status & MASK_ADV7511_MSEN_DETECT) || state->edid.segments)) {
  1093. v4l2_dbg(1, debug, sd, "%s: hotplug and (rx-sense or edid)\n", __func__);
  1094. if (!state->have_monitor) {
  1095. v4l2_dbg(1, debug, sd, "%s: monitor detected\n", __func__);
  1096. state->have_monitor = true;
  1097. adv7511_set_isr(sd, true);
  1098. if (!adv7511_s_power(sd, true)) {
  1099. v4l2_dbg(1, debug, sd, "%s: monitor detected, powerup failed\n", __func__);
  1100. return;
  1101. }
  1102. adv7511_setup(sd);
  1103. adv7511_notify_monitor_detect(sd);
  1104. state->edid.read_retries = EDID_MAX_RETRIES;
  1105. queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
  1106. }
  1107. } else if (status & MASK_ADV7511_HPD_DETECT) {
  1108. v4l2_dbg(1, debug, sd, "%s: hotplug detected\n", __func__);
  1109. state->edid.read_retries = EDID_MAX_RETRIES;
  1110. queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
  1111. } else if (!(status & MASK_ADV7511_HPD_DETECT)) {
  1112. v4l2_dbg(1, debug, sd, "%s: hotplug not detected\n", __func__);
  1113. if (state->have_monitor) {
  1114. v4l2_dbg(1, debug, sd, "%s: monitor not detected\n", __func__);
  1115. state->have_monitor = false;
  1116. adv7511_notify_monitor_detect(sd);
  1117. }
  1118. adv7511_s_power(sd, false);
  1119. memset(&state->edid, 0, sizeof(struct adv7511_state_edid));
  1120. adv7511_notify_no_edid(sd);
  1121. }
  1122. }
  1123. static bool edid_block_verify_crc(u8 *edid_block)
  1124. {
  1125. u8 sum = 0;
  1126. int i;
  1127. for (i = 0; i < 128; i++)
  1128. sum += edid_block[i];
  1129. return sum == 0;
  1130. }
  1131. static bool edid_verify_crc(struct v4l2_subdev *sd, u32 segment)
  1132. {
  1133. struct adv7511_state *state = get_adv7511_state(sd);
  1134. u32 blocks = state->edid.blocks;
  1135. u8 *data = state->edid.data;
  1136. if (!edid_block_verify_crc(&data[segment * 256]))
  1137. return false;
  1138. if ((segment + 1) * 2 <= blocks)
  1139. return edid_block_verify_crc(&data[segment * 256 + 128]);
  1140. return true;
  1141. }
  1142. static bool edid_verify_header(struct v4l2_subdev *sd, u32 segment)
  1143. {
  1144. static const u8 hdmi_header[] = {
  1145. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
  1146. };
  1147. struct adv7511_state *state = get_adv7511_state(sd);
  1148. u8 *data = state->edid.data;
  1149. if (segment != 0)
  1150. return true;
  1151. return !memcmp(data, hdmi_header, sizeof(hdmi_header));
  1152. }
  1153. static bool adv7511_check_edid_status(struct v4l2_subdev *sd)
  1154. {
  1155. struct adv7511_state *state = get_adv7511_state(sd);
  1156. u8 edidRdy = adv7511_rd(sd, 0xc5);
  1157. v4l2_dbg(1, debug, sd, "%s: edid ready (retries: %d)\n",
  1158. __func__, EDID_MAX_RETRIES - state->edid.read_retries);
  1159. if (state->edid.complete)
  1160. return true;
  1161. if (edidRdy & MASK_ADV7511_EDID_RDY) {
  1162. int segment = adv7511_rd(sd, 0xc4);
  1163. struct adv7511_edid_detect ed;
  1164. if (segment >= EDID_MAX_SEGM) {
  1165. v4l2_err(sd, "edid segment number too big\n");
  1166. return false;
  1167. }
  1168. v4l2_dbg(1, debug, sd, "%s: got segment %d\n", __func__, segment);
  1169. adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]);
  1170. adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]);
  1171. if (segment == 0) {
  1172. state->edid.blocks = state->edid.data[0x7e] + 1;
  1173. v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n", __func__, state->edid.blocks);
  1174. }
  1175. if (!edid_verify_crc(sd, segment) ||
  1176. !edid_verify_header(sd, segment)) {
  1177. /* edid crc error, force reread of edid segment */
  1178. v4l2_err(sd, "%s: edid crc or header error\n", __func__);
  1179. state->have_monitor = false;
  1180. adv7511_s_power(sd, false);
  1181. adv7511_s_power(sd, true);
  1182. return false;
  1183. }
  1184. /* one more segment read ok */
  1185. state->edid.segments = segment + 1;
  1186. v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, 0x1);
  1187. if (((state->edid.data[0x7e] >> 1) + 1) > state->edid.segments) {
  1188. /* Request next EDID segment */
  1189. v4l2_dbg(1, debug, sd, "%s: request segment %d\n", __func__, state->edid.segments);
  1190. adv7511_wr(sd, 0xc9, 0xf);
  1191. adv7511_wr(sd, 0xc4, state->edid.segments);
  1192. state->edid.read_retries = EDID_MAX_RETRIES;
  1193. queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
  1194. return false;
  1195. }
  1196. v4l2_dbg(1, debug, sd, "%s: edid complete with %d segment(s)\n", __func__, state->edid.segments);
  1197. state->edid.complete = true;
  1198. /* report when we have all segments
  1199. but report only for segment 0
  1200. */
  1201. ed.present = true;
  1202. ed.segment = 0;
  1203. state->edid_detect_counter++;
  1204. v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed);
  1205. return ed.present;
  1206. }
  1207. return false;
  1208. }
  1209. /* ----------------------------------------------------------------------- */
  1210. /* Setup ADV7511 */
  1211. static void adv7511_init_setup(struct v4l2_subdev *sd)
  1212. {
  1213. struct adv7511_state *state = get_adv7511_state(sd);
  1214. struct adv7511_state_edid *edid = &state->edid;
  1215. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  1216. /* clear all interrupts */
  1217. adv7511_wr(sd, 0x96, 0xff);
  1218. /*
  1219. * Stop HPD from resetting a lot of registers.
  1220. * It might leave the chip in a partly un-initialized state,
  1221. * in particular with regards to hotplug bounces.
  1222. */
  1223. adv7511_wr_and_or(sd, 0xd6, 0x3f, 0xc0);
  1224. memset(edid, 0, sizeof(struct adv7511_state_edid));
  1225. state->have_monitor = false;
  1226. adv7511_set_isr(sd, false);
  1227. adv7511_s_stream(sd, false);
  1228. adv7511_s_audio_stream(sd, false);
  1229. }
  1230. static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1231. {
  1232. struct adv7511_state *state;
  1233. struct adv7511_platform_data *pdata = client->dev.platform_data;
  1234. struct v4l2_ctrl_handler *hdl;
  1235. struct v4l2_subdev *sd;
  1236. u8 chip_id[2];
  1237. int err = -EIO;
  1238. /* Check if the adapter supports the needed features */
  1239. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1240. return -EIO;
  1241. state = devm_kzalloc(&client->dev, sizeof(struct adv7511_state), GFP_KERNEL);
  1242. if (!state)
  1243. return -ENOMEM;
  1244. /* Platform data */
  1245. if (!pdata) {
  1246. v4l_err(client, "No platform data!\n");
  1247. return -ENODEV;
  1248. }
  1249. memcpy(&state->pdata, pdata, sizeof(state->pdata));
  1250. state->fmt_code = MEDIA_BUS_FMT_RGB888_1X24;
  1251. state->colorspace = V4L2_COLORSPACE_SRGB;
  1252. sd = &state->sd;
  1253. v4l2_dbg(1, debug, sd, "detecting adv7511 client on address 0x%x\n",
  1254. client->addr << 1);
  1255. v4l2_i2c_subdev_init(sd, client, &adv7511_ops);
  1256. hdl = &state->hdl;
  1257. v4l2_ctrl_handler_init(hdl, 10);
  1258. /* add in ascending ID order */
  1259. state->hdmi_mode_ctrl = v4l2_ctrl_new_std_menu(hdl, &adv7511_ctrl_ops,
  1260. V4L2_CID_DV_TX_MODE, V4L2_DV_TX_MODE_HDMI,
  1261. 0, V4L2_DV_TX_MODE_DVI_D);
  1262. state->hotplug_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  1263. V4L2_CID_DV_TX_HOTPLUG, 0, 1, 0, 0);
  1264. state->rx_sense_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  1265. V4L2_CID_DV_TX_RXSENSE, 0, 1, 0, 0);
  1266. state->have_edid0_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  1267. V4L2_CID_DV_TX_EDID_PRESENT, 0, 1, 0, 0);
  1268. state->rgb_quantization_range_ctrl =
  1269. v4l2_ctrl_new_std_menu(hdl, &adv7511_ctrl_ops,
  1270. V4L2_CID_DV_TX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  1271. 0, V4L2_DV_RGB_RANGE_AUTO);
  1272. sd->ctrl_handler = hdl;
  1273. if (hdl->error) {
  1274. err = hdl->error;
  1275. goto err_hdl;
  1276. }
  1277. state->hdmi_mode_ctrl->is_private = true;
  1278. state->hotplug_ctrl->is_private = true;
  1279. state->rx_sense_ctrl->is_private = true;
  1280. state->have_edid0_ctrl->is_private = true;
  1281. state->rgb_quantization_range_ctrl->is_private = true;
  1282. state->pad.flags = MEDIA_PAD_FL_SINK;
  1283. err = media_entity_init(&sd->entity, 1, &state->pad, 0);
  1284. if (err)
  1285. goto err_hdl;
  1286. /* EDID and CEC i2c addr */
  1287. state->i2c_edid_addr = state->pdata.i2c_edid << 1;
  1288. state->i2c_cec_addr = state->pdata.i2c_cec << 1;
  1289. state->i2c_pktmem_addr = state->pdata.i2c_pktmem << 1;
  1290. state->chip_revision = adv7511_rd(sd, 0x0);
  1291. chip_id[0] = adv7511_rd(sd, 0xf5);
  1292. chip_id[1] = adv7511_rd(sd, 0xf6);
  1293. if (chip_id[0] != 0x75 || chip_id[1] != 0x11) {
  1294. v4l2_err(sd, "chip_id != 0x7511, read 0x%02x%02x\n", chip_id[0], chip_id[1]);
  1295. err = -EIO;
  1296. goto err_entity;
  1297. }
  1298. state->i2c_edid = i2c_new_dummy(client->adapter, state->i2c_edid_addr >> 1);
  1299. if (state->i2c_edid == NULL) {
  1300. v4l2_err(sd, "failed to register edid i2c client\n");
  1301. err = -ENOMEM;
  1302. goto err_entity;
  1303. }
  1304. state->i2c_pktmem = i2c_new_dummy(client->adapter, state->i2c_pktmem_addr >> 1);
  1305. if (state->i2c_pktmem == NULL) {
  1306. v4l2_err(sd, "failed to register pktmem i2c client\n");
  1307. err = -ENOMEM;
  1308. goto err_unreg_edid;
  1309. }
  1310. adv7511_wr(sd, 0xe2, 0x01); /* power down cec section */
  1311. state->work_queue = create_singlethread_workqueue(sd->name);
  1312. if (state->work_queue == NULL) {
  1313. v4l2_err(sd, "could not create workqueue\n");
  1314. err = -ENOMEM;
  1315. goto err_unreg_pktmem;
  1316. }
  1317. INIT_DELAYED_WORK(&state->edid_handler, adv7511_edid_handler);
  1318. adv7511_init_setup(sd);
  1319. adv7511_set_isr(sd, true);
  1320. adv7511_check_monitor_present_status(sd);
  1321. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  1322. client->addr << 1, client->adapter->name);
  1323. return 0;
  1324. err_unreg_pktmem:
  1325. i2c_unregister_device(state->i2c_pktmem);
  1326. err_unreg_edid:
  1327. i2c_unregister_device(state->i2c_edid);
  1328. err_entity:
  1329. media_entity_cleanup(&sd->entity);
  1330. err_hdl:
  1331. v4l2_ctrl_handler_free(&state->hdl);
  1332. return err;
  1333. }
  1334. /* ----------------------------------------------------------------------- */
  1335. static int adv7511_remove(struct i2c_client *client)
  1336. {
  1337. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1338. struct adv7511_state *state = get_adv7511_state(sd);
  1339. state->chip_revision = -1;
  1340. v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
  1341. client->addr << 1, client->adapter->name);
  1342. adv7511_init_setup(sd);
  1343. cancel_delayed_work(&state->edid_handler);
  1344. i2c_unregister_device(state->i2c_edid);
  1345. i2c_unregister_device(state->i2c_pktmem);
  1346. destroy_workqueue(state->work_queue);
  1347. v4l2_device_unregister_subdev(sd);
  1348. media_entity_cleanup(&sd->entity);
  1349. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1350. return 0;
  1351. }
  1352. /* ----------------------------------------------------------------------- */
  1353. static struct i2c_device_id adv7511_id[] = {
  1354. { "adv7511", 0 },
  1355. { }
  1356. };
  1357. MODULE_DEVICE_TABLE(i2c, adv7511_id);
  1358. static struct i2c_driver adv7511_driver = {
  1359. .driver = {
  1360. .name = "adv7511",
  1361. },
  1362. .probe = adv7511_probe,
  1363. .remove = adv7511_remove,
  1364. .id_table = adv7511_id,
  1365. };
  1366. module_i2c_driver(adv7511_driver);