xref: /openbmc/linux/drivers/media/i2c/max9271.c (revision cd6d421e)
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.
9  *
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.
14  *
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.
17  */
18 
19 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 
23 #include "max9271.h"
24 
25 static int max9271_read(struct max9271_device *dev, u8 reg)
26 {
27 	int ret;
28 
29 	dev_dbg(&dev->client->dev, "%s(0x%02x)\n", __func__, reg);
30 
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);
36 
37 	return ret;
38 }
39 
40 static int max9271_write(struct max9271_device *dev, u8 reg, u8 val)
41 {
42 	int ret;
43 
44 	dev_dbg(&dev->client->dev, "%s(0x%02x, 0x%02x)\n", __func__, reg, val);
45 
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);
51 
52 	return ret;
53 }
54 
55 /*
56  * max9271_pclk_detect() - Detect valid pixel clock from image sensor
57  *
58  * Wait up to 10ms for a valid pixel clock.
59  *
60  * Returns 0 for success, < 0 for pixel clock not properly detected
61  */
62 static int max9271_pclk_detect(struct max9271_device *dev)
63 {
64 	unsigned int i;
65 	int ret;
66 
67 	for (i = 0; i < 100; i++) {
68 		ret = max9271_read(dev, 0x15);
69 		if (ret < 0)
70 			return ret;
71 
72 		if (ret & MAX9271_PCLKDET)
73 			return 0;
74 
75 		usleep_range(50, 100);
76 	}
77 
78 	dev_err(&dev->client->dev, "Unable to detect valid pixel clock\n");
79 
80 	return -EIO;
81 }
82 
83 int max9271_set_serial_link(struct max9271_device *dev, bool enable)
84 {
85 	int ret;
86 	u8 val = MAX9271_REVCCEN | MAX9271_FWDCCEN;
87 
88 	if (enable) {
89 		ret = max9271_pclk_detect(dev);
90 		if (ret)
91 			return ret;
92 
93 		val |= MAX9271_SEREN;
94 	} else {
95 		val |= MAX9271_CLINKEN;
96 	}
97 
98 	/*
99 	 * The serializer temporarily disables the reverse control channel for
100 	 * 350µs after starting/stopping the forward serial link, but the
101 	 * deserializer synchronization time isn't clearly documented.
102 	 *
103 	 * According to the serializer datasheet we should wait 3ms, while
104 	 * according to the deserializer datasheet we should wait 5ms.
105 	 *
106 	 * Short delays here appear to show bit-errors in the writes following.
107 	 * Therefore a conservative delay seems best here.
108 	 */
109 	max9271_write(dev, 0x04, val);
110 	usleep_range(5000, 8000);
111 
112 	return 0;
113 }
114 EXPORT_SYMBOL_GPL(max9271_set_serial_link);
115 
116 int max9271_configure_i2c(struct max9271_device *dev, u8 i2c_config)
117 {
118 	int ret;
119 
120 	ret = max9271_write(dev, 0x0d, i2c_config);
121 	if (ret)
122 		return ret;
123 
124 	/* The delay required after an I2C bus configuration change is not
125 	 * characterized in the serializer manual. Sleep up to 5msec to
126 	 * stay safe.
127 	 */
128 	usleep_range(3500, 5000);
129 
130 	return 0;
131 }
132 EXPORT_SYMBOL_GPL(max9271_configure_i2c);
133 
134 int max9271_set_high_threshold(struct max9271_device *dev, bool enable)
135 {
136 	int ret;
137 
138 	ret = max9271_read(dev, 0x08);
139 	if (ret < 0)
140 		return ret;
141 
142 	/*
143 	 * Enable or disable reverse channel high threshold to increase
144 	 * immunity to power supply noise.
145 	 */
146 	max9271_write(dev, 0x08, enable ? ret | BIT(0) : ret & ~BIT(0));
147 	usleep_range(2000, 2500);
148 
149 	return 0;
150 }
151 EXPORT_SYMBOL_GPL(max9271_set_high_threshold);
152 
153 int max9271_configure_gmsl_link(struct max9271_device *dev)
154 {
155 	/*
156 	 * Configure the GMSL link:
157 	 *
158 	 * - Double input mode, high data rate, 24-bit mode
159 	 * - Latch input data on PCLKIN rising edge
160 	 * - Enable HS/VS encoding
161 	 * - 1-bit parity error detection
162 	 *
163 	 * TODO: Make the GMSL link configuration parametric.
164 	 */
165 	max9271_write(dev, 0x07, MAX9271_DBL | MAX9271_HVEN |
166 		      MAX9271_EDC_1BIT_PARITY);
167 	usleep_range(5000, 8000);
168 
169 	/*
170 	 * Adjust spread spectrum to +4% and auto-detect pixel clock
171 	 * and serial link rate.
172 	 */
173 	max9271_write(dev, 0x02, MAX9271_SPREAD_SPECT_4 | MAX9271_R02_RES |
174 		      MAX9271_PCLK_AUTODETECT | MAX9271_SERIAL_AUTODETECT);
175 	usleep_range(5000, 8000);
176 
177 	return 0;
178 }
179 EXPORT_SYMBOL_GPL(max9271_configure_gmsl_link);
180 
181 int max9271_set_gpios(struct max9271_device *dev, u8 gpio_mask)
182 {
183 	int ret;
184 
185 	ret = max9271_read(dev, 0x0f);
186 	if (ret < 0)
187 		return 0;
188 
189 	ret |= gpio_mask;
190 	ret = max9271_write(dev, 0x0f, ret);
191 	if (ret < 0) {
192 		dev_err(&dev->client->dev, "Failed to set gpio (%d)\n", ret);
193 		return ret;
194 	}
195 
196 	usleep_range(3500, 5000);
197 
198 	return 0;
199 }
200 EXPORT_SYMBOL_GPL(max9271_set_gpios);
201 
202 int max9271_clear_gpios(struct max9271_device *dev, u8 gpio_mask)
203 {
204 	int ret;
205 
206 	ret = max9271_read(dev, 0x0f);
207 	if (ret < 0)
208 		return 0;
209 
210 	ret &= ~gpio_mask;
211 	ret = max9271_write(dev, 0x0f, ret);
212 	if (ret < 0) {
213 		dev_err(&dev->client->dev, "Failed to clear gpio (%d)\n", ret);
214 		return ret;
215 	}
216 
217 	usleep_range(3500, 5000);
218 
219 	return 0;
220 }
221 EXPORT_SYMBOL_GPL(max9271_clear_gpios);
222 
223 int max9271_enable_gpios(struct max9271_device *dev, u8 gpio_mask)
224 {
225 	int ret;
226 
227 	ret = max9271_read(dev, 0x0e);
228 	if (ret < 0)
229 		return 0;
230 
231 	/* BIT(0) reserved: GPO is always enabled. */
232 	ret |= (gpio_mask & ~BIT(0));
233 	ret = max9271_write(dev, 0x0e, ret);
234 	if (ret < 0) {
235 		dev_err(&dev->client->dev, "Failed to enable gpio (%d)\n", ret);
236 		return ret;
237 	}
238 
239 	usleep_range(3500, 5000);
240 
241 	return 0;
242 }
243 EXPORT_SYMBOL_GPL(max9271_enable_gpios);
244 
245 int max9271_disable_gpios(struct max9271_device *dev, u8 gpio_mask)
246 {
247 	int ret;
248 
249 	ret = max9271_read(dev, 0x0e);
250 	if (ret < 0)
251 		return 0;
252 
253 	/* BIT(0) reserved: GPO cannot be disabled */
254 	ret &= ~(gpio_mask | BIT(0));
255 	ret = max9271_write(dev, 0x0e, ret);
256 	if (ret < 0) {
257 		dev_err(&dev->client->dev, "Failed to disable gpio (%d)\n", ret);
258 		return ret;
259 	}
260 
261 	usleep_range(3500, 5000);
262 
263 	return 0;
264 }
265 EXPORT_SYMBOL_GPL(max9271_disable_gpios);
266 
267 int max9271_verify_id(struct max9271_device *dev)
268 {
269 	int ret;
270 
271 	ret = max9271_read(dev, 0x1e);
272 	if (ret < 0) {
273 		dev_err(&dev->client->dev, "MAX9271 ID read failed (%d)\n",
274 			ret);
275 		return ret;
276 	}
277 
278 	if (ret != MAX9271_ID) {
279 		dev_err(&dev->client->dev, "MAX9271 ID mismatch (0x%02x)\n",
280 			ret);
281 		return -ENXIO;
282 	}
283 
284 	return 0;
285 }
286 EXPORT_SYMBOL_GPL(max9271_verify_id);
287 
288 int max9271_set_address(struct max9271_device *dev, u8 addr)
289 {
290 	int ret;
291 
292 	ret = max9271_write(dev, 0x00, addr << 1);
293 	if (ret < 0) {
294 		dev_err(&dev->client->dev,
295 			"MAX9271 I2C address change failed (%d)\n", ret);
296 		return ret;
297 	}
298 	usleep_range(3500, 5000);
299 
300 	return 0;
301 }
302 EXPORT_SYMBOL_GPL(max9271_set_address);
303 
304 int max9271_set_deserializer_address(struct max9271_device *dev, u8 addr)
305 {
306 	int ret;
307 
308 	ret = max9271_write(dev, 0x01, addr << 1);
309 	if (ret < 0) {
310 		dev_err(&dev->client->dev,
311 			"MAX9271 deserializer address set failed (%d)\n", ret);
312 		return ret;
313 	}
314 	usleep_range(3500, 5000);
315 
316 	return 0;
317 }
318 EXPORT_SYMBOL_GPL(max9271_set_deserializer_address);
319 
320 int max9271_set_translation(struct max9271_device *dev, u8 source, u8 dest)
321 {
322 	int ret;
323 
324 	ret = max9271_write(dev, 0x09, source << 1);
325 	if (ret < 0) {
326 		dev_err(&dev->client->dev,
327 			"MAX9271 I2C translation setup failed (%d)\n", ret);
328 		return ret;
329 	}
330 	usleep_range(3500, 5000);
331 
332 	ret = max9271_write(dev, 0x0a, dest << 1);
333 	if (ret < 0) {
334 		dev_err(&dev->client->dev,
335 			"MAX9271 I2C translation setup failed (%d)\n", ret);
336 		return ret;
337 	}
338 	usleep_range(3500, 5000);
339 
340 	return 0;
341 }
342 EXPORT_SYMBOL_GPL(max9271_set_translation);
343 
344 MODULE_DESCRIPTION("Maxim MAX9271 GMSL Serializer");
345 MODULE_AUTHOR("Jacopo Mondi");
346 MODULE_LICENSE("GPL v2");
347