Linux Audio

Check our new training course

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