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 | |
| 38 | static int mipi_dsi_device_match(struct device *dev, struct device_driver *drv) |
| 39 | { |
| 40 | return of_driver_match_device(dev, drv); |
| 41 | } |
| 42 | |
| 43 | static 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 | |
| 54 | static 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 | |
| 60 | static 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 | |
| 68 | static const struct device_type mipi_dsi_device_type = { |
| 69 | .release = mipi_dsi_dev_release, |
| 70 | }; |
| 71 | |
| 72 | static 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 | |
| 90 | static 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 | |
| 99 | static struct mipi_dsi_device * |
| 100 | of_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", ®); |
| 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 | |
| 141 | int mipi_dsi_host_register(struct mipi_dsi_host *host) |
| 142 | { |
| 143 | struct device_node *node; |
| 144 | |
Andrzej Hajda | e49640d | 2014-03-28 12:52:37 +0100 | [diff] [blame] | 145 | 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 Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 149 | of_mipi_dsi_device_add(host, node); |
Andrzej Hajda | e49640d | 2014-03-28 12:52:37 +0100 | [diff] [blame] | 150 | } |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | EXPORT_SYMBOL(mipi_dsi_host_register); |
| 155 | |
| 156 | static 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 | |
| 165 | void mipi_dsi_host_unregister(struct mipi_dsi_host *host) |
| 166 | { |
| 167 | device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn); |
| 168 | } |
| 169 | EXPORT_SYMBOL(mipi_dsi_host_unregister); |
| 170 | |
| 171 | /** |
| 172 | * mipi_dsi_attach - attach a DSI device to its DSI host |
| 173 | * @dsi: DSI peripheral |
| 174 | */ |
| 175 | int 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 | } |
| 184 | EXPORT_SYMBOL(mipi_dsi_attach); |
| 185 | |
| 186 | /** |
| 187 | * mipi_dsi_detach - detach a DSI device from its DSI host |
| 188 | * @dsi: DSI peripheral |
| 189 | */ |
| 190 | int 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 | } |
| 199 | EXPORT_SYMBOL(mipi_dsi_detach); |
| 200 | |
Thierry Reding | 9eb491f | 2014-10-14 11:12:32 +0200 | [diff] [blame] | 201 | static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi, |
| 202 | struct mipi_dsi_msg *msg) |
| 203 | { |
| 204 | const struct mipi_dsi_host_ops *ops = dsi->host->ops; |
| 205 | |
| 206 | if (!ops || !ops->transfer) |
| 207 | return -ENOSYS; |
| 208 | |
| 209 | if (dsi->mode_flags & MIPI_DSI_MODE_LPM) |
| 210 | msg->flags |= MIPI_DSI_MSG_USE_LPM; |
| 211 | |
| 212 | return ops->transfer(dsi->host, msg); |
| 213 | } |
| 214 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 215 | /** |
Thierry Reding | 02acb76 | 2014-11-04 14:59:14 +0100 | [diff] [blame] | 216 | * mipi_dsi_packet_format_is_short - check if a packet is of the short format |
| 217 | * @type: MIPI DSI data type of the packet |
| 218 | * |
| 219 | * Return: true if the packet for the given data type is a short packet, false |
| 220 | * otherwise. |
| 221 | */ |
| 222 | bool mipi_dsi_packet_format_is_short(u8 type) |
| 223 | { |
| 224 | switch (type) { |
| 225 | case MIPI_DSI_V_SYNC_START: |
| 226 | case MIPI_DSI_V_SYNC_END: |
| 227 | case MIPI_DSI_H_SYNC_START: |
| 228 | case MIPI_DSI_H_SYNC_END: |
| 229 | case MIPI_DSI_END_OF_TRANSMISSION: |
| 230 | case MIPI_DSI_COLOR_MODE_OFF: |
| 231 | case MIPI_DSI_COLOR_MODE_ON: |
| 232 | case MIPI_DSI_SHUTDOWN_PERIPHERAL: |
| 233 | case MIPI_DSI_TURN_ON_PERIPHERAL: |
| 234 | case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM: |
| 235 | case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM: |
| 236 | case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM: |
| 237 | case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM: |
| 238 | case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM: |
| 239 | case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM: |
| 240 | case MIPI_DSI_DCS_SHORT_WRITE: |
| 241 | case MIPI_DSI_DCS_SHORT_WRITE_PARAM: |
| 242 | case MIPI_DSI_DCS_READ: |
| 243 | case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE: |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | return false; |
| 248 | } |
| 249 | EXPORT_SYMBOL(mipi_dsi_packet_format_is_short); |
| 250 | |
| 251 | /** |
| 252 | * mipi_dsi_packet_format_is_long - check if a packet is of the long format |
| 253 | * @type: MIPI DSI data type of the packet |
| 254 | * |
| 255 | * Return: true if the packet for the given data type is a long packet, false |
| 256 | * otherwise. |
| 257 | */ |
| 258 | bool mipi_dsi_packet_format_is_long(u8 type) |
| 259 | { |
| 260 | switch (type) { |
| 261 | case MIPI_DSI_NULL_PACKET: |
| 262 | case MIPI_DSI_BLANKING_PACKET: |
| 263 | case MIPI_DSI_GENERIC_LONG_WRITE: |
| 264 | case MIPI_DSI_DCS_LONG_WRITE: |
| 265 | case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20: |
| 266 | case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24: |
| 267 | case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16: |
| 268 | case MIPI_DSI_PACKED_PIXEL_STREAM_30: |
| 269 | case MIPI_DSI_PACKED_PIXEL_STREAM_36: |
| 270 | case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12: |
| 271 | case MIPI_DSI_PACKED_PIXEL_STREAM_16: |
| 272 | case MIPI_DSI_PACKED_PIXEL_STREAM_18: |
| 273 | case MIPI_DSI_PIXEL_STREAM_3BYTE_18: |
| 274 | case MIPI_DSI_PACKED_PIXEL_STREAM_24: |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | return false; |
| 279 | } |
| 280 | EXPORT_SYMBOL(mipi_dsi_packet_format_is_long); |
| 281 | |
| 282 | /** |
Thierry Reding | a52879e | 2014-10-16 13:44:02 +0200 | [diff] [blame] | 283 | * mipi_dsi_create_packet - create a packet from a message according to the |
| 284 | * DSI protocol |
| 285 | * @packet: pointer to a DSI packet structure |
| 286 | * @msg: message to translate into a packet |
| 287 | * |
| 288 | * Return: 0 on success or a negative error code on failure. |
| 289 | */ |
| 290 | int mipi_dsi_create_packet(struct mipi_dsi_packet *packet, |
| 291 | const struct mipi_dsi_msg *msg) |
| 292 | { |
| 293 | const u8 *tx = msg->tx_buf; |
| 294 | |
| 295 | if (!packet || !msg) |
| 296 | return -EINVAL; |
| 297 | |
| 298 | /* do some minimum sanity checking */ |
| 299 | if (!mipi_dsi_packet_format_is_short(msg->type) && |
| 300 | !mipi_dsi_packet_format_is_long(msg->type)) |
| 301 | return -EINVAL; |
| 302 | |
| 303 | if (msg->channel > 3) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | memset(packet, 0, sizeof(*packet)); |
| 307 | packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f); |
| 308 | |
| 309 | /* TODO: compute ECC if hardware support is not available */ |
| 310 | |
| 311 | /* |
| 312 | * Long write packets contain the word count in header bytes 1 and 2. |
| 313 | * The payload follows the header and is word count bytes long. |
| 314 | * |
| 315 | * Short write packets encode up to two parameters in header bytes 1 |
| 316 | * and 2. |
| 317 | */ |
| 318 | if (mipi_dsi_packet_format_is_long(msg->type)) { |
| 319 | packet->header[1] = (msg->tx_len >> 0) & 0xff; |
| 320 | packet->header[2] = (msg->tx_len >> 8) & 0xff; |
| 321 | |
| 322 | packet->payload_length = msg->tx_len; |
| 323 | packet->payload = tx; |
| 324 | } else { |
| 325 | packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0; |
| 326 | packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0; |
| 327 | } |
| 328 | |
| 329 | packet->size = sizeof(packet->header) + packet->payload_length; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | EXPORT_SYMBOL(mipi_dsi_create_packet); |
| 334 | |
YoungJun Cho | dbf30b6 | 2014-08-05 09:27:15 +0200 | [diff] [blame] | 335 | /* |
| 336 | * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the |
| 337 | * the payload in a long packet transmitted from the peripheral back to the |
| 338 | * host processor |
| 339 | * @dsi: DSI peripheral device |
| 340 | * @value: the maximum size of the payload |
| 341 | * |
| 342 | * Return: 0 on success or a negative error code on failure. |
| 343 | */ |
| 344 | int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi, |
| 345 | u16 value) |
| 346 | { |
| 347 | u8 tx[2] = { value & 0xff, value >> 8 }; |
| 348 | struct mipi_dsi_msg msg = { |
| 349 | .channel = dsi->channel, |
| 350 | .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE, |
| 351 | .tx_len = sizeof(tx), |
| 352 | .tx_buf = tx, |
| 353 | }; |
| 354 | |
| 355 | return mipi_dsi_device_transfer(dsi, &msg); |
| 356 | } |
| 357 | EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size); |
| 358 | |
Thierry Reding | a52879e | 2014-10-16 13:44:02 +0200 | [diff] [blame] | 359 | /** |
Thierry Reding | 550ab84 | 2014-08-05 10:36:21 +0200 | [diff] [blame^] | 360 | * mipi_dsi_generic_write() - transmit data using a generic write packet |
| 361 | * @dsi: DSI peripheral device |
| 362 | * @payload: buffer containing the payload |
| 363 | * @size: size of payload buffer |
| 364 | * |
| 365 | * This function will automatically choose the right data type depending on |
| 366 | * the payload length. |
| 367 | * |
| 368 | * Return: The number of bytes transmitted on success or a negative error code |
| 369 | * on failure. |
| 370 | */ |
| 371 | ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload, |
| 372 | size_t size) |
| 373 | { |
| 374 | struct mipi_dsi_msg msg = { |
| 375 | .channel = dsi->channel, |
| 376 | .tx_buf = payload, |
| 377 | .tx_len = size |
| 378 | }; |
| 379 | |
| 380 | switch (size) { |
| 381 | case 0: |
| 382 | msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM; |
| 383 | break; |
| 384 | |
| 385 | case 1: |
| 386 | msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM; |
| 387 | break; |
| 388 | |
| 389 | case 2: |
| 390 | msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM; |
| 391 | break; |
| 392 | |
| 393 | default: |
| 394 | msg.type = MIPI_DSI_GENERIC_LONG_WRITE; |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | return mipi_dsi_device_transfer(dsi, &msg); |
| 399 | } |
| 400 | EXPORT_SYMBOL(mipi_dsi_generic_write); |
| 401 | |
| 402 | /** |
| 403 | * mipi_dsi_generic_read() - receive data using a generic read packet |
| 404 | * @dsi: DSI peripheral device |
| 405 | * @params: buffer containing the request parameters |
| 406 | * @num_params: number of request parameters |
| 407 | * @data: buffer in which to return the received data |
| 408 | * @size: size of receive buffer |
| 409 | * |
| 410 | * This function will automatically choose the right data type depending on |
| 411 | * the number of parameters passed in. |
| 412 | * |
| 413 | * Return: The number of bytes successfully read or a negative error code on |
| 414 | * failure. |
| 415 | */ |
| 416 | ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params, |
| 417 | size_t num_params, void *data, size_t size) |
| 418 | { |
| 419 | struct mipi_dsi_msg msg = { |
| 420 | .channel = dsi->channel, |
| 421 | .tx_len = num_params, |
| 422 | .tx_buf = params, |
| 423 | .rx_len = size, |
| 424 | .rx_buf = data |
| 425 | }; |
| 426 | |
| 427 | switch (num_params) { |
| 428 | case 0: |
| 429 | msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM; |
| 430 | break; |
| 431 | |
| 432 | case 1: |
| 433 | msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM; |
| 434 | break; |
| 435 | |
| 436 | case 2: |
| 437 | msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM; |
| 438 | break; |
| 439 | |
| 440 | default: |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | |
| 444 | return mipi_dsi_device_transfer(dsi, &msg); |
| 445 | } |
| 446 | EXPORT_SYMBOL(mipi_dsi_generic_read); |
| 447 | |
| 448 | /** |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 449 | * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload |
| 450 | * @dsi: DSI peripheral device |
| 451 | * @data: buffer containing data to be transmitted |
| 452 | * @len: size of transmission buffer |
| 453 | * |
| 454 | * This function will automatically choose the right data type depending on |
| 455 | * the command payload length. |
| 456 | * |
| 457 | * Return: The number of bytes successfully transmitted or a negative error |
| 458 | * code on failure. |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 459 | */ |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 460 | ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi, |
| 461 | const void *data, size_t len) |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 462 | { |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 463 | struct mipi_dsi_msg msg = { |
Thierry Reding | 3c523d7 | 2014-07-21 12:28:25 +0200 | [diff] [blame] | 464 | .channel = dsi->channel, |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 465 | .tx_buf = data, |
| 466 | .tx_len = len |
| 467 | }; |
| 468 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 469 | switch (len) { |
| 470 | case 0: |
| 471 | return -EINVAL; |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 472 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 473 | case 1: |
| 474 | msg.type = MIPI_DSI_DCS_SHORT_WRITE; |
| 475 | break; |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 476 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 477 | case 2: |
| 478 | msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM; |
| 479 | break; |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 480 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 481 | default: |
| 482 | msg.type = MIPI_DSI_DCS_LONG_WRITE; |
| 483 | break; |
| 484 | } |
| 485 | |
Thierry Reding | 9eb491f | 2014-10-14 11:12:32 +0200 | [diff] [blame] | 486 | return mipi_dsi_device_transfer(dsi, &msg); |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 487 | } |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 488 | EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer); |
| 489 | |
| 490 | /** |
| 491 | * mipi_dsi_dcs_write() - send DCS write command |
| 492 | * @dsi: DSI peripheral device |
| 493 | * @cmd: DCS command |
| 494 | * @data: buffer containing the command payload |
| 495 | * @len: command payload length |
| 496 | * |
| 497 | * This function will automatically choose the right data type depending on |
| 498 | * the command payload length. |
| 499 | * |
| 500 | * Return: The number of bytes successfully transmitted or a negative error |
| 501 | * code on failure. |
| 502 | */ |
| 503 | ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd, |
| 504 | const void *data, size_t len) |
| 505 | { |
| 506 | ssize_t err; |
| 507 | size_t size; |
| 508 | u8 *tx; |
| 509 | |
| 510 | if (len > 0) { |
| 511 | size = 1 + len; |
| 512 | |
| 513 | tx = kmalloc(size, GFP_KERNEL); |
| 514 | if (!tx) |
| 515 | return -ENOMEM; |
| 516 | |
| 517 | /* concatenate the DCS command byte and the payload */ |
| 518 | tx[0] = cmd; |
| 519 | memcpy(&tx[1], data, len); |
| 520 | } else { |
| 521 | tx = &cmd; |
| 522 | size = 1; |
| 523 | } |
| 524 | |
| 525 | err = mipi_dsi_dcs_write_buffer(dsi, tx, size); |
| 526 | |
| 527 | if (len > 0) |
| 528 | kfree(tx); |
| 529 | |
| 530 | return err; |
| 531 | } |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 532 | EXPORT_SYMBOL(mipi_dsi_dcs_write); |
| 533 | |
| 534 | /** |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 535 | * mipi_dsi_dcs_read() - send DCS read request command |
| 536 | * @dsi: DSI peripheral device |
| 537 | * @cmd: DCS command |
| 538 | * @data: buffer in which to receive data |
| 539 | * @len: size of receive buffer |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 540 | * |
Thierry Reding | 960dd61 | 2014-07-21 15:47:10 +0200 | [diff] [blame] | 541 | * 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] | 542 | */ |
Thierry Reding | 3c523d7 | 2014-07-21 12:28:25 +0200 | [diff] [blame] | 543 | ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data, |
| 544 | size_t len) |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 545 | { |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 546 | struct mipi_dsi_msg msg = { |
Thierry Reding | 3c523d7 | 2014-07-21 12:28:25 +0200 | [diff] [blame] | 547 | .channel = dsi->channel, |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 548 | .type = MIPI_DSI_DCS_READ, |
| 549 | .tx_buf = &cmd, |
| 550 | .tx_len = 1, |
| 551 | .rx_buf = data, |
| 552 | .rx_len = len |
| 553 | }; |
| 554 | |
Thierry Reding | 9eb491f | 2014-10-14 11:12:32 +0200 | [diff] [blame] | 555 | return mipi_dsi_device_transfer(dsi, &msg); |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 556 | } |
| 557 | EXPORT_SYMBOL(mipi_dsi_dcs_read); |
| 558 | |
| 559 | static int mipi_dsi_drv_probe(struct device *dev) |
| 560 | { |
| 561 | struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver); |
| 562 | struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev); |
| 563 | |
| 564 | return drv->probe(dsi); |
| 565 | } |
| 566 | |
| 567 | static int mipi_dsi_drv_remove(struct device *dev) |
| 568 | { |
| 569 | struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver); |
| 570 | struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev); |
| 571 | |
| 572 | return drv->remove(dsi); |
| 573 | } |
| 574 | |
Thierry Reding | d162180 | 2014-04-29 17:19:57 +0200 | [diff] [blame] | 575 | static void mipi_dsi_drv_shutdown(struct device *dev) |
| 576 | { |
| 577 | struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver); |
| 578 | struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev); |
| 579 | |
| 580 | drv->shutdown(dsi); |
| 581 | } |
| 582 | |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 583 | /** |
| 584 | * mipi_dsi_driver_register - register a driver for DSI devices |
| 585 | * @drv: DSI driver structure |
| 586 | */ |
| 587 | int mipi_dsi_driver_register(struct mipi_dsi_driver *drv) |
| 588 | { |
| 589 | drv->driver.bus = &mipi_dsi_bus_type; |
| 590 | if (drv->probe) |
| 591 | drv->driver.probe = mipi_dsi_drv_probe; |
| 592 | if (drv->remove) |
| 593 | drv->driver.remove = mipi_dsi_drv_remove; |
Thierry Reding | d162180 | 2014-04-29 17:19:57 +0200 | [diff] [blame] | 594 | if (drv->shutdown) |
| 595 | drv->driver.shutdown = mipi_dsi_drv_shutdown; |
Andrzej Hajda | 068a002 | 2013-12-04 16:35:12 +0100 | [diff] [blame] | 596 | |
| 597 | return driver_register(&drv->driver); |
| 598 | } |
| 599 | EXPORT_SYMBOL(mipi_dsi_driver_register); |
| 600 | |
| 601 | /** |
| 602 | * mipi_dsi_driver_unregister - unregister a driver for DSI devices |
| 603 | * @drv: DSI driver structure |
| 604 | */ |
| 605 | void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv) |
| 606 | { |
| 607 | driver_unregister(&drv->driver); |
| 608 | } |
| 609 | EXPORT_SYMBOL(mipi_dsi_driver_unregister); |
| 610 | |
| 611 | static int __init mipi_dsi_bus_init(void) |
| 612 | { |
| 613 | return bus_register(&mipi_dsi_bus_type); |
| 614 | } |
| 615 | postcore_initcall(mipi_dsi_bus_init); |
| 616 | |
| 617 | MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>"); |
| 618 | MODULE_DESCRIPTION("MIPI DSI Bus"); |
| 619 | MODULE_LICENSE("GPL and additional rights"); |