1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * LCD-OLinuXino support for panel driver
4  *
5  * Copyright (C) 2018 Olimex Ltd.
6  *   Author: Stefan Mavrodiev <stefan@olimex.com>
7  */
8 
9 #include <linux/backlight.h>
10 #include <linux/crc32.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/of.h>
16 #include <linux/regulator/consumer.h>
17 
18 #include <video/videomode.h>
19 #include <video/display_timing.h>
20 
21 #include <drm/drm_device.h>
22 #include <drm/drm_modes.h>
23 #include <drm/drm_panel.h>
24 
25 #define LCD_OLINUXINO_HEADER_MAGIC	0x4F4CB727
26 #define LCD_OLINUXINO_DATA_LEN		256
27 
28 struct lcd_olinuxino_mode {
29 	u32 pixelclock;
30 	u32 hactive;
31 	u32 hfp;
32 	u32 hbp;
33 	u32 hpw;
34 	u32 vactive;
35 	u32 vfp;
36 	u32 vbp;
37 	u32 vpw;
38 	u32 refresh;
39 	u32 flags;
40 };
41 
42 struct lcd_olinuxino_info {
43 	char name[32];
44 	u32 width_mm;
45 	u32 height_mm;
46 	u32 bpc;
47 	u32 bus_format;
48 	u32 bus_flag;
49 } __attribute__((__packed__));
50 
51 struct lcd_olinuxino_eeprom {
52 	u32 header;
53 	u32 id;
54 	char revision[4];
55 	u32 serial;
56 	struct lcd_olinuxino_info info;
57 	u32 num_modes;
58 	u8 reserved[180];
59 	u32 checksum;
60 } __attribute__((__packed__));
61 
62 struct lcd_olinuxino {
63 	struct drm_panel panel;
64 	struct device *dev;
65 	struct i2c_client *client;
66 	struct mutex mutex;
67 
68 	bool prepared;
69 	bool enabled;
70 
71 	struct backlight_device *backlight;
72 	struct regulator *supply;
73 	struct gpio_desc *enable_gpio;
74 
75 	struct lcd_olinuxino_eeprom eeprom;
76 };
77 
78 static inline struct lcd_olinuxino *to_lcd_olinuxino(struct drm_panel *panel)
79 {
80 	return container_of(panel, struct lcd_olinuxino, panel);
81 }
82 
83 static int lcd_olinuxino_disable(struct drm_panel *panel)
84 {
85 	struct lcd_olinuxino *lcd = to_lcd_olinuxino(panel);
86 
87 	if (!lcd->enabled)
88 		return 0;
89 
90 	backlight_disable(lcd->backlight);
91 
92 	lcd->enabled = false;
93 
94 	return 0;
95 }
96 
97 static int lcd_olinuxino_unprepare(struct drm_panel *panel)
98 {
99 	struct lcd_olinuxino *lcd = to_lcd_olinuxino(panel);
100 
101 	if (!lcd->prepared)
102 		return 0;
103 
104 	gpiod_set_value_cansleep(lcd->enable_gpio, 0);
105 	regulator_disable(lcd->supply);
106 
107 	lcd->prepared = false;
108 
109 	return 0;
110 }
111 
112 static int lcd_olinuxino_prepare(struct drm_panel *panel)
113 {
114 	struct lcd_olinuxino *lcd = to_lcd_olinuxino(panel);
115 	int ret;
116 
117 	if (lcd->prepared)
118 		return 0;
119 
120 	ret = regulator_enable(lcd->supply);
121 	if (ret < 0)
122 		return ret;
123 
124 	gpiod_set_value_cansleep(lcd->enable_gpio, 1);
125 	lcd->prepared = true;
126 
127 	return 0;
128 }
129 
130 static int lcd_olinuxino_enable(struct drm_panel *panel)
131 {
132 	struct lcd_olinuxino *lcd = to_lcd_olinuxino(panel);
133 
134 	if (lcd->enabled)
135 		return 0;
136 
137 	backlight_enable(lcd->backlight);
138 
139 	lcd->enabled = true;
140 
141 	return 0;
142 }
143 
144 static int lcd_olinuxino_get_modes(struct drm_panel *panel)
145 {
146 	struct lcd_olinuxino *lcd = to_lcd_olinuxino(panel);
147 	struct drm_connector *connector = lcd->panel.connector;
148 	struct lcd_olinuxino_info *lcd_info = &lcd->eeprom.info;
149 	struct drm_device *drm = lcd->panel.drm;
150 	struct lcd_olinuxino_mode *lcd_mode;
151 	struct drm_display_mode *mode;
152 	u32 i, num = 0;
153 
154 	for (i = 0; i < lcd->eeprom.num_modes; i++) {
155 		lcd_mode = (struct lcd_olinuxino_mode *)
156 			   &lcd->eeprom.reserved[i * sizeof(*lcd_mode)];
157 
158 		mode = drm_mode_create(drm);
159 		if (!mode) {
160 			dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
161 				lcd_mode->hactive,
162 				lcd_mode->vactive,
163 				lcd_mode->refresh);
164 				continue;
165 		}
166 
167 		mode->clock = lcd_mode->pixelclock;
168 		mode->hdisplay = lcd_mode->hactive;
169 		mode->hsync_start = lcd_mode->hactive + lcd_mode->hfp;
170 		mode->hsync_end = lcd_mode->hactive + lcd_mode->hfp +
171 				  lcd_mode->hpw;
172 		mode->htotal = lcd_mode->hactive + lcd_mode->hfp +
173 			       lcd_mode->hpw + lcd_mode->hbp;
174 		mode->vdisplay = lcd_mode->vactive;
175 		mode->vsync_start = lcd_mode->vactive + lcd_mode->vfp;
176 		mode->vsync_end = lcd_mode->vactive + lcd_mode->vfp +
177 				  lcd_mode->vpw;
178 		mode->vtotal = lcd_mode->vactive + lcd_mode->vfp +
179 			       lcd_mode->vpw + lcd_mode->vbp;
180 		mode->vrefresh = lcd_mode->refresh;
181 
182 		/* Always make the first mode preferred */
183 		if (i == 0)
184 			mode->type |= DRM_MODE_TYPE_PREFERRED;
185 		mode->type |= DRM_MODE_TYPE_DRIVER;
186 
187 		drm_mode_set_name(mode);
188 		drm_mode_probed_add(connector, mode);
189 
190 		num++;
191 	}
192 
193 	connector->display_info.width_mm = lcd_info->width_mm;
194 	connector->display_info.height_mm = lcd_info->height_mm;
195 	connector->display_info.bpc = lcd_info->bpc;
196 
197 	if (lcd_info->bus_format)
198 		drm_display_info_set_bus_formats(&connector->display_info,
199 						 &lcd_info->bus_format, 1);
200 	connector->display_info.bus_flags = lcd_info->bus_flag;
201 
202 	return num;
203 }
204 
205 static const struct drm_panel_funcs lcd_olinuxino_funcs = {
206 	.disable = lcd_olinuxino_disable,
207 	.unprepare = lcd_olinuxino_unprepare,
208 	.prepare = lcd_olinuxino_prepare,
209 	.enable = lcd_olinuxino_enable,
210 	.get_modes = lcd_olinuxino_get_modes,
211 };
212 
213 static int lcd_olinuxino_probe(struct i2c_client *client,
214 			       const struct i2c_device_id *id)
215 {
216 	struct device *dev = &client->dev;
217 	struct lcd_olinuxino *lcd;
218 	u32 checksum, i;
219 	int ret = 0;
220 
221 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
222 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK))
223 		return -ENODEV;
224 
225 	lcd = devm_kzalloc(dev, sizeof(*lcd), GFP_KERNEL);
226 	if (!lcd)
227 		return -ENOMEM;
228 
229 	i2c_set_clientdata(client, lcd);
230 	lcd->dev = dev;
231 	lcd->client = client;
232 
233 	mutex_init(&lcd->mutex);
234 
235 	/* Copy data into buffer */
236 	for (i = 0; i < LCD_OLINUXINO_DATA_LEN; i += I2C_SMBUS_BLOCK_MAX) {
237 		mutex_lock(&lcd->mutex);
238 		ret = i2c_smbus_read_i2c_block_data(client,
239 						    i,
240 						    I2C_SMBUS_BLOCK_MAX,
241 						    (u8 *)&lcd->eeprom + i);
242 		mutex_unlock(&lcd->mutex);
243 		if (ret < 0) {
244 			dev_err(dev, "error reading from device at %02x\n", i);
245 			return ret;
246 		}
247 	}
248 
249 	/* Check configuration checksum */
250 	checksum = ~crc32(~0, (u8 *)&lcd->eeprom, 252);
251 	if (checksum != lcd->eeprom.checksum) {
252 		dev_err(dev, "configuration checksum does not match!\n");
253 		return -EINVAL;
254 	}
255 
256 	/* Check magic header */
257 	if (lcd->eeprom.header != LCD_OLINUXINO_HEADER_MAGIC) {
258 		dev_err(dev, "magic header does not match\n");
259 		return -EINVAL;
260 	}
261 
262 	dev_info(dev, "Detected %s, Rev. %s, Serial: %08x\n",
263 		 lcd->eeprom.info.name,
264 		 lcd->eeprom.revision,
265 		 lcd->eeprom.serial);
266 
267 	/*
268 	 * The eeprom can hold up to 4 modes.
269 	 * If the stored value is bigger, overwrite it.
270 	 */
271 	if (lcd->eeprom.num_modes > 4) {
272 		dev_warn(dev, "invalid number of modes, falling back to 4\n");
273 		lcd->eeprom.num_modes = 4;
274 	}
275 
276 	lcd->enabled = false;
277 	lcd->prepared = false;
278 
279 	lcd->supply = devm_regulator_get(dev, "power");
280 	if (IS_ERR(lcd->supply))
281 		return PTR_ERR(lcd->supply);
282 
283 	lcd->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
284 	if (IS_ERR(lcd->enable_gpio))
285 		return PTR_ERR(lcd->enable_gpio);
286 
287 	lcd->backlight = devm_of_find_backlight(dev);
288 	if (IS_ERR(lcd->backlight))
289 		return PTR_ERR(lcd->backlight);
290 
291 	drm_panel_init(&lcd->panel);
292 	lcd->panel.dev = dev;
293 	lcd->panel.funcs = &lcd_olinuxino_funcs;
294 
295 	return drm_panel_add(&lcd->panel);
296 }
297 
298 static int lcd_olinuxino_remove(struct i2c_client *client)
299 {
300 	struct lcd_olinuxino *panel = i2c_get_clientdata(client);
301 
302 	drm_panel_remove(&panel->panel);
303 
304 	lcd_olinuxino_disable(&panel->panel);
305 	lcd_olinuxino_unprepare(&panel->panel);
306 
307 	return 0;
308 }
309 
310 static const struct of_device_id lcd_olinuxino_of_ids[] = {
311 	{ .compatible = "olimex,lcd-olinuxino" },
312 	{ }
313 };
314 MODULE_DEVICE_TABLE(of, lcd_olinuxino_of_ids);
315 
316 static struct i2c_driver lcd_olinuxino_driver = {
317 	.driver = {
318 		.name = "lcd_olinuxino",
319 		.of_match_table = lcd_olinuxino_of_ids,
320 	},
321 	.probe = lcd_olinuxino_probe,
322 	.remove = lcd_olinuxino_remove,
323 };
324 
325 module_i2c_driver(lcd_olinuxino_driver);
326 
327 MODULE_AUTHOR("Stefan Mavrodiev <stefan@olimex.com>");
328 MODULE_DESCRIPTION("LCD-OLinuXino driver");
329 MODULE_LICENSE("GPL");
330