s1d13700_drm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 by whoever set panel_busy */
  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, panel_busy */
  82. u8 *pending_frame;
  83. unsigned int dirty_y0, dirty_y1;
  84. bool bl_on; /* requested; the worker drives the pin */
  85. bool panel_busy; /* CRTC enable/disable owns the panel; worker waits */
  86. };
  87. static struct s1d_device *to_s1d(struct drm_device *drm)
  88. {
  89. return container_of(drm, struct s1d_device, drm);
  90. }
  91. /* ---- transport ---------------------------------------------------------- */
  92. static int s1d_xfer(void *ctx, const u8 *buf, size_t len)
  93. {
  94. struct s1d_device *s = ctx;
  95. int ret = i2c_master_send(s->client, buf, len);
  96. if (ret < 0)
  97. return ret;
  98. return ret == len ? 0 : -EIO;
  99. }
  100. static void s1d_delay_ms(void *ctx, unsigned int ms)
  101. {
  102. msleep(ms);
  103. }
  104. static const struct s1d_ops s1d_panel_ops = {
  105. .xfer = s1d_xfer,
  106. .delay_ms = s1d_delay_ms,
  107. };
  108. /* ---- worker ------------------------------------------------------------- */
  109. static void s1d_flush_work(struct work_struct *work)
  110. {
  111. struct s1d_device *s = container_of(to_delayed_work(work), struct s1d_device,
  112. flush_work);
  113. unsigned int stride = s->panel.stride, y0, y1;
  114. bool bl_on;
  115. int idx, ret = 0, bl_ret = 0;
  116. mutex_lock(&s->lock);
  117. /* a transaction is under way; s1d_panel_release() queues us again */
  118. if (s->panel_busy) {
  119. mutex_unlock(&s->lock);
  120. return;
  121. }
  122. bl_on = s->bl_on;
  123. y0 = s->dirty_y0;
  124. y1 = s->dirty_y1;
  125. s->dirty_y0 = s->cfg.height;
  126. s->dirty_y1 = 0;
  127. if (y1 > y0)
  128. memcpy(s->work_frame + y0 * stride, s->pending_frame + y0 * stride,
  129. (y1 - y0) * stride);
  130. mutex_unlock(&s->lock);
  131. if (!drm_dev_enter(&s->drm, &idx))
  132. return;
  133. /*
  134. * Between transactions, so a request made during one waits for this.
  135. * Before the first init this only records the level for init.
  136. */
  137. if (bl_on != s->panel.backlight) {
  138. bl_ret = s1d_panel_backlight(&s->panel, bl_on);
  139. if (bl_ret)
  140. drm_err_ratelimited(&s->drm, "backlight failed: %d\n", bl_ret);
  141. }
  142. if (y1 > y0) {
  143. ret = s->panel_ready ? 0 : s1d_panel_init(&s->panel);
  144. if (!ret) {
  145. s->panel_ready = true;
  146. /* on failure the core rewrites the whole frame next time */
  147. ret = s1d_panel_update(&s->panel, s->work_frame, y0, y1);
  148. }
  149. if (ret) {
  150. drm_err_ratelimited(&s->drm, "panel update failed: %d\n", ret);
  151. /*
  152. * Retry even if no new damage arrives. Marking everything
  153. * dirty also refreshes all of work_frame, which a re-init
  154. * needs.
  155. */
  156. mutex_lock(&s->lock);
  157. s->dirty_y0 = 0;
  158. s->dirty_y1 = s->cfg.height;
  159. mutex_unlock(&s->lock);
  160. }
  161. }
  162. /* a failed backlight write left panel.backlight unchanged: retried too */
  163. if (ret || bl_ret) {
  164. queue_delayed_work(system_long_wq, &s->flush_work,
  165. msecs_to_jiffies(s->retry_ms));
  166. s->retry_ms = min(s->retry_ms * 2, RETRY_MAX_MS);
  167. } else {
  168. s->retry_ms = RETRY_MIN_MS;
  169. }
  170. drm_dev_exit(idx);
  171. }
  172. /* ---- plane -------------------------------------------------------------- */
  173. static int s1d_plane_atomic_check(struct drm_plane *plane, struct drm_atomic_state *state)
  174. {
  175. struct drm_plane_state *ps = drm_atomic_get_new_plane_state(state, plane);
  176. struct drm_crtc_state *cs = NULL;
  177. /* lines are read as u32 */
  178. if (ps->fb && ps->fb->pitches[0] % 4)
  179. return -EINVAL;
  180. if (ps->crtc)
  181. cs = drm_atomic_get_new_crtc_state(state, ps->crtc);
  182. return drm_atomic_helper_check_plane_state(ps, cs, DRM_PLANE_NO_SCALING,
  183. DRM_PLANE_NO_SCALING, false, false);
  184. }
  185. static void s1d_plane_atomic_update(struct drm_plane *plane, struct drm_atomic_state *state)
  186. {
  187. struct drm_plane_state *old = drm_atomic_get_old_plane_state(state, plane);
  188. struct drm_plane_state *ps = drm_atomic_get_new_plane_state(state, plane);
  189. struct drm_shadow_plane_state *shadow = to_drm_shadow_plane_state(ps);
  190. struct s1d_device *s = to_s1d(plane->dev);
  191. struct drm_framebuffer *fb = ps->fb;
  192. struct drm_atomic_helper_damage_iter iter;
  193. struct drm_rect clip;
  194. unsigned int ox, oy;
  195. const u8 *base;
  196. bool any = false;
  197. int idx;
  198. if (!fb || !ps->visible)
  199. return;
  200. if (shadow->data[0].is_iomem) {
  201. drm_warn_once(plane->dev, "framebuffers in I/O memory are not supported\n");
  202. return;
  203. }
  204. if (drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE)) {
  205. drm_err_ratelimited(plane->dev, "CPU access to framebuffer failed\n");
  206. return;
  207. }
  208. if (!drm_dev_enter(plane->dev, &idx))
  209. goto out_cpu;
  210. /* the panel origin is the plane's source origin in the framebuffer */
  211. ox = ps->src.x1 >> 16;
  212. oy = ps->src.y1 >> 16;
  213. base = (const u8 *)shadow->data[0].vaddr + oy * fb->pitches[0] + ox * 4;
  214. mutex_lock(&s->lock);
  215. drm_atomic_helper_damage_iter_init(&iter, old, ps);
  216. drm_atomic_for_each_plane_damage(&iter, &clip) {
  217. unsigned int y0 = clip.y1 - oy, y1 = clip.y2 - oy;
  218. s1d_convert_xrgb8888(&s->cfg, base, fb->pitches[0], s->pending_frame,
  219. clip.x1 - ox, y0, clip.x2 - ox, y1);
  220. s->dirty_y0 = min(s->dirty_y0, y0);
  221. s->dirty_y1 = max(s->dirty_y1, y1);
  222. any = true;
  223. }
  224. mutex_unlock(&s->lock);
  225. /* new damage overrides any retry backoff */
  226. if (any)
  227. mod_delayed_work(system_long_wq, &s->flush_work, 0);
  228. drm_dev_exit(idx);
  229. out_cpu:
  230. drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
  231. }
  232. static const struct drm_plane_helper_funcs s1d_plane_helper_funcs = {
  233. DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
  234. .atomic_check = s1d_plane_atomic_check,
  235. .atomic_update = s1d_plane_atomic_update,
  236. };
  237. static const struct drm_plane_funcs s1d_plane_funcs = {
  238. .update_plane = drm_atomic_helper_update_plane,
  239. .disable_plane = drm_atomic_helper_disable_plane,
  240. .destroy = drm_plane_cleanup,
  241. DRM_GEM_SHADOW_PLANE_FUNCS,
  242. };
  243. /* ---- CRTC --------------------------------------------------------------- */
  244. /*
  245. * Take the panel from the worker for a transaction of our own. Backlight
  246. * requests made meanwhile are only recorded; the worker would otherwise
  247. * interleave its writes with ours in the shared transfer buffer.
  248. */
  249. static void s1d_panel_claim(struct s1d_device *s)
  250. {
  251. mutex_lock(&s->lock);
  252. s->panel_busy = true;
  253. mutex_unlock(&s->lock);
  254. /* a worker already running finishes first; one starting now backs off */
  255. cancel_delayed_work_sync(&s->flush_work);
  256. }
  257. /* Hand the panel back, and let the worker apply what queued meanwhile */
  258. static void s1d_panel_release(struct s1d_device *s)
  259. {
  260. bool kick;
  261. mutex_lock(&s->lock);
  262. s->panel_busy = false;
  263. kick = s->bl_on != s->panel.backlight || s->dirty_y1 > s->dirty_y0;
  264. mutex_unlock(&s->lock);
  265. if (kick)
  266. mod_delayed_work(system_long_wq, &s->flush_work, 0);
  267. }
  268. static void s1d_crtc_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
  269. {
  270. struct s1d_device *s = to_s1d(crtc->dev);
  271. int idx, ret;
  272. if (!drm_dev_enter(crtc->dev, &idx))
  273. return;
  274. /*
  275. * Enables run before plane updates (commit_tail_rpm), so this frame's
  276. * damage is not queued yet; a dropped retry is covered by the full
  277. * damage a modeset brings.
  278. */
  279. s1d_panel_claim(s);
  280. s->retry_ms = RETRY_MIN_MS;
  281. /* VRAM survives a plain disable, so only the display needs turning on */
  282. if (s->panel_ready && !s1d_panel_display(&s->panel, true))
  283. goto out;
  284. /* init drives the backlight pin too, so give it the current request */
  285. mutex_lock(&s->lock);
  286. s->panel.backlight = s->bl_on;
  287. mutex_unlock(&s->lock);
  288. /* VRAM is cleared by init, so the shadow and work frame start at zero */
  289. ret = s1d_panel_init(&s->panel);
  290. if (ret)
  291. drm_err(&s->drm, "panel init failed: %d\n", ret);
  292. s->panel_ready = !ret;
  293. memset(s->work_frame, 0, s->panel.size);
  294. out:
  295. s1d_panel_release(s);
  296. drm_dev_exit(idx);
  297. }
  298. static void s1d_crtc_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
  299. {
  300. struct s1d_device *s = to_s1d(crtc->dev);
  301. int idx;
  302. if (!drm_dev_enter(crtc->dev, &idx))
  303. return;
  304. s1d_panel_claim(s);
  305. if (s->panel_ready)
  306. s1d_panel_display(&s->panel, false);
  307. /* the backlight stays usable with the display off */
  308. s1d_panel_release(s);
  309. drm_dev_exit(idx);
  310. }
  311. static const struct drm_crtc_helper_funcs s1d_crtc_helper_funcs = {
  312. .atomic_check = drm_crtc_helper_atomic_check,
  313. .atomic_enable = s1d_crtc_atomic_enable,
  314. .atomic_disable = s1d_crtc_atomic_disable,
  315. };
  316. static const struct drm_crtc_funcs s1d_crtc_funcs = {
  317. .reset = drm_atomic_helper_crtc_reset,
  318. .destroy = drm_crtc_cleanup,
  319. .set_config = drm_atomic_helper_set_config,
  320. .page_flip = drm_atomic_helper_page_flip,
  321. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  322. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  323. };
  324. /* ---- encoder and connector ---------------------------------------------- */
  325. static const struct drm_encoder_funcs s1d_encoder_funcs = {
  326. .destroy = drm_encoder_cleanup,
  327. };
  328. static int s1d_connector_get_modes(struct drm_connector *connector)
  329. {
  330. return drm_connector_helper_get_modes_fixed(connector,
  331. &to_s1d(connector->dev)->mode);
  332. }
  333. static const struct drm_connector_helper_funcs s1d_connector_helper_funcs = {
  334. .get_modes = s1d_connector_get_modes,
  335. };
  336. static const struct drm_connector_funcs s1d_connector_funcs = {
  337. .reset = drm_atomic_helper_connector_reset,
  338. .fill_modes = drm_helper_probe_single_connector_modes,
  339. .destroy = drm_connector_cleanup,
  340. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  341. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  342. };
  343. /* ---- device ------------------------------------------------------------- */
  344. /* enables before plane updates, so the first frame reaches an initialised panel */
  345. static const struct drm_mode_config_helper_funcs s1d_mode_config_helpers = {
  346. .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
  347. };
  348. static const struct drm_mode_config_funcs s1d_mode_config_funcs = {
  349. .fb_create = drm_gem_fb_create_with_dirty,
  350. .atomic_check = drm_atomic_helper_check,
  351. .atomic_commit = drm_atomic_helper_commit,
  352. };
  353. static const u32 s1d_formats[] = { DRM_FORMAT_XRGB8888 };
  354. static const u64 s1d_modifiers[] = { DRM_FORMAT_MOD_LINEAR, DRM_FORMAT_MOD_INVALID };
  355. DEFINE_DRM_GEM_FOPS(s1d_fops);
  356. static const struct drm_driver s1d_drm_driver = {
  357. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  358. .name = DRIVER_NAME,
  359. .desc = "S1D13700 LCD behind a TCA9539 expander",
  360. .major = 1,
  361. .minor = 0,
  362. .fops = &s1d_fops,
  363. DRM_GEM_SHMEM_DRIVER_OPS,
  364. DRM_FBDEV_SHMEM_DRIVER_OPS,
  365. };
  366. static int s1d_kms_init(struct s1d_device *s)
  367. {
  368. struct drm_device *drm = &s->drm;
  369. int ret;
  370. ret = drmm_mode_config_init(drm);
  371. if (ret)
  372. return ret;
  373. drm->mode_config.funcs = &s1d_mode_config_funcs;
  374. drm->mode_config.helper_private = &s1d_mode_config_helpers;
  375. drm->mode_config.min_width = s->cfg.width;
  376. drm->mode_config.max_width = s->cfg.width;
  377. drm->mode_config.min_height = s->cfg.height;
  378. drm->mode_config.max_height = s->cfg.height;
  379. drm->mode_config.preferred_depth = 24;
  380. ret = drm_universal_plane_init(drm, &s->plane, 0, &s1d_plane_funcs,
  381. s1d_formats, ARRAY_SIZE(s1d_formats),
  382. s1d_modifiers, DRM_PLANE_TYPE_PRIMARY, NULL);
  383. if (ret)
  384. return ret;
  385. drm_plane_helper_add(&s->plane, &s1d_plane_helper_funcs);
  386. drm_plane_enable_fb_damage_clips(&s->plane);
  387. ret = drm_crtc_init_with_planes(drm, &s->crtc, &s->plane, NULL,
  388. &s1d_crtc_funcs, NULL);
  389. if (ret)
  390. return ret;
  391. drm_crtc_helper_add(&s->crtc, &s1d_crtc_helper_funcs);
  392. ret = drm_encoder_init(drm, &s->encoder, &s1d_encoder_funcs,
  393. DRM_MODE_ENCODER_NONE, NULL);
  394. if (ret)
  395. return ret;
  396. s->encoder.possible_crtcs = drm_crtc_mask(&s->crtc);
  397. ret = drm_connector_init(drm, &s->connector, &s1d_connector_funcs,
  398. DRM_MODE_CONNECTOR_Unknown);
  399. if (ret)
  400. return ret;
  401. drm_connector_helper_add(&s->connector, &s1d_connector_helper_funcs);
  402. ret = drm_connector_attach_encoder(&s->connector, &s->encoder);
  403. if (ret)
  404. return ret;
  405. drm_mode_config_reset(drm);
  406. return 0;
  407. }
  408. /* ---- backlight ---------------------------------------------------------- */
  409. /*
  410. * Only record the request and kick the worker. It applies it between panel
  411. * transactions: after a frame in progress, or once enable/disable is done.
  412. */
  413. static int s1d_bl_update(struct backlight_device *bd)
  414. {
  415. struct s1d_device *s = bl_get_data(bd);
  416. mutex_lock(&s->lock);
  417. s->bl_on = backlight_get_brightness(bd) > 0;
  418. mutex_unlock(&s->lock);
  419. mod_delayed_work(system_long_wq, &s->flush_work, 0);
  420. return 0;
  421. }
  422. static const struct backlight_ops s1d_bl_ops = {
  423. .options = BL_CORE_SUSPENDRESUME,
  424. .update_status = s1d_bl_update,
  425. };
  426. static void s1d_cancel_work(void *data)
  427. {
  428. struct s1d_device *s = data;
  429. cancel_delayed_work_sync(&s->flush_work);
  430. }
  431. static int s1d_backlight_init(struct s1d_device *s)
  432. {
  433. const struct backlight_properties props = {
  434. .type = BACKLIGHT_RAW,
  435. .max_brightness = 1,
  436. .brightness = 1,
  437. };
  438. struct backlight_device *bd;
  439. int ret;
  440. /*
  441. * devm undoes in reverse: this runs after the backlight is gone, so a
  442. * brightness write during unbind cannot leave work behind.
  443. */
  444. ret = devm_add_action_or_reset(&s->client->dev, s1d_cancel_work, s);
  445. if (ret)
  446. return ret;
  447. s->bl_on = true;
  448. /* the name the frontends look for by default */
  449. bd = devm_backlight_device_register(&s->client->dev, "radio-backlight",
  450. &s->client->dev, s, &s1d_bl_ops, &props);
  451. return PTR_ERR_OR_ZERO(bd);
  452. }
  453. static void s1d_debugfs_init(struct s1d_device *s)
  454. {
  455. struct dentry *d = s->client->debugfs;
  456. struct s1d_stats *st = &s->panel.stats;
  457. debugfs_create_ulong("lcd_writes", 0444, d, &st->writes);
  458. debugfs_create_ulong("data_bytes", 0444, d, &st->data_bytes);
  459. debugfs_create_ulong("seeks", 0444, d, &st->seeks);
  460. debugfs_create_ulong("dir_changes", 0444, d, &st->dir_changes);
  461. debugfs_create_ulong("i2c_msgs", 0444, d, &st->i2c_msgs);
  462. debugfs_create_ulong("i2c_bytes", 0444, d, &st->i2c_bytes);
  463. }
  464. static int s1d_probe(struct i2c_client *client)
  465. {
  466. struct device *dev = &client->dev;
  467. const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
  468. struct drm_display_mode mode = {
  469. DRM_SIMPLE_MODE(width, height, 0, 0),
  470. };
  471. struct s1d_device *s;
  472. size_t tx_cap = TX_CAP;
  473. unsigned int size;
  474. u8 *shadow, *tx;
  475. int ret;
  476. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  477. return dev_err_probe(dev, -EOPNOTSUPP, "adapter lacks raw I2C\n");
  478. /* a plain register read tells us whether the expander is there at all */
  479. ret = i2c_smbus_read_byte_data(client, TCA_REG_CONFIG0);
  480. if (ret < 0)
  481. return dev_err_probe(dev, ret, "expander not responding\n");
  482. s = devm_drm_dev_alloc(dev, &s1d_drm_driver, struct s1d_device, drm);
  483. if (IS_ERR(s))
  484. return PTR_ERR(s);
  485. s->client = client;
  486. s->cfg = (struct s1d_config){ width, height, bpp, tcr };
  487. s->mode = mode;
  488. s->dirty_y0 = height;
  489. s->retry_ms = RETRY_MIN_MS;
  490. INIT_DELAYED_WORK(&s->flush_work, s1d_flush_work);
  491. ret = drmm_mutex_init(&s->drm, &s->lock);
  492. if (ret)
  493. return ret;
  494. size = s1d_frame_size(&s->cfg);
  495. if (!size)
  496. return dev_err_probe(dev, -EINVAL, "invalid panel configuration\n");
  497. if (quirks && quirks->max_write_len && quirks->max_write_len < tx_cap)
  498. tx_cap = quirks->max_write_len;
  499. shadow = devm_kzalloc(dev, size, GFP_KERNEL);
  500. s->work_frame = devm_kzalloc(dev, size, GFP_KERNEL);
  501. s->pending_frame = devm_kzalloc(dev, size, GFP_KERNEL);
  502. tx = devm_kmalloc(dev, tx_cap, GFP_KERNEL); /* must be DMA-safe */
  503. if (!shadow || !s->work_frame || !s->pending_frame || !tx)
  504. return -ENOMEM;
  505. ret = s1d_panel_setup(&s->panel, &s->cfg, &s1d_panel_ops, s, shadow, tx, tx_cap);
  506. if (ret)
  507. return dev_err_probe(dev, ret, "panel setup failed\n");
  508. ret = s1d_kms_init(s);
  509. if (ret)
  510. return dev_err_probe(dev, ret, "KMS init failed\n");
  511. ret = s1d_backlight_init(s);
  512. if (ret)
  513. return dev_err_probe(dev, ret, "backlight registration failed\n");
  514. i2c_set_clientdata(client, s);
  515. ret = drm_dev_register(&s->drm, 0);
  516. if (ret)
  517. return dev_err_probe(dev, ret, "DRM registration failed\n");
  518. s1d_debugfs_init(s);
  519. drm_client_setup(&s->drm, NULL);
  520. return 0;
  521. }
  522. static void s1d_remove(struct i2c_client *client)
  523. {
  524. struct s1d_device *s = i2c_get_clientdata(client);
  525. drm_dev_unplug(&s->drm);
  526. cancel_delayed_work_sync(&s->flush_work);
  527. /* atomic_disable is a no-op once unplugged, so blank the panel here */
  528. if (s->panel_ready)
  529. s1d_panel_display(&s->panel, false);
  530. drm_atomic_helper_shutdown(&s->drm);
  531. }
  532. static void s1d_shutdown(struct i2c_client *client)
  533. {
  534. struct s1d_device *s = i2c_get_clientdata(client);
  535. drm_atomic_helper_shutdown(&s->drm);
  536. cancel_delayed_work_sync(&s->flush_work);
  537. }
  538. static int s1d_suspend(struct device *dev)
  539. {
  540. struct s1d_device *s = dev_get_drvdata(dev);
  541. int ret;
  542. ret = drm_mode_config_helper_suspend(&s->drm);
  543. if (ret)
  544. return ret;
  545. /*
  546. * The backlight's own suspend ran first and asked for it off; send
  547. * that, then stop the worker before the controller may lose power.
  548. */
  549. flush_delayed_work(&s->flush_work);
  550. cancel_delayed_work_sync(&s->flush_work);
  551. s->panel_ready = false;
  552. return 0;
  553. }
  554. static int s1d_resume(struct device *dev)
  555. {
  556. struct s1d_device *s = dev_get_drvdata(dev);
  557. return drm_mode_config_helper_resume(&s->drm);
  558. }
  559. static DEFINE_SIMPLE_DEV_PM_OPS(s1d_pm_ops, s1d_suspend, s1d_resume);
  560. static const struct of_device_id s1d_of_match[] = {
  561. { .compatible = "k4be,s1d13700-tca9539" },
  562. { }
  563. };
  564. MODULE_DEVICE_TABLE(of, s1d_of_match);
  565. static const struct i2c_device_id s1d_id[] = {
  566. { "s1d13700-tca9539" },
  567. { }
  568. };
  569. MODULE_DEVICE_TABLE(i2c, s1d_id);
  570. static struct i2c_driver s1d_i2c_driver = {
  571. .driver = {
  572. .name = DRIVER_NAME,
  573. .of_match_table = s1d_of_match,
  574. .pm = pm_sleep_ptr(&s1d_pm_ops),
  575. },
  576. .probe = s1d_probe,
  577. .remove = s1d_remove,
  578. .shutdown = s1d_shutdown,
  579. .id_table = s1d_id,
  580. };
  581. module_i2c_driver(s1d_i2c_driver);
  582. MODULE_DESCRIPTION("DRM driver for an S1D13700 LCD behind a TCA9539 expander");
  583. MODULE_LICENSE("GPL");