/* SPDX-License-Identifier: GPL-2.0 */1/*2* MIPI Camera Control Interface (CCI) register access helpers.3*4* Copyright (C) 2023 Hans de Goede <[email protected]>5*/6#ifndef _V4L2_CCI_H7#define _V4L2_CCI_H89#include <linux/bitfield.h>10#include <linux/bits.h>11#include <linux/types.h>1213struct i2c_client;14struct regmap;1516/**17* struct cci_reg_sequence - An individual write from a sequence of CCI writes18*19* @reg: Register address, use CCI_REG#() macros to encode reg width20* @val: Register value21*22* Register/value pairs for sequences of writes.23*/24struct cci_reg_sequence {25u32 reg;26u64 val;27};2829/*30* Macros to define register address with the register width encoded31* into the higher bits.32*/33#define CCI_REG_ADDR_MASK GENMASK(15, 0)34#define CCI_REG_WIDTH_SHIFT 1635#define CCI_REG_WIDTH_MASK GENMASK(19, 16)36/*37* Private CCI register flags, for the use of drivers.38*/39#define CCI_REG_PRIVATE_SHIFT 28U40#define CCI_REG_PRIVATE_MASK GENMASK(31U, CCI_REG_PRIVATE_SHIFT)4142#define CCI_REG_WIDTH_BYTES(x) FIELD_GET(CCI_REG_WIDTH_MASK, x)43#define CCI_REG_WIDTH(x) (CCI_REG_WIDTH_BYTES(x) << 3)44#define CCI_REG_ADDR(x) FIELD_GET(CCI_REG_ADDR_MASK, x)45#define CCI_REG_LE BIT(20)4647#define CCI_REG8(x) ((1 << CCI_REG_WIDTH_SHIFT) | (x))48#define CCI_REG16(x) ((2 << CCI_REG_WIDTH_SHIFT) | (x))49#define CCI_REG24(x) ((3 << CCI_REG_WIDTH_SHIFT) | (x))50#define CCI_REG32(x) ((4 << CCI_REG_WIDTH_SHIFT) | (x))51#define CCI_REG64(x) ((8 << CCI_REG_WIDTH_SHIFT) | (x))52#define CCI_REG16_LE(x) (CCI_REG_LE | (2U << CCI_REG_WIDTH_SHIFT) | (x))53#define CCI_REG24_LE(x) (CCI_REG_LE | (3U << CCI_REG_WIDTH_SHIFT) | (x))54#define CCI_REG32_LE(x) (CCI_REG_LE | (4U << CCI_REG_WIDTH_SHIFT) | (x))55#define CCI_REG64_LE(x) (CCI_REG_LE | (8U << CCI_REG_WIDTH_SHIFT) | (x))5657/**58* cci_read() - Read a value from a single CCI register59*60* @map: Register map to read from61* @reg: Register address to read, use CCI_REG#() macros to encode reg width62* @val: Pointer to store read value63* @err: Optional pointer to store errors, if a previous error is set64* then the read will be skipped65*66* Return: %0 on success or a negative error code on failure.67*/68int cci_read(struct regmap *map, u32 reg, u64 *val, int *err);6970/**71* cci_write() - Write a value to a single CCI register72*73* @map: Register map to write to74* @reg: Register address to write, use CCI_REG#() macros to encode reg width75* @val: Value to be written76* @err: Optional pointer to store errors, if a previous error is set77* then the write will be skipped78*79* Return: %0 on success or a negative error code on failure.80*/81int cci_write(struct regmap *map, u32 reg, u64 val, int *err);8283/**84* cci_update_bits() - Perform a read/modify/write cycle on85* a single CCI register86*87* @map: Register map to update88* @reg: Register address to update, use CCI_REG#() macros to encode reg width89* @mask: Bitmask to change90* @val: New value for bitmask91* @err: Optional pointer to store errors, if a previous error is set92* then the update will be skipped93*94* Note this uses read-modify-write to update the bits, atomicity with regards95* to other cci_*() register access functions is NOT guaranteed.96*97* Return: %0 on success or a negative error code on failure.98*/99int cci_update_bits(struct regmap *map, u32 reg, u64 mask, u64 val, int *err);100101/**102* cci_multi_reg_write() - Write multiple registers to the device103*104* @map: Register map to write to105* @regs: Array of structures containing register-address, -value pairs to be106* written, register-addresses use CCI_REG#() macros to encode reg width107* @num_regs: Number of registers to write108* @err: Optional pointer to store errors, if a previous error is set109* then the write will be skipped110*111* Write multiple registers to the device where the set of register, value112* pairs are supplied in any order, possibly not all in a single range.113*114* Use of the CCI_REG#() macros to encode reg width is mandatory.115*116* For raw lists of register-address, -value pairs with only 8 bit117* wide writes regmap_multi_reg_write() can be used instead.118*119* Return: %0 on success or a negative error code on failure.120*/121int cci_multi_reg_write(struct regmap *map, const struct cci_reg_sequence *regs,122unsigned int num_regs, int *err);123124#if IS_ENABLED(CONFIG_V4L2_CCI_I2C)125/**126* devm_cci_regmap_init_i2c() - Create regmap to use with cci_*() register127* access functions128*129* @client: i2c_client to create the regmap for130* @reg_addr_bits: register address width to use (8 or 16)131*132* Note the memory for the created regmap is devm() managed, tied to the client.133*134* Return: %0 on success or a negative error code on failure.135*/136struct regmap *devm_cci_regmap_init_i2c(struct i2c_client *client,137int reg_addr_bits);138#endif139140#endif141142143