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