Thierry Reding | 3ad33ae | 2017-03-13 16:53:59 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 NVIDIA Corporation. All rights reserved. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sub license, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the |
| 12 | * next paragraph) shall be included in all copies or substantial portions |
| 13 | * of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/slab.h> |
| 25 | |
| 26 | #include <drm/drm_scdc_helper.h> |
| 27 | |
| 28 | /** |
| 29 | * DOC: scdc helpers |
| 30 | * |
| 31 | * Status and Control Data Channel (SCDC) is a mechanism introduced by the |
| 32 | * HDMI 2.0 specification. It is a point-to-point protocol that allows the |
| 33 | * HDMI source and HDMI sink to exchange data. The same I2C interface that |
| 34 | * is used to access EDID serves as the transport mechanism for SCDC. |
| 35 | */ |
| 36 | |
| 37 | #define SCDC_I2C_SLAVE_ADDRESS 0x54 |
| 38 | |
| 39 | /** |
| 40 | * drm_scdc_read - read a block of data from SCDC |
| 41 | * @adapter: I2C controller |
| 42 | * @offset: start offset of block to read |
| 43 | * @buffer: return location for the block to read |
| 44 | * @size: size of the block to read |
| 45 | * |
| 46 | * Reads a block of data from SCDC, starting at a given offset. |
| 47 | * |
| 48 | * Returns: |
| 49 | * 0 on success, negative error code on failure. |
| 50 | */ |
| 51 | ssize_t drm_scdc_read(struct i2c_adapter *adapter, u8 offset, void *buffer, |
| 52 | size_t size) |
| 53 | { |
| 54 | int ret; |
| 55 | struct i2c_msg msgs[2] = { |
| 56 | { |
| 57 | .addr = SCDC_I2C_SLAVE_ADDRESS, |
| 58 | .flags = 0, |
| 59 | .len = 1, |
| 60 | .buf = &offset, |
| 61 | }, { |
| 62 | .addr = SCDC_I2C_SLAVE_ADDRESS, |
| 63 | .flags = I2C_M_RD, |
| 64 | .len = size, |
| 65 | .buf = buffer, |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs)); |
| 70 | if (ret < 0) |
| 71 | return ret; |
| 72 | if (ret != ARRAY_SIZE(msgs)) |
| 73 | return -EPROTO; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | EXPORT_SYMBOL(drm_scdc_read); |
| 78 | |
| 79 | /** |
| 80 | * drm_scdc_write - write a block of data to SCDC |
| 81 | * @adapter: I2C controller |
| 82 | * @offset: start offset of block to write |
| 83 | * @buffer: block of data to write |
| 84 | * @size: size of the block to write |
| 85 | * |
| 86 | * Writes a block of data to SCDC, starting at a given offset. |
| 87 | * |
| 88 | * Returns: |
| 89 | * 0 on success, negative error code on failure. |
| 90 | */ |
| 91 | ssize_t drm_scdc_write(struct i2c_adapter *adapter, u8 offset, |
| 92 | const void *buffer, size_t size) |
| 93 | { |
| 94 | struct i2c_msg msg = { |
| 95 | .addr = SCDC_I2C_SLAVE_ADDRESS, |
| 96 | .flags = 0, |
| 97 | .len = 1 + size, |
| 98 | .buf = NULL, |
| 99 | }; |
| 100 | void *data; |
| 101 | int err; |
| 102 | |
| 103 | data = kmalloc(1 + size, GFP_TEMPORARY); |
| 104 | if (!data) |
| 105 | return -ENOMEM; |
| 106 | |
| 107 | msg.buf = data; |
| 108 | |
| 109 | memcpy(data, &offset, sizeof(offset)); |
| 110 | memcpy(data + 1, buffer, size); |
| 111 | |
| 112 | err = i2c_transfer(adapter, &msg, 1); |
| 113 | |
| 114 | kfree(data); |
| 115 | |
| 116 | if (err < 0) |
| 117 | return err; |
| 118 | if (err != 1) |
| 119 | return -EPROTO; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | EXPORT_SYMBOL(drm_scdc_write); |