ti-sn65dsi86.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. * datasheet: https://www.ti.com/lit/ds/symlink/sn65dsi86.pdf
  5. */
  6. #include <linux/atomic.h>
  7. #include <linux/auxiliary_bus.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/bits.h>
  10. #include <linux/clk.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/i2c.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/module.h>
  17. #include <linux/of_graph.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/pwm.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <asm/unaligned.h>
  23. #include <drm/display/drm_dp_aux_bus.h>
  24. #include <drm/display/drm_dp_helper.h>
  25. #include <drm/drm_atomic.h>
  26. #include <drm/drm_atomic_helper.h>
  27. #include <drm/drm_bridge.h>
  28. #include <drm/drm_bridge_connector.h>
  29. #include <drm/drm_edid.h>
  30. #include <drm/drm_mipi_dsi.h>
  31. #include <drm/drm_of.h>
  32. #include <drm/drm_panel.h>
  33. #include <drm/drm_print.h>
  34. #include <drm/drm_probe_helper.h>
  35. #define SN_DEVICE_REV_REG 0x08
  36. #define SN_DPPLL_SRC_REG 0x0A
  37. #define DPPLL_CLK_SRC_DSICLK BIT(0)
  38. #define REFCLK_FREQ_MASK GENMASK(3, 1)
  39. #define REFCLK_FREQ(x) ((x) << 1)
  40. #define DPPLL_SRC_DP_PLL_LOCK BIT(7)
  41. #define SN_PLL_ENABLE_REG 0x0D
  42. #define SN_DSI_LANES_REG 0x10
  43. #define CHA_DSI_LANES_MASK GENMASK(4, 3)
  44. #define CHA_DSI_LANES(x) ((x) << 3)
  45. #define SN_DSIA_CLK_FREQ_REG 0x12
  46. #define SN_CHA_ACTIVE_LINE_LENGTH_LOW_REG 0x20
  47. #define SN_CHA_VERTICAL_DISPLAY_SIZE_LOW_REG 0x24
  48. #define SN_CHA_HSYNC_PULSE_WIDTH_LOW_REG 0x2C
  49. #define SN_CHA_HSYNC_PULSE_WIDTH_HIGH_REG 0x2D
  50. #define CHA_HSYNC_POLARITY BIT(7)
  51. #define SN_CHA_VSYNC_PULSE_WIDTH_LOW_REG 0x30
  52. #define SN_CHA_VSYNC_PULSE_WIDTH_HIGH_REG 0x31
  53. #define CHA_VSYNC_POLARITY BIT(7)
  54. #define SN_CHA_HORIZONTAL_BACK_PORCH_REG 0x34
  55. #define SN_CHA_VERTICAL_BACK_PORCH_REG 0x36
  56. #define SN_CHA_HORIZONTAL_FRONT_PORCH_REG 0x38
  57. #define SN_CHA_VERTICAL_FRONT_PORCH_REG 0x3A
  58. #define SN_LN_ASSIGN_REG 0x59
  59. #define LN_ASSIGN_WIDTH 2
  60. #define SN_ENH_FRAME_REG 0x5A
  61. #define VSTREAM_ENABLE BIT(3)
  62. #define LN_POLRS_OFFSET 4
  63. #define LN_POLRS_MASK 0xf0
  64. #define SN_DATA_FORMAT_REG 0x5B
  65. #define BPP_18_RGB BIT(0)
  66. #define SN_HPD_DISABLE_REG 0x5C
  67. #define HPD_DISABLE BIT(0)
  68. #define HPD_DEBOUNCED_STATE BIT(4)
  69. #define SN_GPIO_IO_REG 0x5E
  70. #define SN_GPIO_INPUT_SHIFT 4
  71. #define SN_GPIO_OUTPUT_SHIFT 0
  72. #define SN_GPIO_CTRL_REG 0x5F
  73. #define SN_GPIO_MUX_INPUT 0
  74. #define SN_GPIO_MUX_OUTPUT 1
  75. #define SN_GPIO_MUX_SPECIAL 2
  76. #define SN_GPIO_MUX_MASK 0x3
  77. #define SN_AUX_WDATA_REG(x) (0x64 + (x))
  78. #define SN_AUX_ADDR_19_16_REG 0x74
  79. #define SN_AUX_ADDR_15_8_REG 0x75
  80. #define SN_AUX_ADDR_7_0_REG 0x76
  81. #define SN_AUX_ADDR_MASK GENMASK(19, 0)
  82. #define SN_AUX_LENGTH_REG 0x77
  83. #define SN_AUX_CMD_REG 0x78
  84. #define AUX_CMD_SEND BIT(0)
  85. #define AUX_CMD_REQ(x) ((x) << 4)
  86. #define SN_AUX_RDATA_REG(x) (0x79 + (x))
  87. #define SN_SSC_CONFIG_REG 0x93
  88. #define DP_NUM_LANES_MASK GENMASK(5, 4)
  89. #define DP_NUM_LANES(x) ((x) << 4)
  90. #define SN_DATARATE_CONFIG_REG 0x94
  91. #define DP_DATARATE_MASK GENMASK(7, 5)
  92. #define DP_DATARATE(x) ((x) << 5)
  93. #define SN_TRAINING_SETTING_REG 0x95
  94. #define SCRAMBLE_DISABLE BIT(4)
  95. #define SN_ML_TX_MODE_REG 0x96
  96. #define ML_TX_MAIN_LINK_OFF 0
  97. #define ML_TX_NORMAL_MODE BIT(0)
  98. #define SN_PWM_PRE_DIV_REG 0xA0
  99. #define SN_BACKLIGHT_SCALE_REG 0xA1
  100. #define BACKLIGHT_SCALE_MAX 0xFFFF
  101. #define SN_BACKLIGHT_REG 0xA3
  102. #define SN_PWM_EN_INV_REG 0xA5
  103. #define SN_PWM_INV_MASK BIT(0)
  104. #define SN_PWM_EN_MASK BIT(1)
  105. #define SN_AUX_CMD_STATUS_REG 0xF4
  106. #define AUX_IRQ_STATUS_AUX_RPLY_TOUT BIT(3)
  107. #define AUX_IRQ_STATUS_AUX_SHORT BIT(5)
  108. #define AUX_IRQ_STATUS_NAT_I2C_FAIL BIT(6)
  109. #define MIN_DSI_CLK_FREQ_MHZ 40
  110. /* fudge factor required to account for 8b/10b encoding */
  111. #define DP_CLK_FUDGE_NUM 10
  112. #define DP_CLK_FUDGE_DEN 8
  113. /* Matches DP_AUX_MAX_PAYLOAD_BYTES (for now) */
  114. #define SN_AUX_MAX_PAYLOAD_BYTES 16
  115. #define SN_REGULATOR_SUPPLY_NUM 4
  116. #define SN_MAX_DP_LANES 4
  117. #define SN_NUM_GPIOS 4
  118. #define SN_GPIO_PHYSICAL_OFFSET 1
  119. #define SN_LINK_TRAINING_TRIES 10
  120. #define SN_PWM_GPIO_IDX 3 /* 4th GPIO */
  121. /**
  122. * struct ti_sn65dsi86 - Platform data for ti-sn65dsi86 driver.
  123. * @bridge_aux: AUX-bus sub device for MIPI-to-eDP bridge functionality.
  124. * @gpio_aux: AUX-bus sub device for GPIO controller functionality.
  125. * @aux_aux: AUX-bus sub device for eDP AUX channel functionality.
  126. * @pwm_aux: AUX-bus sub device for PWM controller functionality.
  127. *
  128. * @dev: Pointer to the top level (i2c) device.
  129. * @regmap: Regmap for accessing i2c.
  130. * @aux: Our aux channel.
  131. * @bridge: Our bridge.
  132. * @connector: Our connector.
  133. * @host_node: Remote DSI node.
  134. * @dsi: Our MIPI DSI source.
  135. * @refclk: Our reference clock.
  136. * @next_bridge: The bridge on the eDP side.
  137. * @enable_gpio: The GPIO we toggle to enable the bridge.
  138. * @supplies: Data for bulk enabling/disabling our regulators.
  139. * @dp_lanes: Count of dp_lanes we're using.
  140. * @ln_assign: Value to program to the LN_ASSIGN register.
  141. * @ln_polrs: Value for the 4-bit LN_POLRS field of SN_ENH_FRAME_REG.
  142. * @comms_enabled: If true then communication over the aux channel is enabled.
  143. * @comms_mutex: Protects modification of comms_enabled.
  144. *
  145. * @gchip: If we expose our GPIOs, this is used.
  146. * @gchip_output: A cache of whether we've set GPIOs to output. This
  147. * serves double-duty of keeping track of the direction and
  148. * also keeping track of whether we've incremented the
  149. * pm_runtime reference count for this pin, which we do
  150. * whenever a pin is configured as an output. This is a
  151. * bitmap so we can do atomic ops on it without an extra
  152. * lock so concurrent users of our 4 GPIOs don't stomp on
  153. * each other's read-modify-write.
  154. *
  155. * @pchip: pwm_chip if the PWM is exposed.
  156. * @pwm_enabled: Used to track if the PWM signal is currently enabled.
  157. * @pwm_pin_busy: Track if GPIO4 is currently requested for GPIO or PWM.
  158. * @pwm_refclk_freq: Cache for the reference clock input to the PWM.
  159. */
  160. struct ti_sn65dsi86 {
  161. struct auxiliary_device *bridge_aux;
  162. struct auxiliary_device *gpio_aux;
  163. struct auxiliary_device *aux_aux;
  164. struct auxiliary_device *pwm_aux;
  165. struct device *dev;
  166. struct regmap *regmap;
  167. struct drm_dp_aux aux;
  168. struct drm_bridge bridge;
  169. struct drm_connector *connector;
  170. struct device_node *host_node;
  171. struct mipi_dsi_device *dsi;
  172. struct clk *refclk;
  173. struct drm_bridge *next_bridge;
  174. struct gpio_desc *enable_gpio;
  175. struct regulator_bulk_data supplies[SN_REGULATOR_SUPPLY_NUM];
  176. int dp_lanes;
  177. u8 ln_assign;
  178. u8 ln_polrs;
  179. bool comms_enabled;
  180. struct mutex comms_mutex;
  181. #if defined(CONFIG_OF_GPIO)
  182. struct gpio_chip gchip;
  183. DECLARE_BITMAP(gchip_output, SN_NUM_GPIOS);
  184. #endif
  185. #if defined(CONFIG_PWM)
  186. struct pwm_chip pchip;
  187. bool pwm_enabled;
  188. atomic_t pwm_pin_busy;
  189. #endif
  190. unsigned int pwm_refclk_freq;
  191. };
  192. static const struct regmap_range ti_sn65dsi86_volatile_ranges[] = {
  193. { .range_min = 0, .range_max = 0xFF },
  194. };
  195. static const struct regmap_access_table ti_sn_bridge_volatile_table = {
  196. .yes_ranges = ti_sn65dsi86_volatile_ranges,
  197. .n_yes_ranges = ARRAY_SIZE(ti_sn65dsi86_volatile_ranges),
  198. };
  199. static const struct regmap_config ti_sn65dsi86_regmap_config = {
  200. .reg_bits = 8,
  201. .val_bits = 8,
  202. .volatile_table = &ti_sn_bridge_volatile_table,
  203. .cache_type = REGCACHE_NONE,
  204. .max_register = 0xFF,
  205. };
  206. static int __maybe_unused ti_sn65dsi86_read_u16(struct ti_sn65dsi86 *pdata,
  207. unsigned int reg, u16 *val)
  208. {
  209. u8 buf[2];
  210. int ret;
  211. ret = regmap_bulk_read(pdata->regmap, reg, buf, ARRAY_SIZE(buf));
  212. if (ret)
  213. return ret;
  214. *val = buf[0] | (buf[1] << 8);
  215. return 0;
  216. }
  217. static void ti_sn65dsi86_write_u16(struct ti_sn65dsi86 *pdata,
  218. unsigned int reg, u16 val)
  219. {
  220. u8 buf[2] = { val & 0xff, val >> 8 };
  221. regmap_bulk_write(pdata->regmap, reg, buf, ARRAY_SIZE(buf));
  222. }
  223. static u32 ti_sn_bridge_get_dsi_freq(struct ti_sn65dsi86 *pdata)
  224. {
  225. u32 bit_rate_khz, clk_freq_khz;
  226. struct drm_display_mode *mode =
  227. &pdata->bridge.encoder->crtc->state->adjusted_mode;
  228. bit_rate_khz = mode->clock *
  229. mipi_dsi_pixel_format_to_bpp(pdata->dsi->format);
  230. clk_freq_khz = bit_rate_khz / (pdata->dsi->lanes * 2);
  231. return clk_freq_khz;
  232. }
  233. /* clk frequencies supported by bridge in Hz in case derived from REFCLK pin */
  234. static const u32 ti_sn_bridge_refclk_lut[] = {
  235. 12000000,
  236. 19200000,
  237. 26000000,
  238. 27000000,
  239. 38400000,
  240. };
  241. /* clk frequencies supported by bridge in Hz in case derived from DACP/N pin */
  242. static const u32 ti_sn_bridge_dsiclk_lut[] = {
  243. 468000000,
  244. 384000000,
  245. 416000000,
  246. 486000000,
  247. 460800000,
  248. };
  249. static void ti_sn_bridge_set_refclk_freq(struct ti_sn65dsi86 *pdata)
  250. {
  251. int i;
  252. u32 refclk_rate;
  253. const u32 *refclk_lut;
  254. size_t refclk_lut_size;
  255. if (pdata->refclk) {
  256. refclk_rate = clk_get_rate(pdata->refclk);
  257. refclk_lut = ti_sn_bridge_refclk_lut;
  258. refclk_lut_size = ARRAY_SIZE(ti_sn_bridge_refclk_lut);
  259. clk_prepare_enable(pdata->refclk);
  260. } else {
  261. refclk_rate = ti_sn_bridge_get_dsi_freq(pdata) * 1000;
  262. refclk_lut = ti_sn_bridge_dsiclk_lut;
  263. refclk_lut_size = ARRAY_SIZE(ti_sn_bridge_dsiclk_lut);
  264. }
  265. /* for i equals to refclk_lut_size means default frequency */
  266. for (i = 0; i < refclk_lut_size; i++)
  267. if (refclk_lut[i] == refclk_rate)
  268. break;
  269. /* avoid buffer overflow and "1" is the default rate in the datasheet. */
  270. if (i >= refclk_lut_size)
  271. i = 1;
  272. regmap_update_bits(pdata->regmap, SN_DPPLL_SRC_REG, REFCLK_FREQ_MASK,
  273. REFCLK_FREQ(i));
  274. /*
  275. * The PWM refclk is based on the value written to SN_DPPLL_SRC_REG,
  276. * regardless of its actual sourcing.
  277. */
  278. pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i];
  279. }
  280. static void ti_sn65dsi86_enable_comms(struct ti_sn65dsi86 *pdata)
  281. {
  282. mutex_lock(&pdata->comms_mutex);
  283. /* configure bridge ref_clk */
  284. ti_sn_bridge_set_refclk_freq(pdata);
  285. /*
  286. * HPD on this bridge chip is a bit useless. This is an eDP bridge
  287. * so the HPD is an internal signal that's only there to signal that
  288. * the panel is done powering up. ...but the bridge chip debounces
  289. * this signal by between 100 ms and 400 ms (depending on process,
  290. * voltage, and temperate--I measured it at about 200 ms). One
  291. * particular panel asserted HPD 84 ms after it was powered on meaning
  292. * that we saw HPD 284 ms after power on. ...but the same panel said
  293. * that instead of looking at HPD you could just hardcode a delay of
  294. * 200 ms. We'll assume that the panel driver will have the hardcoded
  295. * delay in its prepare and always disable HPD.
  296. *
  297. * If HPD somehow makes sense on some future panel we'll have to
  298. * change this to be conditional on someone specifying that HPD should
  299. * be used.
  300. */
  301. regmap_update_bits(pdata->regmap, SN_HPD_DISABLE_REG, HPD_DISABLE,
  302. HPD_DISABLE);
  303. pdata->comms_enabled = true;
  304. mutex_unlock(&pdata->comms_mutex);
  305. }
  306. static void ti_sn65dsi86_disable_comms(struct ti_sn65dsi86 *pdata)
  307. {
  308. mutex_lock(&pdata->comms_mutex);
  309. pdata->comms_enabled = false;
  310. clk_disable_unprepare(pdata->refclk);
  311. mutex_unlock(&pdata->comms_mutex);
  312. }
  313. static int __maybe_unused ti_sn65dsi86_resume(struct device *dev)
  314. {
  315. struct ti_sn65dsi86 *pdata = dev_get_drvdata(dev);
  316. int ret;
  317. ret = regulator_bulk_enable(SN_REGULATOR_SUPPLY_NUM, pdata->supplies);
  318. if (ret) {
  319. DRM_ERROR("failed to enable supplies %d\n", ret);
  320. return ret;
  321. }
  322. /* td2: min 100 us after regulators before enabling the GPIO */
  323. usleep_range(100, 110);
  324. gpiod_set_value(pdata->enable_gpio, 1);
  325. /*
  326. * If we have a reference clock we can enable communication w/ the
  327. * panel (including the aux channel) w/out any need for an input clock
  328. * so we can do it in resume which lets us read the EDID before
  329. * pre_enable(). Without a reference clock we need the MIPI reference
  330. * clock so reading early doesn't work.
  331. */
  332. if (pdata->refclk)
  333. ti_sn65dsi86_enable_comms(pdata);
  334. return ret;
  335. }
  336. static int __maybe_unused ti_sn65dsi86_suspend(struct device *dev)
  337. {
  338. struct ti_sn65dsi86 *pdata = dev_get_drvdata(dev);
  339. int ret;
  340. if (pdata->refclk)
  341. ti_sn65dsi86_disable_comms(pdata);
  342. gpiod_set_value(pdata->enable_gpio, 0);
  343. ret = regulator_bulk_disable(SN_REGULATOR_SUPPLY_NUM, pdata->supplies);
  344. if (ret)
  345. DRM_ERROR("failed to disable supplies %d\n", ret);
  346. return ret;
  347. }
  348. static const struct dev_pm_ops ti_sn65dsi86_pm_ops = {
  349. SET_RUNTIME_PM_OPS(ti_sn65dsi86_suspend, ti_sn65dsi86_resume, NULL)
  350. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  351. pm_runtime_force_resume)
  352. };
  353. static int status_show(struct seq_file *s, void *data)
  354. {
  355. struct ti_sn65dsi86 *pdata = s->private;
  356. unsigned int reg, val;
  357. seq_puts(s, "STATUS REGISTERS:\n");
  358. pm_runtime_get_sync(pdata->dev);
  359. /* IRQ Status Registers, see Table 31 in datasheet */
  360. for (reg = 0xf0; reg <= 0xf8; reg++) {
  361. regmap_read(pdata->regmap, reg, &val);
  362. seq_printf(s, "[0x%02x] = 0x%08x\n", reg, val);
  363. }
  364. pm_runtime_put_autosuspend(pdata->dev);
  365. return 0;
  366. }
  367. DEFINE_SHOW_ATTRIBUTE(status);
  368. static void ti_sn65dsi86_debugfs_remove(void *data)
  369. {
  370. debugfs_remove_recursive(data);
  371. }
  372. static void ti_sn65dsi86_debugfs_init(struct ti_sn65dsi86 *pdata)
  373. {
  374. struct device *dev = pdata->dev;
  375. struct dentry *debugfs;
  376. int ret;
  377. debugfs = debugfs_create_dir(dev_name(dev), NULL);
  378. /*
  379. * We might get an error back if debugfs wasn't enabled in the kernel
  380. * so let's just silently return upon failure.
  381. */
  382. if (IS_ERR_OR_NULL(debugfs))
  383. return;
  384. ret = devm_add_action_or_reset(dev, ti_sn65dsi86_debugfs_remove, debugfs);
  385. if (ret)
  386. return;
  387. debugfs_create_file("status", 0600, debugfs, pdata, &status_fops);
  388. }
  389. /* -----------------------------------------------------------------------------
  390. * Auxiliary Devices (*not* AUX)
  391. */
  392. static void ti_sn65dsi86_uninit_aux(void *data)
  393. {
  394. auxiliary_device_uninit(data);
  395. }
  396. static void ti_sn65dsi86_delete_aux(void *data)
  397. {
  398. auxiliary_device_delete(data);
  399. }
  400. static void ti_sn65dsi86_aux_device_release(struct device *dev)
  401. {
  402. struct auxiliary_device *aux = container_of(dev, struct auxiliary_device, dev);
  403. kfree(aux);
  404. }
  405. static int ti_sn65dsi86_add_aux_device(struct ti_sn65dsi86 *pdata,
  406. struct auxiliary_device **aux_out,
  407. const char *name)
  408. {
  409. struct device *dev = pdata->dev;
  410. struct auxiliary_device *aux;
  411. int ret;
  412. aux = kzalloc(sizeof(*aux), GFP_KERNEL);
  413. if (!aux)
  414. return -ENOMEM;
  415. aux->name = name;
  416. aux->dev.parent = dev;
  417. aux->dev.release = ti_sn65dsi86_aux_device_release;
  418. device_set_of_node_from_dev(&aux->dev, dev);
  419. ret = auxiliary_device_init(aux);
  420. if (ret) {
  421. kfree(aux);
  422. return ret;
  423. }
  424. ret = devm_add_action_or_reset(dev, ti_sn65dsi86_uninit_aux, aux);
  425. if (ret)
  426. return ret;
  427. ret = auxiliary_device_add(aux);
  428. if (ret)
  429. return ret;
  430. ret = devm_add_action_or_reset(dev, ti_sn65dsi86_delete_aux, aux);
  431. if (!ret)
  432. *aux_out = aux;
  433. return ret;
  434. }
  435. /* -----------------------------------------------------------------------------
  436. * AUX Adapter
  437. */
  438. static struct ti_sn65dsi86 *aux_to_ti_sn65dsi86(struct drm_dp_aux *aux)
  439. {
  440. return container_of(aux, struct ti_sn65dsi86, aux);
  441. }
  442. static ssize_t ti_sn_aux_transfer(struct drm_dp_aux *aux,
  443. struct drm_dp_aux_msg *msg)
  444. {
  445. struct ti_sn65dsi86 *pdata = aux_to_ti_sn65dsi86(aux);
  446. u32 request = msg->request & ~(DP_AUX_I2C_MOT | DP_AUX_I2C_WRITE_STATUS_UPDATE);
  447. u32 request_val = AUX_CMD_REQ(msg->request);
  448. u8 *buf = msg->buffer;
  449. unsigned int len = msg->size;
  450. unsigned int val;
  451. int ret;
  452. u8 addr_len[SN_AUX_LENGTH_REG + 1 - SN_AUX_ADDR_19_16_REG];
  453. if (len > SN_AUX_MAX_PAYLOAD_BYTES)
  454. return -EINVAL;
  455. pm_runtime_get_sync(pdata->dev);
  456. mutex_lock(&pdata->comms_mutex);
  457. /*
  458. * If someone tries to do a DDC over AUX transaction before pre_enable()
  459. * on a device without a dedicated reference clock then we just can't
  460. * do it. Fail right away. This prevents non-refclk users from reading
  461. * the EDID before enabling the panel but such is life.
  462. */
  463. if (!pdata->comms_enabled) {
  464. ret = -EIO;
  465. goto exit;
  466. }
  467. switch (request) {
  468. case DP_AUX_NATIVE_WRITE:
  469. case DP_AUX_I2C_WRITE:
  470. case DP_AUX_NATIVE_READ:
  471. case DP_AUX_I2C_READ:
  472. regmap_write(pdata->regmap, SN_AUX_CMD_REG, request_val);
  473. /* Assume it's good */
  474. msg->reply = 0;
  475. break;
  476. default:
  477. ret = -EINVAL;
  478. goto exit;
  479. }
  480. BUILD_BUG_ON(sizeof(addr_len) != sizeof(__be32));
  481. put_unaligned_be32((msg->address & SN_AUX_ADDR_MASK) << 8 | len,
  482. addr_len);
  483. regmap_bulk_write(pdata->regmap, SN_AUX_ADDR_19_16_REG, addr_len,
  484. ARRAY_SIZE(addr_len));
  485. if (request == DP_AUX_NATIVE_WRITE || request == DP_AUX_I2C_WRITE)
  486. regmap_bulk_write(pdata->regmap, SN_AUX_WDATA_REG(0), buf, len);
  487. /* Clear old status bits before start so we don't get confused */
  488. regmap_write(pdata->regmap, SN_AUX_CMD_STATUS_REG,
  489. AUX_IRQ_STATUS_NAT_I2C_FAIL |
  490. AUX_IRQ_STATUS_AUX_RPLY_TOUT |
  491. AUX_IRQ_STATUS_AUX_SHORT);
  492. regmap_write(pdata->regmap, SN_AUX_CMD_REG, request_val | AUX_CMD_SEND);
  493. /* Zero delay loop because i2c transactions are slow already */
  494. ret = regmap_read_poll_timeout(pdata->regmap, SN_AUX_CMD_REG, val,
  495. !(val & AUX_CMD_SEND), 0, 50 * 1000);
  496. if (ret)
  497. goto exit;
  498. ret = regmap_read(pdata->regmap, SN_AUX_CMD_STATUS_REG, &val);
  499. if (ret)
  500. goto exit;
  501. if (val & AUX_IRQ_STATUS_AUX_RPLY_TOUT) {
  502. /*
  503. * The hardware tried the message seven times per the DP spec
  504. * but it hit a timeout. We ignore defers here because they're
  505. * handled in hardware.
  506. */
  507. ret = -ETIMEDOUT;
  508. goto exit;
  509. }
  510. if (val & AUX_IRQ_STATUS_AUX_SHORT) {
  511. ret = regmap_read(pdata->regmap, SN_AUX_LENGTH_REG, &len);
  512. if (ret)
  513. goto exit;
  514. } else if (val & AUX_IRQ_STATUS_NAT_I2C_FAIL) {
  515. switch (request) {
  516. case DP_AUX_I2C_WRITE:
  517. case DP_AUX_I2C_READ:
  518. msg->reply |= DP_AUX_I2C_REPLY_NACK;
  519. break;
  520. case DP_AUX_NATIVE_READ:
  521. case DP_AUX_NATIVE_WRITE:
  522. msg->reply |= DP_AUX_NATIVE_REPLY_NACK;
  523. break;
  524. }
  525. len = 0;
  526. goto exit;
  527. }
  528. if (request != DP_AUX_NATIVE_WRITE && request != DP_AUX_I2C_WRITE && len != 0)
  529. ret = regmap_bulk_read(pdata->regmap, SN_AUX_RDATA_REG(0), buf, len);
  530. exit:
  531. mutex_unlock(&pdata->comms_mutex);
  532. pm_runtime_mark_last_busy(pdata->dev);
  533. pm_runtime_put_autosuspend(pdata->dev);
  534. if (ret)
  535. return ret;
  536. return len;
  537. }
  538. static int ti_sn_aux_probe(struct auxiliary_device *adev,
  539. const struct auxiliary_device_id *id)
  540. {
  541. struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);
  542. int ret;
  543. pdata->aux.name = "ti-sn65dsi86-aux";
  544. pdata->aux.dev = &adev->dev;
  545. pdata->aux.transfer = ti_sn_aux_transfer;
  546. drm_dp_aux_init(&pdata->aux);
  547. ret = devm_of_dp_aux_populate_ep_devices(&pdata->aux);
  548. if (ret)
  549. return ret;
  550. /*
  551. * The eDP to MIPI bridge parts don't work until the AUX channel is
  552. * setup so we don't add it in the main driver probe, we add it now.
  553. */
  554. return ti_sn65dsi86_add_aux_device(pdata, &pdata->bridge_aux, "bridge");
  555. }
  556. static const struct auxiliary_device_id ti_sn_aux_id_table[] = {
  557. { .name = "ti_sn65dsi86.aux", },
  558. {},
  559. };
  560. static struct auxiliary_driver ti_sn_aux_driver = {
  561. .name = "aux",
  562. .probe = ti_sn_aux_probe,
  563. .id_table = ti_sn_aux_id_table,
  564. };
  565. /*------------------------------------------------------------------------------
  566. * DRM Bridge
  567. */
  568. static struct ti_sn65dsi86 *bridge_to_ti_sn65dsi86(struct drm_bridge *bridge)
  569. {
  570. return container_of(bridge, struct ti_sn65dsi86, bridge);
  571. }
  572. static int ti_sn_attach_host(struct auxiliary_device *adev, struct ti_sn65dsi86 *pdata)
  573. {
  574. int val;
  575. struct mipi_dsi_host *host;
  576. struct mipi_dsi_device *dsi;
  577. struct device *dev = pdata->dev;
  578. const struct mipi_dsi_device_info info = { .type = "ti_sn_bridge",
  579. .channel = 0,
  580. .node = NULL,
  581. };
  582. host = of_find_mipi_dsi_host_by_node(pdata->host_node);
  583. if (!host)
  584. return -EPROBE_DEFER;
  585. dsi = devm_mipi_dsi_device_register_full(&adev->dev, host, &info);
  586. if (IS_ERR(dsi))
  587. return PTR_ERR(dsi);
  588. /* TODO: setting to 4 MIPI lanes always for now */
  589. dsi->lanes = 4;
  590. dsi->format = MIPI_DSI_FMT_RGB888;
  591. dsi->mode_flags = MIPI_DSI_MODE_VIDEO;
  592. /* check if continuous dsi clock is required or not */
  593. pm_runtime_get_sync(dev);
  594. regmap_read(pdata->regmap, SN_DPPLL_SRC_REG, &val);
  595. pm_runtime_put_autosuspend(dev);
  596. if (!(val & DPPLL_CLK_SRC_DSICLK))
  597. dsi->mode_flags |= MIPI_DSI_CLOCK_NON_CONTINUOUS;
  598. pdata->dsi = dsi;
  599. return devm_mipi_dsi_attach(&adev->dev, dsi);
  600. }
  601. static int ti_sn_bridge_attach(struct drm_bridge *bridge,
  602. enum drm_bridge_attach_flags flags)
  603. {
  604. struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
  605. int ret;
  606. pdata->aux.drm_dev = bridge->dev;
  607. ret = drm_dp_aux_register(&pdata->aux);
  608. if (ret < 0) {
  609. drm_err(bridge->dev, "Failed to register DP AUX channel: %d\n", ret);
  610. return ret;
  611. }
  612. /*
  613. * Attach the next bridge.
  614. * We never want the next bridge to *also* create a connector.
  615. */
  616. ret = drm_bridge_attach(bridge->encoder, pdata->next_bridge,
  617. &pdata->bridge, flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  618. if (ret < 0)
  619. goto err_initted_aux;
  620. if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
  621. return 0;
  622. pdata->connector = drm_bridge_connector_init(pdata->bridge.dev,
  623. pdata->bridge.encoder);
  624. if (IS_ERR(pdata->connector)) {
  625. ret = PTR_ERR(pdata->connector);
  626. goto err_initted_aux;
  627. }
  628. drm_connector_attach_encoder(pdata->connector, pdata->bridge.encoder);
  629. return 0;
  630. err_initted_aux:
  631. drm_dp_aux_unregister(&pdata->aux);
  632. return ret;
  633. }
  634. static void ti_sn_bridge_detach(struct drm_bridge *bridge)
  635. {
  636. drm_dp_aux_unregister(&bridge_to_ti_sn65dsi86(bridge)->aux);
  637. }
  638. static enum drm_mode_status
  639. ti_sn_bridge_mode_valid(struct drm_bridge *bridge,
  640. const struct drm_display_info *info,
  641. const struct drm_display_mode *mode)
  642. {
  643. /* maximum supported resolution is 4K at 60 fps */
  644. if (mode->clock > 594000)
  645. return MODE_CLOCK_HIGH;
  646. /*
  647. * The front and back porch registers are 8 bits, and pulse width
  648. * registers are 15 bits, so reject any modes with larger periods.
  649. */
  650. if ((mode->hsync_start - mode->hdisplay) > 0xff)
  651. return MODE_HBLANK_WIDE;
  652. if ((mode->vsync_start - mode->vdisplay) > 0xff)
  653. return MODE_VBLANK_WIDE;
  654. if ((mode->hsync_end - mode->hsync_start) > 0x7fff)
  655. return MODE_HSYNC_WIDE;
  656. if ((mode->vsync_end - mode->vsync_start) > 0x7fff)
  657. return MODE_VSYNC_WIDE;
  658. if ((mode->htotal - mode->hsync_end) > 0xff)
  659. return MODE_HBLANK_WIDE;
  660. if ((mode->vtotal - mode->vsync_end) > 0xff)
  661. return MODE_VBLANK_WIDE;
  662. return MODE_OK;
  663. }
  664. static void ti_sn_bridge_atomic_disable(struct drm_bridge *bridge,
  665. struct drm_bridge_state *old_bridge_state)
  666. {
  667. struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
  668. /* disable video stream */
  669. regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE, 0);
  670. }
  671. static void ti_sn_bridge_set_dsi_rate(struct ti_sn65dsi86 *pdata)
  672. {
  673. unsigned int bit_rate_mhz, clk_freq_mhz;
  674. unsigned int val;
  675. struct drm_display_mode *mode =
  676. &pdata->bridge.encoder->crtc->state->adjusted_mode;
  677. /* set DSIA clk frequency */
  678. bit_rate_mhz = (mode->clock / 1000) *
  679. mipi_dsi_pixel_format_to_bpp(pdata->dsi->format);
  680. clk_freq_mhz = bit_rate_mhz / (pdata->dsi->lanes * 2);
  681. /* for each increment in val, frequency increases by 5MHz */
  682. val = (MIN_DSI_CLK_FREQ_MHZ / 5) +
  683. (((clk_freq_mhz - MIN_DSI_CLK_FREQ_MHZ) / 5) & 0xFF);
  684. regmap_write(pdata->regmap, SN_DSIA_CLK_FREQ_REG, val);
  685. }
  686. static unsigned int ti_sn_bridge_get_bpp(struct drm_connector *connector)
  687. {
  688. if (connector->display_info.bpc <= 6)
  689. return 18;
  690. else
  691. return 24;
  692. }
  693. /*
  694. * LUT index corresponds to register value and
  695. * LUT values corresponds to dp data rate supported
  696. * by the bridge in Mbps unit.
  697. */
  698. static const unsigned int ti_sn_bridge_dp_rate_lut[] = {
  699. 0, 1620, 2160, 2430, 2700, 3240, 4320, 5400
  700. };
  701. static int ti_sn_bridge_calc_min_dp_rate_idx(struct ti_sn65dsi86 *pdata, unsigned int bpp)
  702. {
  703. unsigned int bit_rate_khz, dp_rate_mhz;
  704. unsigned int i;
  705. struct drm_display_mode *mode =
  706. &pdata->bridge.encoder->crtc->state->adjusted_mode;
  707. /* Calculate minimum bit rate based on our pixel clock. */
  708. bit_rate_khz = mode->clock * bpp;
  709. /* Calculate minimum DP data rate, taking 80% as per DP spec */
  710. dp_rate_mhz = DIV_ROUND_UP(bit_rate_khz * DP_CLK_FUDGE_NUM,
  711. 1000 * pdata->dp_lanes * DP_CLK_FUDGE_DEN);
  712. for (i = 1; i < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut) - 1; i++)
  713. if (ti_sn_bridge_dp_rate_lut[i] >= dp_rate_mhz)
  714. break;
  715. return i;
  716. }
  717. static unsigned int ti_sn_bridge_read_valid_rates(struct ti_sn65dsi86 *pdata)
  718. {
  719. unsigned int valid_rates = 0;
  720. unsigned int rate_per_200khz;
  721. unsigned int rate_mhz;
  722. u8 dpcd_val;
  723. int ret;
  724. int i, j;
  725. ret = drm_dp_dpcd_readb(&pdata->aux, DP_EDP_DPCD_REV, &dpcd_val);
  726. if (ret != 1) {
  727. DRM_DEV_ERROR(pdata->dev,
  728. "Can't read eDP rev (%d), assuming 1.1\n", ret);
  729. dpcd_val = DP_EDP_11;
  730. }
  731. if (dpcd_val >= DP_EDP_14) {
  732. /* eDP 1.4 devices must provide a custom table */
  733. __le16 sink_rates[DP_MAX_SUPPORTED_RATES];
  734. ret = drm_dp_dpcd_read(&pdata->aux, DP_SUPPORTED_LINK_RATES,
  735. sink_rates, sizeof(sink_rates));
  736. if (ret != sizeof(sink_rates)) {
  737. DRM_DEV_ERROR(pdata->dev,
  738. "Can't read supported rate table (%d)\n", ret);
  739. /* By zeroing we'll fall back to DP_MAX_LINK_RATE. */
  740. memset(sink_rates, 0, sizeof(sink_rates));
  741. }
  742. for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
  743. rate_per_200khz = le16_to_cpu(sink_rates[i]);
  744. if (!rate_per_200khz)
  745. break;
  746. rate_mhz = rate_per_200khz * 200 / 1000;
  747. for (j = 0;
  748. j < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut);
  749. j++) {
  750. if (ti_sn_bridge_dp_rate_lut[j] == rate_mhz)
  751. valid_rates |= BIT(j);
  752. }
  753. }
  754. for (i = 0; i < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut); i++) {
  755. if (valid_rates & BIT(i))
  756. return valid_rates;
  757. }
  758. DRM_DEV_ERROR(pdata->dev,
  759. "No matching eDP rates in table; falling back\n");
  760. }
  761. /* On older versions best we can do is use DP_MAX_LINK_RATE */
  762. ret = drm_dp_dpcd_readb(&pdata->aux, DP_MAX_LINK_RATE, &dpcd_val);
  763. if (ret != 1) {
  764. DRM_DEV_ERROR(pdata->dev,
  765. "Can't read max rate (%d); assuming 5.4 GHz\n",
  766. ret);
  767. dpcd_val = DP_LINK_BW_5_4;
  768. }
  769. switch (dpcd_val) {
  770. default:
  771. DRM_DEV_ERROR(pdata->dev,
  772. "Unexpected max rate (%#x); assuming 5.4 GHz\n",
  773. (int)dpcd_val);
  774. fallthrough;
  775. case DP_LINK_BW_5_4:
  776. valid_rates |= BIT(7);
  777. fallthrough;
  778. case DP_LINK_BW_2_7:
  779. valid_rates |= BIT(4);
  780. fallthrough;
  781. case DP_LINK_BW_1_62:
  782. valid_rates |= BIT(1);
  783. break;
  784. }
  785. return valid_rates;
  786. }
  787. static void ti_sn_bridge_set_video_timings(struct ti_sn65dsi86 *pdata)
  788. {
  789. struct drm_display_mode *mode =
  790. &pdata->bridge.encoder->crtc->state->adjusted_mode;
  791. u8 hsync_polarity = 0, vsync_polarity = 0;
  792. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  793. hsync_polarity = CHA_HSYNC_POLARITY;
  794. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  795. vsync_polarity = CHA_VSYNC_POLARITY;
  796. ti_sn65dsi86_write_u16(pdata, SN_CHA_ACTIVE_LINE_LENGTH_LOW_REG,
  797. mode->hdisplay);
  798. ti_sn65dsi86_write_u16(pdata, SN_CHA_VERTICAL_DISPLAY_SIZE_LOW_REG,
  799. mode->vdisplay);
  800. regmap_write(pdata->regmap, SN_CHA_HSYNC_PULSE_WIDTH_LOW_REG,
  801. (mode->hsync_end - mode->hsync_start) & 0xFF);
  802. regmap_write(pdata->regmap, SN_CHA_HSYNC_PULSE_WIDTH_HIGH_REG,
  803. (((mode->hsync_end - mode->hsync_start) >> 8) & 0x7F) |
  804. hsync_polarity);
  805. regmap_write(pdata->regmap, SN_CHA_VSYNC_PULSE_WIDTH_LOW_REG,
  806. (mode->vsync_end - mode->vsync_start) & 0xFF);
  807. regmap_write(pdata->regmap, SN_CHA_VSYNC_PULSE_WIDTH_HIGH_REG,
  808. (((mode->vsync_end - mode->vsync_start) >> 8) & 0x7F) |
  809. vsync_polarity);
  810. regmap_write(pdata->regmap, SN_CHA_HORIZONTAL_BACK_PORCH_REG,
  811. (mode->htotal - mode->hsync_end) & 0xFF);
  812. regmap_write(pdata->regmap, SN_CHA_VERTICAL_BACK_PORCH_REG,
  813. (mode->vtotal - mode->vsync_end) & 0xFF);
  814. regmap_write(pdata->regmap, SN_CHA_HORIZONTAL_FRONT_PORCH_REG,
  815. (mode->hsync_start - mode->hdisplay) & 0xFF);
  816. regmap_write(pdata->regmap, SN_CHA_VERTICAL_FRONT_PORCH_REG,
  817. (mode->vsync_start - mode->vdisplay) & 0xFF);
  818. usleep_range(10000, 10500); /* 10ms delay recommended by spec */
  819. }
  820. static unsigned int ti_sn_get_max_lanes(struct ti_sn65dsi86 *pdata)
  821. {
  822. u8 data;
  823. int ret;
  824. ret = drm_dp_dpcd_readb(&pdata->aux, DP_MAX_LANE_COUNT, &data);
  825. if (ret != 1) {
  826. DRM_DEV_ERROR(pdata->dev,
  827. "Can't read lane count (%d); assuming 4\n", ret);
  828. return 4;
  829. }
  830. return data & DP_LANE_COUNT_MASK;
  831. }
  832. static int ti_sn_link_training(struct ti_sn65dsi86 *pdata, int dp_rate_idx,
  833. const char **last_err_str)
  834. {
  835. unsigned int val;
  836. int ret;
  837. int i;
  838. /* set dp clk frequency value */
  839. regmap_update_bits(pdata->regmap, SN_DATARATE_CONFIG_REG,
  840. DP_DATARATE_MASK, DP_DATARATE(dp_rate_idx));
  841. /* enable DP PLL */
  842. regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 1);
  843. ret = regmap_read_poll_timeout(pdata->regmap, SN_DPPLL_SRC_REG, val,
  844. val & DPPLL_SRC_DP_PLL_LOCK, 1000,
  845. 50 * 1000);
  846. if (ret) {
  847. *last_err_str = "DP_PLL_LOCK polling failed";
  848. goto exit;
  849. }
  850. /*
  851. * We'll try to link train several times. As part of link training
  852. * the bridge chip will write DP_SET_POWER_D0 to DP_SET_POWER. If
  853. * the panel isn't ready quite it might respond NAK here which means
  854. * we need to try again.
  855. */
  856. for (i = 0; i < SN_LINK_TRAINING_TRIES; i++) {
  857. /* Semi auto link training mode */
  858. regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0x0A);
  859. ret = regmap_read_poll_timeout(pdata->regmap, SN_ML_TX_MODE_REG, val,
  860. val == ML_TX_MAIN_LINK_OFF ||
  861. val == ML_TX_NORMAL_MODE, 1000,
  862. 500 * 1000);
  863. if (ret) {
  864. *last_err_str = "Training complete polling failed";
  865. } else if (val == ML_TX_MAIN_LINK_OFF) {
  866. *last_err_str = "Link training failed, link is off";
  867. ret = -EIO;
  868. continue;
  869. }
  870. break;
  871. }
  872. /* If we saw quite a few retries, add a note about it */
  873. if (!ret && i > SN_LINK_TRAINING_TRIES / 2)
  874. DRM_DEV_INFO(pdata->dev, "Link training needed %d retries\n", i);
  875. exit:
  876. /* Disable the PLL if we failed */
  877. if (ret)
  878. regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);
  879. return ret;
  880. }
  881. static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,
  882. struct drm_bridge_state *old_bridge_state)
  883. {
  884. struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
  885. struct drm_connector *connector;
  886. const char *last_err_str = "No supported DP rate";
  887. unsigned int valid_rates;
  888. int dp_rate_idx;
  889. unsigned int val;
  890. int ret = -EINVAL;
  891. int max_dp_lanes;
  892. unsigned int bpp;
  893. connector = drm_atomic_get_new_connector_for_encoder(old_bridge_state->base.state,
  894. bridge->encoder);
  895. if (!connector) {
  896. dev_err_ratelimited(pdata->dev, "Could not get the connector\n");
  897. return;
  898. }
  899. max_dp_lanes = ti_sn_get_max_lanes(pdata);
  900. pdata->dp_lanes = min(pdata->dp_lanes, max_dp_lanes);
  901. /* DSI_A lane config */
  902. val = CHA_DSI_LANES(SN_MAX_DP_LANES - pdata->dsi->lanes);
  903. regmap_update_bits(pdata->regmap, SN_DSI_LANES_REG,
  904. CHA_DSI_LANES_MASK, val);
  905. regmap_write(pdata->regmap, SN_LN_ASSIGN_REG, pdata->ln_assign);
  906. regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, LN_POLRS_MASK,
  907. pdata->ln_polrs << LN_POLRS_OFFSET);
  908. /* set dsi clk frequency value */
  909. ti_sn_bridge_set_dsi_rate(pdata);
  910. /*
  911. * The SN65DSI86 only supports ASSR Display Authentication method and
  912. * this method is enabled for eDP panels. An eDP panel must support this
  913. * authentication method. We need to enable this method in the eDP panel
  914. * at DisplayPort address 0x0010A prior to link training.
  915. *
  916. * As only ASSR is supported by SN65DSI86, for full DisplayPort displays
  917. * we need to disable the scrambler.
  918. */
  919. if (pdata->bridge.type == DRM_MODE_CONNECTOR_eDP) {
  920. drm_dp_dpcd_writeb(&pdata->aux, DP_EDP_CONFIGURATION_SET,
  921. DP_ALTERNATE_SCRAMBLER_RESET_ENABLE);
  922. regmap_update_bits(pdata->regmap, SN_TRAINING_SETTING_REG,
  923. SCRAMBLE_DISABLE, 0);
  924. } else {
  925. regmap_update_bits(pdata->regmap, SN_TRAINING_SETTING_REG,
  926. SCRAMBLE_DISABLE, SCRAMBLE_DISABLE);
  927. }
  928. bpp = ti_sn_bridge_get_bpp(connector);
  929. /* Set the DP output format (18 bpp or 24 bpp) */
  930. val = bpp == 18 ? BPP_18_RGB : 0;
  931. regmap_update_bits(pdata->regmap, SN_DATA_FORMAT_REG, BPP_18_RGB, val);
  932. /* DP lane config */
  933. val = DP_NUM_LANES(min(pdata->dp_lanes, 3));
  934. regmap_update_bits(pdata->regmap, SN_SSC_CONFIG_REG, DP_NUM_LANES_MASK,
  935. val);
  936. valid_rates = ti_sn_bridge_read_valid_rates(pdata);
  937. /* Train until we run out of rates */
  938. for (dp_rate_idx = ti_sn_bridge_calc_min_dp_rate_idx(pdata, bpp);
  939. dp_rate_idx < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut);
  940. dp_rate_idx++) {
  941. if (!(valid_rates & BIT(dp_rate_idx)))
  942. continue;
  943. ret = ti_sn_link_training(pdata, dp_rate_idx, &last_err_str);
  944. if (!ret)
  945. break;
  946. }
  947. if (ret) {
  948. DRM_DEV_ERROR(pdata->dev, "%s (%d)\n", last_err_str, ret);
  949. return;
  950. }
  951. /* config video parameters */
  952. ti_sn_bridge_set_video_timings(pdata);
  953. /* enable video stream */
  954. regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE,
  955. VSTREAM_ENABLE);
  956. }
  957. static void ti_sn_bridge_atomic_pre_enable(struct drm_bridge *bridge,
  958. struct drm_bridge_state *old_bridge_state)
  959. {
  960. struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
  961. pm_runtime_get_sync(pdata->dev);
  962. if (!pdata->refclk)
  963. ti_sn65dsi86_enable_comms(pdata);
  964. /* td7: min 100 us after enable before DSI data */
  965. usleep_range(100, 110);
  966. }
  967. static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,
  968. struct drm_bridge_state *old_bridge_state)
  969. {
  970. struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
  971. /* semi auto link training mode OFF */
  972. regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0);
  973. /* Num lanes to 0 as per power sequencing in data sheet */
  974. regmap_update_bits(pdata->regmap, SN_SSC_CONFIG_REG, DP_NUM_LANES_MASK, 0);
  975. /* disable DP PLL */
  976. regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);
  977. if (!pdata->refclk)
  978. ti_sn65dsi86_disable_comms(pdata);
  979. pm_runtime_put_sync(pdata->dev);
  980. }
  981. static enum drm_connector_status ti_sn_bridge_detect(struct drm_bridge *bridge)
  982. {
  983. struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
  984. int val = 0;
  985. pm_runtime_get_sync(pdata->dev);
  986. regmap_read(pdata->regmap, SN_HPD_DISABLE_REG, &val);
  987. pm_runtime_put_autosuspend(pdata->dev);
  988. return val & HPD_DEBOUNCED_STATE ? connector_status_connected
  989. : connector_status_disconnected;
  990. }
  991. static struct edid *ti_sn_bridge_get_edid(struct drm_bridge *bridge,
  992. struct drm_connector *connector)
  993. {
  994. struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
  995. return drm_get_edid(connector, &pdata->aux.ddc);
  996. }
  997. static const struct drm_bridge_funcs ti_sn_bridge_funcs = {
  998. .attach = ti_sn_bridge_attach,
  999. .detach = ti_sn_bridge_detach,
  1000. .mode_valid = ti_sn_bridge_mode_valid,
  1001. .get_edid = ti_sn_bridge_get_edid,
  1002. .detect = ti_sn_bridge_detect,
  1003. .atomic_pre_enable = ti_sn_bridge_atomic_pre_enable,
  1004. .atomic_enable = ti_sn_bridge_atomic_enable,
  1005. .atomic_disable = ti_sn_bridge_atomic_disable,
  1006. .atomic_post_disable = ti_sn_bridge_atomic_post_disable,
  1007. .atomic_reset = drm_atomic_helper_bridge_reset,
  1008. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  1009. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  1010. };
  1011. static void ti_sn_bridge_parse_lanes(struct ti_sn65dsi86 *pdata,
  1012. struct device_node *np)
  1013. {
  1014. u32 lane_assignments[SN_MAX_DP_LANES] = { 0, 1, 2, 3 };
  1015. u32 lane_polarities[SN_MAX_DP_LANES] = { };
  1016. struct device_node *endpoint;
  1017. u8 ln_assign = 0;
  1018. u8 ln_polrs = 0;
  1019. int dp_lanes;
  1020. int i;
  1021. /*
  1022. * Read config from the device tree about lane remapping and lane
  1023. * polarities. These are optional and we assume identity map and
  1024. * normal polarity if nothing is specified. It's OK to specify just
  1025. * data-lanes but not lane-polarities but not vice versa.
  1026. *
  1027. * Error checking is light (we just make sure we don't crash or
  1028. * buffer overrun) and we assume dts is well formed and specifying
  1029. * mappings that the hardware supports.
  1030. */
  1031. endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);
  1032. dp_lanes = drm_of_get_data_lanes_count(endpoint, 1, SN_MAX_DP_LANES);
  1033. if (dp_lanes > 0) {
  1034. of_property_read_u32_array(endpoint, "data-lanes",
  1035. lane_assignments, dp_lanes);
  1036. of_property_read_u32_array(endpoint, "lane-polarities",
  1037. lane_polarities, dp_lanes);
  1038. } else {
  1039. dp_lanes = SN_MAX_DP_LANES;
  1040. }
  1041. of_node_put(endpoint);
  1042. /*
  1043. * Convert into register format. Loop over all lanes even if
  1044. * data-lanes had fewer elements so that we nicely initialize
  1045. * the LN_ASSIGN register.
  1046. */
  1047. for (i = SN_MAX_DP_LANES - 1; i >= 0; i--) {
  1048. ln_assign = ln_assign << LN_ASSIGN_WIDTH | lane_assignments[i];
  1049. ln_polrs = ln_polrs << 1 | lane_polarities[i];
  1050. }
  1051. /* Stash in our struct for when we power on */
  1052. pdata->dp_lanes = dp_lanes;
  1053. pdata->ln_assign = ln_assign;
  1054. pdata->ln_polrs = ln_polrs;
  1055. }
  1056. static int ti_sn_bridge_parse_dsi_host(struct ti_sn65dsi86 *pdata)
  1057. {
  1058. struct device_node *np = pdata->dev->of_node;
  1059. pdata->host_node = of_graph_get_remote_node(np, 0, 0);
  1060. if (!pdata->host_node) {
  1061. DRM_ERROR("remote dsi host node not found\n");
  1062. return -ENODEV;
  1063. }
  1064. return 0;
  1065. }
  1066. static int ti_sn_bridge_probe(struct auxiliary_device *adev,
  1067. const struct auxiliary_device_id *id)
  1068. {
  1069. struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);
  1070. struct device_node *np = pdata->dev->of_node;
  1071. int ret;
  1072. pdata->next_bridge = devm_drm_of_get_bridge(&adev->dev, np, 1, 0);
  1073. if (IS_ERR(pdata->next_bridge))
  1074. return dev_err_probe(&adev->dev, PTR_ERR(pdata->next_bridge),
  1075. "failed to create panel bridge\n");
  1076. ti_sn_bridge_parse_lanes(pdata, np);
  1077. ret = ti_sn_bridge_parse_dsi_host(pdata);
  1078. if (ret)
  1079. return ret;
  1080. pdata->bridge.funcs = &ti_sn_bridge_funcs;
  1081. pdata->bridge.of_node = np;
  1082. pdata->bridge.type = pdata->next_bridge->type == DRM_MODE_CONNECTOR_DisplayPort
  1083. ? DRM_MODE_CONNECTOR_DisplayPort : DRM_MODE_CONNECTOR_eDP;
  1084. if (pdata->bridge.type == DRM_MODE_CONNECTOR_DisplayPort)
  1085. pdata->bridge.ops = DRM_BRIDGE_OP_EDID | DRM_BRIDGE_OP_DETECT;
  1086. drm_bridge_add(&pdata->bridge);
  1087. ret = ti_sn_attach_host(adev, pdata);
  1088. if (ret) {
  1089. dev_err_probe(&adev->dev, ret, "failed to attach dsi host\n");
  1090. goto err_remove_bridge;
  1091. }
  1092. return 0;
  1093. err_remove_bridge:
  1094. drm_bridge_remove(&pdata->bridge);
  1095. return ret;
  1096. }
  1097. static void ti_sn_bridge_remove(struct auxiliary_device *adev)
  1098. {
  1099. struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);
  1100. if (!pdata)
  1101. return;
  1102. drm_bridge_remove(&pdata->bridge);
  1103. of_node_put(pdata->host_node);
  1104. }
  1105. static const struct auxiliary_device_id ti_sn_bridge_id_table[] = {
  1106. { .name = "ti_sn65dsi86.bridge", },
  1107. {},
  1108. };
  1109. static struct auxiliary_driver ti_sn_bridge_driver = {
  1110. .name = "bridge",
  1111. .probe = ti_sn_bridge_probe,
  1112. .remove = ti_sn_bridge_remove,
  1113. .id_table = ti_sn_bridge_id_table,
  1114. };
  1115. /* -----------------------------------------------------------------------------
  1116. * PWM Controller
  1117. */
  1118. #if defined(CONFIG_PWM)
  1119. static int ti_sn_pwm_pin_request(struct ti_sn65dsi86 *pdata)
  1120. {
  1121. return atomic_xchg(&pdata->pwm_pin_busy, 1) ? -EBUSY : 0;
  1122. }
  1123. static void ti_sn_pwm_pin_release(struct ti_sn65dsi86 *pdata)
  1124. {
  1125. atomic_set(&pdata->pwm_pin_busy, 0);
  1126. }
  1127. static struct ti_sn65dsi86 *pwm_chip_to_ti_sn_bridge(struct pwm_chip *chip)
  1128. {
  1129. return container_of(chip, struct ti_sn65dsi86, pchip);
  1130. }
  1131. static int ti_sn_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  1132. {
  1133. struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);
  1134. return ti_sn_pwm_pin_request(pdata);
  1135. }
  1136. static void ti_sn_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  1137. {
  1138. struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);
  1139. ti_sn_pwm_pin_release(pdata);
  1140. }
  1141. /*
  1142. * Limitations:
  1143. * - The PWM signal is not driven when the chip is powered down, or in its
  1144. * reset state and the driver does not implement the "suspend state"
  1145. * described in the documentation. In order to save power, state->enabled is
  1146. * interpreted as denoting if the signal is expected to be valid, and is used
  1147. * to determine if the chip needs to be kept powered.
  1148. * - Changing both period and duty_cycle is not done atomically, neither is the
  1149. * multi-byte register updates, so the output might briefly be undefined
  1150. * during update.
  1151. */
  1152. static int ti_sn_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  1153. const struct pwm_state *state)
  1154. {
  1155. struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);
  1156. unsigned int pwm_en_inv;
  1157. unsigned int backlight;
  1158. unsigned int pre_div;
  1159. unsigned int scale;
  1160. u64 period_max;
  1161. u64 period;
  1162. int ret;
  1163. if (!pdata->pwm_enabled) {
  1164. ret = pm_runtime_get_sync(pdata->dev);
  1165. if (ret < 0) {
  1166. pm_runtime_put_sync(pdata->dev);
  1167. return ret;
  1168. }
  1169. }
  1170. if (state->enabled) {
  1171. if (!pdata->pwm_enabled) {
  1172. /*
  1173. * The chip might have been powered down while we
  1174. * didn't hold a PM runtime reference, so mux in the
  1175. * PWM function on the GPIO pin again.
  1176. */
  1177. ret = regmap_update_bits(pdata->regmap, SN_GPIO_CTRL_REG,
  1178. SN_GPIO_MUX_MASK << (2 * SN_PWM_GPIO_IDX),
  1179. SN_GPIO_MUX_SPECIAL << (2 * SN_PWM_GPIO_IDX));
  1180. if (ret) {
  1181. dev_err(pdata->dev, "failed to mux in PWM function\n");
  1182. goto out;
  1183. }
  1184. }
  1185. /*
  1186. * Per the datasheet the PWM frequency is given by:
  1187. *
  1188. * REFCLK_FREQ
  1189. * PWM_FREQ = -----------------------------------
  1190. * PWM_PRE_DIV * BACKLIGHT_SCALE + 1
  1191. *
  1192. * However, after careful review the author is convinced that
  1193. * the documentation has lost some parenthesis around
  1194. * "BACKLIGHT_SCALE + 1".
  1195. *
  1196. * With the period T_pwm = 1/PWM_FREQ this can be written:
  1197. *
  1198. * T_pwm * REFCLK_FREQ = PWM_PRE_DIV * (BACKLIGHT_SCALE + 1)
  1199. *
  1200. * In order to keep BACKLIGHT_SCALE within its 16 bits,
  1201. * PWM_PRE_DIV must be:
  1202. *
  1203. * T_pwm * REFCLK_FREQ
  1204. * PWM_PRE_DIV >= -------------------------
  1205. * BACKLIGHT_SCALE_MAX + 1
  1206. *
  1207. * To simplify the search and to favour higher resolution of
  1208. * the duty cycle over accuracy of the period, the lowest
  1209. * possible PWM_PRE_DIV is used. Finally the scale is
  1210. * calculated as:
  1211. *
  1212. * T_pwm * REFCLK_FREQ
  1213. * BACKLIGHT_SCALE = ---------------------- - 1
  1214. * PWM_PRE_DIV
  1215. *
  1216. * Here T_pwm is represented in seconds, so appropriate scaling
  1217. * to nanoseconds is necessary.
  1218. */
  1219. /* Minimum T_pwm is 1 / REFCLK_FREQ */
  1220. if (state->period <= NSEC_PER_SEC / pdata->pwm_refclk_freq) {
  1221. ret = -EINVAL;
  1222. goto out;
  1223. }
  1224. /*
  1225. * Maximum T_pwm is 255 * (65535 + 1) / REFCLK_FREQ
  1226. * Limit period to this to avoid overflows
  1227. */
  1228. period_max = div_u64((u64)NSEC_PER_SEC * 255 * (65535 + 1),
  1229. pdata->pwm_refclk_freq);
  1230. period = min(state->period, period_max);
  1231. pre_div = DIV64_U64_ROUND_UP(period * pdata->pwm_refclk_freq,
  1232. (u64)NSEC_PER_SEC * (BACKLIGHT_SCALE_MAX + 1));
  1233. scale = div64_u64(period * pdata->pwm_refclk_freq, (u64)NSEC_PER_SEC * pre_div) - 1;
  1234. /*
  1235. * The documentation has the duty ratio given as:
  1236. *
  1237. * duty BACKLIGHT
  1238. * ------- = ---------------------
  1239. * period BACKLIGHT_SCALE + 1
  1240. *
  1241. * Solve for BACKLIGHT, substituting BACKLIGHT_SCALE according
  1242. * to definition above and adjusting for nanosecond
  1243. * representation of duty cycle gives us:
  1244. */
  1245. backlight = div64_u64(state->duty_cycle * pdata->pwm_refclk_freq,
  1246. (u64)NSEC_PER_SEC * pre_div);
  1247. if (backlight > scale)
  1248. backlight = scale;
  1249. ret = regmap_write(pdata->regmap, SN_PWM_PRE_DIV_REG, pre_div);
  1250. if (ret) {
  1251. dev_err(pdata->dev, "failed to update PWM_PRE_DIV\n");
  1252. goto out;
  1253. }
  1254. ti_sn65dsi86_write_u16(pdata, SN_BACKLIGHT_SCALE_REG, scale);
  1255. ti_sn65dsi86_write_u16(pdata, SN_BACKLIGHT_REG, backlight);
  1256. }
  1257. pwm_en_inv = FIELD_PREP(SN_PWM_EN_MASK, state->enabled) |
  1258. FIELD_PREP(SN_PWM_INV_MASK, state->polarity == PWM_POLARITY_INVERSED);
  1259. ret = regmap_write(pdata->regmap, SN_PWM_EN_INV_REG, pwm_en_inv);
  1260. if (ret) {
  1261. dev_err(pdata->dev, "failed to update PWM_EN/PWM_INV\n");
  1262. goto out;
  1263. }
  1264. pdata->pwm_enabled = state->enabled;
  1265. out:
  1266. if (!pdata->pwm_enabled)
  1267. pm_runtime_put_sync(pdata->dev);
  1268. return ret;
  1269. }
  1270. static int ti_sn_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  1271. struct pwm_state *state)
  1272. {
  1273. struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);
  1274. unsigned int pwm_en_inv;
  1275. unsigned int pre_div;
  1276. u16 backlight;
  1277. u16 scale;
  1278. int ret;
  1279. ret = regmap_read(pdata->regmap, SN_PWM_EN_INV_REG, &pwm_en_inv);
  1280. if (ret)
  1281. return 0;
  1282. ret = ti_sn65dsi86_read_u16(pdata, SN_BACKLIGHT_SCALE_REG, &scale);
  1283. if (ret)
  1284. return 0;
  1285. ret = ti_sn65dsi86_read_u16(pdata, SN_BACKLIGHT_REG, &backlight);
  1286. if (ret)
  1287. return 0;
  1288. ret = regmap_read(pdata->regmap, SN_PWM_PRE_DIV_REG, &pre_div);
  1289. if (ret)
  1290. return 0;
  1291. state->enabled = FIELD_GET(SN_PWM_EN_MASK, pwm_en_inv);
  1292. if (FIELD_GET(SN_PWM_INV_MASK, pwm_en_inv))
  1293. state->polarity = PWM_POLARITY_INVERSED;
  1294. else
  1295. state->polarity = PWM_POLARITY_NORMAL;
  1296. state->period = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * pre_div * (scale + 1),
  1297. pdata->pwm_refclk_freq);
  1298. state->duty_cycle = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * pre_div * backlight,
  1299. pdata->pwm_refclk_freq);
  1300. if (state->duty_cycle > state->period)
  1301. state->duty_cycle = state->period;
  1302. return 0;
  1303. }
  1304. static const struct pwm_ops ti_sn_pwm_ops = {
  1305. .request = ti_sn_pwm_request,
  1306. .free = ti_sn_pwm_free,
  1307. .apply = ti_sn_pwm_apply,
  1308. .get_state = ti_sn_pwm_get_state,
  1309. .owner = THIS_MODULE,
  1310. };
  1311. static int ti_sn_pwm_probe(struct auxiliary_device *adev,
  1312. const struct auxiliary_device_id *id)
  1313. {
  1314. struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);
  1315. pdata->pchip.dev = pdata->dev;
  1316. pdata->pchip.ops = &ti_sn_pwm_ops;
  1317. pdata->pchip.npwm = 1;
  1318. pdata->pchip.of_xlate = of_pwm_single_xlate;
  1319. pdata->pchip.of_pwm_n_cells = 1;
  1320. return pwmchip_add(&pdata->pchip);
  1321. }
  1322. static void ti_sn_pwm_remove(struct auxiliary_device *adev)
  1323. {
  1324. struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);
  1325. pwmchip_remove(&pdata->pchip);
  1326. if (pdata->pwm_enabled)
  1327. pm_runtime_put_sync(pdata->dev);
  1328. }
  1329. static const struct auxiliary_device_id ti_sn_pwm_id_table[] = {
  1330. { .name = "ti_sn65dsi86.pwm", },
  1331. {},
  1332. };
  1333. static struct auxiliary_driver ti_sn_pwm_driver = {
  1334. .name = "pwm",
  1335. .probe = ti_sn_pwm_probe,
  1336. .remove = ti_sn_pwm_remove,
  1337. .id_table = ti_sn_pwm_id_table,
  1338. };
  1339. static int __init ti_sn_pwm_register(void)
  1340. {
  1341. return auxiliary_driver_register(&ti_sn_pwm_driver);
  1342. }
  1343. static void ti_sn_pwm_unregister(void)
  1344. {
  1345. auxiliary_driver_unregister(&ti_sn_pwm_driver);
  1346. }
  1347. #else
  1348. static inline int ti_sn_pwm_pin_request(struct ti_sn65dsi86 *pdata) { return 0; }
  1349. static inline void ti_sn_pwm_pin_release(struct ti_sn65dsi86 *pdata) {}
  1350. static inline int ti_sn_pwm_register(void) { return 0; }
  1351. static inline void ti_sn_pwm_unregister(void) {}
  1352. #endif
  1353. /* -----------------------------------------------------------------------------
  1354. * GPIO Controller
  1355. */
  1356. #if defined(CONFIG_OF_GPIO)
  1357. static int tn_sn_bridge_of_xlate(struct gpio_chip *chip,
  1358. const struct of_phandle_args *gpiospec,
  1359. u32 *flags)
  1360. {
  1361. if (WARN_ON(gpiospec->args_count < chip->of_gpio_n_cells))
  1362. return -EINVAL;
  1363. if (gpiospec->args[0] > chip->ngpio || gpiospec->args[0] < 1)
  1364. return -EINVAL;
  1365. if (flags)
  1366. *flags = gpiospec->args[1];
  1367. return gpiospec->args[0] - SN_GPIO_PHYSICAL_OFFSET;
  1368. }
  1369. static int ti_sn_bridge_gpio_get_direction(struct gpio_chip *chip,
  1370. unsigned int offset)
  1371. {
  1372. struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);
  1373. /*
  1374. * We already have to keep track of the direction because we use
  1375. * that to figure out whether we've powered the device. We can
  1376. * just return that rather than (maybe) powering up the device
  1377. * to ask its direction.
  1378. */
  1379. return test_bit(offset, pdata->gchip_output) ?
  1380. GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
  1381. }
  1382. static int ti_sn_bridge_gpio_get(struct gpio_chip *chip, unsigned int offset)
  1383. {
  1384. struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);
  1385. unsigned int val;
  1386. int ret;
  1387. /*
  1388. * When the pin is an input we don't forcibly keep the bridge
  1389. * powered--we just power it on to read the pin. NOTE: part of
  1390. * the reason this works is that the bridge defaults (when
  1391. * powered back on) to all 4 GPIOs being configured as GPIO input.
  1392. * Also note that if something else is keeping the chip powered the
  1393. * pm_runtime functions are lightweight increments of a refcount.
  1394. */
  1395. pm_runtime_get_sync(pdata->dev);
  1396. ret = regmap_read(pdata->regmap, SN_GPIO_IO_REG, &val);
  1397. pm_runtime_put_autosuspend(pdata->dev);
  1398. if (ret)
  1399. return ret;
  1400. return !!(val & BIT(SN_GPIO_INPUT_SHIFT + offset));
  1401. }
  1402. static void ti_sn_bridge_gpio_set(struct gpio_chip *chip, unsigned int offset,
  1403. int val)
  1404. {
  1405. struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);
  1406. int ret;
  1407. if (!test_bit(offset, pdata->gchip_output)) {
  1408. dev_err(pdata->dev, "Ignoring GPIO set while input\n");
  1409. return;
  1410. }
  1411. val &= 1;
  1412. ret = regmap_update_bits(pdata->regmap, SN_GPIO_IO_REG,
  1413. BIT(SN_GPIO_OUTPUT_SHIFT + offset),
  1414. val << (SN_GPIO_OUTPUT_SHIFT + offset));
  1415. if (ret)
  1416. dev_warn(pdata->dev,
  1417. "Failed to set bridge GPIO %u: %d\n", offset, ret);
  1418. }
  1419. static int ti_sn_bridge_gpio_direction_input(struct gpio_chip *chip,
  1420. unsigned int offset)
  1421. {
  1422. struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);
  1423. int shift = offset * 2;
  1424. int ret;
  1425. if (!test_and_clear_bit(offset, pdata->gchip_output))
  1426. return 0;
  1427. ret = regmap_update_bits(pdata->regmap, SN_GPIO_CTRL_REG,
  1428. SN_GPIO_MUX_MASK << shift,
  1429. SN_GPIO_MUX_INPUT << shift);
  1430. if (ret) {
  1431. set_bit(offset, pdata->gchip_output);
  1432. return ret;
  1433. }
  1434. /*
  1435. * NOTE: if nobody else is powering the device this may fully power
  1436. * it off and when it comes back it will have lost all state, but
  1437. * that's OK because the default is input and we're now an input.
  1438. */
  1439. pm_runtime_put_autosuspend(pdata->dev);
  1440. return 0;
  1441. }
  1442. static int ti_sn_bridge_gpio_direction_output(struct gpio_chip *chip,
  1443. unsigned int offset, int val)
  1444. {
  1445. struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);
  1446. int shift = offset * 2;
  1447. int ret;
  1448. if (test_and_set_bit(offset, pdata->gchip_output))
  1449. return 0;
  1450. pm_runtime_get_sync(pdata->dev);
  1451. /* Set value first to avoid glitching */
  1452. ti_sn_bridge_gpio_set(chip, offset, val);
  1453. /* Set direction */
  1454. ret = regmap_update_bits(pdata->regmap, SN_GPIO_CTRL_REG,
  1455. SN_GPIO_MUX_MASK << shift,
  1456. SN_GPIO_MUX_OUTPUT << shift);
  1457. if (ret) {
  1458. clear_bit(offset, pdata->gchip_output);
  1459. pm_runtime_put_autosuspend(pdata->dev);
  1460. }
  1461. return ret;
  1462. }
  1463. static int ti_sn_bridge_gpio_request(struct gpio_chip *chip, unsigned int offset)
  1464. {
  1465. struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);
  1466. if (offset == SN_PWM_GPIO_IDX)
  1467. return ti_sn_pwm_pin_request(pdata);
  1468. return 0;
  1469. }
  1470. static void ti_sn_bridge_gpio_free(struct gpio_chip *chip, unsigned int offset)
  1471. {
  1472. struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);
  1473. /* We won't keep pm_runtime if we're input, so switch there on free */
  1474. ti_sn_bridge_gpio_direction_input(chip, offset);
  1475. if (offset == SN_PWM_GPIO_IDX)
  1476. ti_sn_pwm_pin_release(pdata);
  1477. }
  1478. static const char * const ti_sn_bridge_gpio_names[SN_NUM_GPIOS] = {
  1479. "GPIO1", "GPIO2", "GPIO3", "GPIO4"
  1480. };
  1481. static int ti_sn_gpio_probe(struct auxiliary_device *adev,
  1482. const struct auxiliary_device_id *id)
  1483. {
  1484. struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);
  1485. int ret;
  1486. /* Only init if someone is going to use us as a GPIO controller */
  1487. if (!of_property_read_bool(pdata->dev->of_node, "gpio-controller"))
  1488. return 0;
  1489. pdata->gchip.label = dev_name(pdata->dev);
  1490. pdata->gchip.parent = pdata->dev;
  1491. pdata->gchip.owner = THIS_MODULE;
  1492. pdata->gchip.of_xlate = tn_sn_bridge_of_xlate;
  1493. pdata->gchip.of_gpio_n_cells = 2;
  1494. pdata->gchip.request = ti_sn_bridge_gpio_request;
  1495. pdata->gchip.free = ti_sn_bridge_gpio_free;
  1496. pdata->gchip.get_direction = ti_sn_bridge_gpio_get_direction;
  1497. pdata->gchip.direction_input = ti_sn_bridge_gpio_direction_input;
  1498. pdata->gchip.direction_output = ti_sn_bridge_gpio_direction_output;
  1499. pdata->gchip.get = ti_sn_bridge_gpio_get;
  1500. pdata->gchip.set = ti_sn_bridge_gpio_set;
  1501. pdata->gchip.can_sleep = true;
  1502. pdata->gchip.names = ti_sn_bridge_gpio_names;
  1503. pdata->gchip.ngpio = SN_NUM_GPIOS;
  1504. pdata->gchip.base = -1;
  1505. ret = devm_gpiochip_add_data(&adev->dev, &pdata->gchip, pdata);
  1506. if (ret)
  1507. dev_err(pdata->dev, "can't add gpio chip\n");
  1508. return ret;
  1509. }
  1510. static const struct auxiliary_device_id ti_sn_gpio_id_table[] = {
  1511. { .name = "ti_sn65dsi86.gpio", },
  1512. {},
  1513. };
  1514. MODULE_DEVICE_TABLE(auxiliary, ti_sn_gpio_id_table);
  1515. static struct auxiliary_driver ti_sn_gpio_driver = {
  1516. .name = "gpio",
  1517. .probe = ti_sn_gpio_probe,
  1518. .id_table = ti_sn_gpio_id_table,
  1519. };
  1520. static int __init ti_sn_gpio_register(void)
  1521. {
  1522. return auxiliary_driver_register(&ti_sn_gpio_driver);
  1523. }
  1524. static void ti_sn_gpio_unregister(void)
  1525. {
  1526. auxiliary_driver_unregister(&ti_sn_gpio_driver);
  1527. }
  1528. #else
  1529. static inline int ti_sn_gpio_register(void) { return 0; }
  1530. static inline void ti_sn_gpio_unregister(void) {}
  1531. #endif
  1532. /* -----------------------------------------------------------------------------
  1533. * Probe & Remove
  1534. */
  1535. static void ti_sn65dsi86_runtime_disable(void *data)
  1536. {
  1537. pm_runtime_dont_use_autosuspend(data);
  1538. pm_runtime_disable(data);
  1539. }
  1540. static int ti_sn65dsi86_parse_regulators(struct ti_sn65dsi86 *pdata)
  1541. {
  1542. unsigned int i;
  1543. const char * const ti_sn_bridge_supply_names[] = {
  1544. "vcca", "vcc", "vccio", "vpll",
  1545. };
  1546. for (i = 0; i < SN_REGULATOR_SUPPLY_NUM; i++)
  1547. pdata->supplies[i].supply = ti_sn_bridge_supply_names[i];
  1548. return devm_regulator_bulk_get(pdata->dev, SN_REGULATOR_SUPPLY_NUM,
  1549. pdata->supplies);
  1550. }
  1551. static int ti_sn65dsi86_probe(struct i2c_client *client,
  1552. const struct i2c_device_id *id)
  1553. {
  1554. struct device *dev = &client->dev;
  1555. struct ti_sn65dsi86 *pdata;
  1556. int ret;
  1557. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1558. DRM_ERROR("device doesn't support I2C\n");
  1559. return -ENODEV;
  1560. }
  1561. pdata = devm_kzalloc(dev, sizeof(struct ti_sn65dsi86), GFP_KERNEL);
  1562. if (!pdata)
  1563. return -ENOMEM;
  1564. dev_set_drvdata(dev, pdata);
  1565. pdata->dev = dev;
  1566. mutex_init(&pdata->comms_mutex);
  1567. pdata->regmap = devm_regmap_init_i2c(client,
  1568. &ti_sn65dsi86_regmap_config);
  1569. if (IS_ERR(pdata->regmap))
  1570. return dev_err_probe(dev, PTR_ERR(pdata->regmap),
  1571. "regmap i2c init failed\n");
  1572. pdata->enable_gpio = devm_gpiod_get_optional(dev, "enable",
  1573. GPIOD_OUT_LOW);
  1574. if (IS_ERR(pdata->enable_gpio))
  1575. return dev_err_probe(dev, PTR_ERR(pdata->enable_gpio),
  1576. "failed to get enable gpio from DT\n");
  1577. ret = ti_sn65dsi86_parse_regulators(pdata);
  1578. if (ret)
  1579. return dev_err_probe(dev, ret, "failed to parse regulators\n");
  1580. pdata->refclk = devm_clk_get_optional(dev, "refclk");
  1581. if (IS_ERR(pdata->refclk))
  1582. return dev_err_probe(dev, PTR_ERR(pdata->refclk),
  1583. "failed to get reference clock\n");
  1584. pm_runtime_enable(dev);
  1585. pm_runtime_set_autosuspend_delay(pdata->dev, 500);
  1586. pm_runtime_use_autosuspend(pdata->dev);
  1587. ret = devm_add_action_or_reset(dev, ti_sn65dsi86_runtime_disable, dev);
  1588. if (ret)
  1589. return ret;
  1590. ti_sn65dsi86_debugfs_init(pdata);
  1591. /*
  1592. * Break ourselves up into a collection of aux devices. The only real
  1593. * motiviation here is to solve the chicken-and-egg problem of probe
  1594. * ordering. The bridge wants the panel to be there when it probes.
  1595. * The panel wants its HPD GPIO (provided by sn65dsi86 on some boards)
  1596. * when it probes. The panel and maybe backlight might want the DDC
  1597. * bus or the pwm_chip. Having sub-devices allows the some sub devices
  1598. * to finish probing even if others return -EPROBE_DEFER and gets us
  1599. * around the problems.
  1600. */
  1601. if (IS_ENABLED(CONFIG_OF_GPIO)) {
  1602. ret = ti_sn65dsi86_add_aux_device(pdata, &pdata->gpio_aux, "gpio");
  1603. if (ret)
  1604. return ret;
  1605. }
  1606. if (IS_ENABLED(CONFIG_PWM)) {
  1607. ret = ti_sn65dsi86_add_aux_device(pdata, &pdata->pwm_aux, "pwm");
  1608. if (ret)
  1609. return ret;
  1610. }
  1611. /*
  1612. * NOTE: At the end of the AUX channel probe we'll add the aux device
  1613. * for the bridge. This is because the bridge can't be used until the
  1614. * AUX channel is there and this is a very simple solution to the
  1615. * dependency problem.
  1616. */
  1617. return ti_sn65dsi86_add_aux_device(pdata, &pdata->aux_aux, "aux");
  1618. }
  1619. static struct i2c_device_id ti_sn65dsi86_id[] = {
  1620. { "ti,sn65dsi86", 0},
  1621. {},
  1622. };
  1623. MODULE_DEVICE_TABLE(i2c, ti_sn65dsi86_id);
  1624. static const struct of_device_id ti_sn65dsi86_match_table[] = {
  1625. {.compatible = "ti,sn65dsi86"},
  1626. {},
  1627. };
  1628. MODULE_DEVICE_TABLE(of, ti_sn65dsi86_match_table);
  1629. static struct i2c_driver ti_sn65dsi86_driver = {
  1630. .driver = {
  1631. .name = "ti_sn65dsi86",
  1632. .of_match_table = ti_sn65dsi86_match_table,
  1633. .pm = &ti_sn65dsi86_pm_ops,
  1634. },
  1635. .probe = ti_sn65dsi86_probe,
  1636. .id_table = ti_sn65dsi86_id,
  1637. };
  1638. static int __init ti_sn65dsi86_init(void)
  1639. {
  1640. int ret;
  1641. ret = i2c_add_driver(&ti_sn65dsi86_driver);
  1642. if (ret)
  1643. return ret;
  1644. ret = ti_sn_gpio_register();
  1645. if (ret)
  1646. goto err_main_was_registered;
  1647. ret = ti_sn_pwm_register();
  1648. if (ret)
  1649. goto err_gpio_was_registered;
  1650. ret = auxiliary_driver_register(&ti_sn_aux_driver);
  1651. if (ret)
  1652. goto err_pwm_was_registered;
  1653. ret = auxiliary_driver_register(&ti_sn_bridge_driver);
  1654. if (ret)
  1655. goto err_aux_was_registered;
  1656. return 0;
  1657. err_aux_was_registered:
  1658. auxiliary_driver_unregister(&ti_sn_aux_driver);
  1659. err_pwm_was_registered:
  1660. ti_sn_pwm_unregister();
  1661. err_gpio_was_registered:
  1662. ti_sn_gpio_unregister();
  1663. err_main_was_registered:
  1664. i2c_del_driver(&ti_sn65dsi86_driver);
  1665. return ret;
  1666. }
  1667. module_init(ti_sn65dsi86_init);
  1668. static void __exit ti_sn65dsi86_exit(void)
  1669. {
  1670. auxiliary_driver_unregister(&ti_sn_bridge_driver);
  1671. auxiliary_driver_unregister(&ti_sn_aux_driver);
  1672. ti_sn_pwm_unregister();
  1673. ti_sn_gpio_unregister();
  1674. i2c_del_driver(&ti_sn65dsi86_driver);
  1675. }
  1676. module_exit(ti_sn65dsi86_exit);
  1677. MODULE_AUTHOR("Sandeep Panda <[email protected]>");
  1678. MODULE_DESCRIPTION("sn65dsi86 DSI to eDP bridge driver");
  1679. MODULE_LICENSE("GPL v2");