Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 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 <video/mipi_display.h> |
| 37 | |
Thierry Reding | 009081e | 2014-08-05 10:41:13 +0200 | [diff] [blame] | 38 | /** |
| 39 | * DOC: dsi helpers |
| 40 | * |
| 41 | * These functions contain some common logic and helpers to deal with MIPI DSI |
| 42 | * peripherals. |
| 43 | * |
| 44 | * Helpers are provided for a number of standard MIPI DSI command as well as a |
| 45 | * subset of the MIPI DCS command set. |
| 46 | */ |
| 47 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 48 | static int mipi_dsi_device_match(struct device *dev, struct device_driver *drv) |
| 49 | { |
| 50 | return of_driver_match_device(dev, drv); |
| 51 | } |
| 52 | |
| 53 | static const struct dev_pm_ops mipi_dsi_device_pm_ops = { |
| 54 | .runtime_suspend = pm_generic_runtime_suspend, |
| 55 | .runtime_resume = pm_generic_runtime_resume, |
| 56 | .suspend = pm_generic_suspend, |
| 57 | .resume = pm_generic_resume, |
| 58 | .freeze = pm_generic_freeze, |
| 59 | .thaw = pm_generic_thaw, |
| 60 | .poweroff = pm_generic_poweroff, |
| 61 | .restore = pm_generic_restore, |
| 62 | }; |
| 63 | |
| 64 | static struct bus_type mipi_dsi_bus_type = { |
| 65 | .name = "mipi-dsi", |
| 66 | .match = mipi_dsi_device_match, |
| 67 | .pm = &mipi_dsi_device_pm_ops, |
| 68 | }; |
| 69 | |
| 70 | static void mipi_dsi_dev_release(struct device *dev) |
| 71 | { |
| 72 | struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev); |
| 73 | |
| 74 | of_node_put(dev->of_node); |
| 75 | kfree(dsi); |
| 76 | } |
| 77 | |
| 78 | static const struct device_type mipi_dsi_device_type = { |
| 79 | .release = mipi_dsi_dev_release, |
| 80 | }; |
| 81 | |
| 82 | static struct mipi_dsi_device *mipi_dsi_device_alloc(struct mipi_dsi_host *host) |
| 83 | { |
| 84 | struct mipi_dsi_device *dsi; |
| 85 | |
| 86 | dsi = kzalloc(sizeof(*dsi), GFP_KERNEL); |
| 87 | if (!dsi) |
| 88 | return ERR_PTR(-ENOMEM); |
| 89 | |
| 90 | dsi->host = host; |
| 91 | dsi->dev.bus = &mipi_dsi_bus_type; |
| 92 | dsi->dev.parent = host->dev; |
| 93 | dsi->dev.type = &mipi_dsi_device_type; |
| 94 | |
| 95 | device_initialize(&dsi->dev); |
| 96 | |
| 97 | return dsi; |
| 98 | } |
| 99 | |
| 100 | static int mipi_dsi_device_add(struct mipi_dsi_device *dsi) |
| 101 | { |
| 102 | struct mipi_dsi_host *host = dsi->host; |
| 103 | |
| 104 | dev_set_name(&dsi->dev, "%s.%d", dev_name(host->dev), dsi->channel); |
| 105 | |
| 106 | return device_add(&dsi->dev); |
| 107 | } |
| 108 | |
| 109 | static struct mipi_dsi_device * |
| 110 | of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node) |
| 111 | { |
| 112 | struct mipi_dsi_device *dsi; |
| 113 | struct device *dev = host->dev; |
| 114 | int ret; |
| 115 | u32 reg; |
| 116 | |
| 117 | ret = of_property_read_u32(node, "reg", ®); |
| 118 | if (ret) { |
| 119 | dev_err(dev, "device node %s has no valid reg property: %d\n", |
| 120 | node->full_name, ret); |
| 121 | return ERR_PTR(-EINVAL); |
| 122 | } |
| 123 | |
| 124 | if (reg > 3) { |
| 125 | dev_err(dev, "device node %s has invalid reg property: %u\n", |
| 126 | node->full_name, reg); |
| 127 | return ERR_PTR(-EINVAL); |
| 128 | } |
| 129 | |
| 130 | dsi = mipi_dsi_device_alloc(host); |
| 131 | if (IS_ERR(dsi)) { |
| 132 | dev_err(dev, "failed to allocate DSI device %s: %ld\n", |
| 133 | node->full_name, PTR_ERR(dsi)); |
| 134 | return dsi; |
| 135 | } |
| 136 | |
| 137 | dsi->dev.of_node = of_node_get(node); |
| 138 | dsi->channel = reg; |
| 139 | |
| 140 | ret = mipi_dsi_device_add(dsi); |
| 141 | if (ret) { |
| 142 | dev_err(dev, "failed to add DSI device %s: %d\n", |
| 143 | node->full_name, ret); |
| 144 | kfree(dsi); |
| 145 | return ERR_PTR(ret); |
| 146 | } |
| 147 | |
| 148 | return dsi; |
| 149 | } |
| 150 | |
| 151 | int mipi_dsi_host_register(struct mipi_dsi_host *host) |
| 152 | { |
| 153 | struct device_node *node; |
| 154 | |
Andrzej Hajda | e49640d | 2014-03-28 12:52:37 +0100 | [diff] [blame] | 155 | for_each_available_child_of_node(host->dev->of_node, node) { |
| 156 | /* skip nodes without reg property */ |
| 157 | if (!of_find_property(node, "reg", NULL)) |
| 158 | continue; |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 159 | of_mipi_dsi_device_add(host, node); |
Andrzej Hajda | e49640d | 2014-03-28 12:52:37 +0100 | [diff] [blame] | 160 | } |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | EXPORT_SYMBOL(mipi_dsi_host_register); |
| 165 | |
| 166 | static int mipi_dsi_remove_device_fn(struct device *dev, void *priv) |
| 167 | { |
| 168 | struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev); |
| 169 | |
| 170 | device_unregister(&dsi->dev); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | void mipi_dsi_host_unregister(struct mipi_dsi_host *host) |
| 176 | { |
| 177 | device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn); |
| 178 | } |
| 179 | EXPORT_SYMBOL(mipi_dsi_host_unregister); |
| 180 | |
| 181 | /** |
| 182 | * mipi_dsi_attach - attach a DSI device to its DSI host |
| 183 | * @dsi: DSI peripheral |
| 184 | */ |
| 185 | int mipi_dsi_attach(struct mipi_dsi_device *dsi) |
| 186 | { |
| 187 | const struct mipi_dsi_host_ops *ops = dsi->host->ops; |
| 188 | |
| 189 | if (!ops || !ops->attach) |
| 190 | return -ENOSYS; |
| 191 | |
| 192 | return ops->attach(dsi->host, dsi); |
| 193 | } |
| 194 | EXPORT_SYMBOL(mipi_dsi_attach); |
| 195 | |
| 196 | /** |
| 197 | * mipi_dsi_detach - detach a DSI device from its DSI host |
| 198 | * @dsi: DSI peripheral |
| 199 | */ |
| 200 | int mipi_dsi_detach(struct mipi_dsi_device *dsi) |
| 201 | { |
| 202 | const struct mipi_dsi_host_ops *ops = dsi->host->ops; |
| 203 | |
| 204 | if (!ops || !ops->detach) |
| 205 | return -ENOSYS; |
| 206 | |
| 207 | return ops->detach(dsi->host, dsi); |
| 208 | } |
| 209 | EXPORT_SYMBOL(mipi_dsi_detach); |
| 210 | |
Thierry Reding | 9eb491f | 2014-10-14 11:12:32 +0200 | [diff] [blame] | 211 | static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi, |
| 212 | struct mipi_dsi_msg *msg) |
| 213 | { |
| 214 | const struct mipi_dsi_host_ops *ops = dsi->host->ops; |
| 215 | |
| 216 | if (!ops || !ops->transfer) |
| 217 | return -ENOSYS; |
| 218 | |
| 219 | if (dsi->mode_flags & MIPI_DSI_MODE_LPM) |
| 220 | msg->flags |= MIPI_DSI_MSG_USE_LPM; |
| 221 | |
| 222 | return ops->transfer(dsi->host, msg); |
| 223 | } |
| 224 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 225 | /** |
Thierry Reding | 02acb76 | 2014-11-04 14:59:14 +0100 | [diff] [blame] | 226 | * mipi_dsi_packet_format_is_short - check if a packet is of the short format |
| 227 | * @type: MIPI DSI data type of the packet |
| 228 | * |
| 229 | * Return: true if the packet for the given data type is a short packet, false |
| 230 | * otherwise. |
| 231 | */ |
| 232 | bool mipi_dsi_packet_format_is_short(u8 type) |
| 233 | { |
| 234 | switch (type) { |
| 235 | case MIPI_DSI_V_SYNC_START: |
| 236 | case MIPI_DSI_V_SYNC_END: |
| 237 | case MIPI_DSI_H_SYNC_START: |
| 238 | case MIPI_DSI_H_SYNC_END: |
| 239 | case MIPI_DSI_END_OF_TRANSMISSION: |
| 240 | case MIPI_DSI_COLOR_MODE_OFF: |
| 241 | case MIPI_DSI_COLOR_MODE_ON: |
| 242 | case MIPI_DSI_SHUTDOWN_PERIPHERAL: |
| 243 | case MIPI_DSI_TURN_ON_PERIPHERAL: |
| 244 | case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM: |
| 245 | case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM: |
| 246 | case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM: |
| 247 | case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM: |
| 248 | case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM: |
| 249 | case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM: |
| 250 | case MIPI_DSI_DCS_SHORT_WRITE: |
| 251 | case MIPI_DSI_DCS_SHORT_WRITE_PARAM: |
| 252 | case MIPI_DSI_DCS_READ: |
| 253 | case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE: |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | return false; |
| 258 | } |
| 259 | EXPORT_SYMBOL(mipi_dsi_packet_format_is_short); |
| 260 | |
| 261 | /** |
| 262 | * mipi_dsi_packet_format_is_long - check if a packet is of the long format |
| 263 | * @type: MIPI DSI data type of the packet |
| 264 | * |
| 265 | * Return: true if the packet for the given data type is a long packet, false |
| 266 | * otherwise. |
| 267 | */ |
| 268 | bool mipi_dsi_packet_format_is_long(u8 type) |
| 269 | { |
| 270 | switch (type) { |
| 271 | case MIPI_DSI_NULL_PACKET: |
| 272 | case MIPI_DSI_BLANKING_PACKET: |
| 273 | case MIPI_DSI_GENERIC_LONG_WRITE: |
| 274 | case MIPI_DSI_DCS_LONG_WRITE: |
| 275 | case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20: |
| 276 | case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24: |
| 277 | case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16: |
| 278 | case MIPI_DSI_PACKED_PIXEL_STREAM_30: |
| 279 | case MIPI_DSI_PACKED_PIXEL_STREAM_36: |
| 280 | case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12: |
| 281 | case MIPI_DSI_PACKED_PIXEL_STREAM_16: |
| 282 | case MIPI_DSI_PACKED_PIXEL_STREAM_18: |
| 283 | case MIPI_DSI_PIXEL_STREAM_3BYTE_18: |
| 284 | case MIPI_DSI_PACKED_PIXEL_STREAM_24: |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | return false; |
| 289 | } |
| 290 | EXPORT_SYMBOL(mipi_dsi_packet_format_is_long); |
| 291 | |
| 292 | /** |
Thierry Reding | a52879e | 2014-10-16 13:44:02 +0200 | [diff] [blame] | 293 | * mipi_dsi_create_packet - create a packet from a message according to the |
| 294 | * DSI protocol |
| 295 | * @packet: pointer to a DSI packet structure |
| 296 | * @msg: message to translate into a packet |
| 297 | * |
| 298 | * Return: 0 on success or a negative error code on failure. |
| 299 | */ |
| 300 | int mipi_dsi_create_packet(struct mipi_dsi_packet *packet, |
| 301 | const struct mipi_dsi_msg *msg) |
| 302 | { |
| 303 | const u8 *tx = msg->tx_buf; |
| 304 | |
| 305 | if (!packet || !msg) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | /* do some minimum sanity checking */ |
| 309 | if (!mipi_dsi_packet_format_is_short(msg->type) && |
| 310 | !mipi_dsi_packet_format_is_long(msg->type)) |
| 311 | return -EINVAL; |
| 312 | |
| 313 | if (msg->channel > 3) |
| 314 | return -EINVAL; |
| 315 | |
| 316 | memset(packet, 0, sizeof(*packet)); |
| 317 | packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f); |
| 318 | |
| 319 | /* TODO: compute ECC if hardware support is not available */ |
| 320 | |
| 321 | /* |
| 322 | * Long write packets contain the word count in header bytes 1 and 2. |
| 323 | * The payload follows the header and is word count bytes long. |
| 324 | * |
| 325 | * Short write packets encode up to two parameters in header bytes 1 |
| 326 | * and 2. |
| 327 | */ |
| 328 | if (mipi_dsi_packet_format_is_long(msg->type)) { |
| 329 | packet->header[1] = (msg->tx_len >> 0) & 0xff; |
| 330 | packet->header[2] = (msg->tx_len >> 8) & 0xff; |
| 331 | |
| 332 | packet->payload_length = msg->tx_len; |
| 333 | packet->payload = tx; |
| 334 | } else { |
| 335 | packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0; |
| 336 | packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0; |
| 337 | } |
| 338 | |
| 339 | packet->size = sizeof(packet->header) + packet->payload_length; |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | EXPORT_SYMBOL(mipi_dsi_create_packet); |
| 344 | |
YoungJun Cho | dbf30b6 | 2014-08-05 09:27:15 +0200 | [diff] [blame] | 345 | /* |
| 346 | * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the |
| 347 | * the payload in a long packet transmitted from the peripheral back to the |
| 348 | * host processor |
| 349 | * @dsi: DSI peripheral device |
| 350 | * @value: the maximum size of the payload |
| 351 | * |
| 352 | * Return: 0 on success or a negative error code on failure. |
| 353 | */ |
| 354 | int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi, |
| 355 | u16 value) |
| 356 | { |
| 357 | u8 tx[2] = { value & 0xff, value >> 8 }; |
| 358 | struct mipi_dsi_msg msg = { |
| 359 | .channel = dsi->channel, |
| 360 | .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE, |
| 361 | .tx_len = sizeof(tx), |
| 362 | .tx_buf = tx, |
| 363 | }; |
| 364 | |
| 365 | return mipi_dsi_device_transfer(dsi, &msg); |
| 366 | } |
| 367 | EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size); |
| 368 | |
Thierry Reding | a52879e | 2014-10-16 13:44:02 +0200 | [diff] [blame] | 369 | /** |
Thierry Reding | 550ab84 | 2014-08-05 10:36:21 +0200 | [diff] [blame] | 370 | * mipi_dsi_generic_write() - transmit data using a generic write packet |
| 371 | * @dsi: DSI peripheral device |
| 372 | * @payload: buffer containing the payload |
| 373 | * @size: size of payload buffer |
| 374 | * |
| 375 | * This function will automatically choose the right data type depending on |
| 376 | * the payload length. |
| 377 | * |
| 378 | * Return: The number of bytes transmitted on success or a negative error code |
| 379 | * on failure. |
| 380 | */ |
| 381 | ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload, |
| 382 | size_t size) |
| 383 | { |
| 384 | struct mipi_dsi_msg msg = { |
| 385 | .channel = dsi->channel, |
| 386 | .tx_buf = payload, |
| 387 | .tx_len = size |
| 388 | }; |
| 389 | |
| 390 | switch (size) { |
| 391 | case 0: |
| 392 | msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM; |
| 393 | break; |
| 394 | |
| 395 | case 1: |
| 396 | msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM; |
| 397 | break; |
| 398 | |
| 399 | case 2: |
| 400 | msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM; |
| 401 | break; |
| 402 | |
| 403 | default: |
| 404 | msg.type = MIPI_DSI_GENERIC_LONG_WRITE; |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | return mipi_dsi_device_transfer(dsi, &msg); |
| 409 | } |
| 410 | EXPORT_SYMBOL(mipi_dsi_generic_write); |
| 411 | |
| 412 | /** |
| 413 | * mipi_dsi_generic_read() - receive data using a generic read packet |
| 414 | * @dsi: DSI peripheral device |
| 415 | * @params: buffer containing the request parameters |
| 416 | * @num_params: number of request parameters |
| 417 | * @data: buffer in which to return the received data |
| 418 | * @size: size of receive buffer |
| 419 | * |
| 420 | * This function will automatically choose the right data type depending on |
| 421 | * the number of parameters passed in. |
| 422 | * |
| 423 | * Return: The number of bytes successfully read or a negative error code on |
| 424 | * failure. |
| 425 | */ |
| 426 | ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params, |
| 427 | size_t num_params, void *data, size_t size) |
| 428 | { |
| 429 | struct mipi_dsi_msg msg = { |
| 430 | .channel = dsi->channel, |
| 431 | .tx_len = num_params, |
| 432 | .tx_buf = params, |
| 433 | .rx_len = size, |
| 434 | .rx_buf = data |
| 435 | }; |
| 436 | |
| 437 | switch (num_params) { |
| 438 | case 0: |
| 439 | msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM; |
| 440 | break; |
| 441 | |
| 442 | case 1: |
| 443 | msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM; |
| 444 | break; |
| 445 | |
| 446 | case 2: |
| 447 | msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM; |
| 448 | break; |
| 449 | |
| 450 | default: |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
| 454 | return mipi_dsi_device_transfer(dsi, &msg); |
| 455 | } |
| 456 | EXPORT_SYMBOL(mipi_dsi_generic_read); |
| 457 | |
| 458 | /** |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 459 | * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload |
| 460 | * @dsi: DSI peripheral device |
| 461 | * @data: buffer containing data to be transmitted |
| 462 | * @len: size of transmission buffer |
| 463 | * |
| 464 | * This function will automatically choose the right data type depending on |
| 465 | * the command payload length. |
| 466 | * |
| 467 | * Return: The number of bytes successfully transmitted or a negative error |
| 468 | * code on failure. |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 469 | */ |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 470 | ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi, |
| 471 | const void *data, size_t len) |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 472 | { |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 473 | struct mipi_dsi_msg msg = { |
Thierry Reding | 3c523d7 | 2014-07-21 12:28:25 +0200 | [diff] [blame] | 474 | .channel = dsi->channel, |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 475 | .tx_buf = data, |
| 476 | .tx_len = len |
| 477 | }; |
| 478 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 479 | switch (len) { |
| 480 | case 0: |
| 481 | return -EINVAL; |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 482 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 483 | case 1: |
| 484 | msg.type = MIPI_DSI_DCS_SHORT_WRITE; |
| 485 | break; |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 486 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 487 | case 2: |
| 488 | msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM; |
| 489 | break; |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 490 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 491 | default: |
| 492 | msg.type = MIPI_DSI_DCS_LONG_WRITE; |
| 493 | break; |
| 494 | } |
| 495 | |
Thierry Reding | 9eb491f | 2014-10-14 11:12:32 +0200 | [diff] [blame] | 496 | return mipi_dsi_device_transfer(dsi, &msg); |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 497 | } |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 498 | EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer); |
| 499 | |
| 500 | /** |
| 501 | * mipi_dsi_dcs_write() - send DCS write command |
| 502 | * @dsi: DSI peripheral device |
| 503 | * @cmd: DCS command |
| 504 | * @data: buffer containing the command payload |
| 505 | * @len: command payload length |
| 506 | * |
| 507 | * This function will automatically choose the right data type depending on |
| 508 | * the command payload length. |
| 509 | * |
| 510 | * Return: The number of bytes successfully transmitted or a negative error |
| 511 | * code on failure. |
| 512 | */ |
| 513 | ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd, |
| 514 | const void *data, size_t len) |
| 515 | { |
| 516 | ssize_t err; |
| 517 | size_t size; |
| 518 | u8 *tx; |
| 519 | |
| 520 | if (len > 0) { |
| 521 | size = 1 + len; |
| 522 | |
| 523 | tx = kmalloc(size, GFP_KERNEL); |
| 524 | if (!tx) |
| 525 | return -ENOMEM; |
| 526 | |
| 527 | /* concatenate the DCS command byte and the payload */ |
| 528 | tx[0] = cmd; |
| 529 | memcpy(&tx[1], data, len); |
| 530 | } else { |
| 531 | tx = &cmd; |
| 532 | size = 1; |
| 533 | } |
| 534 | |
| 535 | err = mipi_dsi_dcs_write_buffer(dsi, tx, size); |
| 536 | |
| 537 | if (len > 0) |
| 538 | kfree(tx); |
| 539 | |
| 540 | return err; |
| 541 | } |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 542 | EXPORT_SYMBOL(mipi_dsi_dcs_write); |
| 543 | |
| 544 | /** |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 545 | * mipi_dsi_dcs_read() - send DCS read request command |
| 546 | * @dsi: DSI peripheral device |
| 547 | * @cmd: DCS command |
| 548 | * @data: buffer in which to receive data |
| 549 | * @len: size of receive buffer |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 550 | * |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 551 | * Return: The number of bytes read or a negative error code on failure. |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 552 | */ |
Thierry Reding | 3c523d7 | 2014-07-21 12:28:25 +0200 | [diff] [blame] | 553 | ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data, |
| 554 | size_t len) |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 555 | { |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 556 | struct mipi_dsi_msg msg = { |
Thierry Reding | 3c523d7 | 2014-07-21 12:28:25 +0200 | [diff] [blame] | 557 | .channel = dsi->channel, |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 558 | .type = MIPI_DSI_DCS_READ, |
| 559 | .tx_buf = &cmd, |
| 560 | .tx_len = 1, |
| 561 | .rx_buf = data, |
| 562 | .rx_len = len |
| 563 | }; |
| 564 | |
Thierry Reding | 9eb491f | 2014-10-14 11:12:32 +0200 | [diff] [blame] | 565 | return mipi_dsi_device_transfer(dsi, &msg); |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 566 | } |
| 567 | EXPORT_SYMBOL(mipi_dsi_dcs_read); |
| 568 | |
YoungJun Cho | 42fe1e7 | 2014-08-05 10:38:31 +0200 | [diff] [blame] | 569 | /** |
Thierry Reding | 083d573 | 2014-08-05 11:14:02 +0200 | [diff] [blame] | 570 | * mipi_dsi_dcs_nop() - send DCS nop packet |
| 571 | * @dsi: DSI peripheral device |
| 572 | * |
| 573 | * Return: 0 on success or a negative error code on failure. |
| 574 | */ |
| 575 | int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi) |
| 576 | { |
| 577 | ssize_t err; |
| 578 | |
| 579 | err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0); |
| 580 | if (err < 0) |
| 581 | return err; |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | EXPORT_SYMBOL(mipi_dsi_dcs_nop); |
| 586 | |
| 587 | /** |
Thierry Reding | 2f16b89 | 2014-08-05 11:15:15 +0200 | [diff] [blame] | 588 | * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module |
| 589 | * @dsi: DSI peripheral device |
| 590 | * |
| 591 | * Return: 0 on success or a negative error code on failure. |
| 592 | */ |
| 593 | int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi) |
| 594 | { |
| 595 | ssize_t err; |
| 596 | |
| 597 | err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0); |
| 598 | if (err < 0) |
| 599 | return err; |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset); |
| 604 | |
| 605 | /** |
Thierry Reding | 3d9a8fc | 2014-08-05 11:17:06 +0200 | [diff] [blame] | 606 | * mipi_dsi_dcs_get_power_mode() - query the display module's current power |
| 607 | * mode |
| 608 | * @dsi: DSI peripheral device |
| 609 | * @mode: return location for the current power mode |
| 610 | * |
| 611 | * Return: 0 on success or a negative error code on failure. |
| 612 | */ |
| 613 | int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode) |
| 614 | { |
| 615 | ssize_t err; |
| 616 | |
| 617 | err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode, |
| 618 | sizeof(*mode)); |
| 619 | if (err <= 0) { |
| 620 | if (err == 0) |
| 621 | err = -ENODATA; |
| 622 | |
| 623 | return err; |
| 624 | } |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode); |
| 629 | |
| 630 | /** |
Thierry Reding | 5cc0af1 | 2014-08-05 11:18:46 +0200 | [diff] [blame^] | 631 | * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image |
| 632 | * data used by the interface |
| 633 | * @dsi: DSI peripheral device |
| 634 | * @format: return location for the pixel format |
| 635 | * |
| 636 | * Return: 0 on success or a negative error code on failure. |
| 637 | */ |
| 638 | int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format) |
| 639 | { |
| 640 | ssize_t err; |
| 641 | |
| 642 | err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format, |
| 643 | sizeof(*format)); |
| 644 | if (err <= 0) { |
| 645 | if (err == 0) |
| 646 | err = -ENODATA; |
| 647 | |
| 648 | return err; |
| 649 | } |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format); |
| 654 | |
| 655 | /** |
YoungJun Cho | 42fe1e7 | 2014-08-05 10:38:31 +0200 | [diff] [blame] | 656 | * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the |
| 657 | * display module except interface communication |
| 658 | * @dsi: DSI peripheral device |
| 659 | * |
| 660 | * Return: 0 on success or a negative error code on failure. |
| 661 | */ |
| 662 | int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi) |
| 663 | { |
| 664 | ssize_t err; |
| 665 | |
| 666 | err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0); |
| 667 | if (err < 0) |
| 668 | return err; |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode); |
| 673 | |
| 674 | /** |
| 675 | * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display |
| 676 | * module |
| 677 | * @dsi: DSI peripheral device |
| 678 | * |
| 679 | * Return: 0 on success or a negative error code on failure. |
| 680 | */ |
| 681 | int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi) |
| 682 | { |
| 683 | ssize_t err; |
| 684 | |
| 685 | err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0); |
| 686 | if (err < 0) |
| 687 | return err; |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode); |
| 692 | |
| 693 | /** |
| 694 | * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the |
| 695 | * display device |
| 696 | * @dsi: DSI peripheral device |
| 697 | * |
| 698 | * Return: 0 on success or a negative error code on failure. |
| 699 | */ |
| 700 | int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi) |
| 701 | { |
| 702 | ssize_t err; |
| 703 | |
| 704 | err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0); |
| 705 | if (err < 0) |
| 706 | return err; |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off); |
| 711 | |
| 712 | /** |
| 713 | * mipi_dsi_dcs_set_display_on() - start displaying the image data on the |
| 714 | * display device |
| 715 | * @dsi: DSI peripheral device |
| 716 | * |
| 717 | * Return: 0 on success or a negative error code on failure |
| 718 | */ |
| 719 | int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi) |
| 720 | { |
| 721 | ssize_t err; |
| 722 | |
| 723 | err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0); |
| 724 | if (err < 0) |
| 725 | return err; |
| 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on); |
| 730 | |
| 731 | /** |
| 732 | * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect |
| 733 | * output signal on the TE signal line |
| 734 | * @dsi: DSI peripheral device |
| 735 | * |
| 736 | * Return: 0 on success or a negative error code on failure |
| 737 | */ |
| 738 | int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi) |
| 739 | { |
| 740 | ssize_t err; |
| 741 | |
| 742 | err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0); |
| 743 | if (err < 0) |
| 744 | return err; |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off); |
| 749 | |
| 750 | /** |
| 751 | * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect |
| 752 | * output signal on the TE signal line. |
| 753 | * @dsi: DSI peripheral device |
| 754 | * @mode: the Tearing Effect Output Line mode |
| 755 | * |
| 756 | * Return: 0 on success or a negative error code on failure |
| 757 | */ |
| 758 | int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi, |
| 759 | enum mipi_dsi_dcs_tear_mode mode) |
| 760 | { |
| 761 | u8 value = mode; |
| 762 | ssize_t err; |
| 763 | |
| 764 | err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value, |
| 765 | sizeof(value)); |
| 766 | if (err < 0) |
| 767 | return err; |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on); |
| 772 | |
Thierry Reding | 5cc0af1 | 2014-08-05 11:18:46 +0200 | [diff] [blame^] | 773 | /** |
| 774 | * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image |
| 775 | * data used by the interface |
| 776 | * @dsi: DSI peripheral device |
| 777 | * @format: pixel format |
| 778 | * |
| 779 | * Return: 0 on success or a negative error code on failure. |
| 780 | */ |
| 781 | int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format) |
| 782 | { |
| 783 | ssize_t err; |
| 784 | |
| 785 | err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format, |
| 786 | sizeof(format)); |
| 787 | if (err < 0) |
| 788 | return err; |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format); |
| 793 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 794 | static int mipi_dsi_drv_probe(struct device *dev) |
| 795 | { |
| 796 | struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver); |
| 797 | struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev); |
| 798 | |
| 799 | return drv->probe(dsi); |
| 800 | } |
| 801 | |
| 802 | static int mipi_dsi_drv_remove(struct device *dev) |
| 803 | { |
| 804 | struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver); |
| 805 | struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev); |
| 806 | |
| 807 | return drv->remove(dsi); |
| 808 | } |
| 809 | |
Thierry Reding | d162180 | 2014-04-29 17:19:57 +0200 | [diff] [blame] | 810 | static void mipi_dsi_drv_shutdown(struct device *dev) |
| 811 | { |
| 812 | struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver); |
| 813 | struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev); |
| 814 | |
| 815 | drv->shutdown(dsi); |
| 816 | } |
| 817 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 818 | /** |
Thierry Reding | 009081e | 2014-08-05 10:41:13 +0200 | [diff] [blame] | 819 | * mipi_dsi_driver_register() - register a driver for DSI devices |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 820 | * @drv: DSI driver structure |
Thierry Reding | 009081e | 2014-08-05 10:41:13 +0200 | [diff] [blame] | 821 | * |
| 822 | * Return: 0 on success or a negative error code on failure. |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 823 | */ |
| 824 | int mipi_dsi_driver_register(struct mipi_dsi_driver *drv) |
| 825 | { |
| 826 | drv->driver.bus = &mipi_dsi_bus_type; |
| 827 | if (drv->probe) |
| 828 | drv->driver.probe = mipi_dsi_drv_probe; |
| 829 | if (drv->remove) |
| 830 | drv->driver.remove = mipi_dsi_drv_remove; |
Thierry Reding | d162180 | 2014-04-29 17:19:57 +0200 | [diff] [blame] | 831 | if (drv->shutdown) |
| 832 | drv->driver.shutdown = mipi_dsi_drv_shutdown; |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 833 | |
| 834 | return driver_register(&drv->driver); |
| 835 | } |
| 836 | EXPORT_SYMBOL(mipi_dsi_driver_register); |
| 837 | |
| 838 | /** |
Thierry Reding | 009081e | 2014-08-05 10:41:13 +0200 | [diff] [blame] | 839 | * mipi_dsi_driver_unregister() - unregister a driver for DSI devices |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 840 | * @drv: DSI driver structure |
Thierry Reding | 009081e | 2014-08-05 10:41:13 +0200 | [diff] [blame] | 841 | * |
| 842 | * Return: 0 on success or a negative error code on failure. |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 843 | */ |
| 844 | void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv) |
| 845 | { |
| 846 | driver_unregister(&drv->driver); |
| 847 | } |
| 848 | EXPORT_SYMBOL(mipi_dsi_driver_unregister); |
| 849 | |
| 850 | static int __init mipi_dsi_bus_init(void) |
| 851 | { |
| 852 | return bus_register(&mipi_dsi_bus_type); |
| 853 | } |
| 854 | postcore_initcall(mipi_dsi_bus_init); |
| 855 | |
| 856 | MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>"); |
| 857 | MODULE_DESCRIPTION("MIPI DSI Bus"); |
| 858 | MODULE_LICENSE("GPL and additional rights"); |