blob: 44ff665f216f924694a8a434136873be23f3706a [file] [log] [blame]
Andrzej Hajda068a0022013-12-04 16:35:12 +01001/*
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 <video/mipi_display.h>
37
38static int mipi_dsi_device_match(struct device *dev, struct device_driver *drv)
39{
40 return of_driver_match_device(dev, drv);
41}
42
43static const struct dev_pm_ops mipi_dsi_device_pm_ops = {
44 .runtime_suspend = pm_generic_runtime_suspend,
45 .runtime_resume = pm_generic_runtime_resume,
46 .suspend = pm_generic_suspend,
47 .resume = pm_generic_resume,
48 .freeze = pm_generic_freeze,
49 .thaw = pm_generic_thaw,
50 .poweroff = pm_generic_poweroff,
51 .restore = pm_generic_restore,
52};
53
54static struct bus_type mipi_dsi_bus_type = {
55 .name = "mipi-dsi",
56 .match = mipi_dsi_device_match,
57 .pm = &mipi_dsi_device_pm_ops,
58};
59
60static void mipi_dsi_dev_release(struct device *dev)
61{
62 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
63
64 of_node_put(dev->of_node);
65 kfree(dsi);
66}
67
68static const struct device_type mipi_dsi_device_type = {
69 .release = mipi_dsi_dev_release,
70};
71
72static struct mipi_dsi_device *mipi_dsi_device_alloc(struct mipi_dsi_host *host)
73{
74 struct mipi_dsi_device *dsi;
75
76 dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
77 if (!dsi)
78 return ERR_PTR(-ENOMEM);
79
80 dsi->host = host;
81 dsi->dev.bus = &mipi_dsi_bus_type;
82 dsi->dev.parent = host->dev;
83 dsi->dev.type = &mipi_dsi_device_type;
84
85 device_initialize(&dsi->dev);
86
87 return dsi;
88}
89
90static int mipi_dsi_device_add(struct mipi_dsi_device *dsi)
91{
92 struct mipi_dsi_host *host = dsi->host;
93
94 dev_set_name(&dsi->dev, "%s.%d", dev_name(host->dev), dsi->channel);
95
96 return device_add(&dsi->dev);
97}
98
99static struct mipi_dsi_device *
100of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
101{
102 struct mipi_dsi_device *dsi;
103 struct device *dev = host->dev;
104 int ret;
105 u32 reg;
106
107 ret = of_property_read_u32(node, "reg", &reg);
108 if (ret) {
109 dev_err(dev, "device node %s has no valid reg property: %d\n",
110 node->full_name, ret);
111 return ERR_PTR(-EINVAL);
112 }
113
114 if (reg > 3) {
115 dev_err(dev, "device node %s has invalid reg property: %u\n",
116 node->full_name, reg);
117 return ERR_PTR(-EINVAL);
118 }
119
120 dsi = mipi_dsi_device_alloc(host);
121 if (IS_ERR(dsi)) {
122 dev_err(dev, "failed to allocate DSI device %s: %ld\n",
123 node->full_name, PTR_ERR(dsi));
124 return dsi;
125 }
126
127 dsi->dev.of_node = of_node_get(node);
128 dsi->channel = reg;
129
130 ret = mipi_dsi_device_add(dsi);
131 if (ret) {
132 dev_err(dev, "failed to add DSI device %s: %d\n",
133 node->full_name, ret);
134 kfree(dsi);
135 return ERR_PTR(ret);
136 }
137
138 return dsi;
139}
140
141int mipi_dsi_host_register(struct mipi_dsi_host *host)
142{
143 struct device_node *node;
144
Andrzej Hajdae49640d2014-03-28 12:52:37 +0100145 for_each_available_child_of_node(host->dev->of_node, node) {
146 /* skip nodes without reg property */
147 if (!of_find_property(node, "reg", NULL))
148 continue;
Andrzej Hajda068a0022013-12-04 16:35:12 +0100149 of_mipi_dsi_device_add(host, node);
Andrzej Hajdae49640d2014-03-28 12:52:37 +0100150 }
Andrzej Hajda068a0022013-12-04 16:35:12 +0100151
152 return 0;
153}
154EXPORT_SYMBOL(mipi_dsi_host_register);
155
156static int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
157{
158 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
159
160 device_unregister(&dsi->dev);
161
162 return 0;
163}
164
165void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
166{
167 device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
168}
169EXPORT_SYMBOL(mipi_dsi_host_unregister);
170
171/**
172 * mipi_dsi_attach - attach a DSI device to its DSI host
173 * @dsi: DSI peripheral
174 */
175int mipi_dsi_attach(struct mipi_dsi_device *dsi)
176{
177 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
178
179 if (!ops || !ops->attach)
180 return -ENOSYS;
181
182 return ops->attach(dsi->host, dsi);
183}
184EXPORT_SYMBOL(mipi_dsi_attach);
185
186/**
187 * mipi_dsi_detach - detach a DSI device from its DSI host
188 * @dsi: DSI peripheral
189 */
190int mipi_dsi_detach(struct mipi_dsi_device *dsi)
191{
192 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
193
194 if (!ops || !ops->detach)
195 return -ENOSYS;
196
197 return ops->detach(dsi->host, dsi);
198}
199EXPORT_SYMBOL(mipi_dsi_detach);
200
201/**
Thierry Reding02acb762014-11-04 14:59:14 +0100202 * mipi_dsi_packet_format_is_short - check if a packet is of the short format
203 * @type: MIPI DSI data type of the packet
204 *
205 * Return: true if the packet for the given data type is a short packet, false
206 * otherwise.
207 */
208bool mipi_dsi_packet_format_is_short(u8 type)
209{
210 switch (type) {
211 case MIPI_DSI_V_SYNC_START:
212 case MIPI_DSI_V_SYNC_END:
213 case MIPI_DSI_H_SYNC_START:
214 case MIPI_DSI_H_SYNC_END:
215 case MIPI_DSI_END_OF_TRANSMISSION:
216 case MIPI_DSI_COLOR_MODE_OFF:
217 case MIPI_DSI_COLOR_MODE_ON:
218 case MIPI_DSI_SHUTDOWN_PERIPHERAL:
219 case MIPI_DSI_TURN_ON_PERIPHERAL:
220 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
221 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
222 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
223 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
224 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
225 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
226 case MIPI_DSI_DCS_SHORT_WRITE:
227 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
228 case MIPI_DSI_DCS_READ:
229 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
230 return true;
231 }
232
233 return false;
234}
235EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
236
237/**
238 * mipi_dsi_packet_format_is_long - check if a packet is of the long format
239 * @type: MIPI DSI data type of the packet
240 *
241 * Return: true if the packet for the given data type is a long packet, false
242 * otherwise.
243 */
244bool mipi_dsi_packet_format_is_long(u8 type)
245{
246 switch (type) {
247 case MIPI_DSI_NULL_PACKET:
248 case MIPI_DSI_BLANKING_PACKET:
249 case MIPI_DSI_GENERIC_LONG_WRITE:
250 case MIPI_DSI_DCS_LONG_WRITE:
251 case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
252 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
253 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
254 case MIPI_DSI_PACKED_PIXEL_STREAM_30:
255 case MIPI_DSI_PACKED_PIXEL_STREAM_36:
256 case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
257 case MIPI_DSI_PACKED_PIXEL_STREAM_16:
258 case MIPI_DSI_PACKED_PIXEL_STREAM_18:
259 case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
260 case MIPI_DSI_PACKED_PIXEL_STREAM_24:
261 return true;
262 }
263
264 return false;
265}
266EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
267
268/**
Thierry Redinga52879e2014-10-16 13:44:02 +0200269 * mipi_dsi_create_packet - create a packet from a message according to the
270 * DSI protocol
271 * @packet: pointer to a DSI packet structure
272 * @msg: message to translate into a packet
273 *
274 * Return: 0 on success or a negative error code on failure.
275 */
276int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
277 const struct mipi_dsi_msg *msg)
278{
279 const u8 *tx = msg->tx_buf;
280
281 if (!packet || !msg)
282 return -EINVAL;
283
284 /* do some minimum sanity checking */
285 if (!mipi_dsi_packet_format_is_short(msg->type) &&
286 !mipi_dsi_packet_format_is_long(msg->type))
287 return -EINVAL;
288
289 if (msg->channel > 3)
290 return -EINVAL;
291
292 memset(packet, 0, sizeof(*packet));
293 packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
294
295 /* TODO: compute ECC if hardware support is not available */
296
297 /*
298 * Long write packets contain the word count in header bytes 1 and 2.
299 * The payload follows the header and is word count bytes long.
300 *
301 * Short write packets encode up to two parameters in header bytes 1
302 * and 2.
303 */
304 if (mipi_dsi_packet_format_is_long(msg->type)) {
305 packet->header[1] = (msg->tx_len >> 0) & 0xff;
306 packet->header[2] = (msg->tx_len >> 8) & 0xff;
307
308 packet->payload_length = msg->tx_len;
309 packet->payload = tx;
310 } else {
311 packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
312 packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
313 }
314
315 packet->size = sizeof(packet->header) + packet->payload_length;
316
317 return 0;
318}
319EXPORT_SYMBOL(mipi_dsi_create_packet);
320
321/**
Andrzej Hajda068a0022013-12-04 16:35:12 +0100322 * mipi_dsi_dcs_write - send DCS write command
323 * @dsi: DSI device
Andrzej Hajda068a0022013-12-04 16:35:12 +0100324 * @data: pointer to the command followed by parameters
325 * @len: length of @data
326 */
Thierry Reding3c523d72014-07-21 12:28:25 +0200327ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, const void *data,
328 size_t len)
Andrzej Hajda068a0022013-12-04 16:35:12 +0100329{
330 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
331 struct mipi_dsi_msg msg = {
Thierry Reding3c523d72014-07-21 12:28:25 +0200332 .channel = dsi->channel,
Andrzej Hajda068a0022013-12-04 16:35:12 +0100333 .tx_buf = data,
334 .tx_len = len
335 };
336
337 if (!ops || !ops->transfer)
338 return -ENOSYS;
339
340 switch (len) {
341 case 0:
342 return -EINVAL;
343 case 1:
344 msg.type = MIPI_DSI_DCS_SHORT_WRITE;
345 break;
346 case 2:
347 msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
348 break;
349 default:
350 msg.type = MIPI_DSI_DCS_LONG_WRITE;
351 break;
352 }
353
Inki Daed87f09a2014-08-13 16:38:23 +0900354 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
355 msg.flags = MIPI_DSI_MSG_USE_LPM;
356
Andrzej Hajda068a0022013-12-04 16:35:12 +0100357 return ops->transfer(dsi->host, &msg);
358}
359EXPORT_SYMBOL(mipi_dsi_dcs_write);
360
361/**
362 * mipi_dsi_dcs_read - send DCS read request command
363 * @dsi: DSI device
Andrzej Hajda068a0022013-12-04 16:35:12 +0100364 * @cmd: DCS read command
365 * @data: pointer to read buffer
366 * @len: length of @data
367 *
368 * Function returns number of read bytes or error code.
369 */
Thierry Reding3c523d72014-07-21 12:28:25 +0200370ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
371 size_t len)
Andrzej Hajda068a0022013-12-04 16:35:12 +0100372{
373 const struct mipi_dsi_host_ops *ops = dsi->host->ops;
374 struct mipi_dsi_msg msg = {
Thierry Reding3c523d72014-07-21 12:28:25 +0200375 .channel = dsi->channel,
Andrzej Hajda068a0022013-12-04 16:35:12 +0100376 .type = MIPI_DSI_DCS_READ,
377 .tx_buf = &cmd,
378 .tx_len = 1,
379 .rx_buf = data,
380 .rx_len = len
381 };
382
383 if (!ops || !ops->transfer)
384 return -ENOSYS;
385
Inki Daed87f09a2014-08-13 16:38:23 +0900386 if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
387 msg.flags = MIPI_DSI_MSG_USE_LPM;
388
Andrzej Hajda068a0022013-12-04 16:35:12 +0100389 return ops->transfer(dsi->host, &msg);
390}
391EXPORT_SYMBOL(mipi_dsi_dcs_read);
392
393static int mipi_dsi_drv_probe(struct device *dev)
394{
395 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
396 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
397
398 return drv->probe(dsi);
399}
400
401static int mipi_dsi_drv_remove(struct device *dev)
402{
403 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
404 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
405
406 return drv->remove(dsi);
407}
408
Thierry Redingd1621802014-04-29 17:19:57 +0200409static void mipi_dsi_drv_shutdown(struct device *dev)
410{
411 struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
412 struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
413
414 drv->shutdown(dsi);
415}
416
Andrzej Hajda068a0022013-12-04 16:35:12 +0100417/**
418 * mipi_dsi_driver_register - register a driver for DSI devices
419 * @drv: DSI driver structure
420 */
421int mipi_dsi_driver_register(struct mipi_dsi_driver *drv)
422{
423 drv->driver.bus = &mipi_dsi_bus_type;
424 if (drv->probe)
425 drv->driver.probe = mipi_dsi_drv_probe;
426 if (drv->remove)
427 drv->driver.remove = mipi_dsi_drv_remove;
Thierry Redingd1621802014-04-29 17:19:57 +0200428 if (drv->shutdown)
429 drv->driver.shutdown = mipi_dsi_drv_shutdown;
Andrzej Hajda068a0022013-12-04 16:35:12 +0100430
431 return driver_register(&drv->driver);
432}
433EXPORT_SYMBOL(mipi_dsi_driver_register);
434
435/**
436 * mipi_dsi_driver_unregister - unregister a driver for DSI devices
437 * @drv: DSI driver structure
438 */
439void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
440{
441 driver_unregister(&drv->driver);
442}
443EXPORT_SYMBOL(mipi_dsi_driver_unregister);
444
445static int __init mipi_dsi_bus_init(void)
446{
447 return bus_register(&mipi_dsi_bus_type);
448}
449postcore_initcall(mipi_dsi_bus_init);
450
451MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
452MODULE_DESCRIPTION("MIPI DSI Bus");
453MODULE_LICENSE("GPL and additional rights");