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>
21 #include <linux/module.h>
25 static int max9271_read(struct max9271_device
*dev
, u8 reg
)
29 dev_dbg(&dev
->client
->dev
, "%s(0x%02x)\n", __func__
, reg
);
31 ret
= i2c_smbus_read_byte_data(dev
->client
, reg
);
33 dev_dbg(&dev
->client
->dev
,
34 "%s: register 0x%02x read failed (%d)\n",
40 static int max9271_write(struct max9271_device
*dev
, u8 reg
, u8 val
)
44 dev_dbg(&dev
->client
->dev
, "%s(0x%02x, 0x%02x)\n", __func__
, reg
, val
);
46 ret
= i2c_smbus_write_byte_data(dev
->client
, reg
, val
);
48 dev_err(&dev
->client
->dev
,
49 "%s: register 0x%02x write failed (%d)\n",
56 * max9271_pclk_detect() - Detect valid pixel clock from image sensor
58 * Wait up to 10ms for a valid pixel clock.
60 * Returns 0 for success, < 0 for pixel clock not properly detected
62 static int max9271_pclk_detect(struct max9271_device
*dev
)
67 for (i
= 0; i
< 100; i
++) {
68 ret
= max9271_read(dev
, 0x15);
72 if (ret
& MAX9271_PCLKDET
)
75 usleep_range(50, 100);
78 dev_err(&dev
->client
->dev
, "Unable to detect valid pixel clock\n");
83 void max9271_wake_up(struct max9271_device
*dev
)
86 * Use the chip default address as this function has to be called
87 * before any other one.
89 dev
->client
->addr
= MAX9271_DEFAULT_ADDR
;
90 i2c_smbus_read_byte(dev
->client
);
91 usleep_range(5000, 8000);
93 EXPORT_SYMBOL_GPL(max9271_wake_up
);
95 int max9271_set_serial_link(struct max9271_device
*dev
, bool enable
)
98 u8 val
= MAX9271_REVCCEN
| MAX9271_FWDCCEN
;
101 ret
= max9271_pclk_detect(dev
);
105 val
|= MAX9271_SEREN
;
107 val
|= MAX9271_CLINKEN
;
111 * The serializer temporarily disables the reverse control channel for
112 * 350µs after starting/stopping the forward serial link, but the
113 * deserializer synchronization time isn't clearly documented.
115 * According to the serializer datasheet we should wait 3ms, while
116 * according to the deserializer datasheet we should wait 5ms.
118 * Short delays here appear to show bit-errors in the writes following.
119 * Therefore a conservative delay seems best here.
121 ret
= max9271_write(dev
, 0x04, val
);
125 usleep_range(5000, 8000);
129 EXPORT_SYMBOL_GPL(max9271_set_serial_link
);
131 int max9271_configure_i2c(struct max9271_device
*dev
, u8 i2c_config
)
135 ret
= max9271_write(dev
, 0x0d, i2c_config
);
139 /* The delay required after an I2C bus configuration change is not
140 * characterized in the serializer manual. Sleep up to 5msec to
143 usleep_range(3500, 5000);
147 EXPORT_SYMBOL_GPL(max9271_configure_i2c
);
149 int max9271_set_high_threshold(struct max9271_device
*dev
, bool enable
)
153 ret
= max9271_read(dev
, 0x08);
158 * Enable or disable reverse channel high threshold to increase
159 * immunity to power supply noise.
161 ret
= max9271_write(dev
, 0x08, enable
? ret
| BIT(0) : ret
& ~BIT(0));
165 usleep_range(2000, 2500);
169 EXPORT_SYMBOL_GPL(max9271_set_high_threshold
);
171 int max9271_configure_gmsl_link(struct max9271_device
*dev
)
176 * Configure the GMSL link:
178 * - Double input mode, high data rate, 24-bit mode
179 * - Latch input data on PCLKIN rising edge
180 * - Enable HS/VS encoding
181 * - 1-bit parity error detection
183 * TODO: Make the GMSL link configuration parametric.
185 ret
= max9271_write(dev
, 0x07, MAX9271_DBL
| MAX9271_HVEN
|
186 MAX9271_EDC_1BIT_PARITY
);
190 usleep_range(5000, 8000);
193 * Adjust spread spectrum to +4% and auto-detect pixel clock
194 * and serial link rate.
196 ret
= max9271_write(dev
, 0x02,
197 MAX9271_SPREAD_SPECT_4
| MAX9271_R02_RES
|
198 MAX9271_PCLK_AUTODETECT
|
199 MAX9271_SERIAL_AUTODETECT
);
203 usleep_range(5000, 8000);
207 EXPORT_SYMBOL_GPL(max9271_configure_gmsl_link
);
209 int max9271_set_gpios(struct max9271_device
*dev
, u8 gpio_mask
)
213 ret
= max9271_read(dev
, 0x0f);
218 ret
= max9271_write(dev
, 0x0f, ret
);
220 dev_err(&dev
->client
->dev
, "Failed to set gpio (%d)\n", ret
);
224 usleep_range(3500, 5000);
228 EXPORT_SYMBOL_GPL(max9271_set_gpios
);
230 int max9271_clear_gpios(struct max9271_device
*dev
, u8 gpio_mask
)
234 ret
= max9271_read(dev
, 0x0f);
239 ret
= max9271_write(dev
, 0x0f, ret
);
241 dev_err(&dev
->client
->dev
, "Failed to clear gpio (%d)\n", ret
);
245 usleep_range(3500, 5000);
249 EXPORT_SYMBOL_GPL(max9271_clear_gpios
);
251 int max9271_enable_gpios(struct max9271_device
*dev
, u8 gpio_mask
)
255 ret
= max9271_read(dev
, 0x0e);
259 /* BIT(0) reserved: GPO is always enabled. */
260 ret
|= (gpio_mask
& ~BIT(0));
261 ret
= max9271_write(dev
, 0x0e, ret
);
263 dev_err(&dev
->client
->dev
, "Failed to enable gpio (%d)\n", ret
);
267 usleep_range(3500, 5000);
271 EXPORT_SYMBOL_GPL(max9271_enable_gpios
);
273 int max9271_disable_gpios(struct max9271_device
*dev
, u8 gpio_mask
)
277 ret
= max9271_read(dev
, 0x0e);
281 /* BIT(0) reserved: GPO cannot be disabled */
282 ret
&= ~(gpio_mask
| BIT(0));
283 ret
= max9271_write(dev
, 0x0e, ret
);
285 dev_err(&dev
->client
->dev
, "Failed to disable gpio (%d)\n", ret
);
289 usleep_range(3500, 5000);
293 EXPORT_SYMBOL_GPL(max9271_disable_gpios
);
295 int max9271_verify_id(struct max9271_device
*dev
)
299 ret
= max9271_read(dev
, 0x1e);
301 dev_err(&dev
->client
->dev
, "MAX9271 ID read failed (%d)\n",
306 if (ret
!= MAX9271_ID
) {
307 dev_err(&dev
->client
->dev
, "MAX9271 ID mismatch (0x%02x)\n",
314 EXPORT_SYMBOL_GPL(max9271_verify_id
);
316 int max9271_set_address(struct max9271_device
*dev
, u8 addr
)
320 ret
= max9271_write(dev
, 0x00, addr
<< 1);
322 dev_err(&dev
->client
->dev
,
323 "MAX9271 I2C address change failed (%d)\n", ret
);
326 usleep_range(3500, 5000);
330 EXPORT_SYMBOL_GPL(max9271_set_address
);
332 int max9271_set_deserializer_address(struct max9271_device
*dev
, u8 addr
)
336 ret
= max9271_write(dev
, 0x01, addr
<< 1);
338 dev_err(&dev
->client
->dev
,
339 "MAX9271 deserializer address set failed (%d)\n", ret
);
342 usleep_range(3500, 5000);
346 EXPORT_SYMBOL_GPL(max9271_set_deserializer_address
);
348 int max9271_set_translation(struct max9271_device
*dev
, u8 source
, u8 dest
)
352 ret
= max9271_write(dev
, 0x09, source
<< 1);
354 dev_err(&dev
->client
->dev
,
355 "MAX9271 I2C translation setup failed (%d)\n", ret
);
358 usleep_range(3500, 5000);
360 ret
= max9271_write(dev
, 0x0a, dest
<< 1);
362 dev_err(&dev
->client
->dev
,
363 "MAX9271 I2C translation setup failed (%d)\n", ret
);
366 usleep_range(3500, 5000);
370 EXPORT_SYMBOL_GPL(max9271_set_translation
);
372 MODULE_DESCRIPTION("Maxim MAX9271 GMSL Serializer");
373 MODULE_AUTHOR("Jacopo Mondi");
374 MODULE_LICENSE("GPL v2");