panel-novatek-nt35560.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * MIPI-DSI Novatek NT35560-based panel controller.
  4. *
  5. * Supported panels include:
  6. * Sony ACX424AKM - a 480x854 AMOLED DSI panel
  7. * Sony ACX424AKP - a 480x864 AMOLED DSI panel
  8. *
  9. * Copyright (C) Linaro Ltd. 2019-2021
  10. * Author: Linus Walleij
  11. * Based on code and know-how from Marcus Lorentzon
  12. * Copyright (C) ST-Ericsson SA 2010
  13. * Based on code and know-how from Johan Olson and Joakim Wesslen
  14. * Copyright (C) Sony Ericsson Mobile Communications 2010
  15. */
  16. #include <linux/backlight.h>
  17. #include <linux/delay.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <video/mipi_display.h>
  24. #include <drm/drm_mipi_dsi.h>
  25. #include <drm/drm_modes.h>
  26. #include <drm/drm_panel.h>
  27. #define NT35560_DCS_READ_ID1 0xDA
  28. #define NT35560_DCS_READ_ID2 0xDB
  29. #define NT35560_DCS_READ_ID3 0xDC
  30. #define NT35560_DCS_SET_MDDI 0xAE
  31. /*
  32. * Sony seems to use vendor ID 0x81
  33. */
  34. #define DISPLAY_SONY_ACX424AKP_ID1 0x8103
  35. #define DISPLAY_SONY_ACX424AKP_ID2 0x811a
  36. #define DISPLAY_SONY_ACX424AKP_ID3 0x811b
  37. /*
  38. * The fourth ID looks like a bug, vendor IDs begin at 0x80
  39. * and panel 00 ... seems like default values.
  40. */
  41. #define DISPLAY_SONY_ACX424AKP_ID4 0x8000
  42. struct nt35560_config {
  43. const struct drm_display_mode *vid_mode;
  44. const struct drm_display_mode *cmd_mode;
  45. };
  46. struct nt35560 {
  47. const struct nt35560_config *conf;
  48. struct drm_panel panel;
  49. struct device *dev;
  50. struct regulator *supply;
  51. struct gpio_desc *reset_gpio;
  52. bool video_mode;
  53. };
  54. static const struct drm_display_mode sony_acx424akp_vid_mode = {
  55. .clock = 27234,
  56. .hdisplay = 480,
  57. .hsync_start = 480 + 15,
  58. .hsync_end = 480 + 15 + 0,
  59. .htotal = 480 + 15 + 0 + 15,
  60. .vdisplay = 864,
  61. .vsync_start = 864 + 14,
  62. .vsync_end = 864 + 14 + 1,
  63. .vtotal = 864 + 14 + 1 + 11,
  64. .width_mm = 48,
  65. .height_mm = 84,
  66. .flags = DRM_MODE_FLAG_PVSYNC,
  67. };
  68. /*
  69. * The timings are not very helpful as the display is used in
  70. * command mode using the maximum HS frequency.
  71. */
  72. static const struct drm_display_mode sony_acx424akp_cmd_mode = {
  73. .clock = 35478,
  74. .hdisplay = 480,
  75. .hsync_start = 480 + 154,
  76. .hsync_end = 480 + 154 + 16,
  77. .htotal = 480 + 154 + 16 + 32,
  78. .vdisplay = 864,
  79. .vsync_start = 864 + 1,
  80. .vsync_end = 864 + 1 + 1,
  81. .vtotal = 864 + 1 + 1 + 1,
  82. /*
  83. * Some desired refresh rate, experiments at the maximum "pixel"
  84. * clock speed (HS clock 420 MHz) yields around 117Hz.
  85. */
  86. .width_mm = 48,
  87. .height_mm = 84,
  88. };
  89. static const struct nt35560_config sony_acx424akp_data = {
  90. .vid_mode = &sony_acx424akp_vid_mode,
  91. .cmd_mode = &sony_acx424akp_cmd_mode,
  92. };
  93. static const struct drm_display_mode sony_acx424akm_vid_mode = {
  94. .clock = 27234,
  95. .hdisplay = 480,
  96. .hsync_start = 480 + 15,
  97. .hsync_end = 480 + 15 + 0,
  98. .htotal = 480 + 15 + 0 + 15,
  99. .vdisplay = 854,
  100. .vsync_start = 854 + 14,
  101. .vsync_end = 854 + 14 + 1,
  102. .vtotal = 854 + 14 + 1 + 11,
  103. .width_mm = 46,
  104. .height_mm = 82,
  105. .flags = DRM_MODE_FLAG_PVSYNC,
  106. };
  107. /*
  108. * The timings are not very helpful as the display is used in
  109. * command mode using the maximum HS frequency.
  110. */
  111. static const struct drm_display_mode sony_acx424akm_cmd_mode = {
  112. .clock = 35478,
  113. .hdisplay = 480,
  114. .hsync_start = 480 + 154,
  115. .hsync_end = 480 + 154 + 16,
  116. .htotal = 480 + 154 + 16 + 32,
  117. .vdisplay = 854,
  118. .vsync_start = 854 + 1,
  119. .vsync_end = 854 + 1 + 1,
  120. .vtotal = 854 + 1 + 1 + 1,
  121. .width_mm = 46,
  122. .height_mm = 82,
  123. };
  124. static const struct nt35560_config sony_acx424akm_data = {
  125. .vid_mode = &sony_acx424akm_vid_mode,
  126. .cmd_mode = &sony_acx424akm_cmd_mode,
  127. };
  128. static inline struct nt35560 *panel_to_nt35560(struct drm_panel *panel)
  129. {
  130. return container_of(panel, struct nt35560, panel);
  131. }
  132. #define FOSC 20 /* 20Mhz */
  133. #define SCALE_FACTOR_NS_DIV_MHZ 1000
  134. static int nt35560_set_brightness(struct backlight_device *bl)
  135. {
  136. struct nt35560 *nt = bl_get_data(bl);
  137. struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
  138. int period_ns = 1023;
  139. int duty_ns = bl->props.brightness;
  140. u8 pwm_ratio;
  141. u8 pwm_div;
  142. u8 par;
  143. int ret;
  144. if (backlight_is_blank(bl)) {
  145. /* Disable backlight */
  146. par = 0x00;
  147. ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY,
  148. &par, 1);
  149. if (ret) {
  150. dev_err(nt->dev, "failed to disable display backlight (%d)\n", ret);
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. /* Calculate the PWM duty cycle in n/256's */
  156. pwm_ratio = max(((duty_ns * 256) / period_ns) - 1, 1);
  157. pwm_div = max(1,
  158. ((FOSC * period_ns) / 256) /
  159. SCALE_FACTOR_NS_DIV_MHZ);
  160. /* Set up PWM dutycycle ONE byte (differs from the standard) */
  161. dev_dbg(nt->dev, "calculated duty cycle %02x\n", pwm_ratio);
  162. ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
  163. &pwm_ratio, 1);
  164. if (ret < 0) {
  165. dev_err(nt->dev, "failed to set display PWM ratio (%d)\n", ret);
  166. return ret;
  167. }
  168. /*
  169. * Sequence to write PWMDIV:
  170. * address data
  171. * 0xF3 0xAA CMD2 Unlock
  172. * 0x00 0x01 Enter CMD2 page 0
  173. * 0X7D 0x01 No reload MTP of CMD2 P1
  174. * 0x22 PWMDIV
  175. * 0x7F 0xAA CMD2 page 1 lock
  176. */
  177. par = 0xaa;
  178. ret = mipi_dsi_dcs_write(dsi, 0xf3, &par, 1);
  179. if (ret < 0) {
  180. dev_err(nt->dev, "failed to unlock CMD 2 (%d)\n", ret);
  181. return ret;
  182. }
  183. par = 0x01;
  184. ret = mipi_dsi_dcs_write(dsi, 0x00, &par, 1);
  185. if (ret < 0) {
  186. dev_err(nt->dev, "failed to enter page 1 (%d)\n", ret);
  187. return ret;
  188. }
  189. par = 0x01;
  190. ret = mipi_dsi_dcs_write(dsi, 0x7d, &par, 1);
  191. if (ret < 0) {
  192. dev_err(nt->dev, "failed to disable MTP reload (%d)\n", ret);
  193. return ret;
  194. }
  195. ret = mipi_dsi_dcs_write(dsi, 0x22, &pwm_div, 1);
  196. if (ret < 0) {
  197. dev_err(nt->dev, "failed to set PWM divisor (%d)\n", ret);
  198. return ret;
  199. }
  200. par = 0xaa;
  201. ret = mipi_dsi_dcs_write(dsi, 0x7f, &par, 1);
  202. if (ret < 0) {
  203. dev_err(nt->dev, "failed to lock CMD 2 (%d)\n", ret);
  204. return ret;
  205. }
  206. /* Enable backlight */
  207. par = 0x24;
  208. ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY,
  209. &par, 1);
  210. if (ret < 0) {
  211. dev_err(nt->dev, "failed to enable display backlight (%d)\n", ret);
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. static const struct backlight_ops nt35560_bl_ops = {
  217. .update_status = nt35560_set_brightness,
  218. };
  219. static const struct backlight_properties nt35560_bl_props = {
  220. .type = BACKLIGHT_RAW,
  221. .brightness = 512,
  222. .max_brightness = 1023,
  223. };
  224. static int nt35560_read_id(struct nt35560 *nt)
  225. {
  226. struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
  227. u8 vendor, version, panel;
  228. u16 val;
  229. int ret;
  230. ret = mipi_dsi_dcs_read(dsi, NT35560_DCS_READ_ID1, &vendor, 1);
  231. if (ret < 0) {
  232. dev_err(nt->dev, "could not vendor ID byte\n");
  233. return ret;
  234. }
  235. ret = mipi_dsi_dcs_read(dsi, NT35560_DCS_READ_ID2, &version, 1);
  236. if (ret < 0) {
  237. dev_err(nt->dev, "could not read device version byte\n");
  238. return ret;
  239. }
  240. ret = mipi_dsi_dcs_read(dsi, NT35560_DCS_READ_ID3, &panel, 1);
  241. if (ret < 0) {
  242. dev_err(nt->dev, "could not read panel ID byte\n");
  243. return ret;
  244. }
  245. if (vendor == 0x00) {
  246. dev_err(nt->dev, "device vendor ID is zero\n");
  247. return -ENODEV;
  248. }
  249. val = (vendor << 8) | panel;
  250. switch (val) {
  251. case DISPLAY_SONY_ACX424AKP_ID1:
  252. case DISPLAY_SONY_ACX424AKP_ID2:
  253. case DISPLAY_SONY_ACX424AKP_ID3:
  254. case DISPLAY_SONY_ACX424AKP_ID4:
  255. dev_info(nt->dev, "MTP vendor: %02x, version: %02x, panel: %02x\n",
  256. vendor, version, panel);
  257. break;
  258. default:
  259. dev_info(nt->dev, "unknown vendor: %02x, version: %02x, panel: %02x\n",
  260. vendor, version, panel);
  261. break;
  262. }
  263. return 0;
  264. }
  265. static int nt35560_power_on(struct nt35560 *nt)
  266. {
  267. int ret;
  268. ret = regulator_enable(nt->supply);
  269. if (ret) {
  270. dev_err(nt->dev, "failed to enable supply (%d)\n", ret);
  271. return ret;
  272. }
  273. /* Assert RESET */
  274. gpiod_set_value_cansleep(nt->reset_gpio, 1);
  275. udelay(20);
  276. /* De-assert RESET */
  277. gpiod_set_value_cansleep(nt->reset_gpio, 0);
  278. usleep_range(11000, 20000);
  279. return 0;
  280. }
  281. static void nt35560_power_off(struct nt35560 *nt)
  282. {
  283. /* Assert RESET */
  284. gpiod_set_value_cansleep(nt->reset_gpio, 1);
  285. usleep_range(11000, 20000);
  286. regulator_disable(nt->supply);
  287. }
  288. static int nt35560_prepare(struct drm_panel *panel)
  289. {
  290. struct nt35560 *nt = panel_to_nt35560(panel);
  291. struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
  292. const u8 mddi = 3;
  293. int ret;
  294. ret = nt35560_power_on(nt);
  295. if (ret)
  296. return ret;
  297. ret = nt35560_read_id(nt);
  298. if (ret) {
  299. dev_err(nt->dev, "failed to read panel ID (%d)\n", ret);
  300. goto err_power_off;
  301. }
  302. /* Enabe tearing mode: send TE (tearing effect) at VBLANK */
  303. ret = mipi_dsi_dcs_set_tear_on(dsi,
  304. MIPI_DSI_DCS_TEAR_MODE_VBLANK);
  305. if (ret) {
  306. dev_err(nt->dev, "failed to enable vblank TE (%d)\n", ret);
  307. goto err_power_off;
  308. }
  309. /*
  310. * Set MDDI
  311. *
  312. * This presumably deactivates the Qualcomm MDDI interface and
  313. * selects DSI, similar code is found in other drivers such as the
  314. * Sharp LS043T1LE01 which makes us suspect that this panel may be
  315. * using a Novatek NT35565 or similar display driver chip that shares
  316. * this command. Due to the lack of documentation we cannot know for
  317. * sure.
  318. */
  319. ret = mipi_dsi_dcs_write(dsi, NT35560_DCS_SET_MDDI,
  320. &mddi, sizeof(mddi));
  321. if (ret < 0) {
  322. dev_err(nt->dev, "failed to set MDDI (%d)\n", ret);
  323. goto err_power_off;
  324. }
  325. /* Exit sleep mode */
  326. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  327. if (ret) {
  328. dev_err(nt->dev, "failed to exit sleep mode (%d)\n", ret);
  329. goto err_power_off;
  330. }
  331. msleep(140);
  332. ret = mipi_dsi_dcs_set_display_on(dsi);
  333. if (ret) {
  334. dev_err(nt->dev, "failed to turn display on (%d)\n", ret);
  335. goto err_power_off;
  336. }
  337. if (nt->video_mode) {
  338. /* In video mode turn peripheral on */
  339. ret = mipi_dsi_turn_on_peripheral(dsi);
  340. if (ret) {
  341. dev_err(nt->dev, "failed to turn on peripheral\n");
  342. goto err_power_off;
  343. }
  344. }
  345. return 0;
  346. err_power_off:
  347. nt35560_power_off(nt);
  348. return ret;
  349. }
  350. static int nt35560_unprepare(struct drm_panel *panel)
  351. {
  352. struct nt35560 *nt = panel_to_nt35560(panel);
  353. struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
  354. int ret;
  355. ret = mipi_dsi_dcs_set_display_off(dsi);
  356. if (ret) {
  357. dev_err(nt->dev, "failed to turn display off (%d)\n", ret);
  358. return ret;
  359. }
  360. /* Enter sleep mode */
  361. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  362. if (ret) {
  363. dev_err(nt->dev, "failed to enter sleep mode (%d)\n", ret);
  364. return ret;
  365. }
  366. msleep(85);
  367. nt35560_power_off(nt);
  368. return 0;
  369. }
  370. static int nt35560_get_modes(struct drm_panel *panel,
  371. struct drm_connector *connector)
  372. {
  373. struct nt35560 *nt = panel_to_nt35560(panel);
  374. const struct nt35560_config *conf = nt->conf;
  375. struct drm_display_mode *mode;
  376. if (nt->video_mode)
  377. mode = drm_mode_duplicate(connector->dev,
  378. conf->vid_mode);
  379. else
  380. mode = drm_mode_duplicate(connector->dev,
  381. conf->cmd_mode);
  382. if (!mode) {
  383. dev_err(panel->dev, "bad mode or failed to add mode\n");
  384. return -EINVAL;
  385. }
  386. drm_mode_set_name(mode);
  387. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  388. connector->display_info.width_mm = mode->width_mm;
  389. connector->display_info.height_mm = mode->height_mm;
  390. drm_mode_probed_add(connector, mode);
  391. return 1; /* Number of modes */
  392. }
  393. static const struct drm_panel_funcs nt35560_drm_funcs = {
  394. .unprepare = nt35560_unprepare,
  395. .prepare = nt35560_prepare,
  396. .get_modes = nt35560_get_modes,
  397. };
  398. static int nt35560_probe(struct mipi_dsi_device *dsi)
  399. {
  400. struct device *dev = &dsi->dev;
  401. struct nt35560 *nt;
  402. int ret;
  403. nt = devm_kzalloc(dev, sizeof(struct nt35560), GFP_KERNEL);
  404. if (!nt)
  405. return -ENOMEM;
  406. nt->video_mode = of_property_read_bool(dev->of_node,
  407. "enforce-video-mode");
  408. mipi_dsi_set_drvdata(dsi, nt);
  409. nt->dev = dev;
  410. nt->conf = of_device_get_match_data(dev);
  411. if (!nt->conf) {
  412. dev_err(dev, "missing device configuration\n");
  413. return -ENODEV;
  414. }
  415. dsi->lanes = 2;
  416. dsi->format = MIPI_DSI_FMT_RGB888;
  417. /*
  418. * FIXME: these come from the ST-Ericsson vendor driver for the
  419. * HREF520 and seems to reflect limitations in the PLLs on that
  420. * platform, if you have the datasheet, please cross-check the
  421. * actual max rates.
  422. */
  423. dsi->lp_rate = 19200000;
  424. dsi->hs_rate = 420160000;
  425. if (nt->video_mode)
  426. /* Burst mode using event for sync */
  427. dsi->mode_flags =
  428. MIPI_DSI_MODE_VIDEO |
  429. MIPI_DSI_MODE_VIDEO_BURST;
  430. else
  431. dsi->mode_flags =
  432. MIPI_DSI_CLOCK_NON_CONTINUOUS;
  433. nt->supply = devm_regulator_get(dev, "vddi");
  434. if (IS_ERR(nt->supply))
  435. return PTR_ERR(nt->supply);
  436. /* This asserts RESET by default */
  437. nt->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  438. GPIOD_OUT_HIGH);
  439. if (IS_ERR(nt->reset_gpio))
  440. return dev_err_probe(dev, PTR_ERR(nt->reset_gpio),
  441. "failed to request GPIO\n");
  442. drm_panel_init(&nt->panel, dev, &nt35560_drm_funcs,
  443. DRM_MODE_CONNECTOR_DSI);
  444. nt->panel.backlight = devm_backlight_device_register(dev, "nt35560", dev, nt,
  445. &nt35560_bl_ops, &nt35560_bl_props);
  446. if (IS_ERR(nt->panel.backlight))
  447. return dev_err_probe(dev, PTR_ERR(nt->panel.backlight),
  448. "failed to register backlight device\n");
  449. drm_panel_add(&nt->panel);
  450. ret = mipi_dsi_attach(dsi);
  451. if (ret < 0) {
  452. drm_panel_remove(&nt->panel);
  453. return ret;
  454. }
  455. return 0;
  456. }
  457. static void nt35560_remove(struct mipi_dsi_device *dsi)
  458. {
  459. struct nt35560 *nt = mipi_dsi_get_drvdata(dsi);
  460. mipi_dsi_detach(dsi);
  461. drm_panel_remove(&nt->panel);
  462. }
  463. static const struct of_device_id nt35560_of_match[] = {
  464. {
  465. .compatible = "sony,acx424akp",
  466. .data = &sony_acx424akp_data,
  467. },
  468. {
  469. .compatible = "sony,acx424akm",
  470. .data = &sony_acx424akm_data,
  471. },
  472. { /* sentinel */ }
  473. };
  474. MODULE_DEVICE_TABLE(of, nt35560_of_match);
  475. static struct mipi_dsi_driver nt35560_driver = {
  476. .probe = nt35560_probe,
  477. .remove = nt35560_remove,
  478. .driver = {
  479. .name = "panel-novatek-nt35560",
  480. .of_match_table = nt35560_of_match,
  481. },
  482. };
  483. module_mipi_dsi_driver(nt35560_driver);
  484. MODULE_AUTHOR("Linus Wallei <[email protected]>");
  485. MODULE_DESCRIPTION("MIPI-DSI Novatek NT35560 Panel Driver");
  486. MODULE_LICENSE("GPL v2");