Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ld9040 AMOLED LCD drm_panel driver.
  4 *
  5 * Copyright (c) 2014 Samsung Electronics Co., Ltd
  6 * Derived from drivers/video/backlight/ld9040.c
  7 *
  8 * Andrzej Hajda <a.hajda@samsung.com>
  9*/
 10
 11#include <linux/backlight.h>
 12#include <linux/delay.h>
 13#include <linux/gpio/consumer.h>
 14#include <linux/module.h>
 15#include <linux/of.h>
 16#include <linux/regulator/consumer.h>
 17#include <linux/spi/spi.h>
 18
 19#include <video/mipi_display.h>
 20#include <video/of_videomode.h>
 21#include <video/videomode.h>
 22
 23#include <drm/drm_modes.h>
 24#include <drm/drm_panel.h>
 25
 26/* Manufacturer Command Set */
 27#define MCS_MANPWR		0xb0
 28#define MCS_ELVSS_ON		0xb1
 29#define MCS_USER_SETTING	0xf0
 30#define MCS_DISPCTL		0xf2
 31#define MCS_POWER_CTRL		0xf4
 32#define MCS_GTCON		0xf7
 33#define MCS_PANEL_CONDITION	0xf8
 34#define MCS_GAMMA_SET1		0xf9
 35#define MCS_GAMMA_CTRL		0xfb
 36
 37/* array of gamma tables for gamma value 2.2 */
 38static u8 const ld9040_gammas[25][22] = {
 39	{ 0xf9, 0x00, 0x13, 0xb2, 0xba, 0xd2, 0x00, 0x30, 0x00, 0xaf, 0xc0,
 40	  0xb8, 0xcd, 0x00, 0x3d, 0x00, 0xa8, 0xb8, 0xb7, 0xcd, 0x00, 0x44 },
 41	{ 0xf9, 0x00, 0x13, 0xb9, 0xb9, 0xd0, 0x00, 0x3c, 0x00, 0xaf, 0xbf,
 42	  0xb6, 0xcb, 0x00, 0x4b, 0x00, 0xa8, 0xb9, 0xb5, 0xcc, 0x00, 0x52 },
 43	{ 0xf9, 0x00, 0x13, 0xba, 0xb9, 0xcd, 0x00, 0x41, 0x00, 0xb0, 0xbe,
 44	  0xb5, 0xc9, 0x00, 0x51, 0x00, 0xa9, 0xb9, 0xb5, 0xca, 0x00, 0x57 },
 45	{ 0xf9, 0x00, 0x13, 0xb9, 0xb8, 0xcd, 0x00, 0x46, 0x00, 0xb1, 0xbc,
 46	  0xb5, 0xc8, 0x00, 0x56, 0x00, 0xaa, 0xb8, 0xb4, 0xc9, 0x00, 0x5d },
 47	{ 0xf9, 0x00, 0x13, 0xba, 0xb8, 0xcb, 0x00, 0x4b, 0x00, 0xb3, 0xbc,
 48	  0xb4, 0xc7, 0x00, 0x5c, 0x00, 0xac, 0xb8, 0xb4, 0xc8, 0x00, 0x62 },
 49	{ 0xf9, 0x00, 0x13, 0xbb, 0xb7, 0xca, 0x00, 0x4f, 0x00, 0xb4, 0xbb,
 50	  0xb3, 0xc7, 0x00, 0x60, 0x00, 0xad, 0xb8, 0xb4, 0xc7, 0x00, 0x67 },
 51	{ 0xf9, 0x00, 0x47, 0xba, 0xb6, 0xca, 0x00, 0x53, 0x00, 0xb5, 0xbb,
 52	  0xb3, 0xc6, 0x00, 0x65, 0x00, 0xae, 0xb8, 0xb3, 0xc7, 0x00, 0x6c },
 53	{ 0xf9, 0x00, 0x71, 0xbb, 0xb5, 0xc8, 0x00, 0x57, 0x00, 0xb5, 0xbb,
 54	  0xb0, 0xc5, 0x00, 0x6a, 0x00, 0xae, 0xb9, 0xb1, 0xc6, 0x00, 0x70 },
 55	{ 0xf9, 0x00, 0x7b, 0xbb, 0xb4, 0xc8, 0x00, 0x5b, 0x00, 0xb5, 0xba,
 56	  0xb1, 0xc4, 0x00, 0x6e, 0x00, 0xae, 0xb9, 0xb0, 0xc5, 0x00, 0x75 },
 57	{ 0xf9, 0x00, 0x82, 0xba, 0xb4, 0xc7, 0x00, 0x5f, 0x00, 0xb5, 0xba,
 58	  0xb0, 0xc3, 0x00, 0x72, 0x00, 0xae, 0xb8, 0xb0, 0xc3, 0x00, 0x7a },
 59	{ 0xf9, 0x00, 0x89, 0xba, 0xb3, 0xc8, 0x00, 0x62, 0x00, 0xb6, 0xba,
 60	  0xaf, 0xc3, 0x00, 0x76, 0x00, 0xaf, 0xb7, 0xae, 0xc4, 0x00, 0x7e },
 61	{ 0xf9, 0x00, 0x8b, 0xb9, 0xb3, 0xc7, 0x00, 0x65, 0x00, 0xb7, 0xb8,
 62	  0xaf, 0xc3, 0x00, 0x7a, 0x00, 0x80, 0xb6, 0xae, 0xc4, 0x00, 0x81 },
 63	{ 0xf9, 0x00, 0x93, 0xba, 0xb3, 0xc5, 0x00, 0x69, 0x00, 0xb8, 0xb9,
 64	  0xae, 0xc1, 0x00, 0x7f, 0x00, 0xb0, 0xb6, 0xae, 0xc3, 0x00, 0x85 },
 65	{ 0xf9, 0x00, 0x97, 0xba, 0xb2, 0xc5, 0x00, 0x6c, 0x00, 0xb8, 0xb8,
 66	  0xae, 0xc1, 0x00, 0x82, 0x00, 0xb0, 0xb6, 0xae, 0xc2, 0x00, 0x89 },
 67	{ 0xf9, 0x00, 0x9a, 0xba, 0xb1, 0xc4, 0x00, 0x6f, 0x00, 0xb8, 0xb8,
 68	  0xad, 0xc0, 0x00, 0x86, 0x00, 0xb0, 0xb7, 0xad, 0xc0, 0x00, 0x8d },
 69	{ 0xf9, 0x00, 0x9c, 0xb9, 0xb0, 0xc4, 0x00, 0x72, 0x00, 0xb8, 0xb8,
 70	  0xac, 0xbf, 0x00, 0x8a, 0x00, 0xb0, 0xb6, 0xac, 0xc0, 0x00, 0x91 },
 71	{ 0xf9, 0x00, 0x9e, 0xba, 0xb0, 0xc2, 0x00, 0x75, 0x00, 0xb9, 0xb8,
 72	  0xab, 0xbe, 0x00, 0x8e, 0x00, 0xb0, 0xb6, 0xac, 0xbf, 0x00, 0x94 },
 73	{ 0xf9, 0x00, 0xa0, 0xb9, 0xaf, 0xc3, 0x00, 0x77, 0x00, 0xb9, 0xb7,
 74	  0xab, 0xbe, 0x00, 0x90, 0x00, 0xb0, 0xb6, 0xab, 0xbf, 0x00, 0x97 },
 75	{ 0xf9, 0x00, 0xa2, 0xb9, 0xaf, 0xc2, 0x00, 0x7a, 0x00, 0xb9, 0xb7,
 76	  0xaa, 0xbd, 0x00, 0x94, 0x00, 0xb0, 0xb5, 0xab, 0xbf, 0x00, 0x9a },
 77	{ 0xf9, 0x00, 0xa4, 0xb9, 0xaf, 0xc1, 0x00, 0x7d, 0x00, 0xb9, 0xb6,
 78	  0xaa, 0xbb, 0x00, 0x97, 0x00, 0xb1, 0xb5, 0xaa, 0xbf, 0x00, 0x9d },
 79	{ 0xf9, 0x00, 0xa4, 0xb8, 0xb0, 0xbf, 0x00, 0x80, 0x00, 0xb8, 0xb6,
 80	  0xaa, 0xbc, 0x00, 0x9a, 0x00, 0xb0, 0xb5, 0xab, 0xbd, 0x00, 0xa0 },
 81	{ 0xf9, 0x00, 0xa8, 0xb8, 0xae, 0xbe, 0x00, 0x84, 0x00, 0xb9, 0xb7,
 82	  0xa8, 0xbc, 0x00, 0x9d, 0x00, 0xb2, 0xb5, 0xaa, 0xbc, 0x00, 0xa4 },
 83	{ 0xf9, 0x00, 0xa9, 0xb6, 0xad, 0xbf, 0x00, 0x86, 0x00, 0xb8, 0xb5,
 84	  0xa8, 0xbc, 0x00, 0xa0, 0x00, 0xb3, 0xb3, 0xa9, 0xbc, 0x00, 0xa7 },
 85	{ 0xf9, 0x00, 0xa9, 0xb7, 0xae, 0xbd, 0x00, 0x89, 0x00, 0xb7, 0xb6,
 86	  0xa8, 0xba, 0x00, 0xa4, 0x00, 0xb1, 0xb4, 0xaa, 0xbb, 0x00, 0xaa },
 87	{ 0xf9, 0x00, 0xa7, 0xb4, 0xae, 0xbf, 0x00, 0x91, 0x00, 0xb2, 0xb4,
 88	  0xaa, 0xbb, 0x00, 0xac, 0x00, 0xb3, 0xb1, 0xaa, 0xbc, 0x00, 0xb3 },
 89};
 90
 91struct ld9040 {
 92	struct device *dev;
 93	struct drm_panel panel;
 94
 95	struct regulator_bulk_data supplies[2];
 96	struct gpio_desc *reset_gpio;
 97	u32 power_on_delay;
 98	u32 reset_delay;
 99	struct videomode vm;
100	u32 width_mm;
101	u32 height_mm;
102
103	int brightness;
104
105	/* This field is tested by functions directly accessing bus before
106	 * transfer, transfer is skipped if it is set. In case of transfer
107	 * failure or unexpected response the field is set to error value.
108	 * Such construct allows to eliminate many checks in higher level
109	 * functions.
110	 */
111	int error;
112};
113
114static inline struct ld9040 *panel_to_ld9040(struct drm_panel *panel)
115{
116	return container_of(panel, struct ld9040, panel);
117}
118
119static int ld9040_clear_error(struct ld9040 *ctx)
120{
121	int ret = ctx->error;
122
123	ctx->error = 0;
124	return ret;
125}
126
127static int ld9040_spi_write_word(struct ld9040 *ctx, u16 data)
128{
129	struct spi_device *spi = to_spi_device(ctx->dev);
130	struct spi_transfer xfer = {
131		.len		= 2,
132		.tx_buf		= &data,
133	};
134	struct spi_message msg;
135
136	spi_message_init(&msg);
137	spi_message_add_tail(&xfer, &msg);
138
139	return spi_sync(spi, &msg);
140}
141
142static void ld9040_dcs_write(struct ld9040 *ctx, const u8 *data, size_t len)
143{
144	int ret = 0;
145
146	if (ctx->error < 0 || len == 0)
147		return;
148
149	dev_dbg(ctx->dev, "writing dcs seq: %*ph\n", (int)len, data);
150	ret = ld9040_spi_write_word(ctx, *data);
151
152	while (!ret && --len) {
153		++data;
154		ret = ld9040_spi_write_word(ctx, *data | 0x100);
155	}
156
157	if (ret) {
158		dev_err(ctx->dev, "error %d writing dcs seq: %*ph\n", ret,
159			(int)len, data);
160		ctx->error = ret;
161	}
162
163	usleep_range(300, 310);
164}
165
166#define ld9040_dcs_write_seq_static(ctx, seq...) \
167({\
168	static const u8 d[] = { seq };\
169	ld9040_dcs_write(ctx, d, ARRAY_SIZE(d));\
170})
171
172static void ld9040_brightness_set(struct ld9040 *ctx)
173{
174	ld9040_dcs_write(ctx, ld9040_gammas[ctx->brightness],
175			 ARRAY_SIZE(ld9040_gammas[ctx->brightness]));
176
177	ld9040_dcs_write_seq_static(ctx, MCS_GAMMA_CTRL, 0x02, 0x5a);
178}
179
180static void ld9040_init(struct ld9040 *ctx)
181{
182	ld9040_dcs_write_seq_static(ctx, MCS_USER_SETTING, 0x5a, 0x5a);
183	ld9040_dcs_write_seq_static(ctx, MCS_PANEL_CONDITION,
184		0x05, 0x5e, 0x96, 0x6b, 0x7d, 0x0d, 0x3f, 0x00,
185		0x00, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
186		0x07, 0x05, 0x1f, 0x1f, 0x1f, 0x00, 0x00);
187	ld9040_dcs_write_seq_static(ctx, MCS_DISPCTL,
188		0x02, 0x06, 0x0a, 0x10, 0x10);
189	ld9040_dcs_write_seq_static(ctx, MCS_MANPWR, 0x04);
190	ld9040_dcs_write_seq_static(ctx, MCS_POWER_CTRL,
191		0x0a, 0x87, 0x25, 0x6a, 0x44, 0x02, 0x88);
192	ld9040_dcs_write_seq_static(ctx, MCS_ELVSS_ON, 0x0f, 0x00, 0x16);
193	ld9040_dcs_write_seq_static(ctx, MCS_GTCON, 0x09, 0x00, 0x00);
194	ld9040_brightness_set(ctx);
195	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_EXIT_SLEEP_MODE);
196	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_ON);
197}
198
199static int ld9040_power_on(struct ld9040 *ctx)
200{
201	int ret;
202
203	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
204	if (ret < 0)
205		return ret;
206
207	msleep(ctx->power_on_delay);
208	gpiod_set_value(ctx->reset_gpio, 0);
209	msleep(ctx->reset_delay);
210	gpiod_set_value(ctx->reset_gpio, 1);
211	msleep(ctx->reset_delay);
212
213	return 0;
214}
215
216static int ld9040_power_off(struct ld9040 *ctx)
217{
218	return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
219}
220
221static int ld9040_disable(struct drm_panel *panel)
222{
223	return 0;
224}
225
226static int ld9040_unprepare(struct drm_panel *panel)
227{
228	struct ld9040 *ctx = panel_to_ld9040(panel);
229
230	msleep(120);
231	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_OFF);
232	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_ENTER_SLEEP_MODE);
233	msleep(40);
234
235	ld9040_clear_error(ctx);
236
237	return ld9040_power_off(ctx);
238}
239
240static int ld9040_prepare(struct drm_panel *panel)
241{
242	struct ld9040 *ctx = panel_to_ld9040(panel);
243	int ret;
244
245	ret = ld9040_power_on(ctx);
246	if (ret < 0)
247		return ret;
248
249	ld9040_init(ctx);
250
251	ret = ld9040_clear_error(ctx);
252
253	if (ret < 0)
254		ld9040_unprepare(panel);
255
256	return ret;
257}
258
259static int ld9040_enable(struct drm_panel *panel)
260{
261	return 0;
262}
263
264static int ld9040_get_modes(struct drm_panel *panel,
265			    struct drm_connector *connector)
266{
267	struct ld9040 *ctx = panel_to_ld9040(panel);
268	struct drm_display_mode *mode;
269
270	mode = drm_mode_create(connector->dev);
271	if (!mode) {
272		dev_err(panel->dev, "failed to create a new display mode\n");
273		return 0;
274	}
275
276	drm_display_mode_from_videomode(&ctx->vm, mode);
277	mode->width_mm = ctx->width_mm;
278	mode->height_mm = ctx->height_mm;
279	connector->display_info.width_mm = mode->width_mm;
280	connector->display_info.height_mm = mode->height_mm;
281
282	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
283	drm_mode_probed_add(connector, mode);
284
285	return 1;
286}
287
288static const struct drm_panel_funcs ld9040_drm_funcs = {
289	.disable = ld9040_disable,
290	.unprepare = ld9040_unprepare,
291	.prepare = ld9040_prepare,
292	.enable = ld9040_enable,
293	.get_modes = ld9040_get_modes,
294};
295
296static int ld9040_parse_dt(struct ld9040 *ctx)
297{
298	struct device *dev = ctx->dev;
299	struct device_node *np = dev->of_node;
300	int ret;
301
302	ret = of_get_videomode(np, &ctx->vm, 0);
303	if (ret < 0)
304		return ret;
305
306	of_property_read_u32(np, "power-on-delay", &ctx->power_on_delay);
307	of_property_read_u32(np, "reset-delay", &ctx->reset_delay);
308	of_property_read_u32(np, "panel-width-mm", &ctx->width_mm);
309	of_property_read_u32(np, "panel-height-mm", &ctx->height_mm);
310
311	return 0;
312}
313
314static int ld9040_bl_update_status(struct backlight_device *dev)
315{
316	struct ld9040 *ctx = bl_get_data(dev);
317
318	ctx->brightness = backlight_get_brightness(dev);
319	ld9040_brightness_set(ctx);
320
321	return 0;
322}
323
324static const struct backlight_ops ld9040_bl_ops = {
325	.update_status  = ld9040_bl_update_status,
326};
327
328static const struct backlight_properties ld9040_bl_props = {
329	.type = BACKLIGHT_RAW,
330	.scale = BACKLIGHT_SCALE_NON_LINEAR,
331	.max_brightness = ARRAY_SIZE(ld9040_gammas) - 1,
332	.brightness = ARRAY_SIZE(ld9040_gammas) - 1,
333};
334
335static int ld9040_probe(struct spi_device *spi)
336{
337	struct backlight_device *bldev;
338	struct device *dev = &spi->dev;
339	struct ld9040 *ctx;
340	int ret;
341
342	ctx = devm_kzalloc(dev, sizeof(struct ld9040), GFP_KERNEL);
343	if (!ctx)
344		return -ENOMEM;
345
346	spi_set_drvdata(spi, ctx);
347
348	ctx->dev = dev;
349	ctx->brightness = ld9040_bl_props.brightness;
350
351	ret = ld9040_parse_dt(ctx);
352	if (ret < 0)
353		return ret;
354
355	ctx->supplies[0].supply = "vdd3";
356	ctx->supplies[1].supply = "vci";
357	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
358				      ctx->supplies);
359	if (ret < 0)
360		return ret;
361
362	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
363	if (IS_ERR(ctx->reset_gpio)) {
364		dev_err(dev, "cannot get reset-gpios %ld\n",
365			PTR_ERR(ctx->reset_gpio));
366		return PTR_ERR(ctx->reset_gpio);
367	}
368
369	spi->bits_per_word = 9;
370	ret = spi_setup(spi);
371	if (ret < 0) {
372		dev_err(dev, "spi setup failed.\n");
373		return ret;
374	}
375
376	drm_panel_init(&ctx->panel, dev, &ld9040_drm_funcs,
377		       DRM_MODE_CONNECTOR_DPI);
378
379	bldev = devm_backlight_device_register(dev, dev_name(dev), dev,
380					       ctx, &ld9040_bl_ops,
381					       &ld9040_bl_props);
382	if (IS_ERR(bldev))
383		return PTR_ERR(bldev);
384
385	drm_panel_add(&ctx->panel);
386
387	return 0;
388}
389
390static void ld9040_remove(struct spi_device *spi)
391{
392	struct ld9040 *ctx = spi_get_drvdata(spi);
393
394	ld9040_power_off(ctx);
395	drm_panel_remove(&ctx->panel);
 
 
396}
397
398static const struct of_device_id ld9040_of_match[] = {
399	{ .compatible = "samsung,ld9040" },
400	{ }
401};
402MODULE_DEVICE_TABLE(of, ld9040_of_match);
403
404static const struct spi_device_id ld9040_ids[] = {
405	{ "ld9040", },
406	{ /* sentinel */ }
407};
408MODULE_DEVICE_TABLE(spi, ld9040_ids);
409
410static struct spi_driver ld9040_driver = {
411	.probe = ld9040_probe,
412	.remove = ld9040_remove,
413	.id_table = ld9040_ids,
414	.driver = {
415		.name = "panel-samsung-ld9040",
416		.of_match_table = ld9040_of_match,
417	},
418};
419module_spi_driver(ld9040_driver);
420
421MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
422MODULE_DESCRIPTION("ld9040 LCD Driver");
423MODULE_LICENSE("GPL v2");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ld9040 AMOLED LCD drm_panel driver.
  4 *
  5 * Copyright (c) 2014 Samsung Electronics Co., Ltd
  6 * Derived from drivers/video/backlight/ld9040.c
  7 *
  8 * Andrzej Hajda <a.hajda@samsung.com>
  9*/
 10
 
 11#include <linux/delay.h>
 12#include <linux/gpio/consumer.h>
 13#include <linux/module.h>
 14#include <linux/of.h>
 15#include <linux/regulator/consumer.h>
 16#include <linux/spi/spi.h>
 17
 18#include <video/mipi_display.h>
 19#include <video/of_videomode.h>
 20#include <video/videomode.h>
 21
 22#include <drm/drm_modes.h>
 23#include <drm/drm_panel.h>
 24
 25/* Manufacturer Command Set */
 26#define MCS_MANPWR		0xb0
 27#define MCS_ELVSS_ON		0xb1
 28#define MCS_USER_SETTING	0xf0
 29#define MCS_DISPCTL		0xf2
 30#define MCS_POWER_CTRL		0xf4
 31#define MCS_GTCON		0xf7
 32#define MCS_PANEL_CONDITION	0xf8
 33#define MCS_GAMMA_SET1		0xf9
 34#define MCS_GAMMA_CTRL		0xfb
 35
 36/* array of gamma tables for gamma value 2.2 */
 37static u8 const ld9040_gammas[25][22] = {
 38	{ 0xf9, 0x00, 0x13, 0xb2, 0xba, 0xd2, 0x00, 0x30, 0x00, 0xaf, 0xc0,
 39	  0xb8, 0xcd, 0x00, 0x3d, 0x00, 0xa8, 0xb8, 0xb7, 0xcd, 0x00, 0x44 },
 40	{ 0xf9, 0x00, 0x13, 0xb9, 0xb9, 0xd0, 0x00, 0x3c, 0x00, 0xaf, 0xbf,
 41	  0xb6, 0xcb, 0x00, 0x4b, 0x00, 0xa8, 0xb9, 0xb5, 0xcc, 0x00, 0x52 },
 42	{ 0xf9, 0x00, 0x13, 0xba, 0xb9, 0xcd, 0x00, 0x41, 0x00, 0xb0, 0xbe,
 43	  0xb5, 0xc9, 0x00, 0x51, 0x00, 0xa9, 0xb9, 0xb5, 0xca, 0x00, 0x57 },
 44	{ 0xf9, 0x00, 0x13, 0xb9, 0xb8, 0xcd, 0x00, 0x46, 0x00, 0xb1, 0xbc,
 45	  0xb5, 0xc8, 0x00, 0x56, 0x00, 0xaa, 0xb8, 0xb4, 0xc9, 0x00, 0x5d },
 46	{ 0xf9, 0x00, 0x13, 0xba, 0xb8, 0xcb, 0x00, 0x4b, 0x00, 0xb3, 0xbc,
 47	  0xb4, 0xc7, 0x00, 0x5c, 0x00, 0xac, 0xb8, 0xb4, 0xc8, 0x00, 0x62 },
 48	{ 0xf9, 0x00, 0x13, 0xbb, 0xb7, 0xca, 0x00, 0x4f, 0x00, 0xb4, 0xbb,
 49	  0xb3, 0xc7, 0x00, 0x60, 0x00, 0xad, 0xb8, 0xb4, 0xc7, 0x00, 0x67 },
 50	{ 0xf9, 0x00, 0x47, 0xba, 0xb6, 0xca, 0x00, 0x53, 0x00, 0xb5, 0xbb,
 51	  0xb3, 0xc6, 0x00, 0x65, 0x00, 0xae, 0xb8, 0xb3, 0xc7, 0x00, 0x6c },
 52	{ 0xf9, 0x00, 0x71, 0xbb, 0xb5, 0xc8, 0x00, 0x57, 0x00, 0xb5, 0xbb,
 53	  0xb0, 0xc5, 0x00, 0x6a, 0x00, 0xae, 0xb9, 0xb1, 0xc6, 0x00, 0x70 },
 54	{ 0xf9, 0x00, 0x7b, 0xbb, 0xb4, 0xc8, 0x00, 0x5b, 0x00, 0xb5, 0xba,
 55	  0xb1, 0xc4, 0x00, 0x6e, 0x00, 0xae, 0xb9, 0xb0, 0xc5, 0x00, 0x75 },
 56	{ 0xf9, 0x00, 0x82, 0xba, 0xb4, 0xc7, 0x00, 0x5f, 0x00, 0xb5, 0xba,
 57	  0xb0, 0xc3, 0x00, 0x72, 0x00, 0xae, 0xb8, 0xb0, 0xc3, 0x00, 0x7a },
 58	{ 0xf9, 0x00, 0x89, 0xba, 0xb3, 0xc8, 0x00, 0x62, 0x00, 0xb6, 0xba,
 59	  0xaf, 0xc3, 0x00, 0x76, 0x00, 0xaf, 0xb7, 0xae, 0xc4, 0x00, 0x7e },
 60	{ 0xf9, 0x00, 0x8b, 0xb9, 0xb3, 0xc7, 0x00, 0x65, 0x00, 0xb7, 0xb8,
 61	  0xaf, 0xc3, 0x00, 0x7a, 0x00, 0x80, 0xb6, 0xae, 0xc4, 0x00, 0x81 },
 62	{ 0xf9, 0x00, 0x93, 0xba, 0xb3, 0xc5, 0x00, 0x69, 0x00, 0xb8, 0xb9,
 63	  0xae, 0xc1, 0x00, 0x7f, 0x00, 0xb0, 0xb6, 0xae, 0xc3, 0x00, 0x85 },
 64	{ 0xf9, 0x00, 0x97, 0xba, 0xb2, 0xc5, 0x00, 0x6c, 0x00, 0xb8, 0xb8,
 65	  0xae, 0xc1, 0x00, 0x82, 0x00, 0xb0, 0xb6, 0xae, 0xc2, 0x00, 0x89 },
 66	{ 0xf9, 0x00, 0x9a, 0xba, 0xb1, 0xc4, 0x00, 0x6f, 0x00, 0xb8, 0xb8,
 67	  0xad, 0xc0, 0x00, 0x86, 0x00, 0xb0, 0xb7, 0xad, 0xc0, 0x00, 0x8d },
 68	{ 0xf9, 0x00, 0x9c, 0xb9, 0xb0, 0xc4, 0x00, 0x72, 0x00, 0xb8, 0xb8,
 69	  0xac, 0xbf, 0x00, 0x8a, 0x00, 0xb0, 0xb6, 0xac, 0xc0, 0x00, 0x91 },
 70	{ 0xf9, 0x00, 0x9e, 0xba, 0xb0, 0xc2, 0x00, 0x75, 0x00, 0xb9, 0xb8,
 71	  0xab, 0xbe, 0x00, 0x8e, 0x00, 0xb0, 0xb6, 0xac, 0xbf, 0x00, 0x94 },
 72	{ 0xf9, 0x00, 0xa0, 0xb9, 0xaf, 0xc3, 0x00, 0x77, 0x00, 0xb9, 0xb7,
 73	  0xab, 0xbe, 0x00, 0x90, 0x00, 0xb0, 0xb6, 0xab, 0xbf, 0x00, 0x97 },
 74	{ 0xf9, 0x00, 0xa2, 0xb9, 0xaf, 0xc2, 0x00, 0x7a, 0x00, 0xb9, 0xb7,
 75	  0xaa, 0xbd, 0x00, 0x94, 0x00, 0xb0, 0xb5, 0xab, 0xbf, 0x00, 0x9a },
 76	{ 0xf9, 0x00, 0xa4, 0xb9, 0xaf, 0xc1, 0x00, 0x7d, 0x00, 0xb9, 0xb6,
 77	  0xaa, 0xbb, 0x00, 0x97, 0x00, 0xb1, 0xb5, 0xaa, 0xbf, 0x00, 0x9d },
 78	{ 0xf9, 0x00, 0xa4, 0xb8, 0xb0, 0xbf, 0x00, 0x80, 0x00, 0xb8, 0xb6,
 79	  0xaa, 0xbc, 0x00, 0x9a, 0x00, 0xb0, 0xb5, 0xab, 0xbd, 0x00, 0xa0 },
 80	{ 0xf9, 0x00, 0xa8, 0xb8, 0xae, 0xbe, 0x00, 0x84, 0x00, 0xb9, 0xb7,
 81	  0xa8, 0xbc, 0x00, 0x9d, 0x00, 0xb2, 0xb5, 0xaa, 0xbc, 0x00, 0xa4 },
 82	{ 0xf9, 0x00, 0xa9, 0xb6, 0xad, 0xbf, 0x00, 0x86, 0x00, 0xb8, 0xb5,
 83	  0xa8, 0xbc, 0x00, 0xa0, 0x00, 0xb3, 0xb3, 0xa9, 0xbc, 0x00, 0xa7 },
 84	{ 0xf9, 0x00, 0xa9, 0xb7, 0xae, 0xbd, 0x00, 0x89, 0x00, 0xb7, 0xb6,
 85	  0xa8, 0xba, 0x00, 0xa4, 0x00, 0xb1, 0xb4, 0xaa, 0xbb, 0x00, 0xaa },
 86	{ 0xf9, 0x00, 0xa7, 0xb4, 0xae, 0xbf, 0x00, 0x91, 0x00, 0xb2, 0xb4,
 87	  0xaa, 0xbb, 0x00, 0xac, 0x00, 0xb3, 0xb1, 0xaa, 0xbc, 0x00, 0xb3 },
 88};
 89
 90struct ld9040 {
 91	struct device *dev;
 92	struct drm_panel panel;
 93
 94	struct regulator_bulk_data supplies[2];
 95	struct gpio_desc *reset_gpio;
 96	u32 power_on_delay;
 97	u32 reset_delay;
 98	struct videomode vm;
 99	u32 width_mm;
100	u32 height_mm;
101
102	int brightness;
103
104	/* This field is tested by functions directly accessing bus before
105	 * transfer, transfer is skipped if it is set. In case of transfer
106	 * failure or unexpected response the field is set to error value.
107	 * Such construct allows to eliminate many checks in higher level
108	 * functions.
109	 */
110	int error;
111};
112
113static inline struct ld9040 *panel_to_ld9040(struct drm_panel *panel)
114{
115	return container_of(panel, struct ld9040, panel);
116}
117
118static int ld9040_clear_error(struct ld9040 *ctx)
119{
120	int ret = ctx->error;
121
122	ctx->error = 0;
123	return ret;
124}
125
126static int ld9040_spi_write_word(struct ld9040 *ctx, u16 data)
127{
128	struct spi_device *spi = to_spi_device(ctx->dev);
129	struct spi_transfer xfer = {
130		.len		= 2,
131		.tx_buf		= &data,
132	};
133	struct spi_message msg;
134
135	spi_message_init(&msg);
136	spi_message_add_tail(&xfer, &msg);
137
138	return spi_sync(spi, &msg);
139}
140
141static void ld9040_dcs_write(struct ld9040 *ctx, const u8 *data, size_t len)
142{
143	int ret = 0;
144
145	if (ctx->error < 0 || len == 0)
146		return;
147
148	dev_dbg(ctx->dev, "writing dcs seq: %*ph\n", (int)len, data);
149	ret = ld9040_spi_write_word(ctx, *data);
150
151	while (!ret && --len) {
152		++data;
153		ret = ld9040_spi_write_word(ctx, *data | 0x100);
154	}
155
156	if (ret) {
157		dev_err(ctx->dev, "error %d writing dcs seq: %*ph\n", ret,
158			(int)len, data);
159		ctx->error = ret;
160	}
161
162	usleep_range(300, 310);
163}
164
165#define ld9040_dcs_write_seq_static(ctx, seq...) \
166({\
167	static const u8 d[] = { seq };\
168	ld9040_dcs_write(ctx, d, ARRAY_SIZE(d));\
169})
170
171static void ld9040_brightness_set(struct ld9040 *ctx)
172{
173	ld9040_dcs_write(ctx, ld9040_gammas[ctx->brightness],
174			 ARRAY_SIZE(ld9040_gammas[ctx->brightness]));
175
176	ld9040_dcs_write_seq_static(ctx, MCS_GAMMA_CTRL, 0x02, 0x5a);
177}
178
179static void ld9040_init(struct ld9040 *ctx)
180{
181	ld9040_dcs_write_seq_static(ctx, MCS_USER_SETTING, 0x5a, 0x5a);
182	ld9040_dcs_write_seq_static(ctx, MCS_PANEL_CONDITION,
183		0x05, 0x65, 0x96, 0x71, 0x7d, 0x19, 0x3b, 0x0d,
184		0x19, 0x7e, 0x0d, 0xe2, 0x00, 0x00, 0x7e, 0x7d,
185		0x07, 0x07, 0x20, 0x20, 0x20, 0x02, 0x02);
186	ld9040_dcs_write_seq_static(ctx, MCS_DISPCTL,
187		0x02, 0x08, 0x08, 0x10, 0x10);
188	ld9040_dcs_write_seq_static(ctx, MCS_MANPWR, 0x04);
189	ld9040_dcs_write_seq_static(ctx, MCS_POWER_CTRL,
190		0x0a, 0x87, 0x25, 0x6a, 0x44, 0x02, 0x88);
191	ld9040_dcs_write_seq_static(ctx, MCS_ELVSS_ON, 0x0d, 0x00, 0x16);
192	ld9040_dcs_write_seq_static(ctx, MCS_GTCON, 0x09, 0x00, 0x00);
193	ld9040_brightness_set(ctx);
194	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_EXIT_SLEEP_MODE);
195	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_ON);
196}
197
198static int ld9040_power_on(struct ld9040 *ctx)
199{
200	int ret;
201
202	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
203	if (ret < 0)
204		return ret;
205
206	msleep(ctx->power_on_delay);
207	gpiod_set_value(ctx->reset_gpio, 0);
208	msleep(ctx->reset_delay);
209	gpiod_set_value(ctx->reset_gpio, 1);
210	msleep(ctx->reset_delay);
211
212	return 0;
213}
214
215static int ld9040_power_off(struct ld9040 *ctx)
216{
217	return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
218}
219
220static int ld9040_disable(struct drm_panel *panel)
221{
222	return 0;
223}
224
225static int ld9040_unprepare(struct drm_panel *panel)
226{
227	struct ld9040 *ctx = panel_to_ld9040(panel);
228
229	msleep(120);
230	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_OFF);
231	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_ENTER_SLEEP_MODE);
232	msleep(40);
233
234	ld9040_clear_error(ctx);
235
236	return ld9040_power_off(ctx);
237}
238
239static int ld9040_prepare(struct drm_panel *panel)
240{
241	struct ld9040 *ctx = panel_to_ld9040(panel);
242	int ret;
243
244	ret = ld9040_power_on(ctx);
245	if (ret < 0)
246		return ret;
247
248	ld9040_init(ctx);
249
250	ret = ld9040_clear_error(ctx);
251
252	if (ret < 0)
253		ld9040_unprepare(panel);
254
255	return ret;
256}
257
258static int ld9040_enable(struct drm_panel *panel)
259{
260	return 0;
261}
262
263static int ld9040_get_modes(struct drm_panel *panel,
264			    struct drm_connector *connector)
265{
266	struct ld9040 *ctx = panel_to_ld9040(panel);
267	struct drm_display_mode *mode;
268
269	mode = drm_mode_create(connector->dev);
270	if (!mode) {
271		dev_err(panel->dev, "failed to create a new display mode\n");
272		return 0;
273	}
274
275	drm_display_mode_from_videomode(&ctx->vm, mode);
276	mode->width_mm = ctx->width_mm;
277	mode->height_mm = ctx->height_mm;
278	connector->display_info.width_mm = mode->width_mm;
279	connector->display_info.height_mm = mode->height_mm;
280
281	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
282	drm_mode_probed_add(connector, mode);
283
284	return 1;
285}
286
287static const struct drm_panel_funcs ld9040_drm_funcs = {
288	.disable = ld9040_disable,
289	.unprepare = ld9040_unprepare,
290	.prepare = ld9040_prepare,
291	.enable = ld9040_enable,
292	.get_modes = ld9040_get_modes,
293};
294
295static int ld9040_parse_dt(struct ld9040 *ctx)
296{
297	struct device *dev = ctx->dev;
298	struct device_node *np = dev->of_node;
299	int ret;
300
301	ret = of_get_videomode(np, &ctx->vm, 0);
302	if (ret < 0)
303		return ret;
304
305	of_property_read_u32(np, "power-on-delay", &ctx->power_on_delay);
306	of_property_read_u32(np, "reset-delay", &ctx->reset_delay);
307	of_property_read_u32(np, "panel-width-mm", &ctx->width_mm);
308	of_property_read_u32(np, "panel-height-mm", &ctx->height_mm);
309
310	return 0;
311}
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313static int ld9040_probe(struct spi_device *spi)
314{
 
315	struct device *dev = &spi->dev;
316	struct ld9040 *ctx;
317	int ret;
318
319	ctx = devm_kzalloc(dev, sizeof(struct ld9040), GFP_KERNEL);
320	if (!ctx)
321		return -ENOMEM;
322
323	spi_set_drvdata(spi, ctx);
324
325	ctx->dev = dev;
326	ctx->brightness = ARRAY_SIZE(ld9040_gammas) - 1;
327
328	ret = ld9040_parse_dt(ctx);
329	if (ret < 0)
330		return ret;
331
332	ctx->supplies[0].supply = "vdd3";
333	ctx->supplies[1].supply = "vci";
334	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
335				      ctx->supplies);
336	if (ret < 0)
337		return ret;
338
339	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
340	if (IS_ERR(ctx->reset_gpio)) {
341		dev_err(dev, "cannot get reset-gpios %ld\n",
342			PTR_ERR(ctx->reset_gpio));
343		return PTR_ERR(ctx->reset_gpio);
344	}
345
346	spi->bits_per_word = 9;
347	ret = spi_setup(spi);
348	if (ret < 0) {
349		dev_err(dev, "spi setup failed.\n");
350		return ret;
351	}
352
353	drm_panel_init(&ctx->panel, dev, &ld9040_drm_funcs,
354		       DRM_MODE_CONNECTOR_DPI);
355
 
 
 
 
 
 
356	drm_panel_add(&ctx->panel);
357
358	return 0;
359}
360
361static int ld9040_remove(struct spi_device *spi)
362{
363	struct ld9040 *ctx = spi_get_drvdata(spi);
364
365	ld9040_power_off(ctx);
366	drm_panel_remove(&ctx->panel);
367
368	return 0;
369}
370
371static const struct of_device_id ld9040_of_match[] = {
372	{ .compatible = "samsung,ld9040" },
373	{ }
374};
375MODULE_DEVICE_TABLE(of, ld9040_of_match);
376
377static const struct spi_device_id ld9040_ids[] = {
378	{ "ld9040", },
379	{ /* sentinel */ }
380};
381MODULE_DEVICE_TABLE(spi, ld9040_ids);
382
383static struct spi_driver ld9040_driver = {
384	.probe = ld9040_probe,
385	.remove = ld9040_remove,
386	.id_table = ld9040_ids,
387	.driver = {
388		.name = "panel-samsung-ld9040",
389		.of_match_table = ld9040_of_match,
390	},
391};
392module_spi_driver(ld9040_driver);
393
394MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
395MODULE_DESCRIPTION("ld9040 LCD Driver");
396MODULE_LICENSE("GPL v2");