1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021-2023 Digiteq Automotive
4 * author: Martin Tuma <martin.tuma@digiteqautomotive.com>
6 * The i2c module unifies the I2C access to the serializes/deserializes. The I2C
7 * chips on the GMSL module use 16b addressing, the FPDL3 chips use standard
13 static int read_r16(struct i2c_client
*client
, u16 reg
, u8
*val
, int len
)
17 struct i2c_msg msg
[2] = {
31 buf
[0] = (reg
>> 8) & 0xff;
32 buf
[1] = (reg
>> 0) & 0xff;
34 ret
= i2c_transfer(client
->adapter
, msg
, 2);
43 static int write_r16(struct i2c_client
*client
, u16 reg
, const u8
*val
, int len
)
47 struct i2c_msg msg
[1] = {
56 if (2 + len
> sizeof(buf
))
59 buf
[0] = (reg
>> 8) & 0xff;
60 buf
[1] = (reg
>> 0) & 0xff;
61 memcpy(&buf
[2], val
, len
);
63 ret
= i2c_transfer(client
->adapter
, msg
, 1);
72 int mgb4_i2c_init(struct mgb4_i2c_client
*client
, struct i2c_adapter
*adap
,
73 struct i2c_board_info
const *info
, int addr_size
)
75 client
->client
= i2c_new_client_device(adap
, info
);
76 if (IS_ERR(client
->client
))
77 return PTR_ERR(client
->client
);
79 client
->addr_size
= addr_size
;
84 void mgb4_i2c_free(struct mgb4_i2c_client
*client
)
86 i2c_unregister_device(client
->client
);
89 s32
mgb4_i2c_read_byte(struct mgb4_i2c_client
*client
, u16 reg
)
94 if (client
->addr_size
== 8)
95 return i2c_smbus_read_byte_data(client
->client
, reg
);
97 ret
= read_r16(client
->client
, reg
, &b
, 1);
104 s32
mgb4_i2c_write_byte(struct mgb4_i2c_client
*client
, u16 reg
, u8 val
)
106 if (client
->addr_size
== 8)
107 return i2c_smbus_write_byte_data(client
->client
, reg
, val
);
109 return write_r16(client
->client
, reg
, &val
, 1);
112 s32
mgb4_i2c_mask_byte(struct mgb4_i2c_client
*client
, u16 reg
, u8 mask
, u8 val
)
117 ret
= mgb4_i2c_read_byte(client
, reg
);
120 val
|= (u8
)ret
& ~mask
;
123 return mgb4_i2c_write_byte(client
, reg
, val
);
126 int mgb4_i2c_configure(struct mgb4_i2c_client
*client
,
127 const struct mgb4_i2c_kv
*values
, size_t count
)
132 for (i
= 0; i
< count
; i
++) {
133 res
= mgb4_i2c_mask_byte(client
, values
[i
].reg
, values
[i
].mask
,