panel-sony-acx424akp.c 13 KB

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