blob: bf37090fa53bcd9a5785ad4fab560391159ee358 [file] [log] [blame]
Vineet Bajaj037d3b02015-04-29 15:46:55 +05301/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#include <mipi_dsi_i2c.h>
31#include <blsp_qup.h>
32#include <i2c_qup.h>
33#include <gsbi.h>
34#include <err.h>
35#include <debug.h>
36
37#define I2C_CLK_FREQ 100000
38#define I2C_SRC_CLK_FREQ 50000000
39
40static struct qup_i2c_dev *i2c_dev;
41
42int mipi_dsi_i2c_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len)
43{
44 if (!buf)
45 return ERR_INVALID_ARGS;
46
47 if(!i2c_dev)
48 return ERR_NOT_VALID;
49
50 struct i2c_msg rd_buf[] = {
51 {addr, I2C_M_WR, 1, &reg},
52 {addr, I2C_M_RD, len, buf}
53 };
54
55 int err = qup_i2c_xfer(i2c_dev, rd_buf, 2);
56 if (err < 0) {
57 dprintf(CRITICAL, "Read reg %x failed\n", reg);
58 return err;
59 }
60
61 return NO_ERROR;
62}
63
64int mipi_dsi_i2c_read_byte(uint8_t addr, uint8_t reg, uint8_t *buf)
65{
66 if (!buf)
67 return ERR_INVALID_ARGS;
68
69 return mipi_dsi_i2c_read(addr, reg, buf, 1);
70}
71
72int mipi_dsi_i2c_write_byte(uint8_t addr, uint8_t reg, uint8_t val)
73{
74 if (!i2c_dev)
75 return ERR_NOT_VALID;
76
77 unsigned char buf[2] = {reg, val};
78 struct i2c_msg msg_buf[] = {
79 {addr, I2C_M_WR, 2, buf},
80 };
81
82 int err = qup_i2c_xfer(i2c_dev, msg_buf, 1);
83 if (err < 0) {
84 dprintf(CRITICAL, "Write reg %x failed\n", reg);
85 return err;
86 }
87 return NO_ERROR;
88}
89
Siddharth Zaveri2472a602015-12-02 17:11:32 -050090int mipi_dsi_i2c_device_init(uint8_t blsp_id, uint8_t qup_id)
Vineet Bajaj037d3b02015-04-29 15:46:55 +053091{
Siddharth Zaveri2472a602015-12-02 17:11:32 -050092 i2c_dev = qup_blsp_i2c_init(blsp_id, qup_id,
Vineet Bajaj037d3b02015-04-29 15:46:55 +053093 I2C_CLK_FREQ, I2C_SRC_CLK_FREQ);
94 if(!i2c_dev) {
95 dprintf(CRITICAL, "mipi_dsi_i2c_device_init() failed\n");
96 return ERR_NOT_VALID;
97 }
98 return NO_ERROR;
99}