parade-ps8622.c 13 KB

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