1 /*
2 * MIPI DSI Bus
3 *
4 * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
5 * Andrzej Hajda <a.hajda@samsung.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #include <drm/drm_mipi_dsi.h>
29
30 #include <linux/device.h>
31 #include <linux/module.h>
32 #include <linux/of_device.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/slab.h>
35
36 #include <drm/drm_dsc.h>
37 #include <drm/drm_print.h>
38 #include <video/mipi_display.h>
39
40 /**
41 * DOC: dsi helpers
42 *
43 * These functions contain some common logic and helpers to deal with MIPI DSI
44 * peripherals.
45 *
46 * Helpers are provided for a number of standard MIPI DSI command as well as a
47 * subset of the MIPI DCS command set.
48 */
49
mipi_dsi_device_match(struct device * dev,struct device_driver * drv)50 static int mipi_dsi_device_match(struct device *dev, struct device_driver *drv)
51 {
52 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
53
54 /* attempt OF style match */
55 if (of_driver_match_device(dev, drv))
56 return 1;
57
58 /* compare DSI device and driver names */
59 if (!strcmp(dsi->name, drv->name))
60 return 1;
61
62 return 0;
63 }
64
mipi_dsi_uevent(struct device * dev,struct kobj_uevent_env * env)65 static int mipi_dsi_uevent(struct device *dev, struct kobj_uevent_env *env)
66 {
67 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
68 int err;
69
70 err = of_device_uevent_modalias(dev, env);
71 if (err != -ENODEV)
72 return err;
73
74 add_uevent_var(env, "MODALIAS=%s%s", MIPI_DSI_MODULE_PREFIX,
75 dsi->name);
76
77 return 0;
78 }
79
80 static const struct dev_pm_ops mipi_dsi_device_pm_ops = {
81 .runtime_suspend = pm_generic_runtime_suspend,
82 .runtime_resume = pm_generic_runtime_resume,
83 .suspend = pm_generic_suspend,
84 .resume = pm_generic_resume,
85 .freeze = pm_generic_freeze,
86 .thaw = pm_generic_thaw,
87 .poweroff = pm_generic_poweroff,
88 .restore = pm_generic_restore,
89 };
90
91 static struct bus_type mipi_dsi_bus_type = {
92 .name = "mipi-dsi",
93 .match = mipi_dsi_device_match,
94 .uevent = mipi_dsi_uevent,
95 .pm = &mipi_dsi_device_pm_ops,
96 };
97
98 /**
99 * of_find_mipi_dsi_device_by_node() - find the MIPI DSI device matching a
100 * device tree node
101 * @np: device tree node
102 *
103 * Return: A pointer to the MIPI DSI device corresponding to @np or NULL if no
104 * such device exists (or has not been registered yet).
105 */
of_find_mipi_dsi_device_by_node(struct device_node * np)106 struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np)
107 {
108 struct device *dev;
109
110 dev = bus_find_device_by_of_node(&mipi_dsi_bus_type, np);
111
112 return dev ? to_mipi_dsi_device(dev) : NULL;
113 }
114 EXPORT_SYMBOL(of_find_mipi_dsi_device_by_node);
115
mipi_dsi_dev_release(struct device * dev)116 static void mipi_dsi_dev_release(struct device *dev)
117 {
118 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
119
120 of_node_put(dev->of_node);
121 kfree(dsi);
122 }
123
124 static const struct device_type mipi_dsi_device_type = {
125 .release = mipi_dsi_dev_release,
126 };
127
mipi_dsi_device_alloc(struct mipi_dsi_host * host)128 static struct mipi_dsi_device *mipi_dsi_device_alloc(struct mipi_dsi_host *host)
129 {
130 struct mipi_dsi_device *dsi;
131
132 dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
133 if (!dsi)
134 return ERR_PTR(-ENOMEM);
135
136 dsi->host = host;
137 dsi->dev.bus = &mipi_dsi_bus_type;
138 dsi->dev.parent = host->dev;
139 dsi->dev.type = &mipi_dsi_device_type;
140
141 device_initialize(&dsi->dev);
142
143 return dsi;
144 }
145
mipi_dsi_device_add(struct mipi_dsi_device * dsi)146 static int mipi_dsi_device_add(struct mipi_dsi_device *dsi)
147 {
148 struct mipi_dsi_host *host = dsi->host;
149
150 dev_set_name(&dsi->dev, "%s.%d", dev_name(host->dev), dsi->channel);
151
152 return device_add(&dsi->dev);
153 }
154
155 #if IS_ENABLED(CONFIG_OF)
156 static struct mipi_dsi_device *
of_mipi_dsi_device_add(struct mipi_dsi_host * host,struct device_node * node)157 of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
158 {
159 struct mipi_dsi_device_info info = { };
160 int ret;
161 u32 reg;
162
163 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
164 drm_err(host, "modalias failure on %pOF\n", node);
165 return ERR_PTR(-EINVAL);
166 }
167
168 ret = of_property_read_u32(node, "reg", ®);
169 if (ret) {
170 drm_err(host, "device node %pOF has no valid reg property: %d\n",
171 node, ret);
172 return ERR_PTR(-EINVAL);
173 }
174
175 info.channel = reg;
176 info.node = of_node_get(node);
177
178 return mipi_dsi_device_register_full(host, &info);
179 }
180 #else
181 static struct mipi_dsi_device *
of_mipi_dsi_device_add(struct mipi_dsi_host * host,struct device_node * node)182 of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
183 {
184 return ERR_PTR(-ENODEV);
185 }
186 #endif
187
188 /**
189 * mipi_dsi_device_register_full - create a MIPI DSI device
190 * @host: DSI host to which this device is connected
191 * @info: pointer to template containing DSI device information
192 *
193 * Create a MIPI DSI device by using the device information provided by
194 * mipi_dsi_device_info template
195 *
196 * Returns:
197 * A pointer to the newly created MIPI DSI device, or, a pointer encoded
198 * with an error
199 */
200 struct mipi_dsi_device *
mipi_dsi_device_register_full(struct mipi_dsi_host * host,const struct mipi_dsi_device_info * info)201 mipi_dsi_device_register_full(struct mipi_dsi_host *host,
202 const struct mipi_dsi_device_info *info)
203 {
204 struct mipi_dsi_device *dsi;
205 int ret;
206
207 if (!info) {
208 drm_err(host, "invalid mipi_dsi_device_info pointer\n");
209 return ERR_PTR(-EINVAL);
210 }
211
212 if (info->channel > 3) {
213 drm_err(host, "invalid virtual channel: %u\n", info->channel);
214 return ERR_PTR(-EINVAL);
215 }
216
217 dsi = mipi_dsi_device_alloc(host);
218 if (IS_ERR(dsi)) {
219 drm_err(host, "failed to allocate DSI device %ld\n",
220 PTR_ERR(dsi));
221 return dsi;
222 }
223
224 dsi->dev.of_node = info->node;
225 dsi->channel = info->channel;
226 strlcpy(dsi->name, info->type, sizeof(dsi->name));
227
228 ret = mipi_dsi_device_add(dsi);
229 if (ret) {
230 drm_err(host, "failed to add DSI device %d\n", ret);
231 kfree(dsi);
232 return ERR_PTR(ret);
233 }
234
235 return dsi;
236 }
237 EXPORT_SYMBOL(mipi_dsi_device_register_full);
238
239 /**
240 * mipi_dsi_device_unregister - unregister MIPI DSI device
241 * @dsi: DSI peripheral device
242 */
mipi_dsi_device_unregister(struct mipi_dsi_device * dsi)243 void mipi_dsi_device_unregister(struct mipi_dsi_device *dsi)
244 {
245 device_unregister(&dsi->dev);
246 }
247 EXPORT_SYMBOL(mipi_dsi_device_unregister);
248
249 static DEFINE_MUTEX(host_lock);
250 static LIST_HEAD(host_list);
251
252 /**
253 * of_find_mipi_dsi_host_by_node() - find the MIPI DSI host matching a
254 * device tree node
255 * @node: device tree node
256 *
257 * Returns:
258 * A pointer to the MIPI DSI host corresponding to @node or NULL if no
259 * such device exists (or has not been registered yet).
260 */
of_find_mipi_dsi_host_by_node(struct device_node * node)261 struct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node)
262 {
263 struct mipi_dsi_host *host;
264
265 mutex_lock(&host_lock);
266
267 list_for_each_entry(host, &host_list, list) {
268 if (host->dev->of_node == node) {
269 mutex_unlock(&host_lock);
270 return host;
271 }
272 }
273
274 mutex_unlock(&host_lock);
275
276 return NULL;
277 }
278 EXPORT_SYMBOL(of_find_mipi_dsi_host_by_node);
279
mipi_dsi_host_register(struct mipi_dsi_host * host)280 int mipi_dsi_host_register(struct mipi_dsi_host *host)
281 {
282 struct device_node *node;
283
284 for_each_available_child_of_node(host->dev->of_node, node) {
285 /* skip nodes without reg property */
286 if (!of_find_property(node, "reg", NULL))
287 continue;
288 of_mipi_dsi_device_add(host, node);
289 }
290
291 mutex_lock(&host_lock);
292 list_add_tail(&host->list, &host_list);
293 mutex_unlock(&host_lock);
294
295 return 0;
296 }
297 EXPORT_SYMBOL(mipi_dsi_host_register);
298
mipi_dsi_remove_device_fn(struct device * dev,void * priv)299 static int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
300 {
301 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
302
303 mipi_dsi_detach(dsi);
304 mipi_dsi_device_unregister(dsi);
305
306 return 0;
307 }
308
mipi_dsi_host_unregister(struct mipi_dsi_host * host)309 void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
310 {
311 device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
312
313 mutex_lock(&host_lock);
314 list_del_init(&host->list);
315 mutex_unlock(&host_lock);
316 }
317 EXPORT_SYMBOL(mipi_dsi_host_unregister);
318
319 /**
320 * mipi_dsi_attach - attach a DSI device to its DSI host
321 * @dsi: DSI peripheral
322 */
mipi_dsi_attach(struct mipi_dsi_device * dsi)323 int mipi_dsi_attach(struct mipi_dsi_device *dsi)
324 {
325 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
326
327 if (!ops || !ops->attach)
328 return -ENOSYS;
329
330 return ops->attach(dsi->host, dsi);
331 }
332 EXPORT_SYMBOL(mipi_dsi_attach);
333
334 /**
335 * mipi_dsi_detach - detach a DSI device from its DSI host
336 * @dsi: DSI peripheral
337 */
mipi_dsi_detach(struct mipi_dsi_device * dsi)338 int mipi_dsi_detach(struct mipi_dsi_device *dsi)
339 {
340 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
341
342 if (!ops || !ops->detach)
343 return -ENOSYS;
344
345 return ops->detach(dsi->host, dsi);
346 }
347 EXPORT_SYMBOL(mipi_dsi_detach);
348
mipi_dsi_device_transfer(struct mipi_dsi_device * dsi,struct mipi_dsi_msg * msg)349 static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
350 struct mipi_dsi_msg *msg)
351 {
352 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
353
354 if (!ops || !ops->transfer)
355 return -ENOSYS;
356
357 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
358 msg->flags |= MIPI_DSI_MSG_USE_LPM;
359
360 return ops->transfer(dsi->host, msg);
361 }
362
363 /**
364 * mipi_dsi_packet_format_is_short - check if a packet is of the short format
365 * @type: MIPI DSI data type of the packet
366 *
367 * Return: true if the packet for the given data type is a short packet, false
368 * otherwise.
369 */
mipi_dsi_packet_format_is_short(u8 type)370 bool mipi_dsi_packet_format_is_short(u8 type)
371 {
372 switch (type) {
373 case MIPI_DSI_V_SYNC_START:
374 case MIPI_DSI_V_SYNC_END:
375 case MIPI_DSI_H_SYNC_START:
376 case MIPI_DSI_H_SYNC_END:
377 case MIPI_DSI_COMPRESSION_MODE:
378 case MIPI_DSI_END_OF_TRANSMISSION:
379 case MIPI_DSI_COLOR_MODE_OFF:
380 case MIPI_DSI_COLOR_MODE_ON:
381 case MIPI_DSI_SHUTDOWN_PERIPHERAL:
382 case MIPI_DSI_TURN_ON_PERIPHERAL:
383 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
384 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
385 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
386 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
387 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
388 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
389 case MIPI_DSI_DCS_SHORT_WRITE:
390 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
391 case MIPI_DSI_DCS_READ:
392 case MIPI_DSI_EXECUTE_QUEUE:
393 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
394 return true;
395 }
396
397 return false;
398 }
399 EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
400
401 /**
402 * mipi_dsi_packet_format_is_long - check if a packet is of the long format
403 * @type: MIPI DSI data type of the packet
404 *
405 * Return: true if the packet for the given data type is a long packet, false
406 * otherwise.
407 */
mipi_dsi_packet_format_is_long(u8 type)408 bool mipi_dsi_packet_format_is_long(u8 type)
409 {
410 switch (type) {
411 case MIPI_DSI_NULL_PACKET:
412 case MIPI_DSI_BLANKING_PACKET:
413 case MIPI_DSI_GENERIC_LONG_WRITE:
414 case MIPI_DSI_DCS_LONG_WRITE:
415 case MIPI_DSI_PICTURE_PARAMETER_SET:
416 case MIPI_DSI_COMPRESSED_PIXEL_STREAM:
417 case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
418 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
419 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
420 case MIPI_DSI_PACKED_PIXEL_STREAM_30:
421 case MIPI_DSI_PACKED_PIXEL_STREAM_36:
422 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
423 case MIPI_DSI_PACKED_PIXEL_STREAM_16:
424 case MIPI_DSI_PACKED_PIXEL_STREAM_18:
425 case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
426 case MIPI_DSI_PACKED_PIXEL_STREAM_24:
427 return true;
428 }
429
430 return false;
431 }
432 EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
433
434 /**
435 * mipi_dsi_create_packet - create a packet from a message according to the
436 * DSI protocol
437 * @packet: pointer to a DSI packet structure
438 * @msg: message to translate into a packet
439 *
440 * Return: 0 on success or a negative error code on failure.
441 */
mipi_dsi_create_packet(struct mipi_dsi_packet * packet,const struct mipi_dsi_msg * msg)442 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
443 const struct mipi_dsi_msg *msg)
444 {
445 if (!packet || !msg)
446 return -EINVAL;
447
448 /* do some minimum sanity checking */
449 if (!mipi_dsi_packet_format_is_short(msg->type) &&
450 !mipi_dsi_packet_format_is_long(msg->type))
451 return -EINVAL;
452
453 if (msg->channel > 3)
454 return -EINVAL;
455
456 memset(packet, 0, sizeof(*packet));
457 packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
458
459 /* TODO: compute ECC if hardware support is not available */
460
461 /*
462 * Long write packets contain the word count in header bytes 1 and 2.
463 * The payload follows the header and is word count bytes long.
464 *
465 * Short write packets encode up to two parameters in header bytes 1
466 * and 2.
467 */
468 if (mipi_dsi_packet_format_is_long(msg->type)) {
469 packet->header[1] = (msg->tx_len >> 0) & 0xff;
470 packet->header[2] = (msg->tx_len >> 8) & 0xff;
471
472 packet->payload_length = msg->tx_len;
473 packet->payload = msg->tx_buf;
474 } else {
475 const u8 *tx = msg->tx_buf;
476
477 packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
478 packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
479 }
480
481 packet->size = sizeof(packet->header) + packet->payload_length;
482
483 return 0;
484 }
485 EXPORT_SYMBOL(mipi_dsi_create_packet);
486
487 /**
488 * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
489 * @dsi: DSI peripheral device
490 *
491 * Return: 0 on success or a negative error code on failure.
492 */
mipi_dsi_shutdown_peripheral(struct mipi_dsi_device * dsi)493 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
494 {
495 struct mipi_dsi_msg msg = {
496 .channel = dsi->channel,
497 .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
498 .tx_buf = (u8 [2]) { 0, 0 },
499 .tx_len = 2,
500 };
501 int ret = mipi_dsi_device_transfer(dsi, &msg);
502
503 return (ret < 0) ? ret : 0;
504 }
505 EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
506
507 /**
508 * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
509 * @dsi: DSI peripheral device
510 *
511 * Return: 0 on success or a negative error code on failure.
512 */
mipi_dsi_turn_on_peripheral(struct mipi_dsi_device * dsi)513 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
514 {
515 struct mipi_dsi_msg msg = {
516 .channel = dsi->channel,
517 .type = MIPI_DSI_TURN_ON_PERIPHERAL,
518 .tx_buf = (u8 [2]) { 0, 0 },
519 .tx_len = 2,
520 };
521 int ret = mipi_dsi_device_transfer(dsi, &msg);
522
523 return (ret < 0) ? ret : 0;
524 }
525 EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
526
527 /*
528 * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
529 * the payload in a long packet transmitted from the peripheral back to the
530 * host processor
531 * @dsi: DSI peripheral device
532 * @value: the maximum size of the payload
533 *
534 * Return: 0 on success or a negative error code on failure.
535 */
mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device * dsi,u16 value)536 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
537 u16 value)
538 {
539 u8 tx[2] = { value & 0xff, value >> 8 };
540 struct mipi_dsi_msg msg = {
541 .channel = dsi->channel,
542 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
543 .tx_len = sizeof(tx),
544 .tx_buf = tx,
545 };
546 int ret = mipi_dsi_device_transfer(dsi, &msg);
547
548 return (ret < 0) ? ret : 0;
549 }
550 EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
551
552 /**
553 * mipi_dsi_compression_mode() - enable/disable DSC on the peripheral
554 * @dsi: DSI peripheral device
555 * @enable: Whether to enable or disable the DSC
556 *
557 * Enable or disable Display Stream Compression on the peripheral using the
558 * default Picture Parameter Set and VESA DSC 1.1 algorithm.
559 *
560 * Return: 0 on success or a negative error code on failure.
561 */
mipi_dsi_compression_mode(struct mipi_dsi_device * dsi,bool enable)562 ssize_t mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable)
563 {
564 /* Note: Needs updating for non-default PPS or algorithm */
565 u8 tx[2] = { enable << 0, 0 };
566 struct mipi_dsi_msg msg = {
567 .channel = dsi->channel,
568 .type = MIPI_DSI_COMPRESSION_MODE,
569 .tx_len = sizeof(tx),
570 .tx_buf = tx,
571 };
572 int ret = mipi_dsi_device_transfer(dsi, &msg);
573
574 return (ret < 0) ? ret : 0;
575 }
576 EXPORT_SYMBOL(mipi_dsi_compression_mode);
577
578 /**
579 * mipi_dsi_picture_parameter_set() - transmit the DSC PPS to the peripheral
580 * @dsi: DSI peripheral device
581 * @pps: VESA DSC 1.1 Picture Parameter Set
582 *
583 * Transmit the VESA DSC 1.1 Picture Parameter Set to the peripheral.
584 *
585 * Return: 0 on success or a negative error code on failure.
586 */
mipi_dsi_picture_parameter_set(struct mipi_dsi_device * dsi,const struct drm_dsc_picture_parameter_set * pps)587 ssize_t mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi,
588 const struct drm_dsc_picture_parameter_set *pps)
589 {
590 struct mipi_dsi_msg msg = {
591 .channel = dsi->channel,
592 .type = MIPI_DSI_PICTURE_PARAMETER_SET,
593 .tx_len = sizeof(*pps),
594 .tx_buf = pps,
595 };
596 int ret = mipi_dsi_device_transfer(dsi, &msg);
597
598 return (ret < 0) ? ret : 0;
599 }
600 EXPORT_SYMBOL(mipi_dsi_picture_parameter_set);
601
602 /**
603 * mipi_dsi_generic_write() - transmit data using a generic write packet
604 * @dsi: DSI peripheral device
605 * @payload: buffer containing the payload
606 * @size: size of payload buffer
607 *
608 * This function will automatically choose the right data type depending on
609 * the payload length.
610 *
611 * Return: The number of bytes transmitted on success or a negative error code
612 * on failure.
613 */
mipi_dsi_generic_write(struct mipi_dsi_device * dsi,const void * payload,size_t size)614 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
615 size_t size)
616 {
617 struct mipi_dsi_msg msg = {
618 .channel = dsi->channel,
619 .tx_buf = payload,
620 .tx_len = size
621 };
622
623 switch (size) {
624 case 0:
625 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
626 break;
627
628 case 1:
629 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
630 break;
631
632 case 2:
633 msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
634 break;
635
636 default:
637 msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
638 break;
639 }
640
641 return mipi_dsi_device_transfer(dsi, &msg);
642 }
643 EXPORT_SYMBOL(mipi_dsi_generic_write);
644
645 /**
646 * mipi_dsi_generic_read() - receive data using a generic read packet
647 * @dsi: DSI peripheral device
648 * @params: buffer containing the request parameters
649 * @num_params: number of request parameters
650 * @data: buffer in which to return the received data
651 * @size: size of receive buffer
652 *
653 * This function will automatically choose the right data type depending on
654 * the number of parameters passed in.
655 *
656 * Return: The number of bytes successfully read or a negative error code on
657 * failure.
658 */
mipi_dsi_generic_read(struct mipi_dsi_device * dsi,const void * params,size_t num_params,void * data,size_t size)659 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
660 size_t num_params, void *data, size_t size)
661 {
662 struct mipi_dsi_msg msg = {
663 .channel = dsi->channel,
664 .tx_len = num_params,
665 .tx_buf = params,
666 .rx_len = size,
667 .rx_buf = data
668 };
669
670 switch (num_params) {
671 case 0:
672 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
673 break;
674
675 case 1:
676 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
677 break;
678
679 case 2:
680 msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
681 break;
682
683 default:
684 return -EINVAL;
685 }
686
687 return mipi_dsi_device_transfer(dsi, &msg);
688 }
689 EXPORT_SYMBOL(mipi_dsi_generic_read);
690
691 /**
692 * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
693 * @dsi: DSI peripheral device
694 * @data: buffer containing data to be transmitted
695 * @len: size of transmission buffer
696 *
697 * This function will automatically choose the right data type depending on
698 * the command payload length.
699 *
700 * Return: The number of bytes successfully transmitted or a negative error
701 * code on failure.
702 */
mipi_dsi_dcs_write_buffer(struct mipi_dsi_device * dsi,const void * data,size_t len)703 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
704 const void *data, size_t len)
705 {
706 struct mipi_dsi_msg msg = {
707 .channel = dsi->channel,
708 .tx_buf = data,
709 .tx_len = len
710 };
711
712 switch (len) {
713 case 0:
714 return -EINVAL;
715
716 case 1:
717 msg.type = MIPI_DSI_DCS_SHORT_WRITE;
718 break;
719
720 case 2:
721 msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
722 break;
723
724 default:
725 msg.type = MIPI_DSI_DCS_LONG_WRITE;
726 break;
727 }
728
729 return mipi_dsi_device_transfer(dsi, &msg);
730 }
731 EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
732
733 /**
734 * mipi_dsi_dcs_write() - send DCS write command
735 * @dsi: DSI peripheral device
736 * @cmd: DCS command
737 * @data: buffer containing the command payload
738 * @len: command payload length
739 *
740 * This function will automatically choose the right data type depending on
741 * the command payload length.
742 *
743 * Return: The number of bytes successfully transmitted or a negative error
744 * code on failure.
745 */
mipi_dsi_dcs_write(struct mipi_dsi_device * dsi,u8 cmd,const void * data,size_t len)746 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
747 const void *data, size_t len)
748 {
749 ssize_t err;
750 size_t size;
751 u8 stack_tx[8];
752 u8 *tx;
753
754 size = 1 + len;
755 if (len > ARRAY_SIZE(stack_tx) - 1) {
756 tx = kmalloc(size, GFP_KERNEL);
757 if (!tx)
758 return -ENOMEM;
759 } else {
760 tx = stack_tx;
761 }
762
763 /* concatenate the DCS command byte and the payload */
764 tx[0] = cmd;
765 if (data)
766 memcpy(&tx[1], data, len);
767
768 err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
769
770 if (tx != stack_tx)
771 kfree(tx);
772
773 return err;
774 }
775 EXPORT_SYMBOL(mipi_dsi_dcs_write);
776
777 /**
778 * mipi_dsi_dcs_read() - send DCS read request command
779 * @dsi: DSI peripheral device
780 * @cmd: DCS command
781 * @data: buffer in which to receive data
782 * @len: size of receive buffer
783 *
784 * Return: The number of bytes read or a negative error code on failure.
785 */
mipi_dsi_dcs_read(struct mipi_dsi_device * dsi,u8 cmd,void * data,size_t len)786 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
787 size_t len)
788 {
789 struct mipi_dsi_msg msg = {
790 .channel = dsi->channel,
791 .type = MIPI_DSI_DCS_READ,
792 .tx_buf = &cmd,
793 .tx_len = 1,
794 .rx_buf = data,
795 .rx_len = len
796 };
797
798 return mipi_dsi_device_transfer(dsi, &msg);
799 }
800 EXPORT_SYMBOL(mipi_dsi_dcs_read);
801
802 /**
803 * mipi_dsi_dcs_nop() - send DCS nop packet
804 * @dsi: DSI peripheral device
805 *
806 * Return: 0 on success or a negative error code on failure.
807 */
mipi_dsi_dcs_nop(struct mipi_dsi_device * dsi)808 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
809 {
810 ssize_t err;
811
812 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
813 if (err < 0)
814 return err;
815
816 return 0;
817 }
818 EXPORT_SYMBOL(mipi_dsi_dcs_nop);
819
820 /**
821 * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
822 * @dsi: DSI peripheral device
823 *
824 * Return: 0 on success or a negative error code on failure.
825 */
mipi_dsi_dcs_soft_reset(struct mipi_dsi_device * dsi)826 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
827 {
828 ssize_t err;
829
830 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
831 if (err < 0)
832 return err;
833
834 return 0;
835 }
836 EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
837
838 /**
839 * mipi_dsi_dcs_get_power_mode() - query the display module's current power
840 * mode
841 * @dsi: DSI peripheral device
842 * @mode: return location for the current power mode
843 *
844 * Return: 0 on success or a negative error code on failure.
845 */
mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device * dsi,u8 * mode)846 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
847 {
848 ssize_t err;
849
850 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
851 sizeof(*mode));
852 if (err <= 0) {
853 if (err == 0)
854 err = -ENODATA;
855
856 return err;
857 }
858
859 return 0;
860 }
861 EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
862
863 /**
864 * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
865 * data used by the interface
866 * @dsi: DSI peripheral device
867 * @format: return location for the pixel format
868 *
869 * Return: 0 on success or a negative error code on failure.
870 */
mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device * dsi,u8 * format)871 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
872 {
873 ssize_t err;
874
875 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
876 sizeof(*format));
877 if (err <= 0) {
878 if (err == 0)
879 err = -ENODATA;
880
881 return err;
882 }
883
884 return 0;
885 }
886 EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
887
888 /**
889 * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
890 * display module except interface communication
891 * @dsi: DSI peripheral device
892 *
893 * Return: 0 on success or a negative error code on failure.
894 */
mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device * dsi)895 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
896 {
897 ssize_t err;
898
899 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
900 if (err < 0)
901 return err;
902
903 return 0;
904 }
905 EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
906
907 /**
908 * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
909 * module
910 * @dsi: DSI peripheral device
911 *
912 * Return: 0 on success or a negative error code on failure.
913 */
mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device * dsi)914 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
915 {
916 ssize_t err;
917
918 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
919 if (err < 0)
920 return err;
921
922 return 0;
923 }
924 EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
925
926 /**
927 * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
928 * display device
929 * @dsi: DSI peripheral device
930 *
931 * Return: 0 on success or a negative error code on failure.
932 */
mipi_dsi_dcs_set_display_off(struct mipi_dsi_device * dsi)933 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
934 {
935 ssize_t err;
936
937 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
938 if (err < 0)
939 return err;
940
941 return 0;
942 }
943 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
944
945 /**
946 * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
947 * display device
948 * @dsi: DSI peripheral device
949 *
950 * Return: 0 on success or a negative error code on failure
951 */
mipi_dsi_dcs_set_display_on(struct mipi_dsi_device * dsi)952 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
953 {
954 ssize_t err;
955
956 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
957 if (err < 0)
958 return err;
959
960 return 0;
961 }
962 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
963
964 /**
965 * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
966 * memory accessed by the host processor
967 * @dsi: DSI peripheral device
968 * @start: first column of frame memory
969 * @end: last column of frame memory
970 *
971 * Return: 0 on success or a negative error code on failure.
972 */
mipi_dsi_dcs_set_column_address(struct mipi_dsi_device * dsi,u16 start,u16 end)973 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
974 u16 end)
975 {
976 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
977 ssize_t err;
978
979 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
980 sizeof(payload));
981 if (err < 0)
982 return err;
983
984 return 0;
985 }
986 EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
987
988 /**
989 * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
990 * memory accessed by the host processor
991 * @dsi: DSI peripheral device
992 * @start: first page of frame memory
993 * @end: last page of frame memory
994 *
995 * Return: 0 on success or a negative error code on failure.
996 */
mipi_dsi_dcs_set_page_address(struct mipi_dsi_device * dsi,u16 start,u16 end)997 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
998 u16 end)
999 {
1000 u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
1001 ssize_t err;
1002
1003 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
1004 sizeof(payload));
1005 if (err < 0)
1006 return err;
1007
1008 return 0;
1009 }
1010 EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
1011
1012 /**
1013 * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
1014 * output signal on the TE signal line
1015 * @dsi: DSI peripheral device
1016 *
1017 * Return: 0 on success or a negative error code on failure
1018 */
mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device * dsi)1019 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
1020 {
1021 ssize_t err;
1022
1023 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
1024 if (err < 0)
1025 return err;
1026
1027 return 0;
1028 }
1029 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
1030
1031 /**
1032 * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
1033 * output signal on the TE signal line.
1034 * @dsi: DSI peripheral device
1035 * @mode: the Tearing Effect Output Line mode
1036 *
1037 * Return: 0 on success or a negative error code on failure
1038 */
mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device * dsi,enum mipi_dsi_dcs_tear_mode mode)1039 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
1040 enum mipi_dsi_dcs_tear_mode mode)
1041 {
1042 u8 value = mode;
1043 ssize_t err;
1044
1045 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
1046 sizeof(value));
1047 if (err < 0)
1048 return err;
1049
1050 return 0;
1051 }
1052 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
1053
1054 /**
1055 * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
1056 * data used by the interface
1057 * @dsi: DSI peripheral device
1058 * @format: pixel format
1059 *
1060 * Return: 0 on success or a negative error code on failure.
1061 */
mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device * dsi,u8 format)1062 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
1063 {
1064 ssize_t err;
1065
1066 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
1067 sizeof(format));
1068 if (err < 0)
1069 return err;
1070
1071 return 0;
1072 }
1073 EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
1074
1075 /**
1076 * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
1077 * the Tearing Effect output signal of the display module
1078 * @dsi: DSI peripheral device
1079 * @scanline: scanline to use as trigger
1080 *
1081 * Return: 0 on success or a negative error code on failure
1082 */
mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device * dsi,u16 scanline)1083 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
1084 {
1085 u8 payload[2] = { scanline >> 8, scanline & 0xff };
1086 ssize_t err;
1087
1088 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_SCANLINE, payload,
1089 sizeof(payload));
1090 if (err < 0)
1091 return err;
1092
1093 return 0;
1094 }
1095 EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
1096
1097 /**
1098 * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
1099 * display
1100 * @dsi: DSI peripheral device
1101 * @brightness: brightness value
1102 *
1103 * Return: 0 on success or a negative error code on failure.
1104 */
mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device * dsi,u16 brightness)1105 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
1106 u16 brightness)
1107 {
1108 u8 payload[2] = { brightness & 0xff, brightness >> 8 };
1109 ssize_t err;
1110
1111 err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
1112 payload, sizeof(payload));
1113 if (err < 0)
1114 return err;
1115
1116 return 0;
1117 }
1118 EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
1119
1120 /**
1121 * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
1122 * of the display
1123 * @dsi: DSI peripheral device
1124 * @brightness: brightness value
1125 *
1126 * Return: 0 on success or a negative error code on failure.
1127 */
mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device * dsi,u16 * brightness)1128 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
1129 u16 *brightness)
1130 {
1131 ssize_t err;
1132
1133 err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
1134 brightness, sizeof(*brightness));
1135 if (err <= 0) {
1136 if (err == 0)
1137 err = -ENODATA;
1138
1139 return err;
1140 }
1141
1142 return 0;
1143 }
1144 EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);
1145
mipi_dsi_drv_probe(struct device * dev)1146 static int mipi_dsi_drv_probe(struct device *dev)
1147 {
1148 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1149 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1150
1151 return drv->probe(dsi);
1152 }
1153
mipi_dsi_drv_remove(struct device * dev)1154 static int mipi_dsi_drv_remove(struct device *dev)
1155 {
1156 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1157 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1158
1159 return drv->remove(dsi);
1160 }
1161
mipi_dsi_drv_shutdown(struct device * dev)1162 static void mipi_dsi_drv_shutdown(struct device *dev)
1163 {
1164 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
1165 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1166
1167 drv->shutdown(dsi);
1168 }
1169
1170 /**
1171 * mipi_dsi_driver_register_full() - register a driver for DSI devices
1172 * @drv: DSI driver structure
1173 * @owner: owner module
1174 *
1175 * Return: 0 on success or a negative error code on failure.
1176 */
mipi_dsi_driver_register_full(struct mipi_dsi_driver * drv,struct module * owner)1177 int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
1178 struct module *owner)
1179 {
1180 drv->driver.bus = &mipi_dsi_bus_type;
1181 drv->driver.owner = owner;
1182
1183 if (drv->probe)
1184 drv->driver.probe = mipi_dsi_drv_probe;
1185 if (drv->remove)
1186 drv->driver.remove = mipi_dsi_drv_remove;
1187 if (drv->shutdown)
1188 drv->driver.shutdown = mipi_dsi_drv_shutdown;
1189
1190 return driver_register(&drv->driver);
1191 }
1192 EXPORT_SYMBOL(mipi_dsi_driver_register_full);
1193
1194 /**
1195 * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
1196 * @drv: DSI driver structure
1197 *
1198 * Return: 0 on success or a negative error code on failure.
1199 */
mipi_dsi_driver_unregister(struct mipi_dsi_driver * drv)1200 void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
1201 {
1202 driver_unregister(&drv->driver);
1203 }
1204 EXPORT_SYMBOL(mipi_dsi_driver_unregister);
1205
mipi_dsi_bus_init(void)1206 static int __init mipi_dsi_bus_init(void)
1207 {
1208 return bus_register(&mipi_dsi_bus_type);
1209 }
1210 postcore_initcall(mipi_dsi_bus_init);
1211
1212 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1213 MODULE_DESCRIPTION("MIPI DSI Bus");
1214 MODULE_LICENSE("GPL and additional rights");
1215