1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017-2020 Jacopo Mondi
4 * Copyright (C) 2017-2020 Kieran Bingham
5 * Copyright (C) 2017-2020 Laurent Pinchart
6 * Copyright (C) 2017-2020 Niklas Söderlund
7 * Copyright (C) 2016 Renesas Electronics Corporation
8 * Copyright (C) 2015 Cogent Embedded, Inc.
10 * This file exports functions to control the Maxim MAX9271 GMSL serializer
11 * chip. This is not a self-contained driver, as MAX9271 is usually embedded in
12 * camera modules with at least one image sensor and optional additional
13 * components, such as uController units or ISPs/DSPs.
15 * Drivers for the camera modules (i.e. rdacm20/21) are expected to use
16 * functions exported from this library driver to maximize code re-use.
19 #include <linux/delay.h>
20 #include <linux/i2c.h>
24 static int max9271_read(struct max9271_device
*dev
, u8 reg
)
28 dev_dbg(&dev
->client
->dev
, "%s(0x%02x)\n", __func__
, reg
);
30 ret
= i2c_smbus_read_byte_data(dev
->client
, reg
);
32 dev_dbg(&dev
->client
->dev
,
33 "%s: register 0x%02x read failed (%d)\n",
39 static int max9271_write(struct max9271_device
*dev
, u8 reg
, u8 val
)
43 dev_dbg(&dev
->client
->dev
, "%s(0x%02x, 0x%02x)\n", __func__
, reg
, val
);
45 ret
= i2c_smbus_write_byte_data(dev
->client
, reg
, val
);
47 dev_err(&dev
->client
->dev
,
48 "%s: register 0x%02x write failed (%d)\n",
55 * max9271_pclk_detect() - Detect valid pixel clock from image sensor
57 * Wait up to 10ms for a valid pixel clock.
59 * Returns 0 for success, < 0 for pixel clock not properly detected
61 static int max9271_pclk_detect(struct max9271_device
*dev
)
66 for (i
= 0; i
< 100; i
++) {
67 ret
= max9271_read(dev
, 0x15);
71 if (ret
& MAX9271_PCLKDET
)
74 usleep_range(50, 100);
77 dev_err(&dev
->client
->dev
, "Unable to detect valid pixel clock\n");
82 int max9271_set_serial_link(struct max9271_device
*dev
, bool enable
)
85 u8 val
= MAX9271_REVCCEN
| MAX9271_FWDCCEN
;
88 ret
= max9271_pclk_detect(dev
);
94 val
|= MAX9271_CLINKEN
;
98 * The serializer temporarily disables the reverse control channel for
99 * 350µs after starting/stopping the forward serial link, but the
100 * deserializer synchronization time isn't clearly documented.
102 * According to the serializer datasheet we should wait 3ms, while
103 * according to the deserializer datasheet we should wait 5ms.
105 * Short delays here appear to show bit-errors in the writes following.
106 * Therefore a conservative delay seems best here.
108 max9271_write(dev
, 0x04, val
);
109 usleep_range(5000, 8000);
113 EXPORT_SYMBOL_GPL(max9271_set_serial_link
);
115 int max9271_configure_i2c(struct max9271_device
*dev
, u8 i2c_config
)
119 ret
= max9271_write(dev
, 0x0d, i2c_config
);
123 /* The delay required after an I2C bus configuration change is not
124 * characterized in the serializer manual. Sleep up to 5msec to
127 usleep_range(3500, 5000);
131 EXPORT_SYMBOL_GPL(max9271_configure_i2c
);
133 int max9271_set_high_threshold(struct max9271_device
*dev
, bool enable
)
137 ret
= max9271_read(dev
, 0x08);
142 * Enable or disable reverse channel high threshold to increase
143 * immunity to power supply noise.
145 max9271_write(dev
, 0x08, enable
? ret
| BIT(0) : ret
& ~BIT(0));
146 usleep_range(2000, 2500);
150 EXPORT_SYMBOL_GPL(max9271_set_high_threshold
);
152 int max9271_configure_gmsl_link(struct max9271_device
*dev
)
155 * Configure the GMSL link:
157 * - Double input mode, high data rate, 24-bit mode
158 * - Latch input data on PCLKIN rising edge
159 * - Enable HS/VS encoding
160 * - 1-bit parity error detection
162 * TODO: Make the GMSL link configuration parametric.
164 max9271_write(dev
, 0x07, MAX9271_DBL
| MAX9271_HVEN
|
165 MAX9271_EDC_1BIT_PARITY
);
166 usleep_range(5000, 8000);
169 * Adjust spread spectrum to +4% and auto-detect pixel clock
170 * and serial link rate.
172 max9271_write(dev
, 0x02, MAX9271_SPREAD_SPECT_4
| MAX9271_R02_RES
|
173 MAX9271_PCLK_AUTODETECT
| MAX9271_SERIAL_AUTODETECT
);
174 usleep_range(5000, 8000);
178 EXPORT_SYMBOL_GPL(max9271_configure_gmsl_link
);
180 int max9271_set_gpios(struct max9271_device
*dev
, u8 gpio_mask
)
184 ret
= max9271_read(dev
, 0x0f);
189 ret
= max9271_write(dev
, 0x0f, ret
);
191 dev_err(&dev
->client
->dev
, "Failed to set gpio (%d)\n", ret
);
195 usleep_range(3500, 5000);
199 EXPORT_SYMBOL_GPL(max9271_set_gpios
);
201 int max9271_clear_gpios(struct max9271_device
*dev
, u8 gpio_mask
)
205 ret
= max9271_read(dev
, 0x0f);
210 ret
= max9271_write(dev
, 0x0f, ret
);
212 dev_err(&dev
->client
->dev
, "Failed to clear gpio (%d)\n", ret
);
216 usleep_range(3500, 5000);
220 EXPORT_SYMBOL_GPL(max9271_clear_gpios
);
222 int max9271_enable_gpios(struct max9271_device
*dev
, u8 gpio_mask
)
226 ret
= max9271_read(dev
, 0x0e);
230 /* BIT(0) reserved: GPO is always enabled. */
231 ret
|= (gpio_mask
& ~BIT(0));
232 ret
= max9271_write(dev
, 0x0e, ret
);
234 dev_err(&dev
->client
->dev
, "Failed to enable gpio (%d)\n", ret
);
238 usleep_range(3500, 5000);
242 EXPORT_SYMBOL_GPL(max9271_enable_gpios
);
244 int max9271_disable_gpios(struct max9271_device
*dev
, u8 gpio_mask
)
248 ret
= max9271_read(dev
, 0x0e);
252 /* BIT(0) reserved: GPO cannot be disabled */
253 ret
&= ~(gpio_mask
| BIT(0));
254 ret
= max9271_write(dev
, 0x0e, ret
);
256 dev_err(&dev
->client
->dev
, "Failed to disable gpio (%d)\n", ret
);
260 usleep_range(3500, 5000);
264 EXPORT_SYMBOL_GPL(max9271_disable_gpios
);
266 int max9271_verify_id(struct max9271_device
*dev
)
270 ret
= max9271_read(dev
, 0x1e);
272 dev_err(&dev
->client
->dev
, "MAX9271 ID read failed (%d)\n",
277 if (ret
!= MAX9271_ID
) {
278 dev_err(&dev
->client
->dev
, "MAX9271 ID mismatch (0x%02x)\n",
285 EXPORT_SYMBOL_GPL(max9271_verify_id
);
287 int max9271_set_address(struct max9271_device
*dev
, u8 addr
)
291 ret
= max9271_write(dev
, 0x00, addr
<< 1);
293 dev_err(&dev
->client
->dev
,
294 "MAX9271 I2C address change failed (%d)\n", ret
);
297 usleep_range(3500, 5000);
301 EXPORT_SYMBOL_GPL(max9271_set_address
);
303 int max9271_set_deserializer_address(struct max9271_device
*dev
, u8 addr
)
307 ret
= max9271_write(dev
, 0x01, addr
<< 1);
309 dev_err(&dev
->client
->dev
,
310 "MAX9271 deserializer address set failed (%d)\n", ret
);
313 usleep_range(3500, 5000);
317 EXPORT_SYMBOL_GPL(max9271_set_deserializer_address
);
319 int max9271_set_translation(struct max9271_device
*dev
, u8 source
, u8 dest
)
323 ret
= max9271_write(dev
, 0x09, source
<< 1);
325 dev_err(&dev
->client
->dev
,
326 "MAX9271 I2C translation setup failed (%d)\n", ret
);
329 usleep_range(3500, 5000);
331 ret
= max9271_write(dev
, 0x0a, dest
<< 1);
333 dev_err(&dev
->client
->dev
,
334 "MAX9271 I2C translation setup failed (%d)\n", ret
);
337 usleep_range(3500, 5000);
341 EXPORT_SYMBOL_GPL(max9271_set_translation
);