drm/panel: Change dlen from u16 to u8
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_scdc_helper.c
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  * The number of bytes read from SCDC or a 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         struct i2c_msg msgs[2] = {
55                 {
56                         .addr = SCDC_I2C_SLAVE_ADDRESS,
57                         .flags = 0,
58                         .len = 1,
59                         .buf = &offset,
60                 }, {
61                         .addr = SCDC_I2C_SLAVE_ADDRESS,
62                         .flags = I2C_M_RD,
63                         .len = size,
64                         .buf = buffer,
65                 }
66         };
67
68         return i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
69 }
70 EXPORT_SYMBOL(drm_scdc_read);
71
72 /**
73  * drm_scdc_write - write a block of data to SCDC
74  * @adapter: I2C controller
75  * @offset: start offset of block to write
76  * @buffer: block of data to write
77  * @size: size of the block to write
78  *
79  * Writes a block of data to SCDC, starting at a given offset.
80  *
81  * Returns:
82  * The number of bytes written to SCDC or a negative error code on failure.
83  */
84 ssize_t drm_scdc_write(struct i2c_adapter *adapter, u8 offset,
85                        const void *buffer, size_t size)
86 {
87         struct i2c_msg msg = {
88                 .addr = SCDC_I2C_SLAVE_ADDRESS,
89                 .flags = 0,
90                 .len = 1 + size,
91                 .buf = NULL,
92         };
93         void *data;
94         int err;
95
96         data = kmalloc(1 + size, GFP_KERNEL);
97         if (!data)
98                 return -ENOMEM;
99
100         msg.buf = data;
101
102         memcpy(data, &offset, sizeof(offset));
103         memcpy(data + 1, buffer, size);
104
105         err = i2c_transfer(adapter, &msg, 1);
106
107         kfree(data);
108
109         return err;
110 }
111 EXPORT_SYMBOL(drm_scdc_write);