parade-ps8622.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Parade PS8622 eDP/LVDS bridge driver
  4. *
  5. * Copyright (C) 2014 Google, Inc.
  6. */
  7. #include <linux/backlight.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/pm.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_bridge.h>
  18. #include <drm/drm_crtc.h>
  19. #include <drm/drm_of.h>
  20. #include <drm/drm_print.h>
  21. #include <drm/drm_probe_helper.h>
  22. /* Brightness scale on the Parade chip */
  23. #define PS8622_MAX_BRIGHTNESS 0xff
  24. /* Timings taken from the version 1.7 datasheet for the PS8622/PS8625 */
  25. #define PS8622_POWER_RISE_T1_MIN_US 10
  26. #define PS8622_POWER_RISE_T1_MAX_US 10000
  27. #define PS8622_RST_HIGH_T2_MIN_US 3000
  28. #define PS8622_RST_HIGH_T2_MAX_US 30000
  29. #define PS8622_PWMO_END_T12_MS 200
  30. #define PS8622_POWER_FALL_T16_MAX_US 10000
  31. #define PS8622_POWER_OFF_T17_MS 500
  32. #if ((PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US) > \
  33. (PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US))
  34. #error "T2.min + T1.max must be less than T2.max + T1.min"
  35. #endif
  36. struct ps8622_bridge {
  37. struct i2c_client *client;
  38. struct drm_bridge bridge;
  39. struct drm_bridge *panel_bridge;
  40. struct regulator *v12;
  41. struct backlight_device *bl;
  42. struct gpio_desc *gpio_slp;
  43. struct gpio_desc *gpio_rst;
  44. u32 max_lane_count;
  45. u32 lane_count;
  46. bool enabled;
  47. };
  48. static inline struct ps8622_bridge *
  49. bridge_to_ps8622(struct drm_bridge *bridge)
  50. {
  51. return container_of(bridge, struct ps8622_bridge, bridge);
  52. }
  53. static int ps8622_set(struct i2c_client *client, u8 page, u8 reg, u8 val)
  54. {
  55. int ret;
  56. struct i2c_adapter *adap = client->adapter;
  57. struct i2c_msg msg;
  58. u8 data[] = {reg, val};
  59. msg.addr = client->addr + page;
  60. msg.flags = 0;
  61. msg.len = sizeof(data);
  62. msg.buf = data;
  63. ret = i2c_transfer(adap, &msg, 1);
  64. if (ret != 1)
  65. pr_warn("PS8622 I2C write (0x%02x,0x%02x,0x%02x) failed: %d\n",
  66. client->addr + page, reg, val, ret);
  67. return !(ret == 1);
  68. }
  69. static int ps8622_send_config(struct ps8622_bridge *ps8622)
  70. {
  71. struct i2c_client *cl = ps8622->client;
  72. int err = 0;
  73. /* HPD low */
  74. err = ps8622_set(cl, 0x02, 0xa1, 0x01);
  75. if (err)
  76. goto error;
  77. /* SW setting: [1:0] SW output 1.2V voltage is lower to 96% */
  78. err = ps8622_set(cl, 0x04, 0x14, 0x01);
  79. if (err)
  80. goto error;
  81. /* RCO SS setting: [5:4] = b01 0.5%, b10 1%, b11 1.5% */
  82. err = ps8622_set(cl, 0x04, 0xe3, 0x20);
  83. if (err)
  84. goto error;
  85. /* [7] RCO SS enable */
  86. err = ps8622_set(cl, 0x04, 0xe2, 0x80);
  87. if (err)
  88. goto error;
  89. /* RPHY Setting
  90. * [3:2] CDR tune wait cycle before measure for fine tune
  91. * b00: 1us b01: 0.5us b10:2us, b11: 4us
  92. */
  93. err = ps8622_set(cl, 0x04, 0x8a, 0x0c);
  94. if (err)
  95. goto error;
  96. /* [3] RFD always on */
  97. err = ps8622_set(cl, 0x04, 0x89, 0x08);
  98. if (err)
  99. goto error;
  100. /* CTN lock in/out: 20000ppm/80000ppm. Lock out 2 times. */
  101. err = ps8622_set(cl, 0x04, 0x71, 0x2d);
  102. if (err)
  103. goto error;
  104. /* 2.7G CDR settings: NOF=40LSB for HBR CDR setting */
  105. err = ps8622_set(cl, 0x04, 0x7d, 0x07);
  106. if (err)
  107. goto error;
  108. /* [1:0] Fmin=+4bands */
  109. err = ps8622_set(cl, 0x04, 0x7b, 0x00);
  110. if (err)
  111. goto error;
  112. /* [7:5] DCO_FTRNG=+-40% */
  113. err = ps8622_set(cl, 0x04, 0x7a, 0xfd);
  114. if (err)
  115. goto error;
  116. /* 1.62G CDR settings: [5:2]NOF=64LSB [1:0]DCO scale is 2/5 */
  117. err = ps8622_set(cl, 0x04, 0xc0, 0x12);
  118. if (err)
  119. goto error;
  120. /* Gitune=-37% */
  121. err = ps8622_set(cl, 0x04, 0xc1, 0x92);
  122. if (err)
  123. goto error;
  124. /* Fbstep=100% */
  125. err = ps8622_set(cl, 0x04, 0xc2, 0x1c);
  126. if (err)
  127. goto error;
  128. /* [7] LOS signal disable */
  129. err = ps8622_set(cl, 0x04, 0x32, 0x80);
  130. if (err)
  131. goto error;
  132. /* RPIO Setting: [7:4] LVDS driver bias current : 75% (250mV swing) */
  133. err = ps8622_set(cl, 0x04, 0x00, 0xb0);
  134. if (err)
  135. goto error;
  136. /* [7:6] Right-bar GPIO output strength is 8mA */
  137. err = ps8622_set(cl, 0x04, 0x15, 0x40);
  138. if (err)
  139. goto error;
  140. /* EQ Training State Machine Setting, RCO calibration start */
  141. err = ps8622_set(cl, 0x04, 0x54, 0x10);
  142. if (err)
  143. goto error;
  144. /* Logic, needs more than 10 I2C command */
  145. /* [4:0] MAX_LANE_COUNT set to max supported lanes */
  146. err = ps8622_set(cl, 0x01, 0x02, 0x80 | ps8622->max_lane_count);
  147. if (err)
  148. goto error;
  149. /* [4:0] LANE_COUNT_SET set to chosen lane count */
  150. err = ps8622_set(cl, 0x01, 0x21, 0x80 | ps8622->lane_count);
  151. if (err)
  152. goto error;
  153. err = ps8622_set(cl, 0x00, 0x52, 0x20);
  154. if (err)
  155. goto error;
  156. /* HPD CP toggle enable */
  157. err = ps8622_set(cl, 0x00, 0xf1, 0x03);
  158. if (err)
  159. goto error;
  160. err = ps8622_set(cl, 0x00, 0x62, 0x41);
  161. if (err)
  162. goto error;
  163. /* Counter number, add 1ms counter delay */
  164. err = ps8622_set(cl, 0x00, 0xf6, 0x01);
  165. if (err)
  166. goto error;
  167. /* [6]PWM function control by DPCD0040f[7], default is PWM block */
  168. err = ps8622_set(cl, 0x00, 0x77, 0x06);
  169. if (err)
  170. goto error;
  171. /* 04h Adjust VTotal toleranceto fix the 30Hz no display issue */
  172. err = ps8622_set(cl, 0x00, 0x4c, 0x04);
  173. if (err)
  174. goto error;
  175. /* DPCD00400='h00, Parade OUI ='h001cf8 */
  176. err = ps8622_set(cl, 0x01, 0xc0, 0x00);
  177. if (err)
  178. goto error;
  179. /* DPCD00401='h1c */
  180. err = ps8622_set(cl, 0x01, 0xc1, 0x1c);
  181. if (err)
  182. goto error;
  183. /* DPCD00402='hf8 */
  184. err = ps8622_set(cl, 0x01, 0xc2, 0xf8);
  185. if (err)
  186. goto error;
  187. /* DPCD403~408 = ASCII code, D2SLV5='h4432534c5635 */
  188. err = ps8622_set(cl, 0x01, 0xc3, 0x44);
  189. if (err)
  190. goto error;
  191. /* DPCD404 */
  192. err = ps8622_set(cl, 0x01, 0xc4, 0x32);
  193. if (err)
  194. goto error;
  195. /* DPCD405 */
  196. err = ps8622_set(cl, 0x01, 0xc5, 0x53);
  197. if (err)
  198. goto error;
  199. /* DPCD406 */
  200. err = ps8622_set(cl, 0x01, 0xc6, 0x4c);
  201. if (err)
  202. goto error;
  203. /* DPCD407 */
  204. err = ps8622_set(cl, 0x01, 0xc7, 0x56);
  205. if (err)
  206. goto error;
  207. /* DPCD408 */
  208. err = ps8622_set(cl, 0x01, 0xc8, 0x35);
  209. if (err)
  210. goto error;
  211. /* DPCD40A, Initial Code major revision '01' */
  212. err = ps8622_set(cl, 0x01, 0xca, 0x01);
  213. if (err)
  214. goto error;
  215. /* DPCD40B, Initial Code minor revision '05' */
  216. err = ps8622_set(cl, 0x01, 0xcb, 0x05);
  217. if (err)
  218. goto error;
  219. if (ps8622->bl) {
  220. /* DPCD720, internal PWM */
  221. err = ps8622_set(cl, 0x01, 0xa5, 0xa0);
  222. if (err)
  223. goto error;
  224. /* FFh for 100% brightness, 0h for 0% brightness */
  225. err = ps8622_set(cl, 0x01, 0xa7,
  226. ps8622->bl->props.brightness);
  227. if (err)
  228. goto error;
  229. } else {
  230. /* DPCD720, external PWM */
  231. err = ps8622_set(cl, 0x01, 0xa5, 0x80);
  232. if (err)
  233. goto error;
  234. }
  235. /* Set LVDS output as 6bit-VESA mapping, single LVDS channel */
  236. err = ps8622_set(cl, 0x01, 0xcc, 0x13);
  237. if (err)
  238. goto error;
  239. /* Enable SSC set by register */
  240. err = ps8622_set(cl, 0x02, 0xb1, 0x20);
  241. if (err)
  242. goto error;
  243. /* Set SSC enabled and +/-1% central spreading */
  244. err = ps8622_set(cl, 0x04, 0x10, 0x16);
  245. if (err)
  246. goto error;
  247. /* Logic end */
  248. /* MPU Clock source: LC => RCO */
  249. err = ps8622_set(cl, 0x04, 0x59, 0x60);
  250. if (err)
  251. goto error;
  252. /* LC -> RCO */
  253. err = ps8622_set(cl, 0x04, 0x54, 0x14);
  254. if (err)
  255. goto error;
  256. /* HPD high */
  257. err = ps8622_set(cl, 0x02, 0xa1, 0x91);
  258. error:
  259. return err ? -EIO : 0;
  260. }
  261. static int ps8622_backlight_update(struct backlight_device *bl)
  262. {
  263. struct ps8622_bridge *ps8622 = dev_get_drvdata(&bl->dev);
  264. int ret, brightness = backlight_get_brightness(bl);
  265. if (!ps8622->enabled)
  266. return -EINVAL;
  267. ret = ps8622_set(ps8622->client, 0x01, 0xa7, brightness);
  268. return ret;
  269. }
  270. static const struct backlight_ops ps8622_backlight_ops = {
  271. .update_status = ps8622_backlight_update,
  272. };
  273. static void ps8622_pre_enable(struct drm_bridge *bridge)
  274. {
  275. struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
  276. int ret;
  277. if (ps8622->enabled)
  278. return;
  279. gpiod_set_value(ps8622->gpio_rst, 0);
  280. if (ps8622->v12) {
  281. ret = regulator_enable(ps8622->v12);
  282. if (ret)
  283. DRM_ERROR("fails to enable ps8622->v12");
  284. }
  285. gpiod_set_value(ps8622->gpio_slp, 1);
  286. /*
  287. * T1 is the range of time that it takes for the power to rise after we
  288. * enable the lcd/ps8622 fet. T2 is the range of time in which the
  289. * data sheet specifies we should deassert the reset pin.
  290. *
  291. * If it takes T1.max for the power to rise, we need to wait atleast
  292. * T2.min before deasserting the reset pin. If it takes T1.min for the
  293. * power to rise, we need to wait at most T2.max before deasserting the
  294. * reset pin.
  295. */
  296. usleep_range(PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US,
  297. PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US);
  298. gpiod_set_value(ps8622->gpio_rst, 1);
  299. /* wait 20ms after RST high */
  300. usleep_range(20000, 30000);
  301. ret = ps8622_send_config(ps8622);
  302. if (ret) {
  303. DRM_ERROR("Failed to send config to bridge (%d)\n", ret);
  304. return;
  305. }
  306. ps8622->enabled = true;
  307. }
  308. static void ps8622_disable(struct drm_bridge *bridge)
  309. {
  310. /* Delay after panel is disabled */
  311. msleep(PS8622_PWMO_END_T12_MS);
  312. }
  313. static void ps8622_post_disable(struct drm_bridge *bridge)
  314. {
  315. struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
  316. if (!ps8622->enabled)
  317. return;
  318. ps8622->enabled = false;
  319. /*
  320. * This doesn't matter if the regulators are turned off, but something
  321. * else might keep them on. In that case, we want to assert the slp gpio
  322. * to lower power.
  323. */
  324. gpiod_set_value(ps8622->gpio_slp, 0);
  325. if (ps8622->v12)
  326. regulator_disable(ps8622->v12);
  327. /*
  328. * Sleep for at least the amount of time that it takes the power rail to
  329. * fall to prevent asserting the rst gpio from doing anything.
  330. */
  331. usleep_range(PS8622_POWER_FALL_T16_MAX_US,
  332. 2 * PS8622_POWER_FALL_T16_MAX_US);
  333. gpiod_set_value(ps8622->gpio_rst, 0);
  334. msleep(PS8622_POWER_OFF_T17_MS);
  335. }
  336. static int ps8622_attach(struct drm_bridge *bridge,
  337. struct drm_encoder *encoder,
  338. enum drm_bridge_attach_flags flags)
  339. {
  340. struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
  341. return drm_bridge_attach(ps8622->bridge.encoder, ps8622->panel_bridge,
  342. &ps8622->bridge, flags);
  343. }
  344. static const struct drm_bridge_funcs ps8622_bridge_funcs = {
  345. .pre_enable = ps8622_pre_enable,
  346. .disable = ps8622_disable,
  347. .post_disable = ps8622_post_disable,
  348. .attach = ps8622_attach,
  349. };
  350. static const struct of_device_id ps8622_devices[] = {
  351. {.compatible = "parade,ps8622",},
  352. {.compatible = "parade,ps8625",},
  353. {}
  354. };
  355. MODULE_DEVICE_TABLE(of, ps8622_devices);
  356. static int ps8622_probe(struct i2c_client *client)
  357. {
  358. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  359. struct device *dev = &client->dev;
  360. struct ps8622_bridge *ps8622;
  361. struct drm_bridge *panel_bridge;
  362. int ret;
  363. ps8622 = devm_drm_bridge_alloc(dev, struct ps8622_bridge, bridge,
  364. &ps8622_bridge_funcs);
  365. if (IS_ERR(ps8622))
  366. return PTR_ERR(ps8622);
  367. panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0);
  368. if (IS_ERR(panel_bridge))
  369. return PTR_ERR(panel_bridge);
  370. ps8622->panel_bridge = panel_bridge;
  371. ps8622->client = client;
  372. ps8622->v12 = devm_regulator_get(dev, "vdd12");
  373. if (IS_ERR(ps8622->v12)) {
  374. dev_info(dev, "no 1.2v regulator found for PS8622\n");
  375. ps8622->v12 = NULL;
  376. }
  377. ps8622->gpio_slp = devm_gpiod_get(dev, "sleep", GPIOD_OUT_HIGH);
  378. if (IS_ERR(ps8622->gpio_slp)) {
  379. ret = PTR_ERR(ps8622->gpio_slp);
  380. dev_err(dev, "cannot get gpio_slp %d\n", ret);
  381. return ret;
  382. }
  383. /*
  384. * Assert the reset pin high to avoid the bridge being
  385. * initialized prematurely
  386. */
  387. ps8622->gpio_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  388. if (IS_ERR(ps8622->gpio_rst)) {
  389. ret = PTR_ERR(ps8622->gpio_rst);
  390. dev_err(dev, "cannot get gpio_rst %d\n", ret);
  391. return ret;
  392. }
  393. ps8622->max_lane_count = id->driver_data;
  394. if (of_property_read_u32(dev->of_node, "lane-count",
  395. &ps8622->lane_count)) {
  396. ps8622->lane_count = ps8622->max_lane_count;
  397. } else if (ps8622->lane_count > ps8622->max_lane_count) {
  398. dev_info(dev, "lane-count property is too high,"
  399. "using max_lane_count\n");
  400. ps8622->lane_count = ps8622->max_lane_count;
  401. }
  402. if (!of_property_read_bool(dev->of_node, "use-external-pwm")) {
  403. ps8622->bl = backlight_device_register("ps8622-backlight",
  404. dev, ps8622, &ps8622_backlight_ops,
  405. NULL);
  406. if (IS_ERR(ps8622->bl)) {
  407. DRM_ERROR("failed to register backlight\n");
  408. ret = PTR_ERR(ps8622->bl);
  409. ps8622->bl = NULL;
  410. return ret;
  411. }
  412. ps8622->bl->props.max_brightness = PS8622_MAX_BRIGHTNESS;
  413. ps8622->bl->props.brightness = PS8622_MAX_BRIGHTNESS;
  414. }
  415. ps8622->bridge.type = DRM_MODE_CONNECTOR_LVDS;
  416. ps8622->bridge.of_node = dev->of_node;
  417. drm_bridge_add(&ps8622->bridge);
  418. i2c_set_clientdata(client, ps8622);
  419. return 0;
  420. }
  421. static void ps8622_remove(struct i2c_client *client)
  422. {
  423. struct ps8622_bridge *ps8622 = i2c_get_clientdata(client);
  424. backlight_device_unregister(ps8622->bl);
  425. drm_bridge_remove(&ps8622->bridge);
  426. }
  427. static const struct i2c_device_id ps8622_i2c_table[] = {
  428. /* Device type, max_lane_count */
  429. {"ps8622", 1},
  430. {"ps8625", 2},
  431. {},
  432. };
  433. MODULE_DEVICE_TABLE(i2c, ps8622_i2c_table);
  434. static struct i2c_driver ps8622_driver = {
  435. .id_table = ps8622_i2c_table,
  436. .probe = ps8622_probe,
  437. .remove = ps8622_remove,
  438. .driver = {
  439. .name = "ps8622",
  440. .of_match_table = ps8622_devices,
  441. },
  442. };
  443. module_i2c_driver(ps8622_driver);
  444. MODULE_AUTHOR("Vincent Palatin <vpalatin@chromium.org>");
  445. MODULE_DESCRIPTION("Parade ps8622/ps8625 eDP-LVDS converter driver");
  446. MODULE_LICENSE("GPL v2");