1 // SPDX-License-Identifier: GPL-2.0-only
3 * DRM driver for Solomon SSD13xx OLED displays
5 * Copyright 2022 Red Hat Inc.
6 * Author: Javier Martinez Canillas <javierm@redhat.com>
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_crtc_helper.h>
24 #include <drm/drm_damage_helper.h>
25 #include <drm/drm_edid.h>
26 #include <drm/drm_fbdev_generic.h>
27 #include <drm/drm_format_helper.h>
28 #include <drm/drm_framebuffer.h>
29 #include <drm/drm_gem_atomic_helper.h>
30 #include <drm/drm_gem_framebuffer_helper.h>
31 #include <drm/drm_gem_shmem_helper.h>
32 #include <drm/drm_managed.h>
33 #include <drm/drm_modes.h>
34 #include <drm/drm_rect.h>
35 #include <drm/drm_probe_helper.h>
39 #define DRIVER_NAME "ssd130x"
40 #define DRIVER_DESC "DRM driver for Solomon SSD13xx OLED displays"
41 #define DRIVER_DATE "20220131"
42 #define DRIVER_MAJOR 1
43 #define DRIVER_MINOR 0
45 #define SSD130X_PAGE_HEIGHT 8
47 #define SSD132X_SEGMENT_WIDTH 2
49 /* ssd13xx commands */
50 #define SSD13XX_CONTRAST 0x81
51 #define SSD13XX_SET_SEG_REMAP 0xa0
52 #define SSD13XX_SET_MULTIPLEX_RATIO 0xa8
53 #define SSD13XX_DISPLAY_OFF 0xae
54 #define SSD13XX_DISPLAY_ON 0xaf
56 #define SSD13XX_SET_SEG_REMAP_MASK GENMASK(0, 0)
57 #define SSD13XX_SET_SEG_REMAP_SET(val) FIELD_PREP(SSD13XX_SET_SEG_REMAP_MASK, (val))
59 /* ssd130x commands */
60 #define SSD130X_PAGE_COL_START_LOW 0x00
61 #define SSD130X_PAGE_COL_START_HIGH 0x10
62 #define SSD130X_SET_ADDRESS_MODE 0x20
63 #define SSD130X_SET_COL_RANGE 0x21
64 #define SSD130X_SET_PAGE_RANGE 0x22
65 #define SSD130X_SET_LOOKUP_TABLE 0x91
66 #define SSD130X_CHARGE_PUMP 0x8d
67 #define SSD130X_START_PAGE_ADDRESS 0xb0
68 #define SSD130X_SET_COM_SCAN_DIR 0xc0
69 #define SSD130X_SET_DISPLAY_OFFSET 0xd3
70 #define SSD130X_SET_CLOCK_FREQ 0xd5
71 #define SSD130X_SET_AREA_COLOR_MODE 0xd8
72 #define SSD130X_SET_PRECHARGE_PERIOD 0xd9
73 #define SSD130X_SET_COM_PINS_CONFIG 0xda
74 #define SSD130X_SET_VCOMH 0xdb
76 /* ssd130x commands accessors */
77 #define SSD130X_PAGE_COL_START_MASK GENMASK(3, 0)
78 #define SSD130X_PAGE_COL_START_HIGH_SET(val) FIELD_PREP(SSD130X_PAGE_COL_START_MASK, (val) >> 4)
79 #define SSD130X_PAGE_COL_START_LOW_SET(val) FIELD_PREP(SSD130X_PAGE_COL_START_MASK, (val))
80 #define SSD130X_START_PAGE_ADDRESS_MASK GENMASK(2, 0)
81 #define SSD130X_START_PAGE_ADDRESS_SET(val) FIELD_PREP(SSD130X_START_PAGE_ADDRESS_MASK, (val))
82 #define SSD130X_SET_COM_SCAN_DIR_MASK GENMASK(3, 3)
83 #define SSD130X_SET_COM_SCAN_DIR_SET(val) FIELD_PREP(SSD130X_SET_COM_SCAN_DIR_MASK, (val))
84 #define SSD130X_SET_CLOCK_DIV_MASK GENMASK(3, 0)
85 #define SSD130X_SET_CLOCK_DIV_SET(val) FIELD_PREP(SSD130X_SET_CLOCK_DIV_MASK, (val))
86 #define SSD130X_SET_CLOCK_FREQ_MASK GENMASK(7, 4)
87 #define SSD130X_SET_CLOCK_FREQ_SET(val) FIELD_PREP(SSD130X_SET_CLOCK_FREQ_MASK, (val))
88 #define SSD130X_SET_PRECHARGE_PERIOD1_MASK GENMASK(3, 0)
89 #define SSD130X_SET_PRECHARGE_PERIOD1_SET(val) FIELD_PREP(SSD130X_SET_PRECHARGE_PERIOD1_MASK, (val))
90 #define SSD130X_SET_PRECHARGE_PERIOD2_MASK GENMASK(7, 4)
91 #define SSD130X_SET_PRECHARGE_PERIOD2_SET(val) FIELD_PREP(SSD130X_SET_PRECHARGE_PERIOD2_MASK, (val))
92 #define SSD130X_SET_COM_PINS_CONFIG1_MASK GENMASK(4, 4)
93 #define SSD130X_SET_COM_PINS_CONFIG1_SET(val) FIELD_PREP(SSD130X_SET_COM_PINS_CONFIG1_MASK, (val))
94 #define SSD130X_SET_COM_PINS_CONFIG2_MASK GENMASK(5, 5)
95 #define SSD130X_SET_COM_PINS_CONFIG2_SET(val) FIELD_PREP(SSD130X_SET_COM_PINS_CONFIG2_MASK, (val))
97 #define SSD130X_SET_ADDRESS_MODE_HORIZONTAL 0x00
98 #define SSD130X_SET_ADDRESS_MODE_VERTICAL 0x01
99 #define SSD130X_SET_ADDRESS_MODE_PAGE 0x02
101 #define SSD130X_SET_AREA_COLOR_MODE_ENABLE 0x1e
102 #define SSD130X_SET_AREA_COLOR_MODE_LOW_POWER 0x05
104 /* ssd132x commands */
105 #define SSD132X_SET_COL_RANGE 0x15
106 #define SSD132X_SET_DEACTIVATE_SCROLL 0x2e
107 #define SSD132X_SET_ROW_RANGE 0x75
108 #define SSD132X_SET_DISPLAY_START 0xa1
109 #define SSD132X_SET_DISPLAY_OFFSET 0xa2
110 #define SSD132X_SET_DISPLAY_NORMAL 0xa4
111 #define SSD132X_SET_FUNCTION_SELECT_A 0xab
112 #define SSD132X_SET_PHASE_LENGTH 0xb1
113 #define SSD132X_SET_CLOCK_FREQ 0xb3
114 #define SSD132X_SET_GPIO 0xb5
115 #define SSD132X_SET_PRECHARGE_PERIOD 0xb6
116 #define SSD132X_SET_GRAY_SCALE_TABLE 0xb8
117 #define SSD132X_SELECT_DEFAULT_TABLE 0xb9
118 #define SSD132X_SET_PRECHARGE_VOLTAGE 0xbc
119 #define SSD130X_SET_VCOMH_VOLTAGE 0xbe
120 #define SSD132X_SET_FUNCTION_SELECT_B 0xd5
122 /* ssd133x commands */
123 #define SSD133X_SET_COL_RANGE 0x15
124 #define SSD133X_SET_ROW_RANGE 0x75
125 #define SSD133X_CONTRAST_A 0x81
126 #define SSD133X_CONTRAST_B 0x82
127 #define SSD133X_CONTRAST_C 0x83
128 #define SSD133X_SET_MASTER_CURRENT 0x87
129 #define SSD132X_SET_PRECHARGE_A 0x8a
130 #define SSD132X_SET_PRECHARGE_B 0x8b
131 #define SSD132X_SET_PRECHARGE_C 0x8c
132 #define SSD133X_SET_DISPLAY_START 0xa1
133 #define SSD133X_SET_DISPLAY_OFFSET 0xa2
134 #define SSD133X_SET_DISPLAY_NORMAL 0xa4
135 #define SSD133X_SET_MASTER_CONFIG 0xad
136 #define SSD133X_POWER_SAVE_MODE 0xb0
137 #define SSD133X_PHASES_PERIOD 0xb1
138 #define SSD133X_SET_CLOCK_FREQ 0xb3
139 #define SSD133X_SET_PRECHARGE_VOLTAGE 0xbb
140 #define SSD133X_SET_VCOMH_VOLTAGE 0xbe
142 #define MAX_CONTRAST 255
144 const struct ssd130x_deviceinfo ssd130x_variants[] = {
146 .default_vcomh = 0x40,
147 .default_dclk_div = 1,
148 .default_dclk_frq = 5,
149 .default_width = 132,
150 .default_height = 64,
152 .family_id = SSD130X_FAMILY,
155 .default_vcomh = 0x34,
156 .default_dclk_div = 1,
157 .default_dclk_frq = 7,
158 .default_width = 132,
159 .default_height = 64,
160 .family_id = SSD130X_FAMILY,
163 .default_vcomh = 0x20,
164 .default_dclk_div = 1,
165 .default_dclk_frq = 8,
166 .need_chargepump = 1,
167 .default_width = 128,
168 .default_height = 64,
169 .family_id = SSD130X_FAMILY,
172 .default_vcomh = 0x20,
173 .default_dclk_div = 2,
174 .default_dclk_frq = 12,
176 .default_width = 128,
177 .default_height = 39,
178 .family_id = SSD130X_FAMILY,
181 .default_vcomh = 0x34,
182 .default_dclk_div = 1,
183 .default_dclk_frq = 10,
184 .default_width = 128,
185 .default_height = 64,
186 .family_id = SSD130X_FAMILY,
190 .default_width = 480,
191 .default_height = 128,
192 .family_id = SSD132X_FAMILY,
195 .default_width = 128,
196 .default_height = 80,
197 .family_id = SSD132X_FAMILY,
200 .default_width = 128,
201 .default_height = 128,
202 .family_id = SSD132X_FAMILY,
207 .default_height = 64,
208 .family_id = SSD133X_FAMILY,
211 EXPORT_SYMBOL_NS_GPL(ssd130x_variants, DRM_SSD130X);
213 struct ssd130x_crtc_state {
214 struct drm_crtc_state base;
215 /* Buffer to store pixels in HW format and written to the panel */
219 struct ssd130x_plane_state {
220 struct drm_shadow_plane_state base;
221 /* Intermediate buffer to convert pixels from XRGB8888 to HW format */
225 static inline struct ssd130x_crtc_state *to_ssd130x_crtc_state(struct drm_crtc_state *state)
227 return container_of(state, struct ssd130x_crtc_state, base);
230 static inline struct ssd130x_plane_state *to_ssd130x_plane_state(struct drm_plane_state *state)
232 return container_of(state, struct ssd130x_plane_state, base.base);
235 static inline struct ssd130x_device *drm_to_ssd130x(struct drm_device *drm)
237 return container_of(drm, struct ssd130x_device, drm);
241 * Helper to write data (SSD13XX_DATA) to the device.
243 static int ssd130x_write_data(struct ssd130x_device *ssd130x, u8 *values, int count)
245 return regmap_bulk_write(ssd130x->regmap, SSD13XX_DATA, values, count);
249 * Helper to write command (SSD13XX_COMMAND). The fist variadic argument
250 * is the command to write and the following are the command options.
252 * Note that the ssd13xx protocol requires each command and option to be
253 * written as a SSD13XX_COMMAND device register value. That is why a call
254 * to regmap_write(..., SSD13XX_COMMAND, ...) is done for each argument.
256 static int ssd130x_write_cmd(struct ssd130x_device *ssd130x, int count,
257 /* u8 cmd, u8 option, ... */...)
266 value = va_arg(ap, int);
267 ret = regmap_write(ssd130x->regmap, SSD13XX_COMMAND, value);
278 /* Set address range for horizontal/vertical addressing modes */
279 static int ssd130x_set_col_range(struct ssd130x_device *ssd130x,
280 u8 col_start, u8 cols)
282 u8 col_end = col_start + cols - 1;
285 if (col_start == ssd130x->col_start && col_end == ssd130x->col_end)
288 ret = ssd130x_write_cmd(ssd130x, 3, SSD130X_SET_COL_RANGE, col_start, col_end);
292 ssd130x->col_start = col_start;
293 ssd130x->col_end = col_end;
297 static int ssd130x_set_page_range(struct ssd130x_device *ssd130x,
298 u8 page_start, u8 pages)
300 u8 page_end = page_start + pages - 1;
303 if (page_start == ssd130x->page_start && page_end == ssd130x->page_end)
306 ret = ssd130x_write_cmd(ssd130x, 3, SSD130X_SET_PAGE_RANGE, page_start, page_end);
310 ssd130x->page_start = page_start;
311 ssd130x->page_end = page_end;
315 /* Set page and column start address for page addressing mode */
316 static int ssd130x_set_page_pos(struct ssd130x_device *ssd130x,
317 u8 page_start, u8 col_start)
320 u32 page, col_low, col_high;
322 page = SSD130X_START_PAGE_ADDRESS |
323 SSD130X_START_PAGE_ADDRESS_SET(page_start);
324 col_low = SSD130X_PAGE_COL_START_LOW |
325 SSD130X_PAGE_COL_START_LOW_SET(col_start);
326 col_high = SSD130X_PAGE_COL_START_HIGH |
327 SSD130X_PAGE_COL_START_HIGH_SET(col_start);
328 ret = ssd130x_write_cmd(ssd130x, 3, page, col_low, col_high);
335 static int ssd130x_pwm_enable(struct ssd130x_device *ssd130x)
337 struct device *dev = ssd130x->dev;
338 struct pwm_state pwmstate;
340 ssd130x->pwm = pwm_get(dev, NULL);
341 if (IS_ERR(ssd130x->pwm)) {
342 dev_err(dev, "Could not get PWM from firmware description!\n");
343 return PTR_ERR(ssd130x->pwm);
346 pwm_init_state(ssd130x->pwm, &pwmstate);
347 pwm_set_relative_duty_cycle(&pwmstate, 50, 100);
348 pwm_apply_might_sleep(ssd130x->pwm, &pwmstate);
351 pwm_enable(ssd130x->pwm);
353 dev_dbg(dev, "Using PWM %s with a %lluns period.\n",
354 ssd130x->pwm->label, pwm_get_period(ssd130x->pwm));
359 static void ssd130x_reset(struct ssd130x_device *ssd130x)
364 /* Reset the screen */
365 gpiod_set_value_cansleep(ssd130x->reset, 1);
367 gpiod_set_value_cansleep(ssd130x->reset, 0);
371 static int ssd130x_power_on(struct ssd130x_device *ssd130x)
373 struct device *dev = ssd130x->dev;
376 ssd130x_reset(ssd130x);
378 ret = regulator_enable(ssd130x->vcc_reg);
380 dev_err(dev, "Failed to enable VCC: %d\n", ret);
384 if (ssd130x->device_info->need_pwm) {
385 ret = ssd130x_pwm_enable(ssd130x);
387 dev_err(dev, "Failed to enable PWM: %d\n", ret);
388 regulator_disable(ssd130x->vcc_reg);
396 static void ssd130x_power_off(struct ssd130x_device *ssd130x)
398 pwm_disable(ssd130x->pwm);
399 pwm_put(ssd130x->pwm);
401 regulator_disable(ssd130x->vcc_reg);
404 static int ssd130x_init(struct ssd130x_device *ssd130x)
406 u32 precharge, dclk, com_invdir, compins, chargepump, seg_remap;
410 /* Set initial contrast */
411 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_CONTRAST, ssd130x->contrast);
415 /* Set segment re-map */
416 seg_remap = (SSD13XX_SET_SEG_REMAP |
417 SSD13XX_SET_SEG_REMAP_SET(ssd130x->seg_remap));
418 ret = ssd130x_write_cmd(ssd130x, 1, seg_remap);
422 /* Set COM direction */
423 com_invdir = (SSD130X_SET_COM_SCAN_DIR |
424 SSD130X_SET_COM_SCAN_DIR_SET(ssd130x->com_invdir));
425 ret = ssd130x_write_cmd(ssd130x, 1, com_invdir);
429 /* Set multiplex ratio value */
430 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_MULTIPLEX_RATIO, ssd130x->height - 1);
434 /* set display offset value */
435 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_DISPLAY_OFFSET, ssd130x->com_offset);
439 /* Set clock frequency */
440 dclk = (SSD130X_SET_CLOCK_DIV_SET(ssd130x->dclk_div - 1) |
441 SSD130X_SET_CLOCK_FREQ_SET(ssd130x->dclk_frq));
442 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_CLOCK_FREQ, dclk);
446 /* Set Area Color Mode ON/OFF & Low Power Display Mode */
447 if (ssd130x->area_color_enable || ssd130x->low_power) {
450 if (ssd130x->area_color_enable)
451 mode |= SSD130X_SET_AREA_COLOR_MODE_ENABLE;
453 if (ssd130x->low_power)
454 mode |= SSD130X_SET_AREA_COLOR_MODE_LOW_POWER;
456 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_AREA_COLOR_MODE, mode);
461 /* Set precharge period in number of ticks from the internal clock */
462 precharge = (SSD130X_SET_PRECHARGE_PERIOD1_SET(ssd130x->prechargep1) |
463 SSD130X_SET_PRECHARGE_PERIOD2_SET(ssd130x->prechargep2));
464 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_PRECHARGE_PERIOD, precharge);
468 /* Set COM pins configuration */
471 * The COM scan mode field values are the inverse of the boolean DT
472 * property "solomon,com-seq". The value 0b means scan from COM0 to
473 * COM[N - 1] while 1b means scan from COM[N - 1] to COM0.
475 scan_mode = !ssd130x->com_seq;
476 compins |= (SSD130X_SET_COM_PINS_CONFIG1_SET(scan_mode) |
477 SSD130X_SET_COM_PINS_CONFIG2_SET(ssd130x->com_lrremap));
478 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_COM_PINS_CONFIG, compins);
483 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_VCOMH, ssd130x->vcomh);
487 /* Turn on the DC-DC Charge Pump */
490 if (ssd130x->device_info->need_chargepump)
491 chargepump |= BIT(2);
493 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_CHARGE_PUMP, chargepump);
497 /* Set lookup table */
498 if (ssd130x->lookup_table_set) {
501 ret = ssd130x_write_cmd(ssd130x, 1, SSD130X_SET_LOOKUP_TABLE);
505 for (i = 0; i < ARRAY_SIZE(ssd130x->lookup_table); i++) {
506 u8 val = ssd130x->lookup_table[i];
508 if (val < 31 || val > 63)
509 dev_warn(ssd130x->dev,
510 "lookup table index %d value out of range 31 <= %d <= 63\n",
512 ret = ssd130x_write_cmd(ssd130x, 1, val);
518 /* Switch to page addressing mode */
519 if (ssd130x->page_address_mode)
520 return ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_ADDRESS_MODE,
521 SSD130X_SET_ADDRESS_MODE_PAGE);
523 /* Switch to horizontal addressing mode */
524 return ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_ADDRESS_MODE,
525 SSD130X_SET_ADDRESS_MODE_HORIZONTAL);
528 static int ssd132x_init(struct ssd130x_device *ssd130x)
532 /* Set initial contrast */
533 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_CONTRAST, 0x80);
537 /* Set column start and end */
538 ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_COL_RANGE, 0x00,
539 ssd130x->width / SSD132X_SEGMENT_WIDTH - 1);
543 /* Set row start and end */
544 ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_ROW_RANGE, 0x00, ssd130x->height - 1);
548 * Horizontal Address Increment
549 * Re-map for Column Address, Nibble and COM
552 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_SEG_REMAP, 0x53);
556 /* Set display start and offset */
557 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_DISPLAY_START, 0x00);
561 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_DISPLAY_OFFSET, 0x00);
565 /* Set display mode normal */
566 ret = ssd130x_write_cmd(ssd130x, 1, SSD132X_SET_DISPLAY_NORMAL);
570 /* Set multiplex ratio value */
571 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_MULTIPLEX_RATIO, ssd130x->height - 1);
575 /* Set phase length */
576 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PHASE_LENGTH, 0x55);
580 /* Select default linear gray scale table */
581 ret = ssd130x_write_cmd(ssd130x, 1, SSD132X_SELECT_DEFAULT_TABLE);
585 /* Set clock frequency */
586 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_CLOCK_FREQ, 0x01);
590 /* Enable internal VDD regulator */
591 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_FUNCTION_SELECT_A, 0x1);
595 /* Set pre-charge period */
596 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_PERIOD, 0x01);
600 /* Set pre-charge voltage */
601 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_VOLTAGE, 0x08);
605 /* Set VCOMH voltage */
606 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_VCOMH_VOLTAGE, 0x07);
610 /* Enable second pre-charge and internal VSL */
611 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_FUNCTION_SELECT_B, 0x62);
618 static int ssd133x_init(struct ssd130x_device *ssd130x)
622 /* Set color A contrast */
623 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_CONTRAST_A, 0x91);
627 /* Set color B contrast */
628 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_CONTRAST_B, 0x50);
632 /* Set color C contrast */
633 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_CONTRAST_C, 0x7d);
637 /* Set master current */
638 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_MASTER_CURRENT, 0x06);
642 /* Set column start and end */
643 ret = ssd130x_write_cmd(ssd130x, 3, SSD133X_SET_COL_RANGE, 0x00, ssd130x->width - 1);
647 /* Set row start and end */
648 ret = ssd130x_write_cmd(ssd130x, 3, SSD133X_SET_ROW_RANGE, 0x00, ssd130x->height - 1);
653 * Horizontal Address Increment
654 * Normal order SA,SB,SC (e.g. RGB)
658 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_SEG_REMAP, 0x20);
662 /* Set display start and offset */
663 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_DISPLAY_START, 0x00);
667 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_DISPLAY_OFFSET, 0x00);
671 /* Set display mode normal */
672 ret = ssd130x_write_cmd(ssd130x, 1, SSD133X_SET_DISPLAY_NORMAL);
676 /* Set multiplex ratio value */
677 ret = ssd130x_write_cmd(ssd130x, 2, SSD13XX_SET_MULTIPLEX_RATIO, ssd130x->height - 1);
681 /* Set master configuration */
682 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_MASTER_CONFIG, 0x8e);
687 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_POWER_SAVE_MODE, 0x0b);
691 /* Set Phase 1 and 2 period */
692 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_PHASES_PERIOD, 0x31);
696 /* Set clock divider */
697 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_CLOCK_FREQ, 0xf0);
701 /* Set pre-charge A */
702 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_A, 0x64);
706 /* Set pre-charge B */
707 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_B, 0x78);
711 /* Set pre-charge C */
712 ret = ssd130x_write_cmd(ssd130x, 2, SSD132X_SET_PRECHARGE_C, 0x64);
716 /* Set pre-charge level */
717 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_PRECHARGE_VOLTAGE, 0x3a);
721 /* Set VCOMH voltage */
722 ret = ssd130x_write_cmd(ssd130x, 2, SSD133X_SET_VCOMH_VOLTAGE, 0x3e);
729 static int ssd130x_update_rect(struct ssd130x_device *ssd130x,
730 struct drm_rect *rect, u8 *buf,
733 unsigned int x = rect->x1;
734 unsigned int y = rect->y1;
735 unsigned int width = drm_rect_width(rect);
736 unsigned int height = drm_rect_height(rect);
737 unsigned int line_length = DIV_ROUND_UP(width, 8);
738 unsigned int page_height = SSD130X_PAGE_HEIGHT;
739 unsigned int pages = DIV_ROUND_UP(height, page_height);
740 struct drm_device *drm = &ssd130x->drm;
744 drm_WARN_ONCE(drm, y % page_height != 0, "y must be aligned to screen page\n");
747 * The screen is divided in pages, each having a height of 8
748 * pixels, and the width of the screen. When sending a byte of
749 * data to the controller, it gives the 8 bits for the current
750 * column. I.e, the first byte are the 8 bits of the first
751 * column, then the 8 bits for the second column, etc.
754 * Representation of the screen, assuming it is 5 bits
755 * wide. Each letter-number combination is a bit that controls
767 * If you want to update this screen, you need to send 5 bytes:
768 * (1) A0 B0 C0 D0 E0 F0 G0 H0
769 * (2) A1 B1 C1 D1 E1 F1 G1 H1
770 * (3) A2 B2 C2 D2 E2 F2 G2 H2
771 * (4) A3 B3 C3 D3 E3 F3 G3 H3
772 * (5) A4 B4 C4 D4 E4 F4 G4 H4
775 if (!ssd130x->page_address_mode) {
778 /* Set address range for horizontal addressing mode */
779 ret = ssd130x_set_col_range(ssd130x, ssd130x->col_offset + x, width);
783 page_start = ssd130x->page_offset + y / page_height;
784 ret = ssd130x_set_page_range(ssd130x, page_start, pages);
789 for (i = 0; i < pages; i++) {
792 /* Last page may be partial */
793 if (page_height * (y / page_height + i + 1) > ssd130x->height)
794 m = ssd130x->height % page_height;
796 for (j = 0; j < width; j++) {
799 for (k = 0; k < m; k++) {
800 u32 idx = (page_height * i + k) * line_length + j / 8;
802 u8 bit = (byte >> (j % 8)) & 1;
806 data_array[array_idx++] = data;
810 * In page addressing mode, the start address needs to be reset,
811 * and each page then needs to be written out separately.
813 if (ssd130x->page_address_mode) {
814 ret = ssd130x_set_page_pos(ssd130x,
815 ssd130x->page_offset + i,
816 ssd130x->col_offset + x);
820 ret = ssd130x_write_data(ssd130x, data_array, width);
828 /* Write out update in one go if we aren't using page addressing mode */
829 if (!ssd130x->page_address_mode)
830 ret = ssd130x_write_data(ssd130x, data_array, width * pages);
835 static int ssd132x_update_rect(struct ssd130x_device *ssd130x,
836 struct drm_rect *rect, u8 *buf,
839 unsigned int x = rect->x1;
840 unsigned int y = rect->y1;
841 unsigned int segment_width = SSD132X_SEGMENT_WIDTH;
842 unsigned int width = drm_rect_width(rect);
843 unsigned int height = drm_rect_height(rect);
844 unsigned int columns = DIV_ROUND_UP(width, segment_width);
845 unsigned int rows = height;
846 struct drm_device *drm = &ssd130x->drm;
851 drm_WARN_ONCE(drm, x % segment_width != 0, "x must be aligned to screen segment\n");
854 * The screen is divided in Segment and Common outputs, where
855 * COM0 to COM[N - 1] are the rows and SEG0 to SEG[M - 1] are
858 * Each Segment has a 4-bit pixel and each Common output has a
859 * row of pixels. When using the (default) horizontal address
860 * increment mode, each byte of data sent to the controller has
861 * two Segments (e.g: SEG0 and SEG1) that are stored in the lower
862 * and higher nibbles of a single byte representing one column.
863 * That is, the first byte are SEG0 (D0[3:0]) and SEG1 (D0[7:4]),
864 * the second byte are SEG2 (D1[3:0]) and SEG3 (D1[7:4]) and so on.
867 /* Set column start and end */
868 ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_COL_RANGE, x / segment_width, columns - 1);
872 /* Set row start and end */
873 ret = ssd130x_write_cmd(ssd130x, 3, SSD132X_SET_ROW_RANGE, y, rows - 1);
877 for (i = 0; i < height; i++) {
878 /* Process pair of pixels and combine them into a single byte */
879 for (j = 0; j < width; j += segment_width) {
880 u8 n1 = buf[i * width + j];
881 u8 n2 = buf[i * width + j + 1];
883 data_array[array_idx++] = (n2 << 4) | n1;
887 /* Write out update in one go since horizontal addressing mode is used */
888 ret = ssd130x_write_data(ssd130x, data_array, columns * rows);
893 static int ssd133x_update_rect(struct ssd130x_device *ssd130x,
894 struct drm_rect *rect, u8 *data_array,
897 unsigned int x = rect->x1;
898 unsigned int y = rect->y1;
899 unsigned int columns = drm_rect_width(rect);
900 unsigned int rows = drm_rect_height(rect);
904 * The screen is divided in Segment and Common outputs, where
905 * COM0 to COM[N - 1] are the rows and SEG0 to SEG[M - 1] are
908 * Each Segment has a 8-bit pixel and each Common output has a
909 * row of pixels. When using the (default) horizontal address
910 * increment mode, each byte of data sent to the controller has
911 * a Segment (e.g: SEG0).
913 * When using the 256 color depth format, each pixel contains 3
914 * sub-pixels for color A, B and C. These have 3 bit, 3 bit and
915 * 2 bits respectively.
918 /* Set column start and end */
919 ret = ssd130x_write_cmd(ssd130x, 3, SSD133X_SET_COL_RANGE, x, columns - 1);
923 /* Set row start and end */
924 ret = ssd130x_write_cmd(ssd130x, 3, SSD133X_SET_ROW_RANGE, y, rows - 1);
928 /* Write out update in one go since horizontal addressing mode is used */
929 ret = ssd130x_write_data(ssd130x, data_array, pitch * rows);
934 static void ssd130x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array)
936 unsigned int pages = DIV_ROUND_UP(ssd130x->height, SSD130X_PAGE_HEIGHT);
937 unsigned int width = ssd130x->width;
940 if (!ssd130x->page_address_mode) {
941 memset(data_array, 0, width * pages);
943 /* Set address range for horizontal addressing mode */
944 ret = ssd130x_set_col_range(ssd130x, ssd130x->col_offset, width);
948 ret = ssd130x_set_page_range(ssd130x, ssd130x->page_offset, pages);
952 /* Write out update in one go if we aren't using page addressing mode */
953 ssd130x_write_data(ssd130x, data_array, width * pages);
956 * In page addressing mode, the start address needs to be reset,
957 * and each page then needs to be written out separately.
959 memset(data_array, 0, width);
961 for (i = 0; i < pages; i++) {
962 ret = ssd130x_set_page_pos(ssd130x,
963 ssd130x->page_offset + i,
964 ssd130x->col_offset);
968 ret = ssd130x_write_data(ssd130x, data_array, width);
975 static void ssd132x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array)
977 unsigned int columns = DIV_ROUND_UP(ssd130x->height, SSD132X_SEGMENT_WIDTH);
978 unsigned int height = ssd130x->height;
980 memset(data_array, 0, columns * height);
982 /* Write out update in one go since horizontal addressing mode is used */
983 ssd130x_write_data(ssd130x, data_array, columns * height);
986 static void ssd133x_clear_screen(struct ssd130x_device *ssd130x, u8 *data_array)
988 const struct drm_format_info *fi = drm_format_info(DRM_FORMAT_RGB332);
994 pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
996 memset(data_array, 0, pitch * ssd130x->height);
998 /* Write out update in one go since horizontal addressing mode is used */
999 ssd130x_write_data(ssd130x, data_array, pitch * ssd130x->height);
1002 static int ssd130x_fb_blit_rect(struct drm_framebuffer *fb,
1003 const struct iosys_map *vmap,
1004 struct drm_rect *rect,
1005 u8 *buf, u8 *data_array,
1006 struct drm_format_conv_state *fmtcnv_state)
1008 struct ssd130x_device *ssd130x = drm_to_ssd130x(fb->dev);
1009 struct iosys_map dst;
1010 unsigned int dst_pitch;
1013 /* Align y to display page boundaries */
1014 rect->y1 = round_down(rect->y1, SSD130X_PAGE_HEIGHT);
1015 rect->y2 = min_t(unsigned int, round_up(rect->y2, SSD130X_PAGE_HEIGHT), ssd130x->height);
1017 dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 8);
1019 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
1023 iosys_map_set_vaddr(&dst, buf);
1024 drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
1026 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
1028 ssd130x_update_rect(ssd130x, rect, buf, data_array);
1033 static int ssd132x_fb_blit_rect(struct drm_framebuffer *fb,
1034 const struct iosys_map *vmap,
1035 struct drm_rect *rect, u8 *buf,
1037 struct drm_format_conv_state *fmtcnv_state)
1039 struct ssd130x_device *ssd130x = drm_to_ssd130x(fb->dev);
1040 unsigned int dst_pitch = drm_rect_width(rect);
1041 struct iosys_map dst;
1044 /* Align x to display segment boundaries */
1045 rect->x1 = round_down(rect->x1, SSD132X_SEGMENT_WIDTH);
1046 rect->x2 = min_t(unsigned int, round_up(rect->x2, SSD132X_SEGMENT_WIDTH),
1049 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
1053 iosys_map_set_vaddr(&dst, buf);
1054 drm_fb_xrgb8888_to_gray8(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
1056 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
1058 ssd132x_update_rect(ssd130x, rect, buf, data_array);
1063 static int ssd133x_fb_blit_rect(struct drm_framebuffer *fb,
1064 const struct iosys_map *vmap,
1065 struct drm_rect *rect, u8 *data_array,
1066 struct drm_format_conv_state *fmtcnv_state)
1068 struct ssd130x_device *ssd130x = drm_to_ssd130x(fb->dev);
1069 const struct drm_format_info *fi = drm_format_info(DRM_FORMAT_RGB332);
1070 unsigned int dst_pitch;
1071 struct iosys_map dst;
1077 dst_pitch = drm_format_info_min_pitch(fi, 0, drm_rect_width(rect));
1079 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
1083 iosys_map_set_vaddr(&dst, data_array);
1084 drm_fb_xrgb8888_to_rgb332(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
1086 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
1088 ssd133x_update_rect(ssd130x, rect, data_array, dst_pitch);
1093 static int ssd130x_primary_plane_atomic_check(struct drm_plane *plane,
1094 struct drm_atomic_state *state)
1096 struct drm_device *drm = plane->dev;
1097 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1098 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1099 struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
1100 struct drm_shadow_plane_state *shadow_plane_state = &ssd130x_state->base;
1101 struct drm_crtc *crtc = plane_state->crtc;
1102 struct drm_crtc_state *crtc_state = NULL;
1103 const struct drm_format_info *fi;
1108 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1110 ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
1111 DRM_PLANE_NO_SCALING,
1112 DRM_PLANE_NO_SCALING,
1116 else if (!plane_state->visible)
1119 fi = drm_format_info(DRM_FORMAT_R1);
1123 pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
1125 if (plane_state->fb->format != fi) {
1128 /* format conversion necessary; reserve buffer */
1129 buf = drm_format_conv_state_reserve(&shadow_plane_state->fmtcnv_state,
1135 ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
1136 if (!ssd130x_state->buffer)
1142 static int ssd132x_primary_plane_atomic_check(struct drm_plane *plane,
1143 struct drm_atomic_state *state)
1145 struct drm_device *drm = plane->dev;
1146 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1147 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1148 struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(plane_state);
1149 struct drm_shadow_plane_state *shadow_plane_state = &ssd130x_state->base;
1150 struct drm_crtc *crtc = plane_state->crtc;
1151 struct drm_crtc_state *crtc_state = NULL;
1152 const struct drm_format_info *fi;
1157 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1159 ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
1160 DRM_PLANE_NO_SCALING,
1161 DRM_PLANE_NO_SCALING,
1165 else if (!plane_state->visible)
1168 fi = drm_format_info(DRM_FORMAT_R8);
1172 pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
1174 if (plane_state->fb->format != fi) {
1177 /* format conversion necessary; reserve buffer */
1178 buf = drm_format_conv_state_reserve(&shadow_plane_state->fmtcnv_state,
1184 ssd130x_state->buffer = kcalloc(pitch, ssd130x->height, GFP_KERNEL);
1185 if (!ssd130x_state->buffer)
1191 static int ssd133x_primary_plane_atomic_check(struct drm_plane *plane,
1192 struct drm_atomic_state *state)
1194 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1195 struct drm_crtc *crtc = plane_state->crtc;
1196 struct drm_crtc_state *crtc_state = NULL;
1200 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1202 ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
1203 DRM_PLANE_NO_SCALING,
1204 DRM_PLANE_NO_SCALING,
1208 else if (!plane_state->visible)
1214 static void ssd130x_primary_plane_atomic_update(struct drm_plane *plane,
1215 struct drm_atomic_state *state)
1217 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1218 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
1219 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
1220 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1221 struct ssd130x_crtc_state *ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1222 struct ssd130x_plane_state *ssd130x_plane_state = to_ssd130x_plane_state(plane_state);
1223 struct drm_framebuffer *fb = plane_state->fb;
1224 struct drm_atomic_helper_damage_iter iter;
1225 struct drm_device *drm = plane->dev;
1226 struct drm_rect dst_clip;
1227 struct drm_rect damage;
1230 if (!drm_dev_enter(drm, &idx))
1233 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
1234 drm_atomic_for_each_plane_damage(&iter, &damage) {
1235 dst_clip = plane_state->dst;
1237 if (!drm_rect_intersect(&dst_clip, &damage))
1240 ssd130x_fb_blit_rect(fb, &shadow_plane_state->data[0], &dst_clip,
1241 ssd130x_plane_state->buffer,
1242 ssd130x_crtc_state->data_array,
1243 &shadow_plane_state->fmtcnv_state);
1249 static void ssd132x_primary_plane_atomic_update(struct drm_plane *plane,
1250 struct drm_atomic_state *state)
1252 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1253 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
1254 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
1255 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1256 struct ssd130x_crtc_state *ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1257 struct ssd130x_plane_state *ssd130x_plane_state = to_ssd130x_plane_state(plane_state);
1258 struct drm_framebuffer *fb = plane_state->fb;
1259 struct drm_atomic_helper_damage_iter iter;
1260 struct drm_device *drm = plane->dev;
1261 struct drm_rect dst_clip;
1262 struct drm_rect damage;
1265 if (!drm_dev_enter(drm, &idx))
1268 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
1269 drm_atomic_for_each_plane_damage(&iter, &damage) {
1270 dst_clip = plane_state->dst;
1272 if (!drm_rect_intersect(&dst_clip, &damage))
1275 ssd132x_fb_blit_rect(fb, &shadow_plane_state->data[0], &dst_clip,
1276 ssd130x_plane_state->buffer,
1277 ssd130x_crtc_state->data_array,
1278 &shadow_plane_state->fmtcnv_state);
1284 static void ssd133x_primary_plane_atomic_update(struct drm_plane *plane,
1285 struct drm_atomic_state *state)
1287 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1288 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
1289 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
1290 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1291 struct ssd130x_crtc_state *ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1292 struct drm_framebuffer *fb = plane_state->fb;
1293 struct drm_atomic_helper_damage_iter iter;
1294 struct drm_device *drm = plane->dev;
1295 struct drm_rect dst_clip;
1296 struct drm_rect damage;
1299 if (!drm_dev_enter(drm, &idx))
1302 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
1303 drm_atomic_for_each_plane_damage(&iter, &damage) {
1304 dst_clip = plane_state->dst;
1306 if (!drm_rect_intersect(&dst_clip, &damage))
1309 ssd133x_fb_blit_rect(fb, &shadow_plane_state->data[0], &dst_clip,
1310 ssd130x_crtc_state->data_array,
1311 &shadow_plane_state->fmtcnv_state);
1317 static void ssd130x_primary_plane_atomic_disable(struct drm_plane *plane,
1318 struct drm_atomic_state *state)
1320 struct drm_device *drm = plane->dev;
1321 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1322 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1323 struct drm_crtc_state *crtc_state;
1324 struct ssd130x_crtc_state *ssd130x_crtc_state;
1327 if (!plane_state->crtc)
1330 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1331 ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1333 if (!drm_dev_enter(drm, &idx))
1336 ssd130x_clear_screen(ssd130x, ssd130x_crtc_state->data_array);
1341 static void ssd132x_primary_plane_atomic_disable(struct drm_plane *plane,
1342 struct drm_atomic_state *state)
1344 struct drm_device *drm = plane->dev;
1345 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1346 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1347 struct drm_crtc_state *crtc_state;
1348 struct ssd130x_crtc_state *ssd130x_crtc_state;
1351 if (!plane_state->crtc)
1354 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1355 ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1357 if (!drm_dev_enter(drm, &idx))
1360 ssd132x_clear_screen(ssd130x, ssd130x_crtc_state->data_array);
1365 static void ssd133x_primary_plane_atomic_disable(struct drm_plane *plane,
1366 struct drm_atomic_state *state)
1368 struct drm_device *drm = plane->dev;
1369 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1370 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
1371 struct drm_crtc_state *crtc_state;
1372 struct ssd130x_crtc_state *ssd130x_crtc_state;
1375 if (!plane_state->crtc)
1378 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
1379 ssd130x_crtc_state = to_ssd130x_crtc_state(crtc_state);
1381 if (!drm_dev_enter(drm, &idx))
1384 ssd133x_clear_screen(ssd130x, ssd130x_crtc_state->data_array);
1389 /* Called during init to allocate the plane's atomic state. */
1390 static void ssd130x_primary_plane_reset(struct drm_plane *plane)
1392 struct ssd130x_plane_state *ssd130x_state;
1394 WARN_ON(plane->state);
1396 ssd130x_state = kzalloc(sizeof(*ssd130x_state), GFP_KERNEL);
1400 __drm_gem_reset_shadow_plane(plane, &ssd130x_state->base);
1403 static struct drm_plane_state *ssd130x_primary_plane_duplicate_state(struct drm_plane *plane)
1405 struct drm_shadow_plane_state *new_shadow_plane_state;
1406 struct ssd130x_plane_state *old_ssd130x_state;
1407 struct ssd130x_plane_state *ssd130x_state;
1409 if (WARN_ON(!plane->state))
1412 old_ssd130x_state = to_ssd130x_plane_state(plane->state);
1413 ssd130x_state = kmemdup(old_ssd130x_state, sizeof(*ssd130x_state), GFP_KERNEL);
1417 /* The buffer is not duplicated and is allocated in .atomic_check */
1418 ssd130x_state->buffer = NULL;
1420 new_shadow_plane_state = &ssd130x_state->base;
1422 __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
1424 return &new_shadow_plane_state->base;
1427 static void ssd130x_primary_plane_destroy_state(struct drm_plane *plane,
1428 struct drm_plane_state *state)
1430 struct ssd130x_plane_state *ssd130x_state = to_ssd130x_plane_state(state);
1432 kfree(ssd130x_state->buffer);
1434 __drm_gem_destroy_shadow_plane_state(&ssd130x_state->base);
1436 kfree(ssd130x_state);
1439 static const struct drm_plane_helper_funcs ssd130x_primary_plane_helper_funcs[] = {
1440 [SSD130X_FAMILY] = {
1441 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
1442 .atomic_check = ssd130x_primary_plane_atomic_check,
1443 .atomic_update = ssd130x_primary_plane_atomic_update,
1444 .atomic_disable = ssd130x_primary_plane_atomic_disable,
1446 [SSD132X_FAMILY] = {
1447 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
1448 .atomic_check = ssd132x_primary_plane_atomic_check,
1449 .atomic_update = ssd132x_primary_plane_atomic_update,
1450 .atomic_disable = ssd132x_primary_plane_atomic_disable,
1452 [SSD133X_FAMILY] = {
1453 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
1454 .atomic_check = ssd133x_primary_plane_atomic_check,
1455 .atomic_update = ssd133x_primary_plane_atomic_update,
1456 .atomic_disable = ssd133x_primary_plane_atomic_disable,
1460 static const struct drm_plane_funcs ssd130x_primary_plane_funcs = {
1461 .update_plane = drm_atomic_helper_update_plane,
1462 .disable_plane = drm_atomic_helper_disable_plane,
1463 .reset = ssd130x_primary_plane_reset,
1464 .atomic_duplicate_state = ssd130x_primary_plane_duplicate_state,
1465 .atomic_destroy_state = ssd130x_primary_plane_destroy_state,
1466 .destroy = drm_plane_cleanup,
1469 static enum drm_mode_status ssd130x_crtc_mode_valid(struct drm_crtc *crtc,
1470 const struct drm_display_mode *mode)
1472 struct ssd130x_device *ssd130x = drm_to_ssd130x(crtc->dev);
1474 if (mode->hdisplay != ssd130x->mode.hdisplay &&
1475 mode->vdisplay != ssd130x->mode.vdisplay)
1476 return MODE_ONE_SIZE;
1477 else if (mode->hdisplay != ssd130x->mode.hdisplay)
1478 return MODE_ONE_WIDTH;
1479 else if (mode->vdisplay != ssd130x->mode.vdisplay)
1480 return MODE_ONE_HEIGHT;
1485 static int ssd130x_crtc_atomic_check(struct drm_crtc *crtc,
1486 struct drm_atomic_state *state)
1488 struct drm_device *drm = crtc->dev;
1489 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1490 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1491 struct ssd130x_crtc_state *ssd130x_state = to_ssd130x_crtc_state(crtc_state);
1492 unsigned int pages = DIV_ROUND_UP(ssd130x->height, SSD130X_PAGE_HEIGHT);
1495 ret = drm_crtc_helper_atomic_check(crtc, state);
1499 ssd130x_state->data_array = kmalloc(ssd130x->width * pages, GFP_KERNEL);
1500 if (!ssd130x_state->data_array)
1506 static int ssd132x_crtc_atomic_check(struct drm_crtc *crtc,
1507 struct drm_atomic_state *state)
1509 struct drm_device *drm = crtc->dev;
1510 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1511 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1512 struct ssd130x_crtc_state *ssd130x_state = to_ssd130x_crtc_state(crtc_state);
1513 unsigned int columns = DIV_ROUND_UP(ssd130x->width, SSD132X_SEGMENT_WIDTH);
1516 ret = drm_crtc_helper_atomic_check(crtc, state);
1520 ssd130x_state->data_array = kmalloc(columns * ssd130x->height, GFP_KERNEL);
1521 if (!ssd130x_state->data_array)
1527 static int ssd133x_crtc_atomic_check(struct drm_crtc *crtc,
1528 struct drm_atomic_state *state)
1530 struct drm_device *drm = crtc->dev;
1531 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1532 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1533 struct ssd130x_crtc_state *ssd130x_state = to_ssd130x_crtc_state(crtc_state);
1534 const struct drm_format_info *fi = drm_format_info(DRM_FORMAT_RGB332);
1541 ret = drm_crtc_helper_atomic_check(crtc, state);
1545 pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
1547 ssd130x_state->data_array = kmalloc(pitch * ssd130x->height, GFP_KERNEL);
1548 if (!ssd130x_state->data_array)
1554 /* Called during init to allocate the CRTC's atomic state. */
1555 static void ssd130x_crtc_reset(struct drm_crtc *crtc)
1557 struct ssd130x_crtc_state *ssd130x_state;
1559 WARN_ON(crtc->state);
1561 ssd130x_state = kzalloc(sizeof(*ssd130x_state), GFP_KERNEL);
1565 __drm_atomic_helper_crtc_reset(crtc, &ssd130x_state->base);
1568 static struct drm_crtc_state *ssd130x_crtc_duplicate_state(struct drm_crtc *crtc)
1570 struct ssd130x_crtc_state *old_ssd130x_state;
1571 struct ssd130x_crtc_state *ssd130x_state;
1573 if (WARN_ON(!crtc->state))
1576 old_ssd130x_state = to_ssd130x_crtc_state(crtc->state);
1577 ssd130x_state = kmemdup(old_ssd130x_state, sizeof(*ssd130x_state), GFP_KERNEL);
1581 /* The buffer is not duplicated and is allocated in .atomic_check */
1582 ssd130x_state->data_array = NULL;
1584 __drm_atomic_helper_crtc_duplicate_state(crtc, &ssd130x_state->base);
1586 return &ssd130x_state->base;
1589 static void ssd130x_crtc_destroy_state(struct drm_crtc *crtc,
1590 struct drm_crtc_state *state)
1592 struct ssd130x_crtc_state *ssd130x_state = to_ssd130x_crtc_state(state);
1594 kfree(ssd130x_state->data_array);
1596 __drm_atomic_helper_crtc_destroy_state(state);
1598 kfree(ssd130x_state);
1602 * The CRTC is always enabled. Screen updates are performed by
1603 * the primary plane's atomic_update function. Disabling clears
1604 * the screen in the primary plane's atomic_disable function.
1606 static const struct drm_crtc_helper_funcs ssd130x_crtc_helper_funcs[] = {
1607 [SSD130X_FAMILY] = {
1608 .mode_valid = ssd130x_crtc_mode_valid,
1609 .atomic_check = ssd130x_crtc_atomic_check,
1611 [SSD132X_FAMILY] = {
1612 .mode_valid = ssd130x_crtc_mode_valid,
1613 .atomic_check = ssd132x_crtc_atomic_check,
1615 [SSD133X_FAMILY] = {
1616 .mode_valid = ssd130x_crtc_mode_valid,
1617 .atomic_check = ssd133x_crtc_atomic_check,
1621 static const struct drm_crtc_funcs ssd130x_crtc_funcs = {
1622 .reset = ssd130x_crtc_reset,
1623 .destroy = drm_crtc_cleanup,
1624 .set_config = drm_atomic_helper_set_config,
1625 .page_flip = drm_atomic_helper_page_flip,
1626 .atomic_duplicate_state = ssd130x_crtc_duplicate_state,
1627 .atomic_destroy_state = ssd130x_crtc_destroy_state,
1630 static void ssd130x_encoder_atomic_enable(struct drm_encoder *encoder,
1631 struct drm_atomic_state *state)
1633 struct drm_device *drm = encoder->dev;
1634 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1637 ret = ssd130x_power_on(ssd130x);
1641 ret = ssd130x_init(ssd130x);
1645 ssd130x_write_cmd(ssd130x, 1, SSD13XX_DISPLAY_ON);
1647 backlight_enable(ssd130x->bl_dev);
1652 ssd130x_power_off(ssd130x);
1656 static void ssd132x_encoder_atomic_enable(struct drm_encoder *encoder,
1657 struct drm_atomic_state *state)
1659 struct drm_device *drm = encoder->dev;
1660 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1663 ret = ssd130x_power_on(ssd130x);
1667 ret = ssd132x_init(ssd130x);
1671 ssd130x_write_cmd(ssd130x, 1, SSD13XX_DISPLAY_ON);
1673 backlight_enable(ssd130x->bl_dev);
1678 ssd130x_power_off(ssd130x);
1681 static void ssd133x_encoder_atomic_enable(struct drm_encoder *encoder,
1682 struct drm_atomic_state *state)
1684 struct drm_device *drm = encoder->dev;
1685 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1688 ret = ssd130x_power_on(ssd130x);
1692 ret = ssd133x_init(ssd130x);
1696 ssd130x_write_cmd(ssd130x, 1, SSD13XX_DISPLAY_ON);
1698 backlight_enable(ssd130x->bl_dev);
1703 ssd130x_power_off(ssd130x);
1706 static void ssd130x_encoder_atomic_disable(struct drm_encoder *encoder,
1707 struct drm_atomic_state *state)
1709 struct drm_device *drm = encoder->dev;
1710 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm);
1712 backlight_disable(ssd130x->bl_dev);
1714 ssd130x_write_cmd(ssd130x, 1, SSD13XX_DISPLAY_OFF);
1716 ssd130x_power_off(ssd130x);
1719 static const struct drm_encoder_helper_funcs ssd130x_encoder_helper_funcs[] = {
1720 [SSD130X_FAMILY] = {
1721 .atomic_enable = ssd130x_encoder_atomic_enable,
1722 .atomic_disable = ssd130x_encoder_atomic_disable,
1724 [SSD132X_FAMILY] = {
1725 .atomic_enable = ssd132x_encoder_atomic_enable,
1726 .atomic_disable = ssd130x_encoder_atomic_disable,
1728 [SSD133X_FAMILY] = {
1729 .atomic_enable = ssd133x_encoder_atomic_enable,
1730 .atomic_disable = ssd130x_encoder_atomic_disable,
1734 static const struct drm_encoder_funcs ssd130x_encoder_funcs = {
1735 .destroy = drm_encoder_cleanup,
1738 static int ssd130x_connector_get_modes(struct drm_connector *connector)
1740 struct ssd130x_device *ssd130x = drm_to_ssd130x(connector->dev);
1741 struct drm_display_mode *mode;
1742 struct device *dev = ssd130x->dev;
1744 mode = drm_mode_duplicate(connector->dev, &ssd130x->mode);
1746 dev_err(dev, "Failed to duplicated mode\n");
1750 drm_mode_probed_add(connector, mode);
1751 drm_set_preferred_mode(connector, mode->hdisplay, mode->vdisplay);
1753 /* There is only a single mode */
1757 static const struct drm_connector_helper_funcs ssd130x_connector_helper_funcs = {
1758 .get_modes = ssd130x_connector_get_modes,
1761 static const struct drm_connector_funcs ssd130x_connector_funcs = {
1762 .reset = drm_atomic_helper_connector_reset,
1763 .fill_modes = drm_helper_probe_single_connector_modes,
1764 .destroy = drm_connector_cleanup,
1765 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1766 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1769 static const struct drm_mode_config_funcs ssd130x_mode_config_funcs = {
1770 .fb_create = drm_gem_fb_create_with_dirty,
1771 .atomic_check = drm_atomic_helper_check,
1772 .atomic_commit = drm_atomic_helper_commit,
1775 static const uint32_t ssd130x_formats[] = {
1776 DRM_FORMAT_XRGB8888,
1779 DEFINE_DRM_GEM_FOPS(ssd130x_fops);
1781 static const struct drm_driver ssd130x_drm_driver = {
1782 DRM_GEM_SHMEM_DRIVER_OPS,
1783 .name = DRIVER_NAME,
1784 .desc = DRIVER_DESC,
1785 .date = DRIVER_DATE,
1786 .major = DRIVER_MAJOR,
1787 .minor = DRIVER_MINOR,
1788 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
1789 .fops = &ssd130x_fops,
1792 static int ssd130x_update_bl(struct backlight_device *bdev)
1794 struct ssd130x_device *ssd130x = bl_get_data(bdev);
1795 int brightness = backlight_get_brightness(bdev);
1798 ssd130x->contrast = brightness;
1800 ret = ssd130x_write_cmd(ssd130x, 1, SSD13XX_CONTRAST);
1804 ret = ssd130x_write_cmd(ssd130x, 1, ssd130x->contrast);
1811 static const struct backlight_ops ssd130xfb_bl_ops = {
1812 .update_status = ssd130x_update_bl,
1815 static void ssd130x_parse_properties(struct ssd130x_device *ssd130x)
1817 struct device *dev = ssd130x->dev;
1819 if (device_property_read_u32(dev, "solomon,width", &ssd130x->width))
1820 ssd130x->width = ssd130x->device_info->default_width;
1822 if (device_property_read_u32(dev, "solomon,height", &ssd130x->height))
1823 ssd130x->height = ssd130x->device_info->default_height;
1825 if (device_property_read_u32(dev, "solomon,page-offset", &ssd130x->page_offset))
1826 ssd130x->page_offset = 1;
1828 if (device_property_read_u32(dev, "solomon,col-offset", &ssd130x->col_offset))
1829 ssd130x->col_offset = 0;
1831 if (device_property_read_u32(dev, "solomon,com-offset", &ssd130x->com_offset))
1832 ssd130x->com_offset = 0;
1834 if (device_property_read_u32(dev, "solomon,prechargep1", &ssd130x->prechargep1))
1835 ssd130x->prechargep1 = 2;
1837 if (device_property_read_u32(dev, "solomon,prechargep2", &ssd130x->prechargep2))
1838 ssd130x->prechargep2 = 2;
1840 if (!device_property_read_u8_array(dev, "solomon,lookup-table",
1841 ssd130x->lookup_table,
1842 ARRAY_SIZE(ssd130x->lookup_table)))
1843 ssd130x->lookup_table_set = 1;
1845 ssd130x->seg_remap = !device_property_read_bool(dev, "solomon,segment-no-remap");
1846 ssd130x->com_seq = device_property_read_bool(dev, "solomon,com-seq");
1847 ssd130x->com_lrremap = device_property_read_bool(dev, "solomon,com-lrremap");
1848 ssd130x->com_invdir = device_property_read_bool(dev, "solomon,com-invdir");
1849 ssd130x->area_color_enable =
1850 device_property_read_bool(dev, "solomon,area-color-enable");
1851 ssd130x->low_power = device_property_read_bool(dev, "solomon,low-power");
1853 ssd130x->contrast = 127;
1854 ssd130x->vcomh = ssd130x->device_info->default_vcomh;
1856 /* Setup display timing */
1857 if (device_property_read_u32(dev, "solomon,dclk-div", &ssd130x->dclk_div))
1858 ssd130x->dclk_div = ssd130x->device_info->default_dclk_div;
1859 if (device_property_read_u32(dev, "solomon,dclk-frq", &ssd130x->dclk_frq))
1860 ssd130x->dclk_frq = ssd130x->device_info->default_dclk_frq;
1863 static int ssd130x_init_modeset(struct ssd130x_device *ssd130x)
1865 enum ssd130x_family_ids family_id = ssd130x->device_info->family_id;
1866 struct drm_display_mode *mode = &ssd130x->mode;
1867 struct device *dev = ssd130x->dev;
1868 struct drm_device *drm = &ssd130x->drm;
1869 unsigned long max_width, max_height;
1870 struct drm_plane *primary_plane;
1871 struct drm_crtc *crtc;
1872 struct drm_encoder *encoder;
1873 struct drm_connector *connector;
1880 ret = drmm_mode_config_init(drm);
1882 dev_err(dev, "DRM mode config init failed: %d\n", ret);
1886 mode->type = DRM_MODE_TYPE_DRIVER;
1888 mode->hdisplay = mode->htotal = ssd130x->width;
1889 mode->hsync_start = mode->hsync_end = ssd130x->width;
1890 mode->vdisplay = mode->vtotal = ssd130x->height;
1891 mode->vsync_start = mode->vsync_end = ssd130x->height;
1892 mode->width_mm = 27;
1893 mode->height_mm = 27;
1895 max_width = max_t(unsigned long, mode->hdisplay, DRM_SHADOW_PLANE_MAX_WIDTH);
1896 max_height = max_t(unsigned long, mode->vdisplay, DRM_SHADOW_PLANE_MAX_HEIGHT);
1898 drm->mode_config.min_width = mode->hdisplay;
1899 drm->mode_config.max_width = max_width;
1900 drm->mode_config.min_height = mode->vdisplay;
1901 drm->mode_config.max_height = max_height;
1902 drm->mode_config.preferred_depth = 24;
1903 drm->mode_config.funcs = &ssd130x_mode_config_funcs;
1907 primary_plane = &ssd130x->primary_plane;
1908 ret = drm_universal_plane_init(drm, primary_plane, 0, &ssd130x_primary_plane_funcs,
1909 ssd130x_formats, ARRAY_SIZE(ssd130x_formats),
1910 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
1912 dev_err(dev, "DRM primary plane init failed: %d\n", ret);
1916 drm_plane_helper_add(primary_plane, &ssd130x_primary_plane_helper_funcs[family_id]);
1918 drm_plane_enable_fb_damage_clips(primary_plane);
1922 crtc = &ssd130x->crtc;
1923 ret = drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
1924 &ssd130x_crtc_funcs, NULL);
1926 dev_err(dev, "DRM crtc init failed: %d\n", ret);
1930 drm_crtc_helper_add(crtc, &ssd130x_crtc_helper_funcs[family_id]);
1934 encoder = &ssd130x->encoder;
1935 ret = drm_encoder_init(drm, encoder, &ssd130x_encoder_funcs,
1936 DRM_MODE_ENCODER_NONE, NULL);
1938 dev_err(dev, "DRM encoder init failed: %d\n", ret);
1942 drm_encoder_helper_add(encoder, &ssd130x_encoder_helper_funcs[family_id]);
1944 encoder->possible_crtcs = drm_crtc_mask(crtc);
1948 connector = &ssd130x->connector;
1949 ret = drm_connector_init(drm, connector, &ssd130x_connector_funcs,
1950 DRM_MODE_CONNECTOR_Unknown);
1952 dev_err(dev, "DRM connector init failed: %d\n", ret);
1956 drm_connector_helper_add(connector, &ssd130x_connector_helper_funcs);
1958 ret = drm_connector_attach_encoder(connector, encoder);
1960 dev_err(dev, "DRM attach connector to encoder failed: %d\n", ret);
1964 drm_mode_config_reset(drm);
1969 static int ssd130x_get_resources(struct ssd130x_device *ssd130x)
1971 struct device *dev = ssd130x->dev;
1973 ssd130x->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
1974 if (IS_ERR(ssd130x->reset))
1975 return dev_err_probe(dev, PTR_ERR(ssd130x->reset),
1976 "Failed to get reset gpio\n");
1978 ssd130x->vcc_reg = devm_regulator_get(dev, "vcc");
1979 if (IS_ERR(ssd130x->vcc_reg))
1980 return dev_err_probe(dev, PTR_ERR(ssd130x->vcc_reg),
1981 "Failed to get VCC regulator\n");
1986 struct ssd130x_device *ssd130x_probe(struct device *dev, struct regmap *regmap)
1988 struct ssd130x_device *ssd130x;
1989 struct backlight_device *bl;
1990 struct drm_device *drm;
1993 ssd130x = devm_drm_dev_alloc(dev, &ssd130x_drm_driver,
1994 struct ssd130x_device, drm);
1995 if (IS_ERR(ssd130x))
1996 return ERR_PTR(dev_err_probe(dev, PTR_ERR(ssd130x),
1997 "Failed to allocate DRM device\n"));
1999 drm = &ssd130x->drm;
2002 ssd130x->regmap = regmap;
2003 ssd130x->device_info = device_get_match_data(dev);
2005 if (ssd130x->device_info->page_mode_only)
2006 ssd130x->page_address_mode = 1;
2008 ssd130x_parse_properties(ssd130x);
2010 ret = ssd130x_get_resources(ssd130x);
2012 return ERR_PTR(ret);
2014 bl = devm_backlight_device_register(dev, dev_name(dev), dev, ssd130x,
2015 &ssd130xfb_bl_ops, NULL);
2017 return ERR_PTR(dev_err_probe(dev, PTR_ERR(bl),
2018 "Unable to register backlight device\n"));
2020 bl->props.brightness = ssd130x->contrast;
2021 bl->props.max_brightness = MAX_CONTRAST;
2022 ssd130x->bl_dev = bl;
2024 ret = ssd130x_init_modeset(ssd130x);
2026 return ERR_PTR(ret);
2028 ret = drm_dev_register(drm, 0);
2030 return ERR_PTR(dev_err_probe(dev, ret, "DRM device register failed\n"));
2032 drm_fbdev_generic_setup(drm, 32);
2036 EXPORT_SYMBOL_GPL(ssd130x_probe);
2038 void ssd130x_remove(struct ssd130x_device *ssd130x)
2040 drm_dev_unplug(&ssd130x->drm);
2041 drm_atomic_helper_shutdown(&ssd130x->drm);
2043 EXPORT_SYMBOL_GPL(ssd130x_remove);
2045 void ssd130x_shutdown(struct ssd130x_device *ssd130x)
2047 drm_atomic_helper_shutdown(&ssd130x->drm);
2049 EXPORT_SYMBOL_GPL(ssd130x_shutdown);
2051 MODULE_DESCRIPTION(DRIVER_DESC);
2052 MODULE_AUTHOR("Javier Martinez Canillas <javierm@redhat.com>");
2053 MODULE_LICENSE("GPL v2");