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