s1d13700_drm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * DRM driver for an S1D13700 LCD controller wired to a TCA9539 I2C expander.
  4. *
  5. * The expander is an ordinary I2C client of this driver rather than a
  6. * gpiochip: the display traffic needs one continuous write alternating both
  7. * ports, which the GPIO API cannot express.
  8. *
  9. * A full frame takes most of a second on the bus, so atomic_update only
  10. * converts the damage into a pending frame; a worker sends it, and damage
  11. * arriving meanwhile is merged into the next pass. A failed pass is retried
  12. * with backoff, redoing the controller init first if that is what failed.
  13. *
  14. * Needs a recent kernel (written against 6.18).
  15. */
  16. #include <linux/backlight.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/delay.h>
  19. #include <linux/i2c.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <linux/of.h>
  23. #include <linux/pm.h>
  24. #include <linux/slab.h>
  25. #include <linux/workqueue.h>
  26. #include <drm/clients/drm_client_setup.h>
  27. #include <drm/drm_atomic.h>
  28. #include <drm/drm_atomic_helper.h>
  29. #include <drm/drm_connector.h>
  30. #include <drm/drm_crtc.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include <drm/drm_damage_helper.h>
  33. #include <drm/drm_drv.h>
  34. #include <drm/drm_encoder.h>
  35. #include <drm/drm_fbdev_shmem.h>
  36. #include <drm/drm_fourcc.h>
  37. #include <drm/drm_framebuffer.h>
  38. #include <drm/drm_gem_atomic_helper.h>
  39. #include <drm/drm_gem_framebuffer_helper.h>
  40. #include <drm/drm_gem_shmem_helper.h>
  41. #include <drm/drm_managed.h>
  42. #include <drm/drm_modes.h>
  43. #include <drm/drm_modeset_helper.h>
  44. #include <drm/drm_modeset_helper_vtables.h>
  45. #include <drm/drm_plane.h>
  46. #include <drm/drm_print.h>
  47. #include <drm/drm_probe_helper.h>
  48. #include "s1d13700_core.h"
  49. #define DRIVER_NAME "s1d13700-tca"
  50. /* Bus held for about 23 ms at 400 kHz, so other devices on it are not starved */
  51. #define TX_CAP 1025u
  52. #define RETRY_MIN_MS 100u
  53. #define RETRY_MAX_MS 5000u
  54. static unsigned int width = 240;
  55. module_param(width, uint, 0444);
  56. MODULE_PARM_DESC(width, "Panel width in pixels");
  57. static unsigned int height = 160;
  58. module_param(height, uint, 0444);
  59. MODULE_PARM_DESC(height, "Panel height in pixels");
  60. static unsigned int bpp = 2;
  61. module_param(bpp, uint, 0444);
  62. MODULE_PARM_DESC(bpp, "Bits per pixel: 1, 2 or 4");
  63. static unsigned int tcr = 162;
  64. module_param(tcr, uint, 0444);
  65. MODULE_PARM_DESC(tcr, "SYSTEM SET TC/R: sets the frame rate");
  66. struct s1d_device {
  67. struct drm_device drm;
  68. struct drm_plane plane;
  69. struct drm_crtc crtc;
  70. struct drm_encoder encoder;
  71. struct drm_connector connector;
  72. struct drm_display_mode mode;
  73. struct i2c_client *client;
  74. struct s1d_config cfg;
  75. /* touched only by the worker, or with the worker stopped */
  76. struct s1d_panel panel;
  77. u8 *work_frame;
  78. bool panel_ready; /* controller initialised and still powered */
  79. unsigned int retry_ms;
  80. struct delayed_work flush_work;
  81. struct mutex lock; /* protects pending_frame, dirty_*, bl_on */
  82. u8 *pending_frame;
  83. unsigned int dirty_y0, dirty_y1;
  84. bool bl_on; /* requested; the worker drives the pin */
  85. };
  86. static struct s1d_device *to_s1d(struct drm_device *drm)
  87. {
  88. return container_of(drm, struct s1d_device, drm);
  89. }
  90. /* ---- transport ---------------------------------------------------------- */
  91. static int s1d_xfer(void *ctx, const u8 *buf, size_t len)
  92. {
  93. struct s1d_device *s = ctx;
  94. int ret = i2c_master_send(s->client, buf, len);
  95. if (ret < 0)
  96. return ret;
  97. return ret == len ? 0 : -EIO;
  98. }
  99. static void s1d_delay_ms(void *ctx, unsigned int ms)
  100. {
  101. msleep(ms);
  102. }
  103. static const struct s1d_ops s1d_panel_ops = {
  104. .xfer = s1d_xfer,
  105. .delay_ms = s1d_delay_ms,
  106. };
  107. /* ---- worker ------------------------------------------------------------- */
  108. static void s1d_flush_work(struct work_struct *work)
  109. {
  110. struct s1d_device *s = container_of(to_delayed_work(work), struct s1d_device,
  111. flush_work);
  112. unsigned int stride = s->panel.stride, y0, y1;
  113. bool bl_on;
  114. int idx, ret;
  115. mutex_lock(&s->lock);
  116. bl_on = s->bl_on;
  117. y0 = s->dirty_y0;
  118. y1 = s->dirty_y1;
  119. s->dirty_y0 = s->cfg.height;
  120. s->dirty_y1 = 0;
  121. if (y1 > y0)
  122. memcpy(s->work_frame + y0 * stride, s->pending_frame + y0 * stride,
  123. (y1 - y0) * stride);
  124. mutex_unlock(&s->lock);
  125. if (!drm_dev_enter(&s->drm, &idx))
  126. return;
  127. /* before the first init this only records the level for init */
  128. if (bl_on != s->panel.backlight) {
  129. ret = s1d_panel_backlight(&s->panel, bl_on);
  130. if (ret)
  131. drm_err_ratelimited(&s->drm, "backlight failed: %d\n", ret);
  132. }
  133. if (y1 <= y0) {
  134. drm_dev_exit(idx);
  135. return;
  136. }
  137. ret = s->panel_ready ? 0 : s1d_panel_init(&s->panel);
  138. if (!ret) {
  139. s->panel_ready = true;
  140. /* on failure the core rewrites the whole frame on the next update */
  141. ret = s1d_panel_update(&s->panel, s->work_frame, y0, y1);
  142. }
  143. if (ret) {
  144. drm_err_ratelimited(&s->drm, "panel update failed: %d\n", ret);
  145. /*
  146. * Retry even if no new damage arrives. Marking everything dirty
  147. * also refreshes all of work_frame, which a re-init needs.
  148. */
  149. mutex_lock(&s->lock);
  150. s->dirty_y0 = 0;
  151. s->dirty_y1 = s->cfg.height;
  152. mutex_unlock(&s->lock);
  153. queue_delayed_work(system_long_wq, &s->flush_work,
  154. msecs_to_jiffies(s->retry_ms));
  155. s->retry_ms = min(s->retry_ms * 2, RETRY_MAX_MS);
  156. } else {
  157. s->retry_ms = RETRY_MIN_MS;
  158. }
  159. drm_dev_exit(idx);
  160. }
  161. /* ---- plane -------------------------------------------------------------- */
  162. static int s1d_plane_atomic_check(struct drm_plane *plane, struct drm_atomic_state *state)
  163. {
  164. struct drm_plane_state *ps = drm_atomic_get_new_plane_state(state, plane);
  165. struct drm_crtc_state *cs = NULL;
  166. /* lines are read as u32 */
  167. if (ps->fb && ps->fb->pitches[0] % 4)
  168. return -EINVAL;
  169. if (ps->crtc)
  170. cs = drm_atomic_get_new_crtc_state(state, ps->crtc);
  171. return drm_atomic_helper_check_plane_state(ps, cs, DRM_PLANE_NO_SCALING,
  172. DRM_PLANE_NO_SCALING, false, false);
  173. }
  174. static void s1d_plane_atomic_update(struct drm_plane *plane, struct drm_atomic_state *state)
  175. {
  176. struct drm_plane_state *old = drm_atomic_get_old_plane_state(state, plane);
  177. struct drm_plane_state *ps = drm_atomic_get_new_plane_state(state, plane);
  178. struct drm_shadow_plane_state *shadow = to_drm_shadow_plane_state(ps);
  179. struct s1d_device *s = to_s1d(plane->dev);
  180. struct drm_framebuffer *fb = ps->fb;
  181. struct drm_atomic_helper_damage_iter iter;
  182. struct drm_rect clip;
  183. unsigned int ox, oy;
  184. const u8 *base;
  185. bool any = false;
  186. int idx;
  187. if (!fb || !ps->visible)
  188. return;
  189. if (shadow->data[0].is_iomem) {
  190. drm_warn_once(plane->dev, "framebuffers in I/O memory are not supported\n");
  191. return;
  192. }
  193. if (drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE)) {
  194. drm_err_ratelimited(plane->dev, "CPU access to framebuffer failed\n");
  195. return;
  196. }
  197. if (!drm_dev_enter(plane->dev, &idx))
  198. goto out_cpu;
  199. /* the panel origin is the plane's source origin in the framebuffer */
  200. ox = ps->src.x1 >> 16;
  201. oy = ps->src.y1 >> 16;
  202. base = (const u8 *)shadow->data[0].vaddr + oy * fb->pitches[0] + ox * 4;
  203. mutex_lock(&s->lock);
  204. drm_atomic_helper_damage_iter_init(&iter, old, ps);
  205. drm_atomic_for_each_plane_damage(&iter, &clip) {
  206. unsigned int y0 = clip.y1 - oy, y1 = clip.y2 - oy;
  207. s1d_convert_xrgb8888(&s->cfg, base, fb->pitches[0], s->pending_frame,
  208. clip.x1 - ox, y0, clip.x2 - ox, y1);
  209. s->dirty_y0 = min(s->dirty_y0, y0);
  210. s->dirty_y1 = max(s->dirty_y1, y1);
  211. any = true;
  212. }
  213. mutex_unlock(&s->lock);
  214. /* new damage overrides any retry backoff */
  215. if (any)
  216. mod_delayed_work(system_long_wq, &s->flush_work, 0);
  217. drm_dev_exit(idx);
  218. out_cpu:
  219. drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
  220. }
  221. static const struct drm_plane_helper_funcs s1d_plane_helper_funcs = {
  222. DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
  223. .atomic_check = s1d_plane_atomic_check,
  224. .atomic_update = s1d_plane_atomic_update,
  225. };
  226. static const struct drm_plane_funcs s1d_plane_funcs = {
  227. .update_plane = drm_atomic_helper_update_plane,
  228. .disable_plane = drm_atomic_helper_disable_plane,
  229. .destroy = drm_plane_cleanup,
  230. DRM_GEM_SHADOW_PLANE_FUNCS,
  231. };
  232. /* ---- CRTC --------------------------------------------------------------- */
  233. static void s1d_crtc_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
  234. {
  235. struct s1d_device *s = to_s1d(crtc->dev);
  236. int idx, ret;
  237. if (!drm_dev_enter(crtc->dev, &idx))
  238. return;
  239. /*
  240. * Enables run before plane updates (commit_tail_rpm), so this frame's
  241. * damage is not queued yet; a dropped retry is covered by the full
  242. * damage a modeset brings.
  243. */
  244. cancel_delayed_work_sync(&s->flush_work);
  245. s->retry_ms = RETRY_MIN_MS;
  246. /* VRAM survives a plain disable, so only the display needs turning on */
  247. if (s->panel_ready && !s1d_panel_display(&s->panel, true))
  248. goto out;
  249. /* init drives the backlight pin too, so give it the current request */
  250. mutex_lock(&s->lock);
  251. s->panel.backlight = s->bl_on;
  252. mutex_unlock(&s->lock);
  253. /* VRAM is cleared by init, so the shadow and work frame start at zero */
  254. ret = s1d_panel_init(&s->panel);
  255. if (ret)
  256. drm_err(&s->drm, "panel init failed: %d\n", ret);
  257. s->panel_ready = !ret;
  258. memset(s->work_frame, 0, s->panel.size);
  259. out:
  260. drm_dev_exit(idx);
  261. }
  262. static void s1d_crtc_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
  263. {
  264. struct s1d_device *s = to_s1d(crtc->dev);
  265. int idx;
  266. if (!drm_dev_enter(crtc->dev, &idx))
  267. return;
  268. cancel_delayed_work_sync(&s->flush_work);
  269. if (s->panel_ready)
  270. s1d_panel_display(&s->panel, false);
  271. drm_dev_exit(idx);
  272. }
  273. static const struct drm_crtc_helper_funcs s1d_crtc_helper_funcs = {
  274. .atomic_check = drm_crtc_helper_atomic_check,
  275. .atomic_enable = s1d_crtc_atomic_enable,
  276. .atomic_disable = s1d_crtc_atomic_disable,
  277. };
  278. static const struct drm_crtc_funcs s1d_crtc_funcs = {
  279. .reset = drm_atomic_helper_crtc_reset,
  280. .destroy = drm_crtc_cleanup,
  281. .set_config = drm_atomic_helper_set_config,
  282. .page_flip = drm_atomic_helper_page_flip,
  283. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  284. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  285. };
  286. /* ---- encoder and connector ---------------------------------------------- */
  287. static const struct drm_encoder_funcs s1d_encoder_funcs = {
  288. .destroy = drm_encoder_cleanup,
  289. };
  290. static int s1d_connector_get_modes(struct drm_connector *connector)
  291. {
  292. return drm_connector_helper_get_modes_fixed(connector,
  293. &to_s1d(connector->dev)->mode);
  294. }
  295. static const struct drm_connector_helper_funcs s1d_connector_helper_funcs = {
  296. .get_modes = s1d_connector_get_modes,
  297. };
  298. static const struct drm_connector_funcs s1d_connector_funcs = {
  299. .reset = drm_atomic_helper_connector_reset,
  300. .fill_modes = drm_helper_probe_single_connector_modes,
  301. .destroy = drm_connector_cleanup,
  302. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  303. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  304. };
  305. /* ---- device ------------------------------------------------------------- */
  306. /* enables before plane updates, so the first frame reaches an initialised panel */
  307. static const struct drm_mode_config_helper_funcs s1d_mode_config_helpers = {
  308. .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
  309. };
  310. static const struct drm_mode_config_funcs s1d_mode_config_funcs = {
  311. .fb_create = drm_gem_fb_create_with_dirty,
  312. .atomic_check = drm_atomic_helper_check,
  313. .atomic_commit = drm_atomic_helper_commit,
  314. };
  315. static const u32 s1d_formats[] = { DRM_FORMAT_XRGB8888 };
  316. static const u64 s1d_modifiers[] = { DRM_FORMAT_MOD_LINEAR, DRM_FORMAT_MOD_INVALID };
  317. DEFINE_DRM_GEM_FOPS(s1d_fops);
  318. static const struct drm_driver s1d_drm_driver = {
  319. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  320. .name = DRIVER_NAME,
  321. .desc = "S1D13700 LCD behind a TCA9539 expander",
  322. .major = 1,
  323. .minor = 0,
  324. .fops = &s1d_fops,
  325. DRM_GEM_SHMEM_DRIVER_OPS,
  326. DRM_FBDEV_SHMEM_DRIVER_OPS,
  327. };
  328. static int s1d_kms_init(struct s1d_device *s)
  329. {
  330. struct drm_device *drm = &s->drm;
  331. int ret;
  332. ret = drmm_mode_config_init(drm);
  333. if (ret)
  334. return ret;
  335. drm->mode_config.funcs = &s1d_mode_config_funcs;
  336. drm->mode_config.helper_private = &s1d_mode_config_helpers;
  337. drm->mode_config.min_width = s->cfg.width;
  338. drm->mode_config.max_width = s->cfg.width;
  339. drm->mode_config.min_height = s->cfg.height;
  340. drm->mode_config.max_height = s->cfg.height;
  341. drm->mode_config.preferred_depth = 24;
  342. ret = drm_universal_plane_init(drm, &s->plane, 0, &s1d_plane_funcs,
  343. s1d_formats, ARRAY_SIZE(s1d_formats),
  344. s1d_modifiers, DRM_PLANE_TYPE_PRIMARY, NULL);
  345. if (ret)
  346. return ret;
  347. drm_plane_helper_add(&s->plane, &s1d_plane_helper_funcs);
  348. drm_plane_enable_fb_damage_clips(&s->plane);
  349. ret = drm_crtc_init_with_planes(drm, &s->crtc, &s->plane, NULL,
  350. &s1d_crtc_funcs, NULL);
  351. if (ret)
  352. return ret;
  353. drm_crtc_helper_add(&s->crtc, &s1d_crtc_helper_funcs);
  354. ret = drm_encoder_init(drm, &s->encoder, &s1d_encoder_funcs,
  355. DRM_MODE_ENCODER_NONE, NULL);
  356. if (ret)
  357. return ret;
  358. s->encoder.possible_crtcs = drm_crtc_mask(&s->crtc);
  359. ret = drm_connector_init(drm, &s->connector, &s1d_connector_funcs,
  360. DRM_MODE_CONNECTOR_Unknown);
  361. if (ret)
  362. return ret;
  363. drm_connector_helper_add(&s->connector, &s1d_connector_helper_funcs);
  364. ret = drm_connector_attach_encoder(&s->connector, &s->encoder);
  365. if (ret)
  366. return ret;
  367. drm_mode_config_reset(drm);
  368. return 0;
  369. }
  370. /* ---- backlight ---------------------------------------------------------- */
  371. /* The panel belongs to the worker, so only record the request and kick it */
  372. static int s1d_bl_update(struct backlight_device *bd)
  373. {
  374. struct s1d_device *s = bl_get_data(bd);
  375. mutex_lock(&s->lock);
  376. s->bl_on = backlight_get_brightness(bd) > 0;
  377. mutex_unlock(&s->lock);
  378. mod_delayed_work(system_long_wq, &s->flush_work, 0);
  379. return 0;
  380. }
  381. static const struct backlight_ops s1d_bl_ops = {
  382. .options = BL_CORE_SUSPENDRESUME,
  383. .update_status = s1d_bl_update,
  384. };
  385. static void s1d_cancel_work(void *data)
  386. {
  387. struct s1d_device *s = data;
  388. cancel_delayed_work_sync(&s->flush_work);
  389. }
  390. static int s1d_backlight_init(struct s1d_device *s)
  391. {
  392. const struct backlight_properties props = {
  393. .type = BACKLIGHT_RAW,
  394. .max_brightness = 1,
  395. .brightness = 1,
  396. };
  397. struct backlight_device *bd;
  398. int ret;
  399. /*
  400. * devm undoes in reverse: this runs after the backlight is gone, so a
  401. * brightness write during unbind cannot leave work behind.
  402. */
  403. ret = devm_add_action_or_reset(&s->client->dev, s1d_cancel_work, s);
  404. if (ret)
  405. return ret;
  406. s->bl_on = true;
  407. /* the name the frontends look for by default */
  408. bd = devm_backlight_device_register(&s->client->dev, "radio-backlight",
  409. &s->client->dev, s, &s1d_bl_ops, &props);
  410. return PTR_ERR_OR_ZERO(bd);
  411. }
  412. static void s1d_debugfs_init(struct s1d_device *s)
  413. {
  414. struct dentry *d = s->client->debugfs;
  415. struct s1d_stats *st = &s->panel.stats;
  416. debugfs_create_ulong("lcd_writes", 0444, d, &st->writes);
  417. debugfs_create_ulong("data_bytes", 0444, d, &st->data_bytes);
  418. debugfs_create_ulong("seeks", 0444, d, &st->seeks);
  419. debugfs_create_ulong("dir_changes", 0444, d, &st->dir_changes);
  420. debugfs_create_ulong("i2c_msgs", 0444, d, &st->i2c_msgs);
  421. debugfs_create_ulong("i2c_bytes", 0444, d, &st->i2c_bytes);
  422. }
  423. static int s1d_probe(struct i2c_client *client)
  424. {
  425. struct device *dev = &client->dev;
  426. const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
  427. struct drm_display_mode mode = {
  428. DRM_SIMPLE_MODE(width, height, 0, 0),
  429. };
  430. struct s1d_device *s;
  431. size_t tx_cap = TX_CAP;
  432. unsigned int size;
  433. u8 *shadow, *tx;
  434. int ret;
  435. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  436. return dev_err_probe(dev, -EOPNOTSUPP, "adapter lacks raw I2C\n");
  437. /* a plain register read tells us whether the expander is there at all */
  438. ret = i2c_smbus_read_byte_data(client, TCA_REG_CONFIG0);
  439. if (ret < 0)
  440. return dev_err_probe(dev, ret, "expander not responding\n");
  441. s = devm_drm_dev_alloc(dev, &s1d_drm_driver, struct s1d_device, drm);
  442. if (IS_ERR(s))
  443. return PTR_ERR(s);
  444. s->client = client;
  445. s->cfg = (struct s1d_config){ width, height, bpp, tcr };
  446. s->mode = mode;
  447. s->dirty_y0 = height;
  448. s->retry_ms = RETRY_MIN_MS;
  449. INIT_DELAYED_WORK(&s->flush_work, s1d_flush_work);
  450. ret = drmm_mutex_init(&s->drm, &s->lock);
  451. if (ret)
  452. return ret;
  453. size = s1d_frame_size(&s->cfg);
  454. if (!size)
  455. return dev_err_probe(dev, -EINVAL, "invalid panel configuration\n");
  456. if (quirks && quirks->max_write_len && quirks->max_write_len < tx_cap)
  457. tx_cap = quirks->max_write_len;
  458. shadow = devm_kzalloc(dev, size, GFP_KERNEL);
  459. s->work_frame = devm_kzalloc(dev, size, GFP_KERNEL);
  460. s->pending_frame = devm_kzalloc(dev, size, GFP_KERNEL);
  461. tx = devm_kmalloc(dev, tx_cap, GFP_KERNEL); /* must be DMA-safe */
  462. if (!shadow || !s->work_frame || !s->pending_frame || !tx)
  463. return -ENOMEM;
  464. ret = s1d_panel_setup(&s->panel, &s->cfg, &s1d_panel_ops, s, shadow, tx, tx_cap);
  465. if (ret)
  466. return dev_err_probe(dev, ret, "panel setup failed\n");
  467. ret = s1d_kms_init(s);
  468. if (ret)
  469. return dev_err_probe(dev, ret, "KMS init failed\n");
  470. ret = s1d_backlight_init(s);
  471. if (ret)
  472. return dev_err_probe(dev, ret, "backlight registration failed\n");
  473. i2c_set_clientdata(client, s);
  474. ret = drm_dev_register(&s->drm, 0);
  475. if (ret)
  476. return dev_err_probe(dev, ret, "DRM registration failed\n");
  477. s1d_debugfs_init(s);
  478. drm_client_setup(&s->drm, NULL);
  479. return 0;
  480. }
  481. static void s1d_remove(struct i2c_client *client)
  482. {
  483. struct s1d_device *s = i2c_get_clientdata(client);
  484. drm_dev_unplug(&s->drm);
  485. cancel_delayed_work_sync(&s->flush_work);
  486. /* atomic_disable is a no-op once unplugged, so blank the panel here */
  487. if (s->panel_ready)
  488. s1d_panel_display(&s->panel, false);
  489. drm_atomic_helper_shutdown(&s->drm);
  490. }
  491. static void s1d_shutdown(struct i2c_client *client)
  492. {
  493. struct s1d_device *s = i2c_get_clientdata(client);
  494. drm_atomic_helper_shutdown(&s->drm);
  495. cancel_delayed_work_sync(&s->flush_work);
  496. }
  497. static int s1d_suspend(struct device *dev)
  498. {
  499. struct s1d_device *s = dev_get_drvdata(dev);
  500. int ret;
  501. ret = drm_mode_config_helper_suspend(&s->drm);
  502. if (ret)
  503. return ret;
  504. /* the CRTC is off and the worker stopped; the controller may lose power */
  505. s->panel_ready = false;
  506. return 0;
  507. }
  508. static int s1d_resume(struct device *dev)
  509. {
  510. struct s1d_device *s = dev_get_drvdata(dev);
  511. return drm_mode_config_helper_resume(&s->drm);
  512. }
  513. static DEFINE_SIMPLE_DEV_PM_OPS(s1d_pm_ops, s1d_suspend, s1d_resume);
  514. static const struct of_device_id s1d_of_match[] = {
  515. { .compatible = "k4be,s1d13700-tca9539" },
  516. { }
  517. };
  518. MODULE_DEVICE_TABLE(of, s1d_of_match);
  519. static const struct i2c_device_id s1d_id[] = {
  520. { "s1d13700-tca9539" },
  521. { }
  522. };
  523. MODULE_DEVICE_TABLE(i2c, s1d_id);
  524. static struct i2c_driver s1d_i2c_driver = {
  525. .driver = {
  526. .name = DRIVER_NAME,
  527. .of_match_table = s1d_of_match,
  528. .pm = pm_sleep_ptr(&s1d_pm_ops),
  529. },
  530. .probe = s1d_probe,
  531. .remove = s1d_remove,
  532. .shutdown = s1d_shutdown,
  533. .id_table = s1d_id,
  534. };
  535. module_i2c_driver(s1d_i2c_driver);
  536. MODULE_DESCRIPTION("DRM driver for an S1D13700 LCD behind a TCA9539 expander");
  537. MODULE_LICENSE("GPL");