drm_dp_helper.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. * Copyright © 2009 Keith Packard
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/errno.h>
  27. #include <linux/sched.h>
  28. #include <linux/i2c.h>
  29. #include <linux/seq_file.h>
  30. #include <drm/drm_dp_helper.h>
  31. #include <drm/drmP.h>
  32. #include "drm_crtc_helper_internal.h"
  33. /**
  34. * DOC: dp helpers
  35. *
  36. * These functions contain some common logic and helpers at various abstraction
  37. * levels to deal with Display Port sink devices and related things like DP aux
  38. * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
  39. * blocks, ...
  40. */
  41. /* Helpers for DP link training */
  42. static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
  43. {
  44. return link_status[r - DP_LANE0_1_STATUS];
  45. }
  46. static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
  47. int lane)
  48. {
  49. int i = DP_LANE0_1_STATUS + (lane >> 1);
  50. int s = (lane & 1) * 4;
  51. u8 l = dp_link_status(link_status, i);
  52. return (l >> s) & 0xf;
  53. }
  54. bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
  55. int lane_count)
  56. {
  57. u8 lane_align;
  58. u8 lane_status;
  59. int lane;
  60. lane_align = dp_link_status(link_status,
  61. DP_LANE_ALIGN_STATUS_UPDATED);
  62. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  63. return false;
  64. for (lane = 0; lane < lane_count; lane++) {
  65. lane_status = dp_get_lane_status(link_status, lane);
  66. if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
  67. return false;
  68. }
  69. return true;
  70. }
  71. EXPORT_SYMBOL(drm_dp_channel_eq_ok);
  72. bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
  73. int lane_count)
  74. {
  75. int lane;
  76. u8 lane_status;
  77. for (lane = 0; lane < lane_count; lane++) {
  78. lane_status = dp_get_lane_status(link_status, lane);
  79. if ((lane_status & DP_LANE_CR_DONE) == 0)
  80. return false;
  81. }
  82. return true;
  83. }
  84. EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
  85. u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
  86. int lane)
  87. {
  88. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  89. int s = ((lane & 1) ?
  90. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  91. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  92. u8 l = dp_link_status(link_status, i);
  93. return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  94. }
  95. EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
  96. u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
  97. int lane)
  98. {
  99. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  100. int s = ((lane & 1) ?
  101. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  102. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  103. u8 l = dp_link_status(link_status, i);
  104. return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  105. }
  106. EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
  107. void drm_dp_link_train_clock_recovery_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
  108. if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
  109. udelay(100);
  110. else
  111. mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
  112. }
  113. EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
  114. void drm_dp_link_train_channel_eq_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
  115. if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
  116. udelay(400);
  117. else
  118. mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
  119. }
  120. EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
  121. u8 drm_dp_link_rate_to_bw_code(int link_rate)
  122. {
  123. switch (link_rate) {
  124. case 162000:
  125. default:
  126. return DP_LINK_BW_1_62;
  127. case 270000:
  128. return DP_LINK_BW_2_7;
  129. case 540000:
  130. return DP_LINK_BW_5_4;
  131. }
  132. }
  133. EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
  134. int drm_dp_bw_code_to_link_rate(u8 link_bw)
  135. {
  136. switch (link_bw) {
  137. case DP_LINK_BW_1_62:
  138. default:
  139. return 162000;
  140. case DP_LINK_BW_2_7:
  141. return 270000;
  142. case DP_LINK_BW_5_4:
  143. return 540000;
  144. }
  145. }
  146. EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
  147. #define AUX_RETRY_INTERVAL 500 /* us */
  148. /**
  149. * DOC: dp helpers
  150. *
  151. * The DisplayPort AUX channel is an abstraction to allow generic, driver-
  152. * independent access to AUX functionality. Drivers can take advantage of
  153. * this by filling in the fields of the drm_dp_aux structure.
  154. *
  155. * Transactions are described using a hardware-independent drm_dp_aux_msg
  156. * structure, which is passed into a driver's .transfer() implementation.
  157. * Both native and I2C-over-AUX transactions are supported.
  158. */
  159. static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
  160. unsigned int offset, void *buffer, size_t size)
  161. {
  162. struct drm_dp_aux_msg msg;
  163. unsigned int retry, native_reply;
  164. int err = 0, ret = 0;
  165. memset(&msg, 0, sizeof(msg));
  166. msg.address = offset;
  167. msg.request = request;
  168. msg.buffer = buffer;
  169. msg.size = size;
  170. mutex_lock(&aux->hw_mutex);
  171. /*
  172. * The specification doesn't give any recommendation on how often to
  173. * retry native transactions. We used to retry 7 times like for
  174. * aux i2c transactions but real world devices this wasn't
  175. * sufficient, bump to 32 which makes Dell 4k monitors happier.
  176. */
  177. for (retry = 0; retry < 32; retry++) {
  178. if (ret != 0 && ret != -ETIMEDOUT) {
  179. usleep_range(AUX_RETRY_INTERVAL,
  180. AUX_RETRY_INTERVAL + 100);
  181. }
  182. ret = aux->transfer(aux, &msg);
  183. if (ret >= 0) {
  184. native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
  185. if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
  186. if (ret == size)
  187. goto unlock;
  188. ret = -EPROTO;
  189. } else
  190. ret = -EIO;
  191. }
  192. /*
  193. * We want the error we return to be the error we received on
  194. * the first transaction, since we may get a different error the
  195. * next time we retry
  196. */
  197. if (!err)
  198. err = ret;
  199. }
  200. DRM_DEBUG_KMS("Too many retries, giving up. First error: %d\n", err);
  201. ret = err;
  202. unlock:
  203. mutex_unlock(&aux->hw_mutex);
  204. return ret;
  205. }
  206. /**
  207. * drm_dp_dpcd_read() - read a series of bytes from the DPCD
  208. * @aux: DisplayPort AUX channel
  209. * @offset: address of the (first) register to read
  210. * @buffer: buffer to store the register values
  211. * @size: number of bytes in @buffer
  212. *
  213. * Returns the number of bytes transferred on success, or a negative error
  214. * code on failure. -EIO is returned if the request was NAKed by the sink or
  215. * if the retry count was exceeded. If not all bytes were transferred, this
  216. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  217. * function, with the exception of -EBUSY (which causes the transaction to
  218. * be retried), are propagated to the caller.
  219. */
  220. ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
  221. void *buffer, size_t size)
  222. {
  223. int ret;
  224. /*
  225. * HP ZR24w corrupts the first DPCD access after entering power save
  226. * mode. Eg. on a read, the entire buffer will be filled with the same
  227. * byte. Do a throw away read to avoid corrupting anything we care
  228. * about. Afterwards things will work correctly until the monitor
  229. * gets woken up and subsequently re-enters power save mode.
  230. *
  231. * The user pressing any button on the monitor is enough to wake it
  232. * up, so there is no particularly good place to do the workaround.
  233. * We just have to do it before any DPCD access and hope that the
  234. * monitor doesn't power down exactly after the throw away read.
  235. */
  236. ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV, buffer,
  237. 1);
  238. if (ret != 1)
  239. return ret;
  240. return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset, buffer,
  241. size);
  242. }
  243. EXPORT_SYMBOL(drm_dp_dpcd_read);
  244. /**
  245. * drm_dp_dpcd_write() - write a series of bytes to the DPCD
  246. * @aux: DisplayPort AUX channel
  247. * @offset: address of the (first) register to write
  248. * @buffer: buffer containing the values to write
  249. * @size: number of bytes in @buffer
  250. *
  251. * Returns the number of bytes transferred on success, or a negative error
  252. * code on failure. -EIO is returned if the request was NAKed by the sink or
  253. * if the retry count was exceeded. If not all bytes were transferred, this
  254. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  255. * function, with the exception of -EBUSY (which causes the transaction to
  256. * be retried), are propagated to the caller.
  257. */
  258. ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
  259. void *buffer, size_t size)
  260. {
  261. return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer,
  262. size);
  263. }
  264. EXPORT_SYMBOL(drm_dp_dpcd_write);
  265. /**
  266. * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
  267. * @aux: DisplayPort AUX channel
  268. * @status: buffer to store the link status in (must be at least 6 bytes)
  269. *
  270. * Returns the number of bytes transferred on success or a negative error
  271. * code on failure.
  272. */
  273. int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
  274. u8 status[DP_LINK_STATUS_SIZE])
  275. {
  276. return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
  277. DP_LINK_STATUS_SIZE);
  278. }
  279. EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
  280. /**
  281. * drm_dp_link_probe() - probe a DisplayPort link for capabilities
  282. * @aux: DisplayPort AUX channel
  283. * @link: pointer to structure in which to return link capabilities
  284. *
  285. * The structure filled in by this function can usually be passed directly
  286. * into drm_dp_link_power_up() and drm_dp_link_configure() to power up and
  287. * configure the link based on the link's capabilities.
  288. *
  289. * Returns 0 on success or a negative error code on failure.
  290. */
  291. int drm_dp_link_probe(struct drm_dp_aux *aux, struct drm_dp_link *link)
  292. {
  293. u8 values[3];
  294. int err;
  295. memset(link, 0, sizeof(*link));
  296. err = drm_dp_dpcd_read(aux, DP_DPCD_REV, values, sizeof(values));
  297. if (err < 0)
  298. return err;
  299. link->revision = values[0];
  300. link->rate = drm_dp_bw_code_to_link_rate(values[1]);
  301. link->num_lanes = values[2] & DP_MAX_LANE_COUNT_MASK;
  302. if (values[2] & DP_ENHANCED_FRAME_CAP)
  303. link->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
  304. return 0;
  305. }
  306. EXPORT_SYMBOL(drm_dp_link_probe);
  307. /**
  308. * drm_dp_link_power_up() - power up a DisplayPort link
  309. * @aux: DisplayPort AUX channel
  310. * @link: pointer to a structure containing the link configuration
  311. *
  312. * Returns 0 on success or a negative error code on failure.
  313. */
  314. int drm_dp_link_power_up(struct drm_dp_aux *aux, struct drm_dp_link *link)
  315. {
  316. u8 value;
  317. int err;
  318. /* DP_SET_POWER register is only available on DPCD v1.1 and later */
  319. if (link->revision < 0x11)
  320. return 0;
  321. err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  322. if (err < 0)
  323. return err;
  324. value &= ~DP_SET_POWER_MASK;
  325. value |= DP_SET_POWER_D0;
  326. err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  327. if (err < 0)
  328. return err;
  329. /*
  330. * According to the DP 1.1 specification, a "Sink Device must exit the
  331. * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
  332. * Control Field" (register 0x600).
  333. */
  334. usleep_range(1000, 2000);
  335. return 0;
  336. }
  337. EXPORT_SYMBOL(drm_dp_link_power_up);
  338. /**
  339. * drm_dp_link_power_down() - power down a DisplayPort link
  340. * @aux: DisplayPort AUX channel
  341. * @link: pointer to a structure containing the link configuration
  342. *
  343. * Returns 0 on success or a negative error code on failure.
  344. */
  345. int drm_dp_link_power_down(struct drm_dp_aux *aux, struct drm_dp_link *link)
  346. {
  347. u8 value;
  348. int err;
  349. /* DP_SET_POWER register is only available on DPCD v1.1 and later */
  350. if (link->revision < 0x11)
  351. return 0;
  352. err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  353. if (err < 0)
  354. return err;
  355. value &= ~DP_SET_POWER_MASK;
  356. value |= DP_SET_POWER_D3;
  357. err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  358. if (err < 0)
  359. return err;
  360. return 0;
  361. }
  362. EXPORT_SYMBOL(drm_dp_link_power_down);
  363. /**
  364. * drm_dp_link_configure() - configure a DisplayPort link
  365. * @aux: DisplayPort AUX channel
  366. * @link: pointer to a structure containing the link configuration
  367. *
  368. * Returns 0 on success or a negative error code on failure.
  369. */
  370. int drm_dp_link_configure(struct drm_dp_aux *aux, struct drm_dp_link *link)
  371. {
  372. u8 values[2];
  373. int err;
  374. values[0] = drm_dp_link_rate_to_bw_code(link->rate);
  375. values[1] = link->num_lanes;
  376. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  377. values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  378. err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
  379. if (err < 0)
  380. return err;
  381. return 0;
  382. }
  383. EXPORT_SYMBOL(drm_dp_link_configure);
  384. /**
  385. * drm_dp_downstream_max_clock() - extract branch device max
  386. * pixel rate for legacy VGA
  387. * converter or max TMDS clock
  388. * rate for others
  389. * @dpcd: DisplayPort configuration data
  390. * @port_cap: port capabilities
  391. *
  392. * Returns max clock in kHz on success or 0 if max clock not defined
  393. */
  394. int drm_dp_downstream_max_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  395. const u8 port_cap[4])
  396. {
  397. int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
  398. bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  399. DP_DETAILED_CAP_INFO_AVAILABLE;
  400. if (!detailed_cap_info)
  401. return 0;
  402. switch (type) {
  403. case DP_DS_PORT_TYPE_VGA:
  404. return port_cap[1] * 8 * 1000;
  405. case DP_DS_PORT_TYPE_DVI:
  406. case DP_DS_PORT_TYPE_HDMI:
  407. case DP_DS_PORT_TYPE_DP_DUALMODE:
  408. return port_cap[1] * 2500;
  409. default:
  410. return 0;
  411. }
  412. }
  413. EXPORT_SYMBOL(drm_dp_downstream_max_clock);
  414. /**
  415. * drm_dp_downstream_max_bpc() - extract branch device max
  416. * bits per component
  417. * @dpcd: DisplayPort configuration data
  418. * @port_cap: port capabilities
  419. *
  420. * Returns max bpc on success or 0 if max bpc not defined
  421. */
  422. int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  423. const u8 port_cap[4])
  424. {
  425. int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
  426. bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  427. DP_DETAILED_CAP_INFO_AVAILABLE;
  428. int bpc;
  429. if (!detailed_cap_info)
  430. return 0;
  431. switch (type) {
  432. case DP_DS_PORT_TYPE_VGA:
  433. case DP_DS_PORT_TYPE_DVI:
  434. case DP_DS_PORT_TYPE_HDMI:
  435. case DP_DS_PORT_TYPE_DP_DUALMODE:
  436. bpc = port_cap[2] & DP_DS_MAX_BPC_MASK;
  437. switch (bpc) {
  438. case DP_DS_8BPC:
  439. return 8;
  440. case DP_DS_10BPC:
  441. return 10;
  442. case DP_DS_12BPC:
  443. return 12;
  444. case DP_DS_16BPC:
  445. return 16;
  446. }
  447. default:
  448. return 0;
  449. }
  450. }
  451. EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
  452. /**
  453. * drm_dp_downstream_id() - identify branch device
  454. * @aux: DisplayPort AUX channel
  455. * @id: DisplayPort branch device id
  456. *
  457. * Returns branch device id on success or NULL on failure
  458. */
  459. int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
  460. {
  461. return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
  462. }
  463. EXPORT_SYMBOL(drm_dp_downstream_id);
  464. /**
  465. * drm_dp_downstream_debug() - debug DP branch devices
  466. * @m: pointer for debugfs file
  467. * @dpcd: DisplayPort configuration data
  468. * @port_cap: port capabilities
  469. * @aux: DisplayPort AUX channel
  470. *
  471. */
  472. void drm_dp_downstream_debug(struct seq_file *m,
  473. const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  474. const u8 port_cap[4], struct drm_dp_aux *aux)
  475. {
  476. bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  477. DP_DETAILED_CAP_INFO_AVAILABLE;
  478. int clk;
  479. int bpc;
  480. char id[6];
  481. int len;
  482. uint8_t rev[2];
  483. int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
  484. bool branch_device = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  485. DP_DWN_STRM_PORT_PRESENT;
  486. seq_printf(m, "\tDP branch device present: %s\n",
  487. branch_device ? "yes" : "no");
  488. if (!branch_device)
  489. return;
  490. switch (type) {
  491. case DP_DS_PORT_TYPE_DP:
  492. seq_puts(m, "\t\tType: DisplayPort\n");
  493. break;
  494. case DP_DS_PORT_TYPE_VGA:
  495. seq_puts(m, "\t\tType: VGA\n");
  496. break;
  497. case DP_DS_PORT_TYPE_DVI:
  498. seq_puts(m, "\t\tType: DVI\n");
  499. break;
  500. case DP_DS_PORT_TYPE_HDMI:
  501. seq_puts(m, "\t\tType: HDMI\n");
  502. break;
  503. case DP_DS_PORT_TYPE_NON_EDID:
  504. seq_puts(m, "\t\tType: others without EDID support\n");
  505. break;
  506. case DP_DS_PORT_TYPE_DP_DUALMODE:
  507. seq_puts(m, "\t\tType: DP++\n");
  508. break;
  509. case DP_DS_PORT_TYPE_WIRELESS:
  510. seq_puts(m, "\t\tType: Wireless\n");
  511. break;
  512. default:
  513. seq_puts(m, "\t\tType: N/A\n");
  514. }
  515. drm_dp_downstream_id(aux, id);
  516. seq_printf(m, "\t\tID: %s\n", id);
  517. len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
  518. if (len > 0)
  519. seq_printf(m, "\t\tHW: %d.%d\n",
  520. (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
  521. len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, &rev, 2);
  522. if (len > 0)
  523. seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
  524. if (detailed_cap_info) {
  525. clk = drm_dp_downstream_max_clock(dpcd, port_cap);
  526. if (clk > 0) {
  527. if (type == DP_DS_PORT_TYPE_VGA)
  528. seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
  529. else
  530. seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
  531. }
  532. bpc = drm_dp_downstream_max_bpc(dpcd, port_cap);
  533. if (bpc > 0)
  534. seq_printf(m, "\t\tMax bpc: %d\n", bpc);
  535. }
  536. }
  537. EXPORT_SYMBOL(drm_dp_downstream_debug);
  538. /*
  539. * I2C-over-AUX implementation
  540. */
  541. static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
  542. {
  543. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  544. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  545. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  546. I2C_FUNC_10BIT_ADDR;
  547. }
  548. static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
  549. {
  550. /*
  551. * In case of i2c defer or short i2c ack reply to a write,
  552. * we need to switch to WRITE_STATUS_UPDATE to drain the
  553. * rest of the message
  554. */
  555. if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
  556. msg->request &= DP_AUX_I2C_MOT;
  557. msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
  558. }
  559. }
  560. #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
  561. #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
  562. #define AUX_STOP_LEN 4
  563. #define AUX_CMD_LEN 4
  564. #define AUX_ADDRESS_LEN 20
  565. #define AUX_REPLY_PAD_LEN 4
  566. #define AUX_LENGTH_LEN 8
  567. /*
  568. * Calculate the duration of the AUX request/reply in usec. Gives the
  569. * "best" case estimate, ie. successful while as short as possible.
  570. */
  571. static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
  572. {
  573. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  574. AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
  575. if ((msg->request & DP_AUX_I2C_READ) == 0)
  576. len += msg->size * 8;
  577. return len;
  578. }
  579. static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
  580. {
  581. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  582. AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
  583. /*
  584. * For read we expect what was asked. For writes there will
  585. * be 0 or 1 data bytes. Assume 0 for the "best" case.
  586. */
  587. if (msg->request & DP_AUX_I2C_READ)
  588. len += msg->size * 8;
  589. return len;
  590. }
  591. #define I2C_START_LEN 1
  592. #define I2C_STOP_LEN 1
  593. #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
  594. #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
  595. /*
  596. * Calculate the length of the i2c transfer in usec, assuming
  597. * the i2c bus speed is as specified. Gives the the "worst"
  598. * case estimate, ie. successful while as long as possible.
  599. * Doesn't account the the "MOT" bit, and instead assumes each
  600. * message includes a START, ADDRESS and STOP. Neither does it
  601. * account for additional random variables such as clock stretching.
  602. */
  603. static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
  604. int i2c_speed_khz)
  605. {
  606. /* AUX bitrate is 1MHz, i2c bitrate as specified */
  607. return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
  608. msg->size * I2C_DATA_LEN +
  609. I2C_STOP_LEN) * 1000, i2c_speed_khz);
  610. }
  611. /*
  612. * Deterine how many retries should be attempted to successfully transfer
  613. * the specified message, based on the estimated durations of the
  614. * i2c and AUX transfers.
  615. */
  616. static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
  617. int i2c_speed_khz)
  618. {
  619. int aux_time_us = drm_dp_aux_req_duration(msg) +
  620. drm_dp_aux_reply_duration(msg);
  621. int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
  622. return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
  623. }
  624. /*
  625. * FIXME currently assumes 10 kHz as some real world devices seem
  626. * to require it. We should query/set the speed via DPCD if supported.
  627. */
  628. static int dp_aux_i2c_speed_khz __read_mostly = 10;
  629. module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
  630. MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
  631. "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
  632. /*
  633. * Transfer a single I2C-over-AUX message and handle various error conditions,
  634. * retrying the transaction as appropriate. It is assumed that the
  635. * aux->transfer function does not modify anything in the msg other than the
  636. * reply field.
  637. *
  638. * Returns bytes transferred on success, or a negative error code on failure.
  639. */
  640. static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
  641. {
  642. unsigned int retry, defer_i2c;
  643. int ret;
  644. /*
  645. * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
  646. * is required to retry at least seven times upon receiving AUX_DEFER
  647. * before giving up the AUX transaction.
  648. *
  649. * We also try to account for the i2c bus speed.
  650. */
  651. int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
  652. for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
  653. ret = aux->transfer(aux, msg);
  654. if (ret < 0) {
  655. if (ret == -EBUSY)
  656. continue;
  657. /*
  658. * While timeouts can be errors, they're usually normal
  659. * behavior (for instance, when a driver tries to
  660. * communicate with a non-existant DisplayPort device).
  661. * Avoid spamming the kernel log with timeout errors.
  662. */
  663. if (ret == -ETIMEDOUT)
  664. DRM_DEBUG_KMS_RATELIMITED("transaction timed out\n");
  665. else
  666. DRM_DEBUG_KMS("transaction failed: %d\n", ret);
  667. return ret;
  668. }
  669. switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
  670. case DP_AUX_NATIVE_REPLY_ACK:
  671. /*
  672. * For I2C-over-AUX transactions this isn't enough, we
  673. * need to check for the I2C ACK reply.
  674. */
  675. break;
  676. case DP_AUX_NATIVE_REPLY_NACK:
  677. DRM_DEBUG_KMS("native nack (result=%d, size=%zu)\n", ret, msg->size);
  678. return -EREMOTEIO;
  679. case DP_AUX_NATIVE_REPLY_DEFER:
  680. DRM_DEBUG_KMS("native defer\n");
  681. /*
  682. * We could check for I2C bit rate capabilities and if
  683. * available adjust this interval. We could also be
  684. * more careful with DP-to-legacy adapters where a
  685. * long legacy cable may force very low I2C bit rates.
  686. *
  687. * For now just defer for long enough to hopefully be
  688. * safe for all use-cases.
  689. */
  690. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  691. continue;
  692. default:
  693. DRM_ERROR("invalid native reply %#04x\n", msg->reply);
  694. return -EREMOTEIO;
  695. }
  696. switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
  697. case DP_AUX_I2C_REPLY_ACK:
  698. /*
  699. * Both native ACK and I2C ACK replies received. We
  700. * can assume the transfer was successful.
  701. */
  702. if (ret != msg->size)
  703. drm_dp_i2c_msg_write_status_update(msg);
  704. return ret;
  705. case DP_AUX_I2C_REPLY_NACK:
  706. DRM_DEBUG_KMS("I2C nack (result=%d, size=%zu\n", ret, msg->size);
  707. aux->i2c_nack_count++;
  708. return -EREMOTEIO;
  709. case DP_AUX_I2C_REPLY_DEFER:
  710. DRM_DEBUG_KMS("I2C defer\n");
  711. /* DP Compliance Test 4.2.2.5 Requirement:
  712. * Must have at least 7 retries for I2C defers on the
  713. * transaction to pass this test
  714. */
  715. aux->i2c_defer_count++;
  716. if (defer_i2c < 7)
  717. defer_i2c++;
  718. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  719. drm_dp_i2c_msg_write_status_update(msg);
  720. continue;
  721. default:
  722. DRM_ERROR("invalid I2C reply %#04x\n", msg->reply);
  723. return -EREMOTEIO;
  724. }
  725. }
  726. DRM_DEBUG_KMS("too many retries, giving up\n");
  727. return -EREMOTEIO;
  728. }
  729. static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
  730. const struct i2c_msg *i2c_msg)
  731. {
  732. msg->request = (i2c_msg->flags & I2C_M_RD) ?
  733. DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
  734. msg->request |= DP_AUX_I2C_MOT;
  735. }
  736. /*
  737. * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
  738. *
  739. * Returns an error code on failure, or a recommended transfer size on success.
  740. */
  741. static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
  742. {
  743. int err, ret = orig_msg->size;
  744. struct drm_dp_aux_msg msg = *orig_msg;
  745. while (msg.size > 0) {
  746. err = drm_dp_i2c_do_msg(aux, &msg);
  747. if (err <= 0)
  748. return err == 0 ? -EPROTO : err;
  749. if (err < msg.size && err < ret) {
  750. DRM_DEBUG_KMS("Partial I2C reply: requested %zu bytes got %d bytes\n",
  751. msg.size, err);
  752. ret = err;
  753. }
  754. msg.size -= err;
  755. msg.buffer += err;
  756. }
  757. return ret;
  758. }
  759. /*
  760. * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
  761. * packets to be as large as possible. If not, the I2C transactions never
  762. * succeed. Hence the default is maximum.
  763. */
  764. static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
  765. module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
  766. MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
  767. "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
  768. static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  769. int num)
  770. {
  771. struct drm_dp_aux *aux = adapter->algo_data;
  772. unsigned int i, j;
  773. unsigned transfer_size;
  774. struct drm_dp_aux_msg msg;
  775. int err = 0;
  776. dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
  777. memset(&msg, 0, sizeof(msg));
  778. for (i = 0; i < num; i++) {
  779. msg.address = msgs[i].addr;
  780. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  781. /* Send a bare address packet to start the transaction.
  782. * Zero sized messages specify an address only (bare
  783. * address) transaction.
  784. */
  785. msg.buffer = NULL;
  786. msg.size = 0;
  787. err = drm_dp_i2c_do_msg(aux, &msg);
  788. /*
  789. * Reset msg.request in case in case it got
  790. * changed into a WRITE_STATUS_UPDATE.
  791. */
  792. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  793. if (err < 0)
  794. break;
  795. /* We want each transaction to be as large as possible, but
  796. * we'll go to smaller sizes if the hardware gives us a
  797. * short reply.
  798. */
  799. transfer_size = dp_aux_i2c_transfer_size;
  800. for (j = 0; j < msgs[i].len; j += msg.size) {
  801. msg.buffer = msgs[i].buf + j;
  802. msg.size = min(transfer_size, msgs[i].len - j);
  803. err = drm_dp_i2c_drain_msg(aux, &msg);
  804. /*
  805. * Reset msg.request in case in case it got
  806. * changed into a WRITE_STATUS_UPDATE.
  807. */
  808. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  809. if (err < 0)
  810. break;
  811. transfer_size = err;
  812. }
  813. if (err < 0)
  814. break;
  815. }
  816. if (err >= 0)
  817. err = num;
  818. /* Send a bare address packet to close out the transaction.
  819. * Zero sized messages specify an address only (bare
  820. * address) transaction.
  821. */
  822. msg.request &= ~DP_AUX_I2C_MOT;
  823. msg.buffer = NULL;
  824. msg.size = 0;
  825. (void)drm_dp_i2c_do_msg(aux, &msg);
  826. return err;
  827. }
  828. static const struct i2c_algorithm drm_dp_i2c_algo = {
  829. .functionality = drm_dp_i2c_functionality,
  830. .master_xfer = drm_dp_i2c_xfer,
  831. };
  832. static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
  833. {
  834. return container_of(i2c, struct drm_dp_aux, ddc);
  835. }
  836. static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
  837. {
  838. mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
  839. }
  840. static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
  841. {
  842. return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
  843. }
  844. static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
  845. {
  846. mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
  847. }
  848. static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
  849. .lock_bus = lock_bus,
  850. .trylock_bus = trylock_bus,
  851. .unlock_bus = unlock_bus,
  852. };
  853. /**
  854. * drm_dp_aux_init() - minimally initialise an aux channel
  855. * @aux: DisplayPort AUX channel
  856. *
  857. * If you need to use the drm_dp_aux's i2c adapter prior to registering it
  858. * with the outside world, call drm_dp_aux_init() first. You must still
  859. * call drm_dp_aux_register() once the connector has been registered to
  860. * allow userspace access to the auxiliary DP channel.
  861. */
  862. void drm_dp_aux_init(struct drm_dp_aux *aux)
  863. {
  864. mutex_init(&aux->hw_mutex);
  865. aux->ddc.algo = &drm_dp_i2c_algo;
  866. aux->ddc.algo_data = aux;
  867. aux->ddc.retries = 3;
  868. aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
  869. }
  870. EXPORT_SYMBOL(drm_dp_aux_init);
  871. /**
  872. * drm_dp_aux_register() - initialise and register aux channel
  873. * @aux: DisplayPort AUX channel
  874. *
  875. * Automatically calls drm_dp_aux_init() if this hasn't been done yet.
  876. *
  877. * Returns 0 on success or a negative error code on failure.
  878. */
  879. int drm_dp_aux_register(struct drm_dp_aux *aux)
  880. {
  881. int ret;
  882. if (!aux->ddc.algo)
  883. drm_dp_aux_init(aux);
  884. aux->ddc.class = I2C_CLASS_DDC;
  885. aux->ddc.owner = THIS_MODULE;
  886. aux->ddc.dev.parent = aux->dev;
  887. aux->ddc.dev.of_node = aux->dev->of_node;
  888. strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
  889. sizeof(aux->ddc.name));
  890. ret = drm_dp_aux_register_devnode(aux);
  891. if (ret)
  892. return ret;
  893. ret = i2c_add_adapter(&aux->ddc);
  894. if (ret) {
  895. drm_dp_aux_unregister_devnode(aux);
  896. return ret;
  897. }
  898. return 0;
  899. }
  900. EXPORT_SYMBOL(drm_dp_aux_register);
  901. /**
  902. * drm_dp_aux_unregister() - unregister an AUX adapter
  903. * @aux: DisplayPort AUX channel
  904. */
  905. void drm_dp_aux_unregister(struct drm_dp_aux *aux)
  906. {
  907. drm_dp_aux_unregister_devnode(aux);
  908. i2c_del_adapter(&aux->ddc);
  909. }
  910. EXPORT_SYMBOL(drm_dp_aux_unregister);
  911. #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
  912. /**
  913. * drm_dp_psr_setup_time() - PSR setup in time usec
  914. * @psr_cap: PSR capabilities from DPCD
  915. *
  916. * Returns:
  917. * PSR setup time for the panel in microseconds, negative
  918. * error code on failure.
  919. */
  920. int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
  921. {
  922. static const u16 psr_setup_time_us[] = {
  923. PSR_SETUP_TIME(330),
  924. PSR_SETUP_TIME(275),
  925. PSR_SETUP_TIME(220),
  926. PSR_SETUP_TIME(165),
  927. PSR_SETUP_TIME(110),
  928. PSR_SETUP_TIME(55),
  929. PSR_SETUP_TIME(0),
  930. };
  931. int i;
  932. i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
  933. if (i >= ARRAY_SIZE(psr_setup_time_us))
  934. return -EINVAL;
  935. return psr_setup_time_us[i];
  936. }
  937. EXPORT_SYMBOL(drm_dp_psr_setup_time);
  938. #undef PSR_SETUP_TIME