panel-novatek-nt35560.c 13 KB

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