phy-fsl-imx8-mipi-dphy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2017,2018 NXP
  4. * Copyright 2019 Purism SPC
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/delay.h>
  10. #include <linux/firmware/imx/ipc.h>
  11. #include <linux/firmware/imx/svc/misc.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <dt-bindings/firmware/imx/rsrc.h>
  22. /* Control and Status Registers(CSR) */
  23. #define PHY_CTRL 0x00
  24. #define CCM_MASK GENMASK(7, 5)
  25. #define CCM(n) FIELD_PREP(CCM_MASK, (n))
  26. #define CCM_1_2V 0x5
  27. #define CA_MASK GENMASK(4, 2)
  28. #define CA_3_51MA 0x4
  29. #define CA(n) FIELD_PREP(CA_MASK, (n))
  30. #define RFB BIT(1)
  31. #define LVDS_EN BIT(0)
  32. /* DPHY registers */
  33. #define DPHY_PD_DPHY 0x00
  34. #define DPHY_M_PRG_HS_PREPARE 0x04
  35. #define DPHY_MC_PRG_HS_PREPARE 0x08
  36. #define DPHY_M_PRG_HS_ZERO 0x0c
  37. #define DPHY_MC_PRG_HS_ZERO 0x10
  38. #define DPHY_M_PRG_HS_TRAIL 0x14
  39. #define DPHY_MC_PRG_HS_TRAIL 0x18
  40. #define DPHY_PD_PLL 0x1c
  41. #define DPHY_TST 0x20
  42. #define DPHY_CN 0x24
  43. #define DPHY_CM 0x28
  44. #define DPHY_CO 0x2c
  45. #define DPHY_LOCK 0x30
  46. #define DPHY_LOCK_BYP 0x34
  47. #define DPHY_REG_BYPASS_PLL 0x4C
  48. #define MBPS(x) ((x) * 1000000)
  49. #define DATA_RATE_MAX_SPEED MBPS(1500)
  50. #define DATA_RATE_MIN_SPEED MBPS(80)
  51. #define PLL_LOCK_SLEEP 10
  52. #define PLL_LOCK_TIMEOUT 1000
  53. #define CN_BUF 0xcb7a89c0
  54. #define CO_BUF 0x63
  55. #define CM(x) ( \
  56. ((x) < 32) ? 0xe0 | ((x) - 16) : \
  57. ((x) < 64) ? 0xc0 | ((x) - 32) : \
  58. ((x) < 128) ? 0x80 | ((x) - 64) : \
  59. ((x) - 128))
  60. #define CN(x) (((x) == 1) ? 0x1f : (((CN_BUF) >> ((x) - 1)) & 0x1f))
  61. #define CO(x) ((CO_BUF) >> (8 - (x)) & 0x03)
  62. /* PHY power on is active low */
  63. #define PWR_ON 0
  64. #define PWR_OFF 1
  65. #define MIN_VCO_FREQ 640000000
  66. #define MAX_VCO_FREQ 1500000000
  67. #define MIN_LVDS_REFCLK_FREQ 24000000
  68. #define MAX_LVDS_REFCLK_FREQ 150000000
  69. enum mixel_dphy_devtype {
  70. MIXEL_IMX8MQ,
  71. MIXEL_IMX8QXP,
  72. };
  73. struct mixel_dphy_devdata {
  74. u8 reg_tx_rcal;
  75. u8 reg_auto_pd_en;
  76. u8 reg_rxlprp;
  77. u8 reg_rxcdrp;
  78. u8 reg_rxhs_settle;
  79. bool is_combo; /* MIPI DPHY and LVDS PHY combo */
  80. };
  81. static const struct mixel_dphy_devdata mixel_dphy_devdata[] = {
  82. [MIXEL_IMX8MQ] = {
  83. .reg_tx_rcal = 0x38,
  84. .reg_auto_pd_en = 0x3c,
  85. .reg_rxlprp = 0x40,
  86. .reg_rxcdrp = 0x44,
  87. .reg_rxhs_settle = 0x48,
  88. .is_combo = false,
  89. },
  90. [MIXEL_IMX8QXP] = {
  91. .is_combo = true,
  92. },
  93. };
  94. struct mixel_dphy_cfg {
  95. /* DPHY PLL parameters */
  96. u32 cm;
  97. u32 cn;
  98. u32 co;
  99. /* DPHY register values */
  100. u8 mc_prg_hs_prepare;
  101. u8 m_prg_hs_prepare;
  102. u8 mc_prg_hs_zero;
  103. u8 m_prg_hs_zero;
  104. u8 mc_prg_hs_trail;
  105. u8 m_prg_hs_trail;
  106. u8 rxhs_settle;
  107. };
  108. struct mixel_dphy_priv {
  109. struct mixel_dphy_cfg cfg;
  110. struct regmap *regmap;
  111. struct regmap *lvds_regmap;
  112. struct clk *phy_ref_clk;
  113. const struct mixel_dphy_devdata *devdata;
  114. struct imx_sc_ipc *ipc_handle;
  115. bool is_slave;
  116. int id;
  117. };
  118. static const struct regmap_config mixel_dphy_regmap_config = {
  119. .reg_bits = 8,
  120. .val_bits = 32,
  121. .reg_stride = 4,
  122. .max_register = DPHY_REG_BYPASS_PLL,
  123. .name = "mipi-dphy",
  124. };
  125. static int phy_write(struct phy *phy, u32 value, unsigned int reg)
  126. {
  127. struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
  128. int ret;
  129. ret = regmap_write(priv->regmap, reg, value);
  130. if (ret < 0)
  131. dev_err(&phy->dev, "Failed to write DPHY reg %d: %d\n", reg,
  132. ret);
  133. return ret;
  134. }
  135. /*
  136. * Find a ratio close to the desired one using continued fraction
  137. * approximation ending either at exact match or maximum allowed
  138. * nominator, denominator.
  139. */
  140. static void get_best_ratio(u32 *pnum, u32 *pdenom, u32 max_n, u32 max_d)
  141. {
  142. u32 a = *pnum;
  143. u32 b = *pdenom;
  144. u32 c;
  145. u32 n[] = {0, 1};
  146. u32 d[] = {1, 0};
  147. u32 whole;
  148. unsigned int i = 1;
  149. while (b) {
  150. i ^= 1;
  151. whole = a / b;
  152. n[i] += (n[i ^ 1] * whole);
  153. d[i] += (d[i ^ 1] * whole);
  154. if ((n[i] > max_n) || (d[i] > max_d)) {
  155. i ^= 1;
  156. break;
  157. }
  158. c = a - (b * whole);
  159. a = b;
  160. b = c;
  161. }
  162. *pnum = n[i];
  163. *pdenom = d[i];
  164. }
  165. static int mixel_dphy_config_from_opts(struct phy *phy,
  166. struct phy_configure_opts_mipi_dphy *dphy_opts,
  167. struct mixel_dphy_cfg *cfg)
  168. {
  169. struct mixel_dphy_priv *priv = dev_get_drvdata(phy->dev.parent);
  170. unsigned long ref_clk = clk_get_rate(priv->phy_ref_clk);
  171. u32 lp_t, numerator, denominator;
  172. unsigned long long tmp;
  173. u32 n;
  174. int i;
  175. if (dphy_opts->hs_clk_rate > DATA_RATE_MAX_SPEED ||
  176. dphy_opts->hs_clk_rate < DATA_RATE_MIN_SPEED)
  177. return -EINVAL;
  178. numerator = dphy_opts->hs_clk_rate;
  179. denominator = ref_clk;
  180. get_best_ratio(&numerator, &denominator, 255, 256);
  181. if (!numerator || !denominator) {
  182. dev_err(&phy->dev, "Invalid %d/%d for %ld/%ld\n",
  183. numerator, denominator,
  184. dphy_opts->hs_clk_rate, ref_clk);
  185. return -EINVAL;
  186. }
  187. while ((numerator < 16) && (denominator <= 128)) {
  188. numerator <<= 1;
  189. denominator <<= 1;
  190. }
  191. /*
  192. * CM ranges between 16 and 255
  193. * CN ranges between 1 and 32
  194. * CO is power of 2: 1, 2, 4, 8
  195. */
  196. i = __ffs(denominator);
  197. if (i > 3)
  198. i = 3;
  199. cfg->cn = denominator >> i;
  200. cfg->co = 1 << i;
  201. cfg->cm = numerator;
  202. if (cfg->cm < 16 || cfg->cm > 255 ||
  203. cfg->cn < 1 || cfg->cn > 32 ||
  204. cfg->co < 1 || cfg->co > 8) {
  205. dev_err(&phy->dev, "Invalid CM/CN/CO values: %u/%u/%u\n",
  206. cfg->cm, cfg->cn, cfg->co);
  207. dev_err(&phy->dev, "for hs_clk/ref_clk=%ld/%ld ~ %d/%d\n",
  208. dphy_opts->hs_clk_rate, ref_clk,
  209. numerator, denominator);
  210. return -EINVAL;
  211. }
  212. dev_dbg(&phy->dev, "hs_clk/ref_clk=%ld/%ld ~ %d/%d\n",
  213. dphy_opts->hs_clk_rate, ref_clk, numerator, denominator);
  214. /* LP clock period */
  215. tmp = 1000000000000LL;
  216. do_div(tmp, dphy_opts->lp_clk_rate); /* ps */
  217. if (tmp > ULONG_MAX)
  218. return -EINVAL;
  219. lp_t = tmp;
  220. dev_dbg(&phy->dev, "LP clock %lu, period: %u ps\n",
  221. dphy_opts->lp_clk_rate, lp_t);
  222. /* hs_prepare: in lp clock periods */
  223. if (2 * dphy_opts->hs_prepare > 5 * lp_t) {
  224. dev_err(&phy->dev,
  225. "hs_prepare (%u) > 2.5 * lp clock period (%u)\n",
  226. dphy_opts->hs_prepare, lp_t);
  227. return -EINVAL;
  228. }
  229. /* 00: lp_t, 01: 1.5 * lp_t, 10: 2 * lp_t, 11: 2.5 * lp_t */
  230. if (dphy_opts->hs_prepare < lp_t) {
  231. n = 0;
  232. } else {
  233. tmp = 2 * (dphy_opts->hs_prepare - lp_t);
  234. do_div(tmp, lp_t);
  235. n = tmp;
  236. }
  237. cfg->m_prg_hs_prepare = n;
  238. /* clk_prepare: in lp clock periods */
  239. if (2 * dphy_opts->clk_prepare > 3 * lp_t) {
  240. dev_err(&phy->dev,
  241. "clk_prepare (%u) > 1.5 * lp clock period (%u)\n",
  242. dphy_opts->clk_prepare, lp_t);
  243. return -EINVAL;
  244. }
  245. /* 00: lp_t, 01: 1.5 * lp_t */
  246. cfg->mc_prg_hs_prepare = dphy_opts->clk_prepare > lp_t ? 1 : 0;
  247. /* hs_zero: formula from NXP BSP */
  248. n = (144 * (dphy_opts->hs_clk_rate / 1000000) - 47500) / 10000;
  249. cfg->m_prg_hs_zero = n < 1 ? 1 : n;
  250. /* clk_zero: formula from NXP BSP */
  251. n = (34 * (dphy_opts->hs_clk_rate / 1000000) - 2500) / 1000;
  252. cfg->mc_prg_hs_zero = n < 1 ? 1 : n;
  253. /* clk_trail, hs_trail: formula from NXP BSP */
  254. n = (103 * (dphy_opts->hs_clk_rate / 1000000) + 10000) / 10000;
  255. if (n > 15)
  256. n = 15;
  257. if (n < 1)
  258. n = 1;
  259. cfg->m_prg_hs_trail = n;
  260. cfg->mc_prg_hs_trail = n;
  261. /* rxhs_settle: formula from NXP BSP */
  262. if (dphy_opts->hs_clk_rate < MBPS(80))
  263. cfg->rxhs_settle = 0x0d;
  264. else if (dphy_opts->hs_clk_rate < MBPS(90))
  265. cfg->rxhs_settle = 0x0c;
  266. else if (dphy_opts->hs_clk_rate < MBPS(125))
  267. cfg->rxhs_settle = 0x0b;
  268. else if (dphy_opts->hs_clk_rate < MBPS(150))
  269. cfg->rxhs_settle = 0x0a;
  270. else if (dphy_opts->hs_clk_rate < MBPS(225))
  271. cfg->rxhs_settle = 0x09;
  272. else if (dphy_opts->hs_clk_rate < MBPS(500))
  273. cfg->rxhs_settle = 0x08;
  274. else
  275. cfg->rxhs_settle = 0x07;
  276. dev_dbg(&phy->dev, "phy_config: %u %u %u %u %u %u %u\n",
  277. cfg->m_prg_hs_prepare, cfg->mc_prg_hs_prepare,
  278. cfg->m_prg_hs_zero, cfg->mc_prg_hs_zero,
  279. cfg->m_prg_hs_trail, cfg->mc_prg_hs_trail,
  280. cfg->rxhs_settle);
  281. return 0;
  282. }
  283. static void mixel_phy_set_hs_timings(struct phy *phy)
  284. {
  285. struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
  286. phy_write(phy, priv->cfg.m_prg_hs_prepare, DPHY_M_PRG_HS_PREPARE);
  287. phy_write(phy, priv->cfg.mc_prg_hs_prepare, DPHY_MC_PRG_HS_PREPARE);
  288. phy_write(phy, priv->cfg.m_prg_hs_zero, DPHY_M_PRG_HS_ZERO);
  289. phy_write(phy, priv->cfg.mc_prg_hs_zero, DPHY_MC_PRG_HS_ZERO);
  290. phy_write(phy, priv->cfg.m_prg_hs_trail, DPHY_M_PRG_HS_TRAIL);
  291. phy_write(phy, priv->cfg.mc_prg_hs_trail, DPHY_MC_PRG_HS_TRAIL);
  292. phy_write(phy, priv->cfg.rxhs_settle, priv->devdata->reg_rxhs_settle);
  293. }
  294. static int mixel_dphy_set_pll_params(struct phy *phy)
  295. {
  296. struct mixel_dphy_priv *priv = dev_get_drvdata(phy->dev.parent);
  297. if (priv->cfg.cm < 16 || priv->cfg.cm > 255 ||
  298. priv->cfg.cn < 1 || priv->cfg.cn > 32 ||
  299. priv->cfg.co < 1 || priv->cfg.co > 8) {
  300. dev_err(&phy->dev, "Invalid CM/CN/CO values! (%u/%u/%u)\n",
  301. priv->cfg.cm, priv->cfg.cn, priv->cfg.co);
  302. return -EINVAL;
  303. }
  304. dev_dbg(&phy->dev, "Using CM:%u CN:%u CO:%u\n",
  305. priv->cfg.cm, priv->cfg.cn, priv->cfg.co);
  306. phy_write(phy, CM(priv->cfg.cm), DPHY_CM);
  307. phy_write(phy, CN(priv->cfg.cn), DPHY_CN);
  308. phy_write(phy, CO(priv->cfg.co), DPHY_CO);
  309. return 0;
  310. }
  311. static int
  312. mixel_dphy_configure_mipi_dphy(struct phy *phy, union phy_configure_opts *opts)
  313. {
  314. struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
  315. struct mixel_dphy_cfg cfg = { 0 };
  316. int ret;
  317. ret = mixel_dphy_config_from_opts(phy, &opts->mipi_dphy, &cfg);
  318. if (ret)
  319. return ret;
  320. /* Update the configuration */
  321. memcpy(&priv->cfg, &cfg, sizeof(struct mixel_dphy_cfg));
  322. phy_write(phy, 0x00, DPHY_LOCK_BYP);
  323. phy_write(phy, 0x01, priv->devdata->reg_tx_rcal);
  324. phy_write(phy, 0x00, priv->devdata->reg_auto_pd_en);
  325. phy_write(phy, 0x02, priv->devdata->reg_rxlprp);
  326. phy_write(phy, 0x02, priv->devdata->reg_rxcdrp);
  327. phy_write(phy, 0x25, DPHY_TST);
  328. mixel_phy_set_hs_timings(phy);
  329. ret = mixel_dphy_set_pll_params(phy);
  330. if (ret < 0)
  331. return ret;
  332. return 0;
  333. }
  334. static int
  335. mixel_dphy_configure_lvds_phy(struct phy *phy, union phy_configure_opts *opts)
  336. {
  337. struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
  338. struct phy_configure_opts_lvds *lvds_opts = &opts->lvds;
  339. unsigned long data_rate;
  340. unsigned long fvco;
  341. u32 rsc;
  342. u32 co;
  343. int ret;
  344. priv->is_slave = lvds_opts->is_slave;
  345. /* LVDS interface pins */
  346. regmap_write(priv->lvds_regmap, PHY_CTRL,
  347. CCM(CCM_1_2V) | CA(CA_3_51MA) | RFB);
  348. /* enable MODE8 only for slave LVDS PHY */
  349. rsc = priv->id ? IMX_SC_R_MIPI_1 : IMX_SC_R_MIPI_0;
  350. ret = imx_sc_misc_set_control(priv->ipc_handle, rsc, IMX_SC_C_DUAL_MODE,
  351. lvds_opts->is_slave);
  352. if (ret) {
  353. dev_err(&phy->dev, "Failed to configure MODE8: %d\n", ret);
  354. return ret;
  355. }
  356. /*
  357. * Choose an appropriate divider ratio to meet the requirement of
  358. * PLL VCO frequency range.
  359. *
  360. * ----- 640MHz ~ 1500MHz ------------ ---------------
  361. * | VCO | ----------------> | CO divider | -> | LVDS data rate|
  362. * ----- FVCO ------------ ---------------
  363. * 1/2/4/8 div 7 * differential_clk_rate
  364. */
  365. data_rate = 7 * lvds_opts->differential_clk_rate;
  366. for (co = 1; co <= 8; co *= 2) {
  367. fvco = data_rate * co;
  368. if (fvco >= MIN_VCO_FREQ)
  369. break;
  370. }
  371. if (fvco < MIN_VCO_FREQ || fvco > MAX_VCO_FREQ) {
  372. dev_err(&phy->dev, "VCO frequency %lu is out of range\n", fvco);
  373. return -ERANGE;
  374. }
  375. /*
  376. * CO is configurable, while CN and CM are not,
  377. * as fixed ratios 1 and 7 are applied respectively.
  378. */
  379. phy_write(phy, __ffs(co), DPHY_CO);
  380. /* set reference clock rate */
  381. clk_set_rate(priv->phy_ref_clk, lvds_opts->differential_clk_rate);
  382. return ret;
  383. }
  384. static int mixel_dphy_configure(struct phy *phy, union phy_configure_opts *opts)
  385. {
  386. if (!opts) {
  387. dev_err(&phy->dev, "No configuration options\n");
  388. return -EINVAL;
  389. }
  390. if (phy->attrs.mode == PHY_MODE_MIPI_DPHY)
  391. return mixel_dphy_configure_mipi_dphy(phy, opts);
  392. else if (phy->attrs.mode == PHY_MODE_LVDS)
  393. return mixel_dphy_configure_lvds_phy(phy, opts);
  394. dev_err(&phy->dev,
  395. "Failed to configure PHY with invalid PHY mode: %d\n", phy->attrs.mode);
  396. return -EINVAL;
  397. }
  398. static int
  399. mixel_dphy_validate_lvds_phy(struct phy *phy, union phy_configure_opts *opts)
  400. {
  401. struct phy_configure_opts_lvds *lvds_cfg = &opts->lvds;
  402. if (lvds_cfg->bits_per_lane_and_dclk_cycle != 7) {
  403. dev_err(&phy->dev, "Invalid bits per LVDS data lane: %u\n",
  404. lvds_cfg->bits_per_lane_and_dclk_cycle);
  405. return -EINVAL;
  406. }
  407. if (lvds_cfg->lanes != 4) {
  408. dev_err(&phy->dev, "Invalid LVDS data lanes: %u\n", lvds_cfg->lanes);
  409. return -EINVAL;
  410. }
  411. if (lvds_cfg->differential_clk_rate < MIN_LVDS_REFCLK_FREQ ||
  412. lvds_cfg->differential_clk_rate > MAX_LVDS_REFCLK_FREQ) {
  413. dev_err(&phy->dev,
  414. "Invalid LVDS differential clock rate: %lu\n",
  415. lvds_cfg->differential_clk_rate);
  416. return -EINVAL;
  417. }
  418. return 0;
  419. }
  420. static int mixel_dphy_validate(struct phy *phy, enum phy_mode mode, int submode,
  421. union phy_configure_opts *opts)
  422. {
  423. if (mode == PHY_MODE_MIPI_DPHY) {
  424. struct mixel_dphy_cfg mipi_dphy_cfg = { 0 };
  425. return mixel_dphy_config_from_opts(phy, &opts->mipi_dphy,
  426. &mipi_dphy_cfg);
  427. } else if (mode == PHY_MODE_LVDS) {
  428. return mixel_dphy_validate_lvds_phy(phy, opts);
  429. }
  430. dev_err(&phy->dev,
  431. "Failed to validate PHY with invalid PHY mode: %d\n", mode);
  432. return -EINVAL;
  433. }
  434. static int mixel_dphy_init(struct phy *phy)
  435. {
  436. phy_write(phy, PWR_OFF, DPHY_PD_PLL);
  437. phy_write(phy, PWR_OFF, DPHY_PD_DPHY);
  438. return 0;
  439. }
  440. static int mixel_dphy_exit(struct phy *phy)
  441. {
  442. phy_write(phy, 0, DPHY_CM);
  443. phy_write(phy, 0, DPHY_CN);
  444. phy_write(phy, 0, DPHY_CO);
  445. return 0;
  446. }
  447. static int mixel_dphy_power_on_mipi_dphy(struct phy *phy)
  448. {
  449. struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
  450. u32 locked;
  451. int ret;
  452. phy_write(phy, PWR_ON, DPHY_PD_PLL);
  453. ret = regmap_read_poll_timeout(priv->regmap, DPHY_LOCK, locked,
  454. locked, PLL_LOCK_SLEEP,
  455. PLL_LOCK_TIMEOUT);
  456. if (ret < 0) {
  457. dev_err(&phy->dev, "Could not get DPHY lock (%d)!\n", ret);
  458. return ret;
  459. }
  460. phy_write(phy, PWR_ON, DPHY_PD_DPHY);
  461. return 0;
  462. }
  463. static int mixel_dphy_power_on_lvds_phy(struct phy *phy)
  464. {
  465. struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
  466. u32 locked;
  467. int ret;
  468. regmap_update_bits(priv->lvds_regmap, PHY_CTRL, LVDS_EN, LVDS_EN);
  469. phy_write(phy, PWR_ON, DPHY_PD_DPHY);
  470. phy_write(phy, PWR_ON, DPHY_PD_PLL);
  471. /* do not wait for slave LVDS PHY being locked */
  472. if (priv->is_slave)
  473. return 0;
  474. ret = regmap_read_poll_timeout(priv->regmap, DPHY_LOCK, locked,
  475. locked, PLL_LOCK_SLEEP,
  476. PLL_LOCK_TIMEOUT);
  477. if (ret < 0) {
  478. dev_err(&phy->dev, "Could not get LVDS PHY lock (%d)!\n", ret);
  479. return ret;
  480. }
  481. return 0;
  482. }
  483. static int mixel_dphy_power_on(struct phy *phy)
  484. {
  485. struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
  486. int ret;
  487. ret = clk_prepare_enable(priv->phy_ref_clk);
  488. if (ret < 0)
  489. return ret;
  490. if (phy->attrs.mode == PHY_MODE_MIPI_DPHY) {
  491. ret = mixel_dphy_power_on_mipi_dphy(phy);
  492. } else if (phy->attrs.mode == PHY_MODE_LVDS) {
  493. ret = mixel_dphy_power_on_lvds_phy(phy);
  494. } else {
  495. dev_err(&phy->dev,
  496. "Failed to power on PHY with invalid PHY mode: %d\n",
  497. phy->attrs.mode);
  498. ret = -EINVAL;
  499. }
  500. if (ret)
  501. goto clock_disable;
  502. return 0;
  503. clock_disable:
  504. clk_disable_unprepare(priv->phy_ref_clk);
  505. return ret;
  506. }
  507. static int mixel_dphy_power_off(struct phy *phy)
  508. {
  509. struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
  510. phy_write(phy, PWR_OFF, DPHY_PD_PLL);
  511. phy_write(phy, PWR_OFF, DPHY_PD_DPHY);
  512. if (phy->attrs.mode == PHY_MODE_LVDS)
  513. regmap_update_bits(priv->lvds_regmap, PHY_CTRL, LVDS_EN, 0);
  514. clk_disable_unprepare(priv->phy_ref_clk);
  515. return 0;
  516. }
  517. static int mixel_dphy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
  518. {
  519. struct mixel_dphy_priv *priv = phy_get_drvdata(phy);
  520. int ret;
  521. if (priv->devdata->is_combo && mode != PHY_MODE_LVDS) {
  522. dev_err(&phy->dev, "Failed to set PHY mode for combo PHY\n");
  523. return -EINVAL;
  524. }
  525. if (!priv->devdata->is_combo && mode != PHY_MODE_MIPI_DPHY) {
  526. dev_err(&phy->dev, "Failed to set PHY mode to MIPI DPHY\n");
  527. return -EINVAL;
  528. }
  529. if (priv->devdata->is_combo) {
  530. u32 rsc = priv->id ? IMX_SC_R_MIPI_1 : IMX_SC_R_MIPI_0;
  531. ret = imx_sc_misc_set_control(priv->ipc_handle,
  532. rsc, IMX_SC_C_MODE,
  533. mode == PHY_MODE_LVDS);
  534. if (ret) {
  535. dev_err(&phy->dev,
  536. "Failed to set PHY mode via SCU ipc: %d\n", ret);
  537. return ret;
  538. }
  539. }
  540. return 0;
  541. }
  542. static const struct phy_ops mixel_dphy_phy_ops = {
  543. .init = mixel_dphy_init,
  544. .exit = mixel_dphy_exit,
  545. .power_on = mixel_dphy_power_on,
  546. .power_off = mixel_dphy_power_off,
  547. .set_mode = mixel_dphy_set_mode,
  548. .configure = mixel_dphy_configure,
  549. .validate = mixel_dphy_validate,
  550. .owner = THIS_MODULE,
  551. };
  552. static const struct of_device_id mixel_dphy_of_match[] = {
  553. { .compatible = "fsl,imx8mq-mipi-dphy",
  554. .data = &mixel_dphy_devdata[MIXEL_IMX8MQ] },
  555. { .compatible = "fsl,imx8qxp-mipi-dphy",
  556. .data = &mixel_dphy_devdata[MIXEL_IMX8QXP] },
  557. { /* sentinel */ },
  558. };
  559. MODULE_DEVICE_TABLE(of, mixel_dphy_of_match);
  560. static int mixel_dphy_probe(struct platform_device *pdev)
  561. {
  562. struct device *dev = &pdev->dev;
  563. struct device_node *np = dev->of_node;
  564. struct phy_provider *phy_provider;
  565. struct mixel_dphy_priv *priv;
  566. struct phy *phy;
  567. void __iomem *base;
  568. int ret;
  569. if (!np)
  570. return -ENODEV;
  571. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  572. if (!priv)
  573. return -ENOMEM;
  574. priv->devdata = of_device_get_match_data(&pdev->dev);
  575. if (!priv->devdata)
  576. return -EINVAL;
  577. base = devm_platform_ioremap_resource(pdev, 0);
  578. if (IS_ERR(base))
  579. return PTR_ERR(base);
  580. priv->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  581. &mixel_dphy_regmap_config);
  582. if (IS_ERR(priv->regmap)) {
  583. dev_err(dev, "Couldn't create the DPHY regmap\n");
  584. return PTR_ERR(priv->regmap);
  585. }
  586. priv->phy_ref_clk = devm_clk_get(&pdev->dev, "phy_ref");
  587. if (IS_ERR(priv->phy_ref_clk)) {
  588. dev_err(dev, "No phy_ref clock found\n");
  589. return PTR_ERR(priv->phy_ref_clk);
  590. }
  591. dev_dbg(dev, "phy_ref clock rate: %lu\n",
  592. clk_get_rate(priv->phy_ref_clk));
  593. if (priv->devdata->is_combo) {
  594. priv->lvds_regmap =
  595. syscon_regmap_lookup_by_phandle(np, "fsl,syscon");
  596. if (IS_ERR(priv->lvds_regmap)) {
  597. ret = PTR_ERR(priv->lvds_regmap);
  598. dev_err_probe(dev, ret, "Failed to get LVDS regmap\n");
  599. return ret;
  600. }
  601. priv->id = of_alias_get_id(np, "mipi_dphy");
  602. if (priv->id < 0) {
  603. dev_err(dev, "Failed to get phy node alias id: %d\n",
  604. priv->id);
  605. return priv->id;
  606. }
  607. ret = imx_scu_get_handle(&priv->ipc_handle);
  608. if (ret) {
  609. dev_err_probe(dev, ret,
  610. "Failed to get SCU ipc handle\n");
  611. return ret;
  612. }
  613. }
  614. dev_set_drvdata(dev, priv);
  615. phy = devm_phy_create(dev, np, &mixel_dphy_phy_ops);
  616. if (IS_ERR(phy)) {
  617. dev_err(dev, "Failed to create phy %ld\n", PTR_ERR(phy));
  618. return PTR_ERR(phy);
  619. }
  620. phy_set_drvdata(phy, priv);
  621. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  622. return PTR_ERR_OR_ZERO(phy_provider);
  623. }
  624. static struct platform_driver mixel_dphy_driver = {
  625. .probe = mixel_dphy_probe,
  626. .driver = {
  627. .name = "mixel-mipi-dphy",
  628. .of_match_table = mixel_dphy_of_match,
  629. }
  630. };
  631. module_platform_driver(mixel_dphy_driver);
  632. MODULE_AUTHOR("NXP Semiconductor");
  633. MODULE_DESCRIPTION("Mixel MIPI-DSI PHY driver");
  634. MODULE_LICENSE("GPL");