xref: /openbmc/linux/include/media/v4l2-cci.h (revision 2984f26a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * MIPI Camera Control Interface (CCI) register access helpers.
4  *
5  * Copyright (C) 2023 Hans de Goede <hansg@kernel.org>
6  */
7 #ifndef _V4L2_CCI_H
8 #define _V4L2_CCI_H
9 
10 #include <linux/bitfield.h>
11 #include <linux/bits.h>
12 #include <linux/types.h>
13 
14 struct i2c_client;
15 struct regmap;
16 
17 /**
18  * struct cci_reg_sequence - An individual write from a sequence of CCI writes
19  *
20  * @reg: Register address, use CCI_REG#() macros to encode reg width
21  * @val: Register value
22  *
23  * Register/value pairs for sequences of writes.
24  */
25 struct cci_reg_sequence {
26 	u32 reg;
27 	u64 val;
28 };
29 
30 /*
31  * Macros to define register address with the register width encoded
32  * into the higher bits.
33  */
34 #define CCI_REG_ADDR_MASK		GENMASK(15, 0)
35 #define CCI_REG_WIDTH_SHIFT		16
36 #define CCI_REG_WIDTH_MASK		GENMASK(19, 16)
37 
38 #define CCI_REG_WIDTH_BYTES(x)		FIELD_GET(CCI_REG_WIDTH_MASK, x)
39 #define CCI_REG_WIDTH(x)		(CCI_REG_WIDTH_BYTES(x) << 3)
40 #define CCI_REG_ADDR(x)			FIELD_GET(CCI_REG_ADDR_MASK, x)
41 #define CCI_REG_LE			BIT(20)
42 
43 #define CCI_REG8(x)			((1 << CCI_REG_WIDTH_SHIFT) | (x))
44 #define CCI_REG16(x)			((2 << CCI_REG_WIDTH_SHIFT) | (x))
45 #define CCI_REG24(x)			((3 << CCI_REG_WIDTH_SHIFT) | (x))
46 #define CCI_REG32(x)			((4 << CCI_REG_WIDTH_SHIFT) | (x))
47 #define CCI_REG64(x)			((8 << CCI_REG_WIDTH_SHIFT) | (x))
48 #define CCI_REG16_LE(x)			(CCI_REG_LE | (2U << CCI_REG_WIDTH_SHIFT) | (x))
49 #define CCI_REG24_LE(x)			(CCI_REG_LE | (3U << CCI_REG_WIDTH_SHIFT) | (x))
50 #define CCI_REG32_LE(x)			(CCI_REG_LE | (4U << CCI_REG_WIDTH_SHIFT) | (x))
51 #define CCI_REG64_LE(x)			(CCI_REG_LE | (8U << CCI_REG_WIDTH_SHIFT) | (x))
52 
53 /**
54  * cci_read() - Read a value from a single CCI register
55  *
56  * @map: Register map to read from
57  * @reg: Register address to read, use CCI_REG#() macros to encode reg width
58  * @val: Pointer to store read value
59  * @err: Optional pointer to store errors, if a previous error is set
60  *       then the read will be skipped
61  *
62  * Return: %0 on success or a negative error code on failure.
63  */
64 int cci_read(struct regmap *map, u32 reg, u64 *val, int *err);
65 
66 /**
67  * cci_write() - Write a value to a single CCI register
68  *
69  * @map: Register map to write to
70  * @reg: Register address to write, use CCI_REG#() macros to encode reg width
71  * @val: Value to be written
72  * @err: Optional pointer to store errors, if a previous error is set
73  *       then the write will be skipped
74  *
75  * Return: %0 on success or a negative error code on failure.
76  */
77 int cci_write(struct regmap *map, u32 reg, u64 val, int *err);
78 
79 /**
80  * cci_update_bits() - Perform a read/modify/write cycle on
81  *                     a single CCI register
82  *
83  * @map: Register map to update
84  * @reg: Register address to update, use CCI_REG#() macros to encode reg width
85  * @mask: Bitmask to change
86  * @val: New value for bitmask
87  * @err: Optional pointer to store errors, if a previous error is set
88  *       then the update will be skipped
89  *
90  * Note this uses read-modify-write to update the bits, atomicity with regards
91  * to other cci_*() register access functions is NOT guaranteed.
92  *
93  * Return: %0 on success or a negative error code on failure.
94  */
95 int cci_update_bits(struct regmap *map, u32 reg, u64 mask, u64 val, int *err);
96 
97 /**
98  * cci_multi_reg_write() - Write multiple registers to the device
99  *
100  * @map: Register map to write to
101  * @regs: Array of structures containing register-address, -value pairs to be
102  *        written, register-addresses use CCI_REG#() macros to encode reg width
103  * @num_regs: Number of registers to write
104  * @err: Optional pointer to store errors, if a previous error is set
105  *       then the write will be skipped
106  *
107  * Write multiple registers to the device where the set of register, value
108  * pairs are supplied in any order, possibly not all in a single range.
109  *
110  * Use of the CCI_REG#() macros to encode reg width is mandatory.
111  *
112  * For raw lists of register-address, -value pairs with only 8 bit
113  * wide writes regmap_multi_reg_write() can be used instead.
114  *
115  * Return: %0 on success or a negative error code on failure.
116  */
117 int cci_multi_reg_write(struct regmap *map, const struct cci_reg_sequence *regs,
118 			unsigned int num_regs, int *err);
119 
120 #if IS_ENABLED(CONFIG_V4L2_CCI_I2C)
121 /**
122  * devm_cci_regmap_init_i2c() - Create regmap to use with cci_*() register
123  *                              access functions
124  *
125  * @client: i2c_client to create the regmap for
126  * @reg_addr_bits: register address width to use (8 or 16)
127  *
128  * Note the memory for the created regmap is devm() managed, tied to the client.
129  *
130  * Return: %0 on success or a negative error code on failure.
131  */
132 struct regmap *devm_cci_regmap_init_i2c(struct i2c_client *client,
133 					int reg_addr_bits);
134 #endif
135 
136 #endif
137