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