1 // SPDX-License-Identifier: GPL-2.0-only
3 * DRM driver for Solomon SSD13xx OLED displays
5 * Copyright 2022 Red Hat Inc.
8 * Based on drivers/video/fbdev/ssd1307fb.c
9 * Copyright 2012 Free Electrons
12 #include <linux/backlight.h>
13 #include <linux/bitfield.h>
14 #include <linux/bits.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/property.h>
18 #include <linux/pwm.h>
19 #include <linux/regulator/consumer.h>
21 #include <drm/drm_atomic.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_client_setup.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_damage_helper.h>
26 #include <drm/drm_edid.h>
27 #include <drm/drm_fbdev_shmem.h>
28 #include <drm/drm_format_helper.h>
29 #include <drm/drm_framebuffer.h>
30 #include <drm/drm_gem_atomic_helper.h>
31 #include <drm/drm_gem_framebuffer_helper.h>
32 #include <drm/drm_gem_shmem_helper.h>
33 #include <drm/drm_managed.h>
34 #include <drm/drm_modes.h>
35 #include <drm/drm_rect.h>
36 #include <drm/drm_probe_helper.h>
40 #define DRIVER_NAME "ssd130x"
41 #define DRIVER_DESC "DRM driver for Solomon SSD13xx OLED displays"
42 #define DRIVER_DATE "20220131"
43 #define DRIVER_MAJOR 1
44 #define DRIVER_MINOR 0
46 #define SSD130X_PAGE_HEIGHT 8
48 #define SSD132X_SEGMENT_WIDTH 2
50 /* ssd13xx commands */
51 #define SSD13XX_CONTRAST 0x81
52 #define SSD13XX_SET_SEG_REMAP 0xa0
53 #define SSD13XX_SET_MULTIPLEX_RATIO 0xa8
54 #define SSD13XX_DISPLAY_OFF 0xae
55 #define SSD13XX_DISPLAY_ON 0xaf
57 #define SSD13XX_SET_SEG_REMAP_MASK GENMASK(0, 0)
58 #define SSD13XX_SET_SEG_REMAP_SET(val) FIELD_PREP(SSD13XX_SET_SEG_REMAP_MASK, (val))
60 /* ssd130x commands */
61 #define SSD130X_PAGE_COL_START_LOW 0x00
62 #define SSD130X_PAGE_COL_START_HIGH 0x10
63 #define SSD130X_SET_ADDRESS_MODE 0x20
64 #define SSD130X_SET_COL_RANGE 0x21
65 #define SSD130X_SET_PAGE_RANGE 0x22
66 #define SSD130X_SET_LOOKUP_TABLE 0x91
67 #define SSD130X_CHARGE_PUMP 0x8d
68 #define SSD130X_START_PAGE_ADDRESS 0xb0
69 #define SSD130X_SET_COM_SCAN_DIR 0xc0
70 #define SSD130X_SET_DISPLAY_OFFSET 0xd3
71 #define SSD130X_SET_CLOCK_FREQ 0xd5
72 #define SSD130X_SET_AREA_COLOR_MODE 0xd8
73 #define SSD130X_SET_PRECHARGE_PERIOD 0xd9
74 #define SSD130X_SET_COM_PINS_CONFIG 0xda
75 #define SSD130X_SET_VCOMH 0xdb
77 /* ssd130x commands accessors */
78 #define SSD130X_PAGE_COL_START_MASK GENMASK(3, 0)
79 #define SSD130X_PAGE_COL_START_HIGH_SET(val) FIELD_PREP(SSD130X_PAGE_COL_START_MASK, (val) >> 4)
80 #define SSD130X_PAGE_COL_START_LOW_SET(val) FIELD_PREP(SSD130X_PAGE_COL_START_MASK, (val))
81 #define SSD130X_START_PAGE_ADDRESS_MASK GENMASK(2, 0)
82 #define SSD130X_START_PAGE_ADDRESS_SET(val) FIELD_PREP(SSD130X_START_PAGE_ADDRESS_MASK, (val))
83 #define SSD130X_SET_COM_SCAN_DIR_MASK GENMASK(3, 3)
84 #define SSD130X_SET_COM_SCAN_DIR_SET(val) FIELD_PREP(SSD130X_SET_COM_SCAN_DIR_MASK, (val))
85 #define SSD130X_SET_CLOCK_DIV_MASK GENMASK(3, 0)
86 #define SSD130X_SET_CLOCK_DIV_SET(val) FIELD_PREP(SSD130X_SET_CLOCK_DIV_MASK, (val))
87 #define SSD130X_SET_CLOCK_FREQ_MASK GENMASK(7, 4)
88 #define SSD130X_SET_CLOCK_FREQ_SET(val) FIELD_PREP(SSD130X_SET_CLOCK_FREQ_MASK, (val))
89 #define SSD130X_SET_PRECHARGE_PERIOD1_MASK GENMASK(3, 0)
90 #define SSD130X_SET_PRECHARGE_PERIOD1_SET(val) FIELD_PREP(SSD130X_SET_PRECHARGE_PERIOD1_MASK, (val))
91 #define SSD130X_SET_PRECHARGE_PERIOD2_MASK GENMASK(7, 4)
92 #define SSD130X_SET_PRECHARGE_PERIOD2_SET(val) FIELD_PREP(SSD130X_SET_PRECHARGE_PERIOD2_MASK, (val))
93 #define SSD130X_SET_COM_PINS_CONFIG1_MASK GENMASK(4, 4)
94 #define SSD130X_SET_COM_PINS_CONFIG1_SET(val) FIELD_PREP(SSD130X_SET_COM_PINS_CONFIG1_MASK, (val))
95 #define SSD130X_SET_COM_PINS_CONFIG2_MASK GENMASK(5, 5)
96 #define SSD130X_SET_COM_PINS_CONFIG2_SET(val) FIELD_PREP(SSD130X_SET_COM_PINS_CONFIG2_MASK, (val))
98 #define SSD130X_SET_ADDRESS_MODE_HORIZONTAL 0x00
99 #define SSD130X_SET_ADDRESS_MODE_VERTICAL 0x01
100 #define SSD130X_SET_ADDRESS_MODE_PAGE 0x02
102 #define SSD130X_SET_AREA_COLOR_MODE_ENABLE 0x1e
103 #define SSD130X_SET_AREA_COLOR_MODE_LOW_POWER 0x05
105 /* ssd132x commands */
106 #define SSD132X_SET_COL_RANGE 0x15
107 #define SSD132X_SET_DEACTIVATE_SCROLL 0x2e
108 #define SSD132X_SET_ROW_RANGE 0x75
109 #define SSD132X_SET_DISPLAY_START 0xa1
110 #define SSD132X_SET_DISPLAY_OFFSET 0xa2
111 #define SSD132X_SET_DISPLAY_NORMAL 0xa4
112 #define SSD132X_SET_FUNCTION_SELECT_A 0xab
113 #define SSD132X_SET_PHASE_LENGTH 0xb1
114 #define SSD132X_SET_CLOCK_FREQ 0xb3
115 #define SSD132X_SET_GPIO 0xb5
116 #define SSD132X_SET_PRECHARGE_PERIOD 0xb6
117 #define SSD132X_SET_GRAY_SCALE_TABLE 0xb8
118 #define SSD132X_SELECT_DEFAULT_TABLE 0xb9
119 #define SSD132X_SET_PRECHARGE_VOLTAGE 0xbc
120 #define SSD130X_SET_VCOMH_VOLTAGE 0xbe
121 #define SSD132X_SET_FUNCTION_SELECT_B 0xd5
123 /* ssd133x commands */
124 #define SSD133X_SET_COL_RANGE 0x15
125 #define SSD133X_SET_ROW_RANGE 0x75
126 #define SSD133X_CONTRAST_A 0x81
127 #define SSD133X_CONTRAST_B 0x82
128 #define SSD133X_CONTRAST_C 0x83
129 #define SSD133X_SET_MASTER_CURRENT 0x87
130 #define SSD132X_SET_PRECHARGE_A 0x8a
131 #define SSD132X_SET_PRECHARGE_B 0x8b
132 #define SSD132X_SET_PRECHARGE_C 0x8c
133 #define SSD133X_SET_DISPLAY_START 0xa1
134 #define SSD133X_SET_DISPLAY_OFFSET 0xa2
135 #define SSD133X_SET_DISPLAY_NORMAL 0xa4
136 #define SSD133X_SET_MASTER_CONFIG 0xad
137 #define SSD133X_POWER_SAVE_MODE 0xb0
138 #define SSD133X_PHASES_PERIOD 0xb1
139 #define SSD133X_SET_CLOCK_FREQ 0xb3
140 #define SSD133X_SET_PRECHARGE_VOLTAGE 0xbb
141 #define SSD133X_SET_VCOMH_VOLTAGE 0xbe
143 #define MAX_CONTRAST 255
145 const struct ssd130x_deviceinfo ssd130x_variants[] = {
147 .default_vcomh = 0x40,
148 .default_dclk_div = 1,
149 .default_dclk_frq = 5,
150 .default_width = 132,
151 .default_height = 64,
153 .family_id = SSD130X_FAMILY,
156 .default_vcomh = 0x34,
157 .default_dclk_div = 1,
158 .default_dclk_frq = 7,
159 .default_width = 132,
160 .default_height = 64,
161 .family_id = SSD130X_FAMILY,
164 .default_vcomh = 0x20,
165 .default_dclk_div = 1,
166 .default_dclk_frq = 8,
167 .need_chargepump = 1,
168 .default_width = 128,
169 .default_height = 64,
170 .family_id = SSD130X_FAMILY,
173 .default_vcomh = 0x20,
174 .default_dclk_div = 2,
175 .default_dclk_frq = 12,
177 .default_width = 128,
178 .default_height = 39,
179 .family_id = SSD130X_FAMILY,
182 .default_vcomh = 0x34,
183 .default_dclk_div = 1,
184 .default_dclk_frq = 10,
185 .default_width = 128,
186 .default_height = 64,
187 .family_id = SSD130X_FAMILY,
191 .default_width = 480,
192 .default_height = 128,
193 .family_id = SSD132X_FAMILY,
196 .default_width = 128,
197 .default_height = 80,
198 .family_id = SSD132X_FAMILY,
201 .default_width = 128,
202 .default_height = 128,
203 .family_id = SSD132X_FAMILY,
208 .default_height = 64,
209 .family_id = SSD133X_FAMILY,
212 EXPORT_SYMBOL_NS_GPL(ssd130x_variants, "DRM_SSD130X");
214 struct ssd130x_crtc_state {
215 struct drm_crtc_state base;
216 /* Buffer to store pixels in HW format and written to the panel */
220 struct ssd130x_plane_state {
221 struct drm_shadow_plane_state base;
222 /* Intermediate buffer to convert pixels from XRGB8888 to HW format */
226 static inline struct ssd130x_crtc_state *to_ssd130x_crtc_state(struct drm_crtc_state *state)
228 return container_of(state, struct ssd130x_crtc_state, base);
231 static inline struct ssd130x_plane_state *to_ssd130x_plane_state(struct drm_plane_state *state)
233 return container_of(state, struct ssd130x_plane_state, base.base);
236 static inline struct ssd130x_device *drm_to_ssd130x(struct drm_device *drm)
238 return container_of(drm, struct ssd130x_device, drm);
242 * Helper to write data (SSD13XX_DATA) to the device.
244 static int ssd130x_write_data(struct ssd130x_device *ssd130x, u8 *values, int count)
246 return regmap_bulk_write(ssd130x->regmap, SSD13XX_DATA, values, count);
250 * Helper to write command (SSD13XX_COMMAND). The fist variadic argument
251 * is the command to write and the following are the command options.
253 * Note that the ssd13xx protocol requires each command and option to be
254 * written as a SSD13XX_COMMAND device register value. That is why a call
255 * to regmap_write(..., SSD13XX_COMMAND, ...) is done for each argument.
257 static int ssd130x_write_cmd(struct ssd130x_device *ssd130x, int count,
258 /* u8 cmd, u8 option, ... */...)
267 value = va_arg(ap, int);
268 ret = regmap_write(ssd130x->regmap, SSD13XX_COMMAND, value);
279 /* Set address range for horizontal/vertical addressing modes */
280 static int ssd130x_set_col_range(struct ssd130x_device *ssd130x,
281 u8 col_start, u8 cols)
283 u8 col_end = col_start + cols - 1;
286 if (col_start == ssd130x->col_start && col_end == ssd130x->col_end)
289 ret = ssd130x_write_cmd(ssd130x, 3, SSD130X_SET_COL_RANGE, col_start, col_end);
293 ssd130x->col_start = col_start;
294 ssd130x->col_end = col_end;
298 static int ssd130x_set_page_range(struct ssd130x_device *ssd130x,
299 u8 page_start, u8 pages)
301 u8 page_end = page_start + pages - 1;
304 if (page_start == ssd130x->page_start && page_end == ssd130x->page_end)
307 ret = ssd130x_write_cmd(ssd130x, 3, SSD130X_SET_PAGE_RANGE, page_start, page_end);
311 ssd130x->page_start = page_start;
312 ssd130x->page_end = page_end;
316 /* Set page and column start address for page addressing mode */
317 static int ssd130x_set_page_pos(struct ssd130x_device *ssd130x,
318 u8 page_start, u8 col_start)
321 u32 page, col_low, col_high;
323 page = SSD130X_START_PAGE_ADDRESS |
324 SSD130X_START_PAGE_ADDRESS_SET(page_start);
325 col_low = SSD130X_PAGE_COL_START_LOW |
326 SSD130X_PAGE_COL_START_LOW_SET(col_start);
327 col_high = SSD130X_PAGE_COL_START_HIGH |
328 SSD130X_PAGE_COL_START_HIGH_SET(col_start);
329 ret = ssd130x_write_cmd(ssd130x, 3, page, col_low, col_high);
336 static int ssd130x_pwm_enable(struct ssd130x_device *ssd130x)
338 struct device *dev = ssd130x->dev;
339 struct pwm_state pwmstate;
341 ssd130x->pwm = pwm_get(dev, NULL);
342 if (IS_ERR(ssd130x->pwm)) {
343 dev_err(dev, "Could not get PWM from firmware description!\n");
344 return PTR_ERR(ssd130x->pwm);
347 pwm_init_state(ssd130x->pwm, &pwmstate);
348 pwm_set_relative_duty_cycle(&pwmstate, 50, 100);
349 pwm_apply_might_sleep(ssd130x->pwm, &pwmstate);
352 pwm_enable(ssd130x->pwm);
354 dev_dbg(dev, "Using PWM %s with a %lluns period.\n",
355 ssd130x->pwm->label, pwm_get_period(ssd130x->pwm));
360 static void ssd130x_reset(struct ssd130x_device *ssd130x)
365 /* Reset the screen */
366 gpiod_set_value_cansleep(ssd130x->reset, 1);
368 gpiod_set_value_cansleep(ssd130x->reset, 0);
372 static int ssd130x_power_on(struct ssd130x_device *ssd130x)
374 struct device *dev = ssd130x->dev;
377 ssd130x_reset(ssd130x);
379 ret = regulator_enable(ssd130x->vcc_reg);
381 dev_err(dev, "Failed to enable VCC: %d\n", ret);
385 if (ssd130x->device_info->need_pwm) {
386 ret = ssd130x_pwm_enable(ssd130x);
388 dev_err(dev, "Failed to enable PWM: %d\n", ret);
389 regulator_disable(ssd130x->vcc_reg);
397 static void ssd130x_power_off(struct ssd130x_device *ssd130x)
399 pwm_disable(ssd130x->pwm);
400 pwm_put(ssd130x->pwm);
402 regulator_disable(ssd130x->vcc_reg);
405 static int ssd130x_init(struct ssd130x_device *ssd130x)
407 u32 precharge, dclk, com_invdir, compins, chargepump, seg_remap;
411 /* Set initial contrast */
412 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_CONTRAST, ssd130x->contrast);
416 /* Set segment re-map */
417 seg_remap = (SSD13XX_SET_SEG_REMAP |
418 SSD13XX_SET_SEG_REMAP_SET(ssd130x->seg_remap));
419 ret = ssd130x_write_cmd(ssd130x, 1, seg_remap);
423 /* Set COM direction */
424 com_invdir = (SSD130X_SET_COM_SCAN_DIR |
425 SSD130X_SET_COM_SCAN_DIR_SET(ssd130x->com_invdir));
426 ret = ssd130x_write_cmd(ssd130x, 1, com_invdir);
430 /* Set multiplex ratio value */
431 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_MULTIPLEX_RATIO, ssd130x->height - 1);
435 /* set display offset value */
436 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_DISPLAY_OFFSET, ssd130x->com_offset);
440 /* Set clock frequency */
441 dclk = (SSD130X_SET_CLOCK_DIV_SET(ssd130x->dclk_div - 1) |
442 SSD130X_SET_CLOCK_FREQ_SET(ssd130x->dclk_frq));
443 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_CLOCK_FREQ, dclk);
447 /* Set Area Color Mode ON/OFF & Low Power Display Mode */
448 if (ssd130x->area_color_enable || ssd130x->low_power) {
451 if (ssd130x->area_color_enable)
452 mode |= SSD130X_SET_AREA_COLOR_MODE_ENABLE;
454 if (ssd130x->low_power)
455 mode |= SSD130X_SET_AREA_COLOR_MODE_LOW_POWER;
457 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_AREA_COLOR_MODE, mode);
462 /* Set precharge period in number of ticks from the internal clock */
463 precharge = (SSD130X_SET_PRECHARGE_PERIOD1_SET(ssd130x->prechargep1) |
464 SSD130X_SET_PRECHARGE_PERIOD2_SET(ssd130x->prechargep2));
465 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_PRECHARGE_PERIOD, precharge);
469 /* Set COM pins configuration */
472 * The COM scan mode field values are the inverse of the boolean DT
473 * property "solomon,com-seq". The value 0b means scan from COM0 to
474 * COM[N - 1] while 1b means scan from COM[N - 1] to COM0.
476 scan_mode = !ssd130x->com_seq;
477 compins |= (SSD130X_SET_COM_PINS_CONFIG1_SET(scan_mode) |
478 SSD130X_SET_COM_PINS_CONFIG2_SET(ssd130x->com_lrremap));
479 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_COM_PINS_CONFIG, compins);
484 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_VCOMH, ssd130x->vcomh);
488 /* Turn on the DC-DC Charge Pump */
491 if (ssd130x->device_info->need_chargepump)
492 chargepump |= BIT(2);
494 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_CHARGE_PUMP, chargepump);
498 /* Set lookup table */
499 if (ssd130x->lookup_table_set) {
502 ret = ssd130x_write_cmd(ssd130x, 1, SSD130X_SET_LOOKUP_TABLE);
506 for (i = 0; i < ARRAY_SIZE(ssd130x->lookup_table); i++) {
507 u8 val = ssd130x->lookup_table[i];
509 if (val < 31 || val > 63)
510 dev_warn(ssd130x->dev,
511 "lookup table index %d value out of range 31 <= %d <= 63\n",
513 ret = ssd130x_write_cmd(ssd130x, 1, val);
519 /* Switch to page addressing mode */
520 if (ssd130x->page_address_mode)
521 return ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_ADDRESS_MODE,
522 SSD130X_SET_ADDRESS_MODE_PAGE);
524 /* Switch to horizontal addressing mode */
525 return ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_ADDRESS_MODE,
526 SSD130X_SET_ADDRESS_MODE_HORIZONTAL);
529 static int ssd132x_init(struct ssd130x_device *ssd130x)
533 /* Set initial contrast */
534 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_CONTRAST, 0x80);
538 /* Set column start and end */
539 ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_COL_RANGE, 0x00,
540 ssd130x->width / SSD132X_SEGMENT_WIDTH - 1);
544 /* Set row start and end */
545 ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_ROW_RANGE, 0x00, ssd130x->height - 1);
549 * Horizontal Address Increment
550 * Re-map for Column Address, Nibble and COM
553 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_SEG_REMAP, 0x53);
557 /* Set display start and offset */
558 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_DISPLAY_START, 0x00);
562 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_DISPLAY_OFFSET, 0x00);
566 /* Set display mode normal */
567 ret = ssd130x_write_cmd(ssd130x, 1, SSD132X_SET_DISPLAY_NORMAL);
571 /* Set multiplex ratio value */
572 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_MULTIPLEX_RATIO, ssd130x->height - 1);
576 /* Set phase length */
577 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PHASE_LENGTH, 0x55);
581 /* Select default linear gray scale table */
582 ret = ssd130x_write_cmd(ssd130x, 1, SSD132X_SELECT_DEFAULT_TABLE);
586 /* Set clock frequency */
587 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_CLOCK_FREQ, 0x01);
591 /* Enable internal VDD regulator */
592 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_FUNCTION_SELECT_A, 0x1);
596 /* Set pre-charge period */
597 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_PERIOD, 0x01);
601 /* Set pre-charge voltage */
602 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_VOLTAGE, 0x08);
606 /* Set VCOMH voltage */
607 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_VCOMH_VOLTAGE, 0x07);
611 /* Enable second pre-charge and internal VSL */
612 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_FUNCTION_SELECT_B, 0x62);
619 static int ssd133x_init(struct ssd130x_device *ssd130x)
623 /* Set color A contrast */
624 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_CONTRAST_A, 0x91);
628 /* Set color B contrast */
629 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_CONTRAST_B, 0x50);
633 /* Set color C contrast */
634 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_CONTRAST_C, 0x7d);
638 /* Set master current */
639 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_MASTER_CURRENT, 0x06);
643 /* Set column start and end */
644 ret = ssd130x_write_cmd(ssd130x, 3, SSD133X_SET_COL_RANGE, 0x00, ssd130x->width - 1);
648 /* Set row start and end */
649 ret = ssd130x_write_cmd(ssd130x, 3, SSD133X_SET_ROW_RANGE, 0x00, ssd130x->height - 1);
654 * Horizontal Address Increment
655 * Normal order SA,SB,SC (e.g. RGB)
659 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_SEG_REMAP, 0x20);
663 /* Set display start and offset */
664 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_DISPLAY_START, 0x00);
668 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_DISPLAY_OFFSET, 0x00);
672 /* Set display mode normal */
673 ret = ssd130x_write_cmd(ssd130x, 1, SSD133X_SET_DISPLAY_NORMAL);
677 /* Set multiplex ratio value */
678 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_MULTIPLEX_RATIO, ssd130x->height - 1);
682 /* Set master configuration */
683 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_MASTER_CONFIG, 0x8e);
688 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_POWER_SAVE_MODE, 0x0b);
692 /* Set Phase 1 and 2 period */
693 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_PHASES_PERIOD, 0x31);
697 /* Set clock divider */
698 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_CLOCK_FREQ, 0xf0);
702 /* Set pre-charge A */
703 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_A, 0x64);
707 /* Set pre-charge B */
708 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_B, 0x78);
712 /* Set pre-charge C */
713 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_C, 0x64);
717 /* Set pre-charge level */
718 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_PRECHARGE_VOLTAGE, 0x3a);
722 /* Set VCOMH voltage */
723 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_VCOMH_VOLTAGE, 0x3e);
730 static int ssd130x_update_rect(struct ssd130x_device *ssd130x,
731 struct drm_rect *rect, u8 *buf,
734 unsigned int x = rect->x1;
735 unsigned int y = rect->y1;
736 unsigned int width = drm_rect_width(rect);
737 unsigned int height = drm_rect_height(rect);
738 unsigned int line_length = DIV_ROUND_UP(width, 8);
739 unsigned int page_height = SSD130X_PAGE_HEIGHT;
740 unsigned int pages = DIV_ROUND_UP(height, page_height);
741 struct drm_device *drm = &ssd130x->drm;
745 drm_WARN_ONCE(drm, y % page_height != 0, "y must be aligned to screen page\n");
748 * The screen is divided in pages, each having a height of 8
749 * pixels, and the width of the screen. When sending a byte of
750 * data to the controller, it gives the 8 bits for the current
751 * column. I.e, the first byte are the 8 bits of the first
752 * column, then the 8 bits for the second column, etc.
755 * Representation of the screen, assuming it is 5 bits
756 * wide. Each letter-number combination is a bit that controls
768 * If you want to update this screen, you need to send 5 bytes:
769 * (1) A0 B0 C0 D0 E0 F0 G0 H0
770 * (2) A1 B1 C1 D1 E1 F1 G1 H1
771 * (3) A2 B2 C2 D2 E2 F2 G2 H2
772 * (4) A3 B3 C3 D3 E3 F3 G3 H3
773 * (5) A4 B4 C4 D4 E4 F4 G4 H4
776 if (!ssd130x->page_address_mode) {
779 /* Set address range for horizontal addressing mode */
780 ret = ssd130x_set_col_range(ssd130x, ssd130x->col_offset + x, width);
784 page_start = ssd130x->page_offset + y / page_height;
785 ret = ssd130x_set_page_range(ssd130x, page_start, pages);
790 for (i = 0; i < pages; i++) {
793 /* Last page may be partial */
794 if (page_height * (y / page_height + i + 1) > ssd130x->height)
795 m = ssd130x->height % page_height;
797 for (j = 0; j < width; j++) {
800 for (k = 0; k < m; k++) {
801 u32 idx = (page_height * i + k) * line_length + j / 8;
803 u8 bit = (byte >> (j % 8)) & 1;
807 data_array[array_idx++] = data;
811 * In page addressing mode, the start address needs to be reset,
812 * and each page then needs to be written out separately.
814 if (ssd130x->page_address_mode) {
815 ret = ssd130x_set_page_pos(ssd130x,
816 ssd130x->page_offset + i,
817 ssd130x->col_offset + x);
821 ret = ssd130x_write_data(ssd130x, data_array, width);
829 /* Write out update in one go if we aren't using page addressing mode */
830 if (!ssd130x->page_address_mode)
831 ret = ssd130x_write_data(ssd130x, data_array, width * pages);
836 static int ssd132x_update_rect(struct ssd130x_device *ssd130x,
837 struct drm_rect *rect, u8 *buf,
840 unsigned int x = rect->x1;
841 unsigned int y = rect->y1;
842 unsigned int segment_width = SSD132X_SEGMENT_WIDTH;
843 unsigned int width = drm_rect_width(rect);
844 unsigned int height = drm_rect_height(rect);
845 unsigned int columns = DIV_ROUND_UP(width, segment_width);
846 unsigned int rows = height;
847 struct drm_device *drm = &ssd130x->drm;
852 drm_WARN_ONCE(drm, x % segment_width != 0, "x must be aligned to screen segment\n");
855 * The screen is divided in Segment and Common outputs, where
856 * COM0 to COM[N - 1] are the rows and SEG0 to SEG[M - 1] are
859 * Each Segment has a 4-bit pixel and each Common output has a
860 * row of pixels. When using the (default) horizontal address
861 * increment mode, each byte of data sent to the controller has
862 * two Segments (e.g: SEG0 and SEG1) that are stored in the lower
863 * and higher nibbles of a single byte representing one column.
864 * That is, the first byte are SEG0 (D0[3:0]) and SEG1 (D0[7:4]),
865 * the second byte are SEG2 (D1[3:0]) and SEG3 (D1[7:4]) and so on.
868 /* Set column start and end */
869 ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_COL_RANGE, x / segment_width, columns - 1);
873 /* Set row start and end */
874 ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_ROW_RANGE, y, rows - 1);
878 for (i = 0; i < height; i++) {
879 /* Process pair of pixels and combine them into a single byte */
880 for (j = 0; j < width; j += segment_width) {
881 u8 n1 = buf[i * width + j];
882 u8 n2 = buf[i * width + j + 1];
884 data_array[array_idx++] = (n2 << 4) | n1;
888 /* Write out update in one go since horizontal addressing mode is used */
889 ret = ssd130x_write_data(ssd130x, data_array, columns * rows);
894 static int ssd133x_update_rect(struct ssd130x_device *ssd130x,
895 struct drm_rect *rect, u8 *data_array,
898 unsigned int x = rect->x1;
899 unsigned int y = rect->y1;
900 unsigned int columns = drm_rect_width(rect);
901 unsigned int rows = drm_rect_height(rect);
905 * The screen is divided in Segment and Common outputs, where
906 * COM0 to COM[N - 1] are the rows and SEG0 to SEG[M - 1] are
909 * Each Segment has a 8-bit pixel and each Common output has a
910 * row of pixels. When using the (default) horizontal address
911 * increment mode, each byte of data sent to the controller has
912 * a Segment (e.g: SEG0).
914 * When using the 256 color depth format, each pixel contains 3
915 * sub-pixels for color A, B and C. These have 3 bit, 3 bit and
916 * 2 bits respectively.
919 /* Set column start and end */
920 ret = ssd130x_write_cmd(ssd130x, 3, SSD133X_SET_COL_RANGE, x, columns - 1);
924 /* Set row start and end */
925 ret = ssd130x_write_cmd(ssd130x, 3, SSD133X_SET_ROW_RANGE, y, rows - 1);
929 /* Write out update in one go since horizontal addressing mode is used */
930 ret = ssd130x_write_data(ssd130x, data_array, pitch * rows);
935 static void ssd130x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array)
937 unsigned int pages = DIV_ROUND_UP(ssd130x->height, SSD130X_PAGE_HEIGHT);
938 unsigned int width = ssd130x->width;
941 if (!ssd130x->page_address_mode) {
942 memset(data_array, 0, width * pages);
944 /* Set address range for horizontal addressing mode */
945 ret = ssd130x_set_col_range(ssd130x, ssd130x->col_offset, width);
949 ret = ssd130x_set_page_range(ssd130x, ssd130x->page_offset, pages);
953 /* Write out update in one go if we aren't using page addressing mode */
954 ssd130x_write_data(ssd130x, data_array, width * pages);
957 * In page addressing mode, the start address needs to be reset,
958 * and each page then needs to be written out separately.
960 memset(data_array, 0, width);
962 for (i = 0; i < pages; i++) {
963 ret = ssd130x_set_page_pos(ssd130x,
964 ssd130x->page_offset + i,
965 ssd130x->col_offset);
969 ret = ssd130x_write_data(ssd130x, data_array, width);
976 static void ssd132x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array)
978 unsigned int columns = DIV_ROUND_UP(ssd130x->height, SSD132X_SEGMENT_WIDTH);
979 unsigned int height = ssd130x->height;
981 memset(data_array, 0, columns * height);
983 /* Write out update in one go since horizontal addressing mode is used */
984 ssd130x_write_data(ssd130x, data_array, columns * height);
987 static void ssd133x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array)
989 const struct drm_format_info *fi = drm_format_info(DRM_FORMAT_RGB332);
995 pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
997 memset(data_array, 0, pitch * ssd130x->height);
999 /* Write out update in one go since horizontal addressing mode is used */
1000 ssd130x_write_data(ssd130x, data_array, pitch * ssd130x->height);
1003 static int ssd130x_fb_blit_rect(struct drm_framebuffer *fb,
1004 const struct iosys_map *vmap,
1005 struct drm_rect *rect,
1006 u8 *buf, u8 *data_array,
1007 struct drm_format_conv_state *fmtcnv_state)
1009 struct ssd130x_device *ssd130x = drm_to_ssd130x(fb->dev);
1010 struct iosys_map dst;
1011 unsigned int dst_pitch;
1014 /* Align y to display page boundaries */
1015 rect->y1 = round_down(rect->y1, SSD130X_PAGE_HEIGHT);
1016 rect->y2 = min_t(unsigned int, round_up(rect->y2, SSD130X_PAGE_HEIGHT), ssd130x->height);
1018 dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 8);
1020 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
1024 iosys_map_set_vaddr(&dst, buf);
1025 drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
1027 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
1029 ssd130x_update_rect(ssd130x, rect, buf, data_array);
1034 static int ssd132x_fb_blit_rect(struct drm_framebuffer *fb,
1035 const struct iosys_map *vmap,
1036 struct drm_rect *rect, u8 *buf,
1038 struct drm_format_conv_state *fmtcnv_state)
1040 struct ssd130x_device *ssd130x = drm_to_ssd130x(fb->dev);
1041 unsigned int dst_pitch = drm_rect_width(rect);
1042 struct iosys_map dst;
1045 /* Align x to display segment boundaries */
1046 rect->x1 = round_down(rect->x1, SSD132X_SEGMENT_WIDTH);
1047 rect->x2 = min_t(unsigned int, round_up(rect->x2, SSD132X_SEGMENT_WIDTH),
1050 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
1054 iosys_map_set_vaddr(&dst, buf);
1055 drm_fb_xrgb8888_to_gray8(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
1057 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
1059 ssd132x_update_rect(ssd130x, rect, buf, data_array);
1064 static int ssd133x_fb_blit_rect(struct drm_framebuffer *fb,
1065 const struct iosys_map *vmap,
1066 struct drm_rect *rect, u8 *data_array,
1067 struct drm_format_conv_state *fmtcnv_state)
1069 struct ssd130x_device *ssd130x = drm_to_ssd130x(fb->dev);
1070 const struct drm_format_info *fi = drm_format_info(DRM_FORMAT_RGB332);
1071 unsigned int dst_pitch;
1072 struct iosys_map dst;
1078 dst_pitch = drm_format_info_min_pitch(fi, 0, drm_rect_width(rect));
1080 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
1084 iosys_map_set_vaddr(&dst, data_array);
1085 drm_fb_xrgb8888_to_rgb332(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
1087 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
1089 ssd133x_update_rect(ssd130x, rect, data_array, dst_pitch);
1094 static int ssd130x_primary_plane_atomic_check(struct drm_plane *plane,
1095 struct drm_atomic_state *state)
1097 struct drm_device *drm = plane->dev;
1098 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1099 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1100 struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
1101 struct drm_shadow_plane_state *shadow_plane_state = &ssd130x_state->base;
1102 struct drm_crtc *crtc = plane_state->crtc;
1103 struct drm_crtc_state *crtc_state = NULL;
1104 const struct drm_format_info *fi;
1109 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1111 ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
1112 DRM_PLANE_NO_SCALING,
1113 DRM_PLANE_NO_SCALING,
1117 else if (!plane_state->visible)
1120 fi = drm_format_info(DRM_FORMAT_R1);
1124 pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
1126 if (plane_state->fb->format != fi) {
1129 /* format conversion necessary; reserve buffer */
1130 buf = drm_format_conv_state_reserve(&shadow_plane_state->fmtcnv_state,
1136 ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
1137 if (!ssd130x_state->buffer)
1143 static int ssd132x_primary_plane_atomic_check(struct drm_plane *plane,
1144 struct drm_atomic_state *state)
1146 struct drm_device *drm = plane->dev;
1147 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1148 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1149 struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
1150 struct drm_shadow_plane_state *shadow_plane_state = &ssd130x_state->base;
1151 struct drm_crtc *crtc = plane_state->crtc;
1152 struct drm_crtc_state *crtc_state = NULL;
1153 const struct drm_format_info *fi;
1158 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1160 ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
1161 DRM_PLANE_NO_SCALING,
1162 DRM_PLANE_NO_SCALING,
1166 else if (!plane_state->visible)
1169 fi = drm_format_info(DRM_FORMAT_R8);
1173 pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
1175 if (plane_state->fb->format != fi) {
1178 /* format conversion necessary; reserve buffer */
1179 buf = drm_format_conv_state_reserve(&shadow_plane_state->fmtcnv_state,
1185 ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
1186 if (!ssd130x_state->buffer)
1192 static int ssd133x_primary_plane_atomic_check(struct drm_plane *plane,
1193 struct drm_atomic_state *state)
1195 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1196 struct drm_crtc *crtc = plane_state->crtc;
1197 struct drm_crtc_state *crtc_state = NULL;
1201 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1203 ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
1204 DRM_PLANE_NO_SCALING,
1205 DRM_PLANE_NO_SCALING,
1209 else if (!plane_state->visible)
1215 static void ssd130x_primary_plane_atomic_update(struct drm_plane *plane,
1216 struct drm_atomic_state *state)
1218 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1219 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
1220 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
1221 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1222 struct ssd130x_crtc_state *ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1223 struct ssd130x_plane_state *ssd130x_plane_state = to_ssd130x_plane_state(plane_state);
1224 struct drm_framebuffer *fb = plane_state->fb;
1225 struct drm_atomic_helper_damage_iter iter;
1226 struct drm_device *drm = plane->dev;
1227 struct drm_rect dst_clip;
1228 struct drm_rect damage;
1231 if (!drm_dev_enter(drm, &idx))
1234 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
1235 drm_atomic_for_each_plane_damage(&iter, &damage) {
1236 dst_clip = plane_state->dst;
1238 if (!drm_rect_intersect(&dst_clip, &damage))
1241 ssd130x_fb_blit_rect(fb, &shadow_plane_state->data[0], &dst_clip,
1242 ssd130x_plane_state->buffer,
1243 ssd130x_crtc_state->data_array,
1244 &shadow_plane_state->fmtcnv_state);
1250 static void ssd132x_primary_plane_atomic_update(struct drm_plane *plane,
1251 struct drm_atomic_state *state)
1253 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1254 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
1255 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
1256 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1257 struct ssd130x_crtc_state *ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1258 struct ssd130x_plane_state *ssd130x_plane_state = to_ssd130x_plane_state(plane_state);
1259 struct drm_framebuffer *fb = plane_state->fb;
1260 struct drm_atomic_helper_damage_iter iter;
1261 struct drm_device *drm = plane->dev;
1262 struct drm_rect dst_clip;
1263 struct drm_rect damage;
1266 if (!drm_dev_enter(drm, &idx))
1269 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
1270 drm_atomic_for_each_plane_damage(&iter, &damage) {
1271 dst_clip = plane_state->dst;
1273 if (!drm_rect_intersect(&dst_clip, &damage))
1276 ssd132x_fb_blit_rect(fb, &shadow_plane_state->data[0], &dst_clip,
1277 ssd130x_plane_state->buffer,
1278 ssd130x_crtc_state->data_array,
1279 &shadow_plane_state->fmtcnv_state);
1285 static void ssd133x_primary_plane_atomic_update(struct drm_plane *plane,
1286 struct drm_atomic_state *state)
1288 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1289 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
1290 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
1291 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1292 struct ssd130x_crtc_state *ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1293 struct drm_framebuffer *fb = plane_state->fb;
1294 struct drm_atomic_helper_damage_iter iter;
1295 struct drm_device *drm = plane->dev;
1296 struct drm_rect dst_clip;
1297 struct drm_rect damage;
1300 if (!drm_dev_enter(drm, &idx))
1303 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
1304 drm_atomic_for_each_plane_damage(&iter, &damage) {
1305 dst_clip = plane_state->dst;
1307 if (!drm_rect_intersect(&dst_clip, &damage))
1310 ssd133x_fb_blit_rect(fb, &shadow_plane_state->data[0], &dst_clip,
1311 ssd130x_crtc_state->data_array,
1312 &shadow_plane_state->fmtcnv_state);
1318 static void ssd130x_primary_plane_atomic_disable(struct drm_plane *plane,
1319 struct drm_atomic_state *state)
1321 struct drm_device *drm = plane->dev;
1322 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1323 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1324 struct drm_crtc_state *crtc_state;
1325 struct ssd130x_crtc_state *ssd130x_crtc_state;
1328 if (!plane_state->crtc)
1331 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1332 ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1334 if (!drm_dev_enter(drm, &idx))
1337 ssd130x_clear_screen(ssd130x, ssd130x_crtc_state->data_array);
1342 static void ssd132x_primary_plane_atomic_disable(struct drm_plane *plane,
1343 struct drm_atomic_state *state)
1345 struct drm_device *drm = plane->dev;
1346 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1347 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1348 struct drm_crtc_state *crtc_state;
1349 struct ssd130x_crtc_state *ssd130x_crtc_state;
1352 if (!plane_state->crtc)
1355 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1356 ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1358 if (!drm_dev_enter(drm, &idx))
1361 ssd132x_clear_screen(ssd130x, ssd130x_crtc_state->data_array);
1366 static void ssd133x_primary_plane_atomic_disable(struct drm_plane *plane,
1367 struct drm_atomic_state *state)
1369 struct drm_device *drm = plane->dev;
1370 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1371 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1372 struct drm_crtc_state *crtc_state;
1373 struct ssd130x_crtc_state *ssd130x_crtc_state;
1376 if (!plane_state->crtc)
1379 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1380 ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1382 if (!drm_dev_enter(drm, &idx))
1385 ssd133x_clear_screen(ssd130x, ssd130x_crtc_state->data_array);
1390 /* Called during init to allocate the plane's atomic state. */
1391 static void ssd130x_primary_plane_reset(struct drm_plane *plane)
1393 struct ssd130x_plane_state *ssd130x_state;
1395 WARN_ON(plane->state);
1397 ssd130x_state = kzalloc(sizeof(*ssd130x_state), GFP_KERNEL);
1401 __drm_gem_reset_shadow_plane(plane, &ssd130x_state->base);
1404 static struct drm_plane_state *ssd130x_primary_plane_duplicate_state(struct drm_plane *plane)
1406 struct drm_shadow_plane_state *new_shadow_plane_state;
1407 struct ssd130x_plane_state *old_ssd130x_state;
1408 struct ssd130x_plane_state *ssd130x_state;
1410 if (WARN_ON(!plane->state))
1413 old_ssd130x_state = to_ssd130x_plane_state(plane->state);
1414 ssd130x_state = kmemdup(old_ssd130x_state, sizeof(*ssd130x_state), GFP_KERNEL);
1418 /* The buffer is not duplicated and is allocated in .atomic_check */
1419 ssd130x_state->buffer = NULL;
1421 new_shadow_plane_state = &ssd130x_state->base;
1423 __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
1425 return &new_shadow_plane_state->base;
1428 static void ssd130x_primary_plane_destroy_state(struct drm_plane *plane,
1429 struct drm_plane_state *state)
1431 struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(state);
1433 kfree(ssd130x_state->buffer);
1435 __drm_gem_destroy_shadow_plane_state(&ssd130x_state->base);
1437 kfree(ssd130x_state);
1440 static const struct drm_plane_helper_funcs ssd130x_primary_plane_helper_funcs[] = {
1441 [SSD130X_FAMILY] = {
1442 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
1443 .atomic_check = ssd130x_primary_plane_atomic_check,
1444 .atomic_update = ssd130x_primary_plane_atomic_update,
1445 .atomic_disable = ssd130x_primary_plane_atomic_disable,
1447 [SSD132X_FAMILY] = {
1448 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
1449 .atomic_check = ssd132x_primary_plane_atomic_check,
1450 .atomic_update = ssd132x_primary_plane_atomic_update,
1451 .atomic_disable = ssd132x_primary_plane_atomic_disable,
1453 [SSD133X_FAMILY] = {
1454 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
1455 .atomic_check = ssd133x_primary_plane_atomic_check,
1456 .atomic_update = ssd133x_primary_plane_atomic_update,
1457 .atomic_disable = ssd133x_primary_plane_atomic_disable,
1461 static const struct drm_plane_funcs ssd130x_primary_plane_funcs = {
1462 .update_plane = drm_atomic_helper_update_plane,
1463 .disable_plane = drm_atomic_helper_disable_plane,
1464 .reset = ssd130x_primary_plane_reset,
1465 .atomic_duplicate_state = ssd130x_primary_plane_duplicate_state,
1466 .atomic_destroy_state = ssd130x_primary_plane_destroy_state,
1467 .destroy = drm_plane_cleanup,
1470 static enum drm_mode_status ssd130x_crtc_mode_valid(struct drm_crtc *crtc,
1471 const struct drm_display_mode *mode)
1473 struct ssd130x_device *ssd130x = drm_to_ssd130x(crtc->dev);
1475 if (mode->hdisplay != ssd130x->mode.hdisplay &&
1476 mode->vdisplay != ssd130x->mode.vdisplay)
1477 return MODE_ONE_SIZE;
1478 else if (mode->hdisplay != ssd130x->mode.hdisplay)
1479 return MODE_ONE_WIDTH;
1480 else if (mode->vdisplay != ssd130x->mode.vdisplay)
1481 return MODE_ONE_HEIGHT;
1486 static int ssd130x_crtc_atomic_check(struct drm_crtc *crtc,
1487 struct drm_atomic_state *state)
1489 struct drm_device *drm = crtc->dev;
1490 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1491 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1492 struct ssd130x_crtc_state *ssd130x_state = to_ssd130x_crtc_state(crtc_state);
1493 unsigned int pages = DIV_ROUND_UP(ssd130x->height, SSD130X_PAGE_HEIGHT);
1496 ret = drm_crtc_helper_atomic_check(crtc, state);
1500 ssd130x_state->data_array = kmalloc(ssd130x->width * pages, GFP_KERNEL);
1501 if (!ssd130x_state->data_array)
1507 static int ssd132x_crtc_atomic_check(struct drm_crtc *crtc,
1508 struct drm_atomic_state *state)
1510 struct drm_device *drm = crtc->dev;
1511 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1512 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1513 struct ssd130x_crtc_state *ssd130x_state = to_ssd130x_crtc_state(crtc_state);
1514 unsigned int columns = DIV_ROUND_UP(ssd130x->width, SSD132X_SEGMENT_WIDTH);
1517 ret = drm_crtc_helper_atomic_check(crtc, state);
1521 ssd130x_state->data_array = kmalloc(columns * ssd130x->height, GFP_KERNEL);
1522 if (!ssd130x_state->data_array)
1528 static int ssd133x_crtc_atomic_check(struct drm_crtc *crtc,
1529 struct drm_atomic_state *state)
1531 struct drm_device *drm = crtc->dev;
1532 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1533 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1534 struct ssd130x_crtc_state *ssd130x_state = to_ssd130x_crtc_state(crtc_state);
1535 const struct drm_format_info *fi = drm_format_info(DRM_FORMAT_RGB332);
1542 ret = drm_crtc_helper_atomic_check(crtc, state);
1546 pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
1548 ssd130x_state->data_array = kmalloc(pitch * ssd130x->height, GFP_KERNEL);
1549 if (!ssd130x_state->data_array)
1555 /* Called during init to allocate the CRTC's atomic state. */
1556 static void ssd130x_crtc_reset(struct drm_crtc *crtc)
1558 struct ssd130x_crtc_state *ssd130x_state;
1560 WARN_ON(crtc->state);
1562 ssd130x_state = kzalloc(sizeof(*ssd130x_state), GFP_KERNEL);
1566 __drm_atomic_helper_crtc_reset(crtc, &ssd130x_state->base);
1569 static struct drm_crtc_state *ssd130x_crtc_duplicate_state(struct drm_crtc *crtc)
1571 struct ssd130x_crtc_state *old_ssd130x_state;
1572 struct ssd130x_crtc_state *ssd130x_state;
1574 if (WARN_ON(!crtc->state))
1577 old_ssd130x_state = to_ssd130x_crtc_state(crtc->state);
1578 ssd130x_state = kmemdup(old_ssd130x_state, sizeof(*ssd130x_state), GFP_KERNEL);
1582 /* The buffer is not duplicated and is allocated in .atomic_check */
1583 ssd130x_state->data_array = NULL;
1585 __drm_atomic_helper_crtc_duplicate_state(crtc, &ssd130x_state->base);
1587 return &ssd130x_state->base;
1590 static void ssd130x_crtc_destroy_state(struct drm_crtc *crtc,
1591 struct drm_crtc_state *state)
1593 struct ssd130x_crtc_state *ssd130x_state = to_ssd130x_crtc_state(state);
1595 kfree(ssd130x_state->data_array);
1597 __drm_atomic_helper_crtc_destroy_state(state);
1599 kfree(ssd130x_state);
1603 * The CRTC is always enabled. Screen updates are performed by
1604 * the primary plane's atomic_update function. Disabling clears
1605 * the screen in the primary plane's atomic_disable function.
1607 static const struct drm_crtc_helper_funcs ssd130x_crtc_helper_funcs[] = {
1608 [SSD130X_FAMILY] = {
1609 .mode_valid = ssd130x_crtc_mode_valid,
1610 .atomic_check = ssd130x_crtc_atomic_check,
1612 [SSD132X_FAMILY] = {
1613 .mode_valid = ssd130x_crtc_mode_valid,
1614 .atomic_check = ssd132x_crtc_atomic_check,
1616 [SSD133X_FAMILY] = {
1617 .mode_valid = ssd130x_crtc_mode_valid,
1618 .atomic_check = ssd133x_crtc_atomic_check,
1622 static const struct drm_crtc_funcs ssd130x_crtc_funcs = {
1623 .reset = ssd130x_crtc_reset,
1624 .destroy = drm_crtc_cleanup,
1625 .set_config = drm_atomic_helper_set_config,
1626 .page_flip = drm_atomic_helper_page_flip,
1627 .atomic_duplicate_state = ssd130x_crtc_duplicate_state,
1628 .atomic_destroy_state = ssd130x_crtc_destroy_state,
1631 static void ssd130x_encoder_atomic_enable(struct drm_encoder *encoder,
1632 struct drm_atomic_state *state)
1634 struct drm_device *drm = encoder->dev;
1635 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1638 ret = ssd130x_power_on(ssd130x);
1642 ret = ssd130x_init(ssd130x);
1646 ssd130x_write_cmd(ssd130x, 1, SSD13XX_DISPLAY_ON);
1648 backlight_enable(ssd130x->bl_dev);
1653 ssd130x_power_off(ssd130x);
1657 static void ssd132x_encoder_atomic_enable(struct drm_encoder *encoder,
1658 struct drm_atomic_state *state)
1660 struct drm_device *drm = encoder->dev;
1661 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1664 ret = ssd130x_power_on(ssd130x);
1668 ret = ssd132x_init(ssd130x);
1672 ssd130x_write_cmd(ssd130x, 1, SSD13XX_DISPLAY_ON);
1674 backlight_enable(ssd130x->bl_dev);
1679 ssd130x_power_off(ssd130x);
1682 static void ssd133x_encoder_atomic_enable(struct drm_encoder *encoder,
1683 struct drm_atomic_state *state)
1685 struct drm_device *drm = encoder->dev;
1686 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1689 ret = ssd130x_power_on(ssd130x);
1693 ret = ssd133x_init(ssd130x);
1697 ssd130x_write_cmd(ssd130x, 1, SSD13XX_DISPLAY_ON);
1699 backlight_enable(ssd130x->bl_dev);
1704 ssd130x_power_off(ssd130x);
1707 static void ssd130x_encoder_atomic_disable(struct drm_encoder *encoder,
1708 struct drm_atomic_state *state)
1710 struct drm_device *drm = encoder->dev;
1711 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1713 backlight_disable(ssd130x->bl_dev);
1715 ssd130x_write_cmd(ssd130x, 1, SSD13XX_DISPLAY_OFF);
1717 ssd130x_power_off(ssd130x);
1720 static const struct drm_encoder_helper_funcs ssd130x_encoder_helper_funcs[] = {
1721 [SSD130X_FAMILY] = {
1722 .atomic_enable = ssd130x_encoder_atomic_enable,
1723 .atomic_disable = ssd130x_encoder_atomic_disable,
1725 [SSD132X_FAMILY] = {
1726 .atomic_enable = ssd132x_encoder_atomic_enable,
1727 .atomic_disable = ssd130x_encoder_atomic_disable,
1729 [SSD133X_FAMILY] = {
1730 .atomic_enable = ssd133x_encoder_atomic_enable,
1731 .atomic_disable = ssd130x_encoder_atomic_disable,
1735 static const struct drm_encoder_funcs ssd130x_encoder_funcs = {
1736 .destroy = drm_encoder_cleanup,
1739 static int ssd130x_connector_get_modes(struct drm_connector *connector)
1741 struct ssd130x_device *ssd130x = drm_to_ssd130x(connector->dev);
1742 struct drm_display_mode *mode;
1743 struct device *dev = ssd130x->dev;
1745 mode = drm_mode_duplicate(connector->dev, &ssd130x->mode);
1747 dev_err(dev, "Failed to duplicated mode\n");
1751 drm_mode_probed_add(connector, mode);
1752 drm_set_preferred_mode(connector, mode->hdisplay, mode->vdisplay);
1754 /* There is only a single mode */
1758 static const struct drm_connector_helper_funcs ssd130x_connector_helper_funcs = {
1759 .get_modes = ssd130x_connector_get_modes,
1762 static const struct drm_connector_funcs ssd130x_connector_funcs = {
1763 .reset = drm_atomic_helper_connector_reset,
1764 .fill_modes = drm_helper_probe_single_connector_modes,
1765 .destroy = drm_connector_cleanup,
1766 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1767 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1770 static const struct drm_mode_config_funcs ssd130x_mode_config_funcs = {
1771 .fb_create = drm_gem_fb_create_with_dirty,
1772 .atomic_check = drm_atomic_helper_check,
1773 .atomic_commit = drm_atomic_helper_commit,
1776 static const uint32_t ssd130x_formats[] = {
1777 DRM_FORMAT_XRGB8888,
1780 DEFINE_DRM_GEM_FOPS(ssd130x_fops);
1782 static const struct drm_driver ssd130x_drm_driver = {
1783 DRM_GEM_SHMEM_DRIVER_OPS,
1784 DRM_FBDEV_SHMEM_DRIVER_OPS,
1785 .name = DRIVER_NAME,
1786 .desc = DRIVER_DESC,
1787 .date = DRIVER_DATE,
1788 .major = DRIVER_MAJOR,
1789 .minor = DRIVER_MINOR,
1790 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
1791 .fops = &ssd130x_fops,
1794 static int ssd130x_update_bl(struct backlight_device *bdev)
1796 struct ssd130x_device *ssd130x = bl_get_data(bdev);
1797 int brightness = backlight_get_brightness(bdev);
1800 ssd130x->contrast = brightness;
1802 ret = ssd130x_write_cmd(ssd130x, 1, SSD13XX_CONTRAST);
1806 ret = ssd130x_write_cmd(ssd130x, 1, ssd130x->contrast);
1813 static const struct backlight_ops ssd130xfb_bl_ops = {
1814 .update_status = ssd130x_update_bl,
1817 static void ssd130x_parse_properties(struct ssd130x_device *ssd130x)
1819 struct device *dev = ssd130x->dev;
1821 if (device_property_read_u32(dev, "solomon,width", &ssd130x->width))
1822 ssd130x->width = ssd130x->device_info->default_width;
1824 if (device_property_read_u32(dev, "solomon,height", &ssd130x->height))
1825 ssd130x->height = ssd130x->device_info->default_height;
1827 if (device_property_read_u32(dev, "solomon,page-offset", &ssd130x->page_offset))
1828 ssd130x->page_offset = 1;
1830 if (device_property_read_u32(dev, "solomon,col-offset", &ssd130x->col_offset))
1831 ssd130x->col_offset = 0;
1833 if (device_property_read_u32(dev, "solomon,com-offset", &ssd130x->com_offset))
1834 ssd130x->com_offset = 0;
1836 if (device_property_read_u32(dev, "solomon,prechargep1", &ssd130x->prechargep1))
1837 ssd130x->prechargep1 = 2;
1839 if (device_property_read_u32(dev, "solomon,prechargep2", &ssd130x->prechargep2))
1840 ssd130x->prechargep2 = 2;
1842 if (!device_property_read_u8_array(dev, "solomon,lookup-table",
1843 ssd130x->lookup_table,
1844 ARRAY_SIZE(ssd130x->lookup_table)))
1845 ssd130x->lookup_table_set = 1;
1847 ssd130x->seg_remap = !device_property_read_bool(dev, "solomon,segment-no-remap");
1848 ssd130x->com_seq = device_property_read_bool(dev, "solomon,com-seq");
1849 ssd130x->com_lrremap = device_property_read_bool(dev, "solomon,com-lrremap");
1850 ssd130x->com_invdir = device_property_read_bool(dev, "solomon,com-invdir");
1851 ssd130x->area_color_enable =
1852 device_property_read_bool(dev, "solomon,area-color-enable");
1853 ssd130x->low_power = device_property_read_bool(dev, "solomon,low-power");
1855 ssd130x->contrast = 127;
1856 ssd130x->vcomh = ssd130x->device_info->default_vcomh;
1858 /* Setup display timing */
1859 if (device_property_read_u32(dev, "solomon,dclk-div", &ssd130x->dclk_div))
1860 ssd130x->dclk_div = ssd130x->device_info->default_dclk_div;
1861 if (device_property_read_u32(dev, "solomon,dclk-frq", &ssd130x->dclk_frq))
1862 ssd130x->dclk_frq = ssd130x->device_info->default_dclk_frq;
1865 static int ssd130x_init_modeset(struct ssd130x_device *ssd130x)
1867 enum ssd130x_family_ids family_id = ssd130x->device_info->family_id;
1868 struct drm_display_mode *mode = &ssd130x->mode;
1869 struct device *dev = ssd130x->dev;
1870 struct drm_device *drm = &ssd130x->drm;
1871 unsigned long max_width, max_height;
1872 struct drm_plane *primary_plane;
1873 struct drm_crtc *crtc;
1874 struct drm_encoder *encoder;
1875 struct drm_connector *connector;
1882 ret = drmm_mode_config_init(drm);
1884 dev_err(dev, "DRM mode config init failed: %d\n", ret);
1888 mode->type = DRM_MODE_TYPE_DRIVER;
1890 mode->hdisplay = mode->htotal = ssd130x->width;
1891 mode->hsync_start = mode->hsync_end = ssd130x->width;
1892 mode->vdisplay = mode->vtotal = ssd130x->height;
1893 mode->vsync_start = mode->vsync_end = ssd130x->height;
1894 mode->width_mm = 27;
1895 mode->height_mm = 27;
1897 max_width = max_t(unsigned long, mode->hdisplay, DRM_SHADOW_PLANE_MAX_WIDTH);
1898 max_height = max_t(unsigned long, mode->vdisplay, DRM_SHADOW_PLANE_MAX_HEIGHT);
1900 drm->mode_config.min_width = mode->hdisplay;
1901 drm->mode_config.max_width = max_width;
1902 drm->mode_config.min_height = mode->vdisplay;
1903 drm->mode_config.max_height = max_height;
1904 drm->mode_config.preferred_depth = 24;
1905 drm->mode_config.funcs = &ssd130x_mode_config_funcs;
1909 primary_plane = &ssd130x->primary_plane;
1910 ret = drm_universal_plane_init(drm, primary_plane, 0, &ssd130x_primary_plane_funcs,
1911 ssd130x_formats, ARRAY_SIZE(ssd130x_formats),
1912 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
1914 dev_err(dev, "DRM primary plane init failed: %d\n", ret);
1918 drm_plane_helper_add(primary_plane, &ssd130x_primary_plane_helper_funcs[family_id]);
1920 drm_plane_enable_fb_damage_clips(primary_plane);
1924 crtc = &ssd130x->crtc;
1925 ret = drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
1926 &ssd130x_crtc_funcs, NULL);
1928 dev_err(dev, "DRM crtc init failed: %d\n", ret);
1932 drm_crtc_helper_add(crtc, &ssd130x_crtc_helper_funcs[family_id]);
1936 encoder = &ssd130x->encoder;
1937 ret = drm_encoder_init(drm, encoder, &ssd130x_encoder_funcs,
1938 DRM_MODE_ENCODER_NONE, NULL);
1940 dev_err(dev, "DRM encoder init failed: %d\n", ret);
1944 drm_encoder_helper_add(encoder, &ssd130x_encoder_helper_funcs[family_id]);
1946 encoder->possible_crtcs = drm_crtc_mask(crtc);
1950 connector = &ssd130x->connector;
1951 ret = drm_connector_init(drm, connector, &ssd130x_connector_funcs,
1952 DRM_MODE_CONNECTOR_Unknown);
1954 dev_err(dev, "DRM connector init failed: %d\n", ret);
1958 drm_connector_helper_add(connector, &ssd130x_connector_helper_funcs);
1960 ret = drm_connector_attach_encoder(connector, encoder);
1962 dev_err(dev, "DRM attach connector to encoder failed: %d\n", ret);
1966 drm_mode_config_reset(drm);
1971 static int ssd130x_get_resources(struct ssd130x_device *ssd130x)
1973 struct device *dev = ssd130x->dev;
1975 ssd130x->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
1976 if (IS_ERR(ssd130x->reset))
1977 return dev_err_probe(dev, PTR_ERR(ssd130x->reset),
1978 "Failed to get reset gpio\n");
1980 ssd130x->vcc_reg = devm_regulator_get(dev, "vcc");
1981 if (IS_ERR(ssd130x->vcc_reg))
1982 return dev_err_probe(dev, PTR_ERR(ssd130x->vcc_reg),
1983 "Failed to get VCC regulator\n");
1988 struct ssd130x_device *ssd130x_probe(struct device *dev, struct regmap *regmap)
1990 struct ssd130x_device *ssd130x;
1991 struct backlight_device *bl;
1992 struct drm_device *drm;
1995 ssd130x = devm_drm_dev_alloc(dev, &ssd130x_drm_driver,
1996 struct ssd130x_device, drm);
1997 if (IS_ERR(ssd130x))
1998 return ERR_PTR(dev_err_probe(dev, PTR_ERR(ssd130x),
1999 "Failed to allocate DRM device\n"));
2001 drm = &ssd130x->drm;
2004 ssd130x->regmap = regmap;
2005 ssd130x->device_info = device_get_match_data(dev);
2007 if (ssd130x->device_info->page_mode_only)
2008 ssd130x->page_address_mode = 1;
2010 ssd130x_parse_properties(ssd130x);
2012 ret = ssd130x_get_resources(ssd130x);
2014 return ERR_PTR(ret);
2016 bl = devm_backlight_device_register(dev, dev_name(dev), dev, ssd130x,
2017 &ssd130xfb_bl_ops, NULL);
2019 return ERR_PTR(dev_err_probe(dev, PTR_ERR(bl),
2020 "Unable to register backlight device\n"));
2022 bl->props.brightness = ssd130x->contrast;
2023 bl->props.max_brightness = MAX_CONTRAST;
2024 ssd130x->bl_dev = bl;
2026 ret = ssd130x_init_modeset(ssd130x);
2028 return ERR_PTR(ret);
2030 ret = drm_dev_register(drm, 0);
2032 return ERR_PTR(dev_err_probe(dev, ret, "DRM device register failed\n"));
2034 drm_client_setup(drm, NULL);
2038 EXPORT_SYMBOL_GPL(ssd130x_probe);
2040 void ssd130x_remove(struct ssd130x_device *ssd130x)
2042 drm_dev_unplug(&ssd130x->drm);
2043 drm_atomic_helper_shutdown(&ssd130x->drm);
2045 EXPORT_SYMBOL_GPL(ssd130x_remove);
2047 void ssd130x_shutdown(struct ssd130x_device *ssd130x)
2049 drm_atomic_helper_shutdown(&ssd130x->drm);
2051 EXPORT_SYMBOL_GPL(ssd130x_shutdown);
2053 MODULE_DESCRIPTION(DRIVER_DESC);
2055 MODULE_LICENSE("GPL v2");