134009bffSJacopo Mondi /* SPDX-License-Identifier: GPL-2.0+ */ 234009bffSJacopo Mondi /* 334009bffSJacopo Mondi * Copyright (C) 2017-2020 Jacopo Mondi 434009bffSJacopo Mondi * Copyright (C) 2017-2020 Kieran Bingham 534009bffSJacopo Mondi * Copyright (C) 2017-2020 Laurent Pinchart 634009bffSJacopo Mondi * Copyright (C) 2017-2020 Niklas Söderlund 734009bffSJacopo Mondi * Copyright (C) 2016 Renesas Electronics Corporation 834009bffSJacopo Mondi * Copyright (C) 2015 Cogent Embedded, Inc. 934009bffSJacopo Mondi */ 1034009bffSJacopo Mondi 1134009bffSJacopo Mondi #include <linux/i2c.h> 1234009bffSJacopo Mondi 1334009bffSJacopo Mondi #define MAX9271_DEFAULT_ADDR 0x40 1434009bffSJacopo Mondi 1534009bffSJacopo Mondi /* Register 0x02 */ 1634009bffSJacopo Mondi #define MAX9271_SPREAD_SPECT_0 (0 << 5) 1734009bffSJacopo Mondi #define MAX9271_SPREAD_SPECT_05 (1 << 5) 1834009bffSJacopo Mondi #define MAX9271_SPREAD_SPECT_15 (2 << 5) 1934009bffSJacopo Mondi #define MAX9271_SPREAD_SPECT_1 (5 << 5) 2034009bffSJacopo Mondi #define MAX9271_SPREAD_SPECT_2 (3 << 5) 2134009bffSJacopo Mondi #define MAX9271_SPREAD_SPECT_3 (6 << 5) 2234009bffSJacopo Mondi #define MAX9271_SPREAD_SPECT_4 (7 << 5) 2334009bffSJacopo Mondi #define MAX9271_R02_RES BIT(4) 2434009bffSJacopo Mondi #define MAX9271_PCLK_AUTODETECT (3 << 2) 2534009bffSJacopo Mondi #define MAX9271_SERIAL_AUTODETECT (0x03) 2634009bffSJacopo Mondi /* Register 0x04 */ 2734009bffSJacopo Mondi #define MAX9271_SEREN BIT(7) 2834009bffSJacopo Mondi #define MAX9271_CLINKEN BIT(6) 2934009bffSJacopo Mondi #define MAX9271_PRBSEN BIT(5) 3034009bffSJacopo Mondi #define MAX9271_SLEEP BIT(4) 3134009bffSJacopo Mondi #define MAX9271_INTTYPE_I2C (0 << 2) 3234009bffSJacopo Mondi #define MAX9271_INTTYPE_UART (1 << 2) 3334009bffSJacopo Mondi #define MAX9271_INTTYPE_NONE (2 << 2) 3434009bffSJacopo Mondi #define MAX9271_REVCCEN BIT(1) 3534009bffSJacopo Mondi #define MAX9271_FWDCCEN BIT(0) 3634009bffSJacopo Mondi /* Register 0x07 */ 3734009bffSJacopo Mondi #define MAX9271_DBL BIT(7) 3834009bffSJacopo Mondi #define MAX9271_DRS BIT(6) 3934009bffSJacopo Mondi #define MAX9271_BWS BIT(5) 4034009bffSJacopo Mondi #define MAX9271_ES BIT(4) 4134009bffSJacopo Mondi #define MAX9271_HVEN BIT(2) 4234009bffSJacopo Mondi #define MAX9271_EDC_1BIT_PARITY (0 << 0) 4334009bffSJacopo Mondi #define MAX9271_EDC_6BIT_CRC (1 << 0) 4434009bffSJacopo Mondi #define MAX9271_EDC_6BIT_HAMMING (2 << 0) 4534009bffSJacopo Mondi /* Register 0x08 */ 4634009bffSJacopo Mondi #define MAX9271_INVVS BIT(7) 4734009bffSJacopo Mondi #define MAX9271_INVHS BIT(6) 4834009bffSJacopo Mondi #define MAX9271_REV_LOGAIN BIT(3) 4934009bffSJacopo Mondi #define MAX9271_REV_HIVTH BIT(0) 5034009bffSJacopo Mondi /* Register 0x09 */ 5134009bffSJacopo Mondi #define MAX9271_ID 0x09 5234009bffSJacopo Mondi /* Register 0x0d */ 5334009bffSJacopo Mondi #define MAX9271_I2CLOCACK BIT(7) 5434009bffSJacopo Mondi #define MAX9271_I2CSLVSH_1046NS_469NS (3 << 5) 5534009bffSJacopo Mondi #define MAX9271_I2CSLVSH_938NS_352NS (2 << 5) 5634009bffSJacopo Mondi #define MAX9271_I2CSLVSH_469NS_234NS (1 << 5) 5734009bffSJacopo Mondi #define MAX9271_I2CSLVSH_352NS_117NS (0 << 5) 5834009bffSJacopo Mondi #define MAX9271_I2CMSTBT_837KBPS (7 << 2) 5934009bffSJacopo Mondi #define MAX9271_I2CMSTBT_533KBPS (6 << 2) 6034009bffSJacopo Mondi #define MAX9271_I2CMSTBT_339KBPS (5 << 2) 6134009bffSJacopo Mondi #define MAX9271_I2CMSTBT_173KBPS (4 << 2) 6234009bffSJacopo Mondi #define MAX9271_I2CMSTBT_105KBPS (3 << 2) 6334009bffSJacopo Mondi #define MAX9271_I2CMSTBT_84KBPS (2 << 2) 6434009bffSJacopo Mondi #define MAX9271_I2CMSTBT_28KBPS (1 << 2) 6534009bffSJacopo Mondi #define MAX9271_I2CMSTBT_8KBPS (0 << 2) 6634009bffSJacopo Mondi #define MAX9271_I2CSLVTO_NONE (3 << 0) 6734009bffSJacopo Mondi #define MAX9271_I2CSLVTO_1024US (2 << 0) 6834009bffSJacopo Mondi #define MAX9271_I2CSLVTO_256US (1 << 0) 6934009bffSJacopo Mondi #define MAX9271_I2CSLVTO_64US (0 << 0) 7034009bffSJacopo Mondi /* Register 0x0f */ 7134009bffSJacopo Mondi #define MAX9271_GPIO5OUT BIT(5) 7234009bffSJacopo Mondi #define MAX9271_GPIO4OUT BIT(4) 7334009bffSJacopo Mondi #define MAX9271_GPIO3OUT BIT(3) 7434009bffSJacopo Mondi #define MAX9271_GPIO2OUT BIT(2) 7534009bffSJacopo Mondi #define MAX9271_GPIO1OUT BIT(1) 7634009bffSJacopo Mondi #define MAX9271_GPO BIT(0) 7734009bffSJacopo Mondi /* Register 0x15 */ 7834009bffSJacopo Mondi #define MAX9271_PCLKDET BIT(0) 7934009bffSJacopo Mondi 8034009bffSJacopo Mondi /** 8134009bffSJacopo Mondi * struct max9271_device - max9271 device 8234009bffSJacopo Mondi * @client: The i2c client for the max9271 instance 8334009bffSJacopo Mondi */ 8434009bffSJacopo Mondi struct max9271_device { 8534009bffSJacopo Mondi struct i2c_client *client; 8634009bffSJacopo Mondi }; 8734009bffSJacopo Mondi 8834009bffSJacopo Mondi /** 89*9e0bf839SJacopo Mondi * max9271_wake_up() - Wake up the serializer by issuing an i2c transaction 90*9e0bf839SJacopo Mondi * @dev: The max9271 device 91*9e0bf839SJacopo Mondi * 92*9e0bf839SJacopo Mondi * This function shall be called before any other interaction with the 93*9e0bf839SJacopo Mondi * serializer. 94*9e0bf839SJacopo Mondi */ 95*9e0bf839SJacopo Mondi void max9271_wake_up(struct max9271_device *dev); 96*9e0bf839SJacopo Mondi 97*9e0bf839SJacopo Mondi /** 9834009bffSJacopo Mondi * max9271_set_serial_link() - Enable/disable serial link 9934009bffSJacopo Mondi * @dev: The max9271 device 10034009bffSJacopo Mondi * @enable: Serial link enable/disable flag 10134009bffSJacopo Mondi * 10234009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 10334009bffSJacopo Mondi */ 10434009bffSJacopo Mondi int max9271_set_serial_link(struct max9271_device *dev, bool enable); 10534009bffSJacopo Mondi 10634009bffSJacopo Mondi /** 10734009bffSJacopo Mondi * max9271_configure_i2c() - Configure I2C bus parameters 10834009bffSJacopo Mondi * @dev: The max9271 device 10934009bffSJacopo Mondi * @i2c_config: The I2C bus configuration bit mask 11034009bffSJacopo Mondi * 11134009bffSJacopo Mondi * Configure MAX9271 I2C interface. The bus configuration provided in the 11234009bffSJacopo Mondi * @i2c_config parameter shall be assembled using bit values defined by the 11334009bffSJacopo Mondi * MAX9271_I2C* macros. 11434009bffSJacopo Mondi * 11534009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 11634009bffSJacopo Mondi */ 11734009bffSJacopo Mondi int max9271_configure_i2c(struct max9271_device *dev, u8 i2c_config); 11834009bffSJacopo Mondi 11934009bffSJacopo Mondi /** 12034009bffSJacopo Mondi * max9271_set_high_threshold() - Enable or disable reverse channel high 12134009bffSJacopo Mondi * threshold 12234009bffSJacopo Mondi * @dev: The max9271 device 12334009bffSJacopo Mondi * @enable: High threshold enable/disable flag 12434009bffSJacopo Mondi * 12534009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 12634009bffSJacopo Mondi */ 12734009bffSJacopo Mondi int max9271_set_high_threshold(struct max9271_device *dev, bool enable); 12834009bffSJacopo Mondi 12934009bffSJacopo Mondi /** 13034009bffSJacopo Mondi * max9271_configure_gmsl_link() - Configure the GMSL link 13134009bffSJacopo Mondi * @dev: The max9271 device 13234009bffSJacopo Mondi * 13334009bffSJacopo Mondi * FIXME: the GMSL link configuration is currently hardcoded and performed 13434009bffSJacopo Mondi * by programming registers 0x04, 0x07 and 0x02. 13534009bffSJacopo Mondi * 13634009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 13734009bffSJacopo Mondi */ 13834009bffSJacopo Mondi int max9271_configure_gmsl_link(struct max9271_device *dev); 13934009bffSJacopo Mondi 14034009bffSJacopo Mondi /** 14134009bffSJacopo Mondi * max9271_set_gpios() - Set gpio lines to physical high value 14234009bffSJacopo Mondi * @dev: The max9271 device 14334009bffSJacopo Mondi * @gpio_mask: The mask of gpio lines to set to high value 14434009bffSJacopo Mondi * 14534009bffSJacopo Mondi * The @gpio_mask parameter shall be assembled using the MAX9271_GP[IO|O]* 14634009bffSJacopo Mondi * bit values. 14734009bffSJacopo Mondi * 14834009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 14934009bffSJacopo Mondi */ 15034009bffSJacopo Mondi int max9271_set_gpios(struct max9271_device *dev, u8 gpio_mask); 15134009bffSJacopo Mondi 15234009bffSJacopo Mondi /** 15334009bffSJacopo Mondi * max9271_clear_gpios() - Set gpio lines to physical low value 15434009bffSJacopo Mondi * @dev: The max9271 device 15534009bffSJacopo Mondi * @gpio_mask: The mask of gpio lines to set to low value 15634009bffSJacopo Mondi * 15734009bffSJacopo Mondi * The @gpio_mask parameter shall be assembled using the MAX9271_GP[IO|O]* 15834009bffSJacopo Mondi * bit values. 15934009bffSJacopo Mondi * 16034009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 16134009bffSJacopo Mondi */ 16234009bffSJacopo Mondi int max9271_clear_gpios(struct max9271_device *dev, u8 gpio_mask); 16334009bffSJacopo Mondi 16434009bffSJacopo Mondi /** 16534009bffSJacopo Mondi * max9271_enable_gpios() - Enable gpio lines 16634009bffSJacopo Mondi * @dev: The max9271 device 16734009bffSJacopo Mondi * @gpio_mask: The mask of gpio lines to enable 16834009bffSJacopo Mondi * 16934009bffSJacopo Mondi * The @gpio_mask parameter shall be assembled using the MAX9271_GPIO* 17034009bffSJacopo Mondi * bit values. GPO line is always enabled by default. 17134009bffSJacopo Mondi * 17234009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 17334009bffSJacopo Mondi */ 17434009bffSJacopo Mondi int max9271_enable_gpios(struct max9271_device *dev, u8 gpio_mask); 17534009bffSJacopo Mondi 17634009bffSJacopo Mondi /** 17734009bffSJacopo Mondi * max9271_disable_gpios() - Disable gpio lines 17834009bffSJacopo Mondi * @dev: The max9271 device 17934009bffSJacopo Mondi * @gpio_mask: The mask of gpio lines to disable 18034009bffSJacopo Mondi * 18134009bffSJacopo Mondi * The @gpio_mask parameter shall be assembled using the MAX9271_GPIO* 18234009bffSJacopo Mondi * bit values. GPO line is always enabled by default and cannot be disabled. 18334009bffSJacopo Mondi * 18434009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 18534009bffSJacopo Mondi */ 18634009bffSJacopo Mondi int max9271_disable_gpios(struct max9271_device *dev, u8 gpio_mask); 18734009bffSJacopo Mondi 18834009bffSJacopo Mondi /** 18934009bffSJacopo Mondi * max9271_verify_id() - Read and verify MAX9271 id 19034009bffSJacopo Mondi * @dev: The max9271 device 19134009bffSJacopo Mondi * 19234009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 19334009bffSJacopo Mondi */ 19434009bffSJacopo Mondi int max9271_verify_id(struct max9271_device *dev); 19534009bffSJacopo Mondi 19634009bffSJacopo Mondi /** 19734009bffSJacopo Mondi * max9271_set_address() - Program a new I2C address 19834009bffSJacopo Mondi * @dev: The max9271 device 19934009bffSJacopo Mondi * @addr: The new I2C address in 7-bit format 20034009bffSJacopo Mondi * 20134009bffSJacopo Mondi * This function only takes care of programming the new I2C address @addr to 20234009bffSJacopo Mondi * in the MAX9271 chip registers, it is responsiblity of the caller to set 20334009bffSJacopo Mondi * the i2c address client to the @addr value to be able to communicate with 20434009bffSJacopo Mondi * the MAX9271 chip using the I2C framework APIs after this function returns. 20534009bffSJacopo Mondi * 20634009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 20734009bffSJacopo Mondi */ 20834009bffSJacopo Mondi int max9271_set_address(struct max9271_device *dev, u8 addr); 20934009bffSJacopo Mondi 21034009bffSJacopo Mondi /** 21134009bffSJacopo Mondi * max9271_set_deserializer_address() - Program the remote deserializer address 21234009bffSJacopo Mondi * @dev: The max9271 device 21334009bffSJacopo Mondi * @addr: The deserializer I2C address in 7-bit format 21434009bffSJacopo Mondi * 21534009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 21634009bffSJacopo Mondi */ 21734009bffSJacopo Mondi int max9271_set_deserializer_address(struct max9271_device *dev, u8 addr); 21834009bffSJacopo Mondi 21934009bffSJacopo Mondi /** 22034009bffSJacopo Mondi * max9271_set_translation() - Program I2C address translation 22134009bffSJacopo Mondi * @dev: The max9271 device 22234009bffSJacopo Mondi * @source: The I2C source address 22334009bffSJacopo Mondi * @dest: The I2C destination address 22434009bffSJacopo Mondi * 22534009bffSJacopo Mondi * Program address translation from @source to @dest. This is required to 22634009bffSJacopo Mondi * communicate with local devices that do not support address reprogramming. 22734009bffSJacopo Mondi * 22834009bffSJacopo Mondi * TODO: The device supports translation of two address, this function currently 22934009bffSJacopo Mondi * supports a single one. 23034009bffSJacopo Mondi * 23134009bffSJacopo Mondi * Return 0 on success or a negative error code on failure 23234009bffSJacopo Mondi */ 23334009bffSJacopo Mondi int max9271_set_translation(struct max9271_device *dev, u8 source, u8 dest); 234