1 // SPDX-License-Identifier: GPL-2.0-only
3 * DRM driver for Solomon SSD130X OLED displays (SPI bus)
5 * Copyright 2022 Red Hat Inc.
6 * Authors: Javier Martinez Canillas <javierm@redhat.com>
8 #include <linux/spi/spi.h>
9 #include <linux/module.h>
13 #define DRIVER_NAME "ssd130x-spi"
14 #define DRIVER_DESC "DRM driver for Solomon SSD130X OLED displays (SPI)"
16 struct ssd130x_spi_transport {
17 struct spi_device *spi;
22 * The regmap bus .write handler, it is just a wrapper around spi_write()
23 * but toggling the Data/Command control pin (D/C#). Since for 4-wire SPI
24 * a D/C# pin is used, in contrast with I2C where a control byte is sent,
25 * prior to every data byte, that contains a bit with the D/C# value.
27 * These control bytes are considered registers by the ssd130x core driver
28 * and can be used by the ssd130x SPI driver to determine if the data sent
29 * is for a command register or for the Graphic Display Data RAM (GDDRAM).
31 static int ssd130x_spi_write(void *context, const void *data, size_t count)
33 struct ssd130x_spi_transport *t = context;
34 struct spi_device *spi = t->spi;
37 if (*reg == SSD13XX_COMMAND)
38 gpiod_set_value_cansleep(t->dc, 0);
40 if (*reg == SSD13XX_DATA)
41 gpiod_set_value_cansleep(t->dc, 1);
43 /* Remove control byte since is not used in a 4-wire SPI interface */
44 return spi_write(spi, reg + 1, count - 1);
47 /* The ssd130x driver does not read registers but regmap expects a .read */
48 static int ssd130x_spi_read(void *context, const void *reg, size_t reg_size,
49 void *val, size_t val_size)
54 static const struct regmap_config ssd130x_spi_regmap_config = {
57 .write = ssd130x_spi_write,
58 .read = ssd130x_spi_read,
59 .can_multi_write = true,
62 static int ssd130x_spi_probe(struct spi_device *spi)
64 struct ssd130x_spi_transport *t;
65 struct ssd130x_device *ssd130x;
66 struct regmap *regmap;
68 struct device *dev = &spi->dev;
70 dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
72 return dev_err_probe(dev, PTR_ERR(dc),
73 "Failed to get dc gpio\n");
75 t = devm_kzalloc(dev, sizeof(*t), GFP_KERNEL);
77 return dev_err_probe(dev, -ENOMEM,
78 "Failed to allocate SPI transport data\n");
83 regmap = devm_regmap_init(dev, NULL, t, &ssd130x_spi_regmap_config);
85 return PTR_ERR(regmap);
87 ssd130x = ssd130x_probe(dev, regmap);
89 return PTR_ERR(ssd130x);
91 spi_set_drvdata(spi, ssd130x);
96 static void ssd130x_spi_remove(struct spi_device *spi)
98 struct ssd130x_device *ssd130x = spi_get_drvdata(spi);
100 ssd130x_remove(ssd130x);
103 static void ssd130x_spi_shutdown(struct spi_device *spi)
105 struct ssd130x_device *ssd130x = spi_get_drvdata(spi);
107 ssd130x_shutdown(ssd130x);
110 static const struct of_device_id ssd130x_of_match[] = {
113 .compatible = "sinowealth,sh1106",
114 .data = &ssd130x_variants[SH1106_ID],
117 .compatible = "solomon,ssd1305",
118 .data = &ssd130x_variants[SSD1305_ID],
121 .compatible = "solomon,ssd1306",
122 .data = &ssd130x_variants[SSD1306_ID],
125 .compatible = "solomon,ssd1307",
126 .data = &ssd130x_variants[SSD1307_ID],
129 .compatible = "solomon,ssd1309",
130 .data = &ssd130x_variants[SSD1309_ID],
134 MODULE_DEVICE_TABLE(of, ssd130x_of_match);
136 #if IS_MODULE(CONFIG_DRM_SSD130X_SPI)
138 * The SPI core always reports a MODALIAS uevent of the form "spi:<dev>", even
139 * if the device was registered via OF. This means that the module will not be
140 * auto loaded, unless it contains an alias that matches the MODALIAS reported.
142 * To workaround this issue, add a SPI device ID table. Even when this should
143 * not be needed for this driver to match the registered SPI devices.
145 static const struct spi_device_id ssd130x_spi_table[] = {
147 { "sh1106", SH1106_ID },
148 { "ssd1305", SSD1305_ID },
149 { "ssd1306", SSD1306_ID },
150 { "ssd1307", SSD1307_ID },
151 { "ssd1309", SSD1309_ID },
154 MODULE_DEVICE_TABLE(spi, ssd130x_spi_table);
157 static struct spi_driver ssd130x_spi_driver = {
160 .of_match_table = ssd130x_of_match,
162 .probe = ssd130x_spi_probe,
163 .remove = ssd130x_spi_remove,
164 .shutdown = ssd130x_spi_shutdown,
166 module_spi_driver(ssd130x_spi_driver);
168 MODULE_DESCRIPTION(DRIVER_DESC);
169 MODULE_AUTHOR("Javier Martinez Canillas <javierm@redhat.com>");
170 MODULE_LICENSE("GPL");
171 MODULE_IMPORT_NS(DRM_SSD130X);