Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / media / i2c / max9271.c
blobff86c8c4ea61dbbfbe98b67172bd1b41775c94a0
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>
21 #include <linux/module.h>
23 #include "max9271.h"
25 static int max9271_read(struct max9271_device *dev, u8 reg)
27 int ret;
29 dev_dbg(&dev->client->dev, "%s(0x%02x)\n", __func__, reg);
31 ret = i2c_smbus_read_byte_data(dev->client, reg);
32 if (ret < 0)
33 dev_dbg(&dev->client->dev,
34 "%s: register 0x%02x read failed (%d)\n",
35 __func__, reg, ret);
37 return ret;
40 static int max9271_write(struct max9271_device *dev, u8 reg, u8 val)
42 int ret;
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);
47 if (ret < 0)
48 dev_err(&dev->client->dev,
49 "%s: register 0x%02x write failed (%d)\n",
50 __func__, reg, ret);
52 return ret;
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)
64 unsigned int i;
65 int ret;
67 for (i = 0; i < 100; i++) {
68 ret = max9271_read(dev, 0x15);
69 if (ret < 0)
70 return ret;
72 if (ret & MAX9271_PCLKDET)
73 return 0;
75 usleep_range(50, 100);
78 dev_err(&dev->client->dev, "Unable to detect valid pixel clock\n");
80 return -EIO;
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)
97 int ret;
98 u8 val = MAX9271_REVCCEN | MAX9271_FWDCCEN;
100 if (enable) {
101 ret = max9271_pclk_detect(dev);
102 if (ret)
103 return ret;
105 val |= MAX9271_SEREN;
106 } else {
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);
122 if (ret < 0)
123 return ret;
125 usleep_range(5000, 8000);
127 return 0;
129 EXPORT_SYMBOL_GPL(max9271_set_serial_link);
131 int max9271_configure_i2c(struct max9271_device *dev, u8 i2c_config)
133 int ret;
135 ret = max9271_write(dev, 0x0d, i2c_config);
136 if (ret < 0)
137 return ret;
139 /* The delay required after an I2C bus configuration change is not
140 * characterized in the serializer manual. Sleep up to 5msec to
141 * stay safe.
143 usleep_range(3500, 5000);
145 return 0;
147 EXPORT_SYMBOL_GPL(max9271_configure_i2c);
149 int max9271_set_high_threshold(struct max9271_device *dev, bool enable)
151 int ret;
153 ret = max9271_read(dev, 0x08);
154 if (ret < 0)
155 return ret;
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));
162 if (ret < 0)
163 return ret;
165 usleep_range(2000, 2500);
167 return 0;
169 EXPORT_SYMBOL_GPL(max9271_set_high_threshold);
171 int max9271_configure_gmsl_link(struct max9271_device *dev)
173 int ret;
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);
187 if (ret < 0)
188 return ret;
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);
200 if (ret < 0)
201 return ret;
203 usleep_range(5000, 8000);
205 return 0;
207 EXPORT_SYMBOL_GPL(max9271_configure_gmsl_link);
209 int max9271_set_gpios(struct max9271_device *dev, u8 gpio_mask)
211 int ret;
213 ret = max9271_read(dev, 0x0f);
214 if (ret < 0)
215 return 0;
217 ret |= gpio_mask;
218 ret = max9271_write(dev, 0x0f, ret);
219 if (ret < 0) {
220 dev_err(&dev->client->dev, "Failed to set gpio (%d)\n", ret);
221 return ret;
224 usleep_range(3500, 5000);
226 return 0;
228 EXPORT_SYMBOL_GPL(max9271_set_gpios);
230 int max9271_clear_gpios(struct max9271_device *dev, u8 gpio_mask)
232 int ret;
234 ret = max9271_read(dev, 0x0f);
235 if (ret < 0)
236 return 0;
238 ret &= ~gpio_mask;
239 ret = max9271_write(dev, 0x0f, ret);
240 if (ret < 0) {
241 dev_err(&dev->client->dev, "Failed to clear gpio (%d)\n", ret);
242 return ret;
245 usleep_range(3500, 5000);
247 return 0;
249 EXPORT_SYMBOL_GPL(max9271_clear_gpios);
251 int max9271_enable_gpios(struct max9271_device *dev, u8 gpio_mask)
253 int ret;
255 ret = max9271_read(dev, 0x0e);
256 if (ret < 0)
257 return 0;
259 /* BIT(0) reserved: GPO is always enabled. */
260 ret |= (gpio_mask & ~BIT(0));
261 ret = max9271_write(dev, 0x0e, ret);
262 if (ret < 0) {
263 dev_err(&dev->client->dev, "Failed to enable gpio (%d)\n", ret);
264 return ret;
267 usleep_range(3500, 5000);
269 return 0;
271 EXPORT_SYMBOL_GPL(max9271_enable_gpios);
273 int max9271_disable_gpios(struct max9271_device *dev, u8 gpio_mask)
275 int ret;
277 ret = max9271_read(dev, 0x0e);
278 if (ret < 0)
279 return 0;
281 /* BIT(0) reserved: GPO cannot be disabled */
282 ret &= ~(gpio_mask | BIT(0));
283 ret = max9271_write(dev, 0x0e, ret);
284 if (ret < 0) {
285 dev_err(&dev->client->dev, "Failed to disable gpio (%d)\n", ret);
286 return ret;
289 usleep_range(3500, 5000);
291 return 0;
293 EXPORT_SYMBOL_GPL(max9271_disable_gpios);
295 int max9271_verify_id(struct max9271_device *dev)
297 int ret;
299 ret = max9271_read(dev, 0x1e);
300 if (ret < 0) {
301 dev_err(&dev->client->dev, "MAX9271 ID read failed (%d)\n",
302 ret);
303 return ret;
306 if (ret != MAX9271_ID) {
307 dev_err(&dev->client->dev, "MAX9271 ID mismatch (0x%02x)\n",
308 ret);
309 return -ENXIO;
312 return 0;
314 EXPORT_SYMBOL_GPL(max9271_verify_id);
316 int max9271_set_address(struct max9271_device *dev, u8 addr)
318 int ret;
320 ret = max9271_write(dev, 0x00, addr << 1);
321 if (ret < 0) {
322 dev_err(&dev->client->dev,
323 "MAX9271 I2C address change failed (%d)\n", ret);
324 return ret;
326 usleep_range(3500, 5000);
328 return 0;
330 EXPORT_SYMBOL_GPL(max9271_set_address);
332 int max9271_set_deserializer_address(struct max9271_device *dev, u8 addr)
334 int ret;
336 ret = max9271_write(dev, 0x01, addr << 1);
337 if (ret < 0) {
338 dev_err(&dev->client->dev,
339 "MAX9271 deserializer address set failed (%d)\n", ret);
340 return ret;
342 usleep_range(3500, 5000);
344 return 0;
346 EXPORT_SYMBOL_GPL(max9271_set_deserializer_address);
348 int max9271_set_translation(struct max9271_device *dev, u8 source, u8 dest)
350 int ret;
352 ret = max9271_write(dev, 0x09, source << 1);
353 if (ret < 0) {
354 dev_err(&dev->client->dev,
355 "MAX9271 I2C translation setup failed (%d)\n", ret);
356 return ret;
358 usleep_range(3500, 5000);
360 ret = max9271_write(dev, 0x0a, dest << 1);
361 if (ret < 0) {
362 dev_err(&dev->client->dev,
363 "MAX9271 I2C translation setup failed (%d)\n", ret);
364 return ret;
366 usleep_range(3500, 5000);
368 return 0;
370 EXPORT_SYMBOL_GPL(max9271_set_translation);
372 MODULE_DESCRIPTION("Maxim MAX9271 GMSL Serializer");
373 MODULE_AUTHOR("Jacopo Mondi");
374 MODULE_LICENSE("GPL v2");