1 /* SPDX-License-Identifier: GPL-2.0 */
3 * MIPI Camera Control Interface (CCI) register access helpers.
5 * Copyright (C) 2023 Hans de Goede <hansg@kernel.org>
10 #include <linux/bitfield.h>
11 #include <linux/bits.h>
12 #include <linux/types.h>
18 * struct cci_reg_sequence - An individual write from a sequence of CCI writes
20 * @reg: Register address, use CCI_REG#() macros to encode reg width
21 * @val: Register value
23 * Register/value pairs for sequences of writes.
25 struct cci_reg_sequence {
31 * Macros to define register address with the register width encoded
32 * into the higher bits.
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)
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)
42 #define CCI_REG8(x) ((1 << CCI_REG_WIDTH_SHIFT) | (x))
43 #define CCI_REG16(x) ((2 << CCI_REG_WIDTH_SHIFT) | (x))
44 #define CCI_REG24(x) ((3 << CCI_REG_WIDTH_SHIFT) | (x))
45 #define CCI_REG32(x) ((4 << CCI_REG_WIDTH_SHIFT) | (x))
46 #define CCI_REG64(x) ((8 << CCI_REG_WIDTH_SHIFT) | (x))
49 * cci_read() - Read a value from a single CCI register
51 * @map: Register map to read from
52 * @reg: Register address to read, use CCI_REG#() macros to encode reg width
53 * @val: Pointer to store read value
54 * @err: Optional pointer to store errors, if a previous error is set
55 * then the read will be skipped
57 * Return: %0 on success or a negative error code on failure.
59 int cci_read(struct regmap *map, u32 reg, u64 *val, int *err);
62 * cci_write() - Write a value to a single CCI register
64 * @map: Register map to write to
65 * @reg: Register address to write, use CCI_REG#() macros to encode reg width
66 * @val: Value to be written
67 * @err: Optional pointer to store errors, if a previous error is set
68 * then the write will be skipped
70 * Return: %0 on success or a negative error code on failure.
72 int cci_write(struct regmap *map, u32 reg, u64 val, int *err);
75 * cci_update_bits() - Perform a read/modify/write cycle on
76 * a single CCI register
78 * @map: Register map to update
79 * @reg: Register address to update, use CCI_REG#() macros to encode reg width
80 * @mask: Bitmask to change
81 * @val: New value for bitmask
82 * @err: Optional pointer to store errors, if a previous error is set
83 * then the update will be skipped
85 * Note this uses read-modify-write to update the bits, atomicity with regards
86 * to other cci_*() register access functions is NOT guaranteed.
88 * Return: %0 on success or a negative error code on failure.
90 int cci_update_bits(struct regmap *map, u32 reg, u64 mask, u64 val, int *err);
93 * cci_multi_reg_write() - Write multiple registers to the device
95 * @map: Register map to write to
96 * @regs: Array of structures containing register-address, -value pairs to be
97 * written, register-addresses use CCI_REG#() macros to encode reg width
98 * @num_regs: Number of registers to write
99 * @err: Optional pointer to store errors, if a previous error is set
100 * then the write will be skipped
102 * Write multiple registers to the device where the set of register, value
103 * pairs are supplied in any order, possibly not all in a single range.
105 * Use of the CCI_REG#() macros to encode reg width is mandatory.
107 * For raw lists of register-address, -value pairs with only 8 bit
108 * wide writes regmap_multi_reg_write() can be used instead.
110 * Return: %0 on success or a negative error code on failure.
112 int cci_multi_reg_write(struct regmap *map, const struct cci_reg_sequence *regs,
113 unsigned int num_regs, int *err);
115 #if IS_ENABLED(CONFIG_V4L2_CCI_I2C)
117 * devm_cci_regmap_init_i2c() - Create regmap to use with cci_*() register
120 * @client: i2c_client to create the regmap for
121 * @reg_addr_bits: register address width to use (8 or 16)
123 * Note the memory for the created regmap is devm() managed, tied to the client.
125 * Return: %0 on success or a negative error code on failure.
127 struct regmap *devm_cci_regmap_init_i2c(struct i2c_client *client,