xref: /linux/drivers/gpu/drm/solomon/ssd130x-spi.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * DRM driver for Solomon SSD13xx OLED displays (SPI bus)
4  *
5  * Copyright 2022 Red Hat Inc.
6  * Authors: Javier Martinez Canillas <javierm@redhat.com>
7  */
8 #include <linux/spi/spi.h>
9 #include <linux/module.h>
10 
11 #include "ssd130x.h"
12 
13 #define DRIVER_NAME	"ssd130x-spi"
14 #define DRIVER_DESC	"DRM driver for Solomon SSD13xx OLED displays (SPI)"
15 
16 struct ssd130x_spi_transport {
17 	struct spi_device *spi;
18 	struct gpio_desc *dc;
19 };
20 
21 /*
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.
26  *
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).
30  */
31 static int ssd130x_spi_write(void *context, const void *data, size_t count)
32 {
33 	struct ssd130x_spi_transport *t = context;
34 	struct spi_device *spi = t->spi;
35 	const u8 *reg = data;
36 
37 	if (*reg == SSD13XX_COMMAND)
38 		gpiod_set_value_cansleep(t->dc, 0);
39 
40 	if (*reg == SSD13XX_DATA)
41 		gpiod_set_value_cansleep(t->dc, 1);
42 
43 	/* Remove control byte since is not used in a 4-wire SPI interface */
44 	return spi_write(spi, reg + 1, count - 1);
45 }
46 
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)
50 {
51 	return -EOPNOTSUPP;
52 }
53 
54 static const struct regmap_config ssd130x_spi_regmap_config = {
55 	.reg_bits = 8,
56 	.val_bits = 8,
57 	.write = ssd130x_spi_write,
58 	.read = ssd130x_spi_read,
59 	.can_multi_write = true,
60 };
61 
62 static int ssd130x_spi_probe(struct spi_device *spi)
63 {
64 	struct ssd130x_spi_transport *t;
65 	struct ssd130x_device *ssd130x;
66 	struct regmap *regmap;
67 	struct gpio_desc *dc;
68 	struct device *dev = &spi->dev;
69 
70 	dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
71 	if (IS_ERR(dc))
72 		return dev_err_probe(dev, PTR_ERR(dc),
73 				     "Failed to get dc gpio\n");
74 
75 	t = devm_kzalloc(dev, sizeof(*t), GFP_KERNEL);
76 	if (!t)
77 		return -ENOMEM;
78 
79 	t->spi = spi;
80 	t->dc = dc;
81 
82 	regmap = devm_regmap_init(dev, NULL, t, &ssd130x_spi_regmap_config);
83 	if (IS_ERR(regmap))
84 		return PTR_ERR(regmap);
85 
86 	ssd130x = ssd130x_probe(dev, regmap);
87 	if (IS_ERR(ssd130x))
88 		return PTR_ERR(ssd130x);
89 
90 	spi_set_drvdata(spi, ssd130x);
91 
92 	return 0;
93 }
94 
95 static void ssd130x_spi_remove(struct spi_device *spi)
96 {
97 	struct ssd130x_device *ssd130x = spi_get_drvdata(spi);
98 
99 	ssd130x_remove(ssd130x);
100 }
101 
102 static void ssd130x_spi_shutdown(struct spi_device *spi)
103 {
104 	struct ssd130x_device *ssd130x = spi_get_drvdata(spi);
105 
106 	ssd130x_shutdown(ssd130x);
107 }
108 
109 static const struct of_device_id ssd130x_of_match[] = {
110 	/* ssd130x family */
111 	{
112 		.compatible = "sinowealth,sh1106",
113 		.data = &ssd130x_variants[SH1106_ID],
114 	},
115 	{
116 		.compatible = "solomon,ssd1305",
117 		.data = &ssd130x_variants[SSD1305_ID],
118 	},
119 	{
120 		.compatible = "solomon,ssd1306",
121 		.data = &ssd130x_variants[SSD1306_ID],
122 	},
123 	{
124 		.compatible = "solomon,ssd1307",
125 		.data = &ssd130x_variants[SSD1307_ID],
126 	},
127 	{
128 		.compatible = "solomon,ssd1309",
129 		.data = &ssd130x_variants[SSD1309_ID],
130 	},
131 	/* ssd132x family */
132 	{
133 		.compatible = "solomon,ssd1322",
134 		.data = &ssd130x_variants[SSD1322_ID],
135 	},
136 	{
137 		.compatible = "solomon,ssd1325",
138 		.data = &ssd130x_variants[SSD1325_ID],
139 	},
140 	{
141 		.compatible = "solomon,ssd1327",
142 		.data = &ssd130x_variants[SSD1327_ID],
143 	},
144 	/* ssd133x family */
145 	{
146 		.compatible = "solomon,ssd1331",
147 		.data = &ssd130x_variants[SSD1331_ID],
148 	},
149 	{ /* sentinel */ }
150 };
151 MODULE_DEVICE_TABLE(of, ssd130x_of_match);
152 
153 /*
154  * The SPI core always reports a MODALIAS uevent of the form "spi:<dev>", even
155  * if the device was registered via OF. This means that the module will not be
156  * auto loaded, unless it contains an alias that matches the MODALIAS reported.
157  *
158  * To workaround this issue, add a SPI device ID table. Even when this should
159  * not be needed for this driver to match the registered SPI devices.
160  */
161 static const struct spi_device_id ssd130x_spi_id[] = {
162 	/* ssd130x family */
163 	{ "sh1106",  SH1106_ID },
164 	{ "ssd1305", SSD1305_ID },
165 	{ "ssd1306", SSD1306_ID },
166 	{ "ssd1307", SSD1307_ID },
167 	{ "ssd1309", SSD1309_ID },
168 	/* ssd132x family */
169 	{ "ssd1322", SSD1322_ID },
170 	{ "ssd1325", SSD1325_ID },
171 	{ "ssd1327", SSD1327_ID },
172 	/* ssd133x family */
173 	{ "ssd1331", SSD1331_ID },
174 	{ /* sentinel */ }
175 };
176 MODULE_DEVICE_TABLE(spi, ssd130x_spi_id);
177 
178 static struct spi_driver ssd130x_spi_driver = {
179 	.driver = {
180 		.name = DRIVER_NAME,
181 		.of_match_table = ssd130x_of_match,
182 	},
183 	.id_table = ssd130x_spi_id,
184 	.probe = ssd130x_spi_probe,
185 	.remove = ssd130x_spi_remove,
186 	.shutdown = ssd130x_spi_shutdown,
187 };
188 module_spi_driver(ssd130x_spi_driver);
189 
190 MODULE_DESCRIPTION(DRIVER_DESC);
191 MODULE_AUTHOR("Javier Martinez Canillas <javierm@redhat.com>");
192 MODULE_LICENSE("GPL");
193 MODULE_IMPORT_NS("DRM_SSD130X");
194