Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / i2c / max9271.c
blobc247db569bab08a4e50cd5d2d1461c7b515c93c8
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
22 #include "max9271.h"
24 static int max9271_read(struct max9271_device *dev, u8 reg)
26 int ret;
28 dev_dbg(&dev->client->dev, "%s(0x%02x)\n", __func__, reg);
30 ret = i2c_smbus_read_byte_data(dev->client, reg);
31 if (ret < 0)
32 dev_dbg(&dev->client->dev,
33 "%s: register 0x%02x read failed (%d)\n",
34 __func__, reg, ret);
36 return ret;
39 static int max9271_write(struct max9271_device *dev, u8 reg, u8 val)
41 int ret;
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);
46 if (ret < 0)
47 dev_err(&dev->client->dev,
48 "%s: register 0x%02x write failed (%d)\n",
49 __func__, reg, ret);
51 return ret;
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)
63 unsigned int i;
64 int ret;
66 for (i = 0; i < 100; i++) {
67 ret = max9271_read(dev, 0x15);
68 if (ret < 0)
69 return ret;
71 if (ret & MAX9271_PCLKDET)
72 return 0;
74 usleep_range(50, 100);
77 dev_err(&dev->client->dev, "Unable to detect valid pixel clock\n");
79 return -EIO;
82 int max9271_set_serial_link(struct max9271_device *dev, bool enable)
84 int ret;
85 u8 val = MAX9271_REVCCEN | MAX9271_FWDCCEN;
87 if (enable) {
88 ret = max9271_pclk_detect(dev);
89 if (ret)
90 return ret;
92 val |= MAX9271_SEREN;
93 } else {
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);
111 return 0;
113 EXPORT_SYMBOL_GPL(max9271_set_serial_link);
115 int max9271_configure_i2c(struct max9271_device *dev, u8 i2c_config)
117 int ret;
119 ret = max9271_write(dev, 0x0d, i2c_config);
120 if (ret)
121 return ret;
123 /* The delay required after an I2C bus configuration change is not
124 * characterized in the serializer manual. Sleep up to 5msec to
125 * stay safe.
127 usleep_range(3500, 5000);
129 return 0;
131 EXPORT_SYMBOL_GPL(max9271_configure_i2c);
133 int max9271_set_high_threshold(struct max9271_device *dev, bool enable)
135 int ret;
137 ret = max9271_read(dev, 0x08);
138 if (ret < 0)
139 return ret;
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);
148 return 0;
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);
176 return 0;
178 EXPORT_SYMBOL_GPL(max9271_configure_gmsl_link);
180 int max9271_set_gpios(struct max9271_device *dev, u8 gpio_mask)
182 int ret;
184 ret = max9271_read(dev, 0x0f);
185 if (ret < 0)
186 return 0;
188 ret |= gpio_mask;
189 ret = max9271_write(dev, 0x0f, ret);
190 if (ret < 0) {
191 dev_err(&dev->client->dev, "Failed to set gpio (%d)\n", ret);
192 return ret;
195 usleep_range(3500, 5000);
197 return 0;
199 EXPORT_SYMBOL_GPL(max9271_set_gpios);
201 int max9271_clear_gpios(struct max9271_device *dev, u8 gpio_mask)
203 int ret;
205 ret = max9271_read(dev, 0x0f);
206 if (ret < 0)
207 return 0;
209 ret &= ~gpio_mask;
210 ret = max9271_write(dev, 0x0f, ret);
211 if (ret < 0) {
212 dev_err(&dev->client->dev, "Failed to clear gpio (%d)\n", ret);
213 return ret;
216 usleep_range(3500, 5000);
218 return 0;
220 EXPORT_SYMBOL_GPL(max9271_clear_gpios);
222 int max9271_enable_gpios(struct max9271_device *dev, u8 gpio_mask)
224 int ret;
226 ret = max9271_read(dev, 0x0e);
227 if (ret < 0)
228 return 0;
230 /* BIT(0) reserved: GPO is always enabled. */
231 ret |= (gpio_mask & ~BIT(0));
232 ret = max9271_write(dev, 0x0e, ret);
233 if (ret < 0) {
234 dev_err(&dev->client->dev, "Failed to enable gpio (%d)\n", ret);
235 return ret;
238 usleep_range(3500, 5000);
240 return 0;
242 EXPORT_SYMBOL_GPL(max9271_enable_gpios);
244 int max9271_disable_gpios(struct max9271_device *dev, u8 gpio_mask)
246 int ret;
248 ret = max9271_read(dev, 0x0e);
249 if (ret < 0)
250 return 0;
252 /* BIT(0) reserved: GPO cannot be disabled */
253 ret &= ~(gpio_mask | BIT(0));
254 ret = max9271_write(dev, 0x0e, ret);
255 if (ret < 0) {
256 dev_err(&dev->client->dev, "Failed to disable gpio (%d)\n", ret);
257 return ret;
260 usleep_range(3500, 5000);
262 return 0;
264 EXPORT_SYMBOL_GPL(max9271_disable_gpios);
266 int max9271_verify_id(struct max9271_device *dev)
268 int ret;
270 ret = max9271_read(dev, 0x1e);
271 if (ret < 0) {
272 dev_err(&dev->client->dev, "MAX9271 ID read failed (%d)\n",
273 ret);
274 return ret;
277 if (ret != MAX9271_ID) {
278 dev_err(&dev->client->dev, "MAX9271 ID mismatch (0x%02x)\n",
279 ret);
280 return -ENXIO;
283 return 0;
285 EXPORT_SYMBOL_GPL(max9271_verify_id);
287 int max9271_set_address(struct max9271_device *dev, u8 addr)
289 int ret;
291 ret = max9271_write(dev, 0x00, addr << 1);
292 if (ret < 0) {
293 dev_err(&dev->client->dev,
294 "MAX9271 I2C address change failed (%d)\n", ret);
295 return ret;
297 usleep_range(3500, 5000);
299 return 0;
301 EXPORT_SYMBOL_GPL(max9271_set_address);
303 int max9271_set_deserializer_address(struct max9271_device *dev, u8 addr)
305 int ret;
307 ret = max9271_write(dev, 0x01, addr << 1);
308 if (ret < 0) {
309 dev_err(&dev->client->dev,
310 "MAX9271 deserializer address set failed (%d)\n", ret);
311 return ret;
313 usleep_range(3500, 5000);
315 return 0;
317 EXPORT_SYMBOL_GPL(max9271_set_deserializer_address);
319 int max9271_set_translation(struct max9271_device *dev, u8 source, u8 dest)
321 int ret;
323 ret = max9271_write(dev, 0x09, source << 1);
324 if (ret < 0) {
325 dev_err(&dev->client->dev,
326 "MAX9271 I2C translation setup failed (%d)\n", ret);
327 return ret;
329 usleep_range(3500, 5000);
331 ret = max9271_write(dev, 0x0a, dest << 1);
332 if (ret < 0) {
333 dev_err(&dev->client->dev,
334 "MAX9271 I2C translation setup failed (%d)\n", ret);
335 return ret;
337 usleep_range(3500, 5000);
339 return 0;
341 EXPORT_SYMBOL_GPL(max9271_set_translation);