Commit | Line | Data |
---|---|---|
67826235 GL |
1 | /* |
2 | * Driver for IMX074 CMOS Image Sensor from Sony | |
3 | * | |
4 | * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de> | |
5 | * | |
6 | * Partially inspired by the IMX074 driver from the Android / MSM tree | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | #include <linux/delay.h> | |
14 | #include <linux/i2c.h> | |
95d20109 | 15 | #include <linux/v4l2-mediabus.h> |
67826235 GL |
16 | #include <linux/slab.h> |
17 | #include <linux/videodev2.h> | |
7a707b89 | 18 | #include <linux/module.h> |
67826235 GL |
19 | |
20 | #include <media/soc_camera.h> | |
9aea470b | 21 | #include <media/v4l2-clk.h> |
67826235 | 22 | #include <media/v4l2-subdev.h> |
67826235 GL |
23 | |
24 | /* IMX074 registers */ | |
25 | ||
26 | #define MODE_SELECT 0x0100 | |
27 | #define IMAGE_ORIENTATION 0x0101 | |
28 | #define GROUPED_PARAMETER_HOLD 0x0104 | |
29 | ||
30 | /* Integration Time */ | |
31 | #define COARSE_INTEGRATION_TIME_HI 0x0202 | |
32 | #define COARSE_INTEGRATION_TIME_LO 0x0203 | |
33 | /* Gain */ | |
34 | #define ANALOGUE_GAIN_CODE_GLOBAL_HI 0x0204 | |
35 | #define ANALOGUE_GAIN_CODE_GLOBAL_LO 0x0205 | |
36 | ||
37 | /* PLL registers */ | |
38 | #define PRE_PLL_CLK_DIV 0x0305 | |
39 | #define PLL_MULTIPLIER 0x0307 | |
40 | #define PLSTATIM 0x302b | |
41 | #define VNDMY_ABLMGSHLMT 0x300a | |
42 | #define Y_OPBADDR_START_DI 0x3014 | |
43 | /* mode setting */ | |
44 | #define FRAME_LENGTH_LINES_HI 0x0340 | |
45 | #define FRAME_LENGTH_LINES_LO 0x0341 | |
46 | #define LINE_LENGTH_PCK_HI 0x0342 | |
47 | #define LINE_LENGTH_PCK_LO 0x0343 | |
48 | #define YADDR_START 0x0347 | |
49 | #define YADDR_END 0x034b | |
50 | #define X_OUTPUT_SIZE_MSB 0x034c | |
51 | #define X_OUTPUT_SIZE_LSB 0x034d | |
52 | #define Y_OUTPUT_SIZE_MSB 0x034e | |
53 | #define Y_OUTPUT_SIZE_LSB 0x034f | |
54 | #define X_EVEN_INC 0x0381 | |
55 | #define X_ODD_INC 0x0383 | |
56 | #define Y_EVEN_INC 0x0385 | |
57 | #define Y_ODD_INC 0x0387 | |
58 | ||
59 | #define HMODEADD 0x3001 | |
60 | #define VMODEADD 0x3016 | |
61 | #define VAPPLINE_START 0x3069 | |
62 | #define VAPPLINE_END 0x306b | |
63 | #define SHUTTER 0x3086 | |
64 | #define HADDAVE 0x30e8 | |
65 | #define LANESEL 0x3301 | |
66 | ||
67 | /* IMX074 supported geometry */ | |
68 | #define IMX074_WIDTH 1052 | |
69 | #define IMX074_HEIGHT 780 | |
70 | ||
71 | /* IMX074 has only one fixed colorspace per pixelcode */ | |
72 | struct imx074_datafmt { | |
73 | enum v4l2_mbus_pixelcode code; | |
74 | enum v4l2_colorspace colorspace; | |
75 | }; | |
76 | ||
77 | struct imx074 { | |
78 | struct v4l2_subdev subdev; | |
79 | const struct imx074_datafmt *fmt; | |
9aea470b | 80 | struct v4l2_clk *clk; |
67826235 GL |
81 | }; |
82 | ||
83 | static const struct imx074_datafmt imx074_colour_fmts[] = { | |
84 | {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB}, | |
85 | }; | |
86 | ||
87 | static struct imx074 *to_imx074(const struct i2c_client *client) | |
88 | { | |
89 | return container_of(i2c_get_clientdata(client), struct imx074, subdev); | |
90 | } | |
91 | ||
92 | /* Find a data format by a pixel code in an array */ | |
93 | static const struct imx074_datafmt *imx074_find_datafmt(enum v4l2_mbus_pixelcode code) | |
94 | { | |
95 | int i; | |
96 | ||
97 | for (i = 0; i < ARRAY_SIZE(imx074_colour_fmts); i++) | |
98 | if (imx074_colour_fmts[i].code == code) | |
99 | return imx074_colour_fmts + i; | |
100 | ||
101 | return NULL; | |
102 | } | |
103 | ||
104 | static int reg_write(struct i2c_client *client, const u16 addr, const u8 data) | |
105 | { | |
106 | struct i2c_adapter *adap = client->adapter; | |
107 | struct i2c_msg msg; | |
108 | unsigned char tx[3]; | |
109 | int ret; | |
110 | ||
111 | msg.addr = client->addr; | |
112 | msg.buf = tx; | |
113 | msg.len = 3; | |
114 | msg.flags = 0; | |
115 | ||
116 | tx[0] = addr >> 8; | |
117 | tx[1] = addr & 0xff; | |
118 | tx[2] = data; | |
119 | ||
120 | ret = i2c_transfer(adap, &msg, 1); | |
121 | ||
122 | mdelay(2); | |
123 | ||
124 | return ret == 1 ? 0 : -EIO; | |
125 | } | |
126 | ||
127 | static int reg_read(struct i2c_client *client, const u16 addr) | |
128 | { | |
129 | u8 buf[2] = {addr >> 8, addr & 0xff}; | |
130 | int ret; | |
131 | struct i2c_msg msgs[] = { | |
132 | { | |
133 | .addr = client->addr, | |
134 | .flags = 0, | |
135 | .len = 2, | |
136 | .buf = buf, | |
137 | }, { | |
138 | .addr = client->addr, | |
139 | .flags = I2C_M_RD, | |
140 | .len = 2, | |
141 | .buf = buf, | |
142 | }, | |
143 | }; | |
144 | ||
145 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); | |
146 | if (ret < 0) { | |
147 | dev_warn(&client->dev, "Reading register %x from %x failed\n", | |
148 | addr, client->addr); | |
149 | return ret; | |
150 | } | |
151 | ||
152 | return buf[0] & 0xff; /* no sign-extension */ | |
153 | } | |
154 | ||
155 | static int imx074_try_fmt(struct v4l2_subdev *sd, | |
156 | struct v4l2_mbus_framefmt *mf) | |
157 | { | |
158 | const struct imx074_datafmt *fmt = imx074_find_datafmt(mf->code); | |
159 | ||
160 | dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code); | |
161 | ||
162 | if (!fmt) { | |
163 | mf->code = imx074_colour_fmts[0].code; | |
164 | mf->colorspace = imx074_colour_fmts[0].colorspace; | |
165 | } | |
166 | ||
167 | mf->width = IMX074_WIDTH; | |
168 | mf->height = IMX074_HEIGHT; | |
169 | mf->field = V4L2_FIELD_NONE; | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
174 | static int imx074_s_fmt(struct v4l2_subdev *sd, | |
175 | struct v4l2_mbus_framefmt *mf) | |
176 | { | |
177 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
178 | struct imx074 *priv = to_imx074(client); | |
179 | ||
180 | dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code); | |
181 | ||
182 | /* MIPI CSI could have changed the format, double-check */ | |
183 | if (!imx074_find_datafmt(mf->code)) | |
184 | return -EINVAL; | |
185 | ||
186 | imx074_try_fmt(sd, mf); | |
187 | ||
188 | priv->fmt = imx074_find_datafmt(mf->code); | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
193 | static int imx074_g_fmt(struct v4l2_subdev *sd, | |
194 | struct v4l2_mbus_framefmt *mf) | |
195 | { | |
196 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
197 | struct imx074 *priv = to_imx074(client); | |
198 | ||
199 | const struct imx074_datafmt *fmt = priv->fmt; | |
200 | ||
201 | mf->code = fmt->code; | |
202 | mf->colorspace = fmt->colorspace; | |
203 | mf->width = IMX074_WIDTH; | |
204 | mf->height = IMX074_HEIGHT; | |
205 | mf->field = V4L2_FIELD_NONE; | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
210 | static int imx074_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a) | |
211 | { | |
212 | struct v4l2_rect *rect = &a->c; | |
213 | ||
214 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
215 | rect->top = 0; | |
216 | rect->left = 0; | |
217 | rect->width = IMX074_WIDTH; | |
218 | rect->height = IMX074_HEIGHT; | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
223 | static int imx074_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a) | |
224 | { | |
225 | a->bounds.left = 0; | |
226 | a->bounds.top = 0; | |
227 | a->bounds.width = IMX074_WIDTH; | |
228 | a->bounds.height = IMX074_HEIGHT; | |
229 | a->defrect = a->bounds; | |
230 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
231 | a->pixelaspect.numerator = 1; | |
232 | a->pixelaspect.denominator = 1; | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | static int imx074_enum_fmt(struct v4l2_subdev *sd, unsigned int index, | |
238 | enum v4l2_mbus_pixelcode *code) | |
239 | { | |
240 | if ((unsigned int)index >= ARRAY_SIZE(imx074_colour_fmts)) | |
241 | return -EINVAL; | |
242 | ||
243 | *code = imx074_colour_fmts[index].code; | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static int imx074_s_stream(struct v4l2_subdev *sd, int enable) | |
248 | { | |
249 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
250 | ||
251 | /* MODE_SELECT: stream or standby */ | |
252 | return reg_write(client, MODE_SELECT, !!enable); | |
253 | } | |
254 | ||
4ec10bac LP |
255 | static int imx074_s_power(struct v4l2_subdev *sd, int on) |
256 | { | |
257 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
25a34811 | 258 | struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); |
9aea470b | 259 | struct imx074 *priv = to_imx074(client); |
4ec10bac | 260 | |
9aea470b | 261 | return soc_camera_set_power(&client->dev, ssdd, priv->clk, on); |
4ec10bac LP |
262 | } |
263 | ||
7cd74ffb GL |
264 | static int imx074_g_mbus_config(struct v4l2_subdev *sd, |
265 | struct v4l2_mbus_config *cfg) | |
266 | { | |
267 | cfg->type = V4L2_MBUS_CSI2; | |
268 | cfg->flags = V4L2_MBUS_CSI2_2_LANE | | |
269 | V4L2_MBUS_CSI2_CHANNEL_0 | | |
270 | V4L2_MBUS_CSI2_CONTINUOUS_CLOCK; | |
271 | ||
272 | return 0; | |
273 | } | |
274 | ||
67826235 GL |
275 | static struct v4l2_subdev_video_ops imx074_subdev_video_ops = { |
276 | .s_stream = imx074_s_stream, | |
277 | .s_mbus_fmt = imx074_s_fmt, | |
278 | .g_mbus_fmt = imx074_g_fmt, | |
279 | .try_mbus_fmt = imx074_try_fmt, | |
280 | .enum_mbus_fmt = imx074_enum_fmt, | |
281 | .g_crop = imx074_g_crop, | |
282 | .cropcap = imx074_cropcap, | |
7cd74ffb | 283 | .g_mbus_config = imx074_g_mbus_config, |
67826235 GL |
284 | }; |
285 | ||
286 | static struct v4l2_subdev_core_ops imx074_subdev_core_ops = { | |
4ec10bac | 287 | .s_power = imx074_s_power, |
67826235 GL |
288 | }; |
289 | ||
290 | static struct v4l2_subdev_ops imx074_subdev_ops = { | |
291 | .core = &imx074_subdev_core_ops, | |
292 | .video = &imx074_subdev_video_ops, | |
293 | }; | |
294 | ||
14178aa5 | 295 | static int imx074_video_probe(struct i2c_client *client) |
67826235 | 296 | { |
4bbc6d52 | 297 | struct v4l2_subdev *subdev = i2c_get_clientdata(client); |
67826235 GL |
298 | int ret; |
299 | u16 id; | |
300 | ||
4bbc6d52 LP |
301 | ret = imx074_s_power(subdev, 1); |
302 | if (ret < 0) | |
303 | return ret; | |
304 | ||
67826235 GL |
305 | /* Read sensor Model ID */ |
306 | ret = reg_read(client, 0); | |
307 | if (ret < 0) | |
4bbc6d52 | 308 | goto done; |
67826235 GL |
309 | |
310 | id = ret << 8; | |
311 | ||
312 | ret = reg_read(client, 1); | |
313 | if (ret < 0) | |
4bbc6d52 | 314 | goto done; |
67826235 GL |
315 | |
316 | id |= ret; | |
317 | ||
318 | dev_info(&client->dev, "Chip ID 0x%04x detected\n", id); | |
319 | ||
4bbc6d52 LP |
320 | if (id != 0x74) { |
321 | ret = -ENODEV; | |
322 | goto done; | |
323 | } | |
67826235 GL |
324 | |
325 | /* PLL Setting EXTCLK=24MHz, 22.5times */ | |
326 | reg_write(client, PLL_MULTIPLIER, 0x2D); | |
327 | reg_write(client, PRE_PLL_CLK_DIV, 0x02); | |
328 | reg_write(client, PLSTATIM, 0x4B); | |
329 | ||
330 | /* 2-lane mode */ | |
331 | reg_write(client, 0x3024, 0x00); | |
332 | ||
333 | reg_write(client, IMAGE_ORIENTATION, 0x00); | |
334 | ||
335 | /* select RAW mode: | |
336 | * 0x08+0x08 = top 8 bits | |
337 | * 0x0a+0x08 = compressed 8-bits | |
338 | * 0x0a+0x0a = 10 bits | |
339 | */ | |
340 | reg_write(client, 0x0112, 0x08); | |
341 | reg_write(client, 0x0113, 0x08); | |
342 | ||
343 | /* Base setting for High frame mode */ | |
344 | reg_write(client, VNDMY_ABLMGSHLMT, 0x80); | |
345 | reg_write(client, Y_OPBADDR_START_DI, 0x08); | |
346 | reg_write(client, 0x3015, 0x37); | |
347 | reg_write(client, 0x301C, 0x01); | |
348 | reg_write(client, 0x302C, 0x05); | |
349 | reg_write(client, 0x3031, 0x26); | |
350 | reg_write(client, 0x3041, 0x60); | |
351 | reg_write(client, 0x3051, 0x24); | |
352 | reg_write(client, 0x3053, 0x34); | |
353 | reg_write(client, 0x3057, 0xC0); | |
354 | reg_write(client, 0x305C, 0x09); | |
355 | reg_write(client, 0x305D, 0x07); | |
356 | reg_write(client, 0x3060, 0x30); | |
357 | reg_write(client, 0x3065, 0x00); | |
358 | reg_write(client, 0x30AA, 0x08); | |
359 | reg_write(client, 0x30AB, 0x1C); | |
360 | reg_write(client, 0x30B0, 0x32); | |
361 | reg_write(client, 0x30B2, 0x83); | |
362 | reg_write(client, 0x30D3, 0x04); | |
363 | reg_write(client, 0x3106, 0x78); | |
364 | reg_write(client, 0x310C, 0x82); | |
365 | reg_write(client, 0x3304, 0x05); | |
366 | reg_write(client, 0x3305, 0x04); | |
367 | reg_write(client, 0x3306, 0x11); | |
368 | reg_write(client, 0x3307, 0x02); | |
369 | reg_write(client, 0x3308, 0x0C); | |
370 | reg_write(client, 0x3309, 0x06); | |
371 | reg_write(client, 0x330A, 0x08); | |
372 | reg_write(client, 0x330B, 0x04); | |
373 | reg_write(client, 0x330C, 0x08); | |
374 | reg_write(client, 0x330D, 0x06); | |
375 | reg_write(client, 0x330E, 0x01); | |
376 | reg_write(client, 0x3381, 0x00); | |
377 | ||
378 | /* V : 1/2V-addition (1,3), H : 1/2H-averaging (1,3) -> Full HD */ | |
379 | /* 1608 = 1560 + 48 (black lines) */ | |
380 | reg_write(client, FRAME_LENGTH_LINES_HI, 0x06); | |
381 | reg_write(client, FRAME_LENGTH_LINES_LO, 0x48); | |
382 | reg_write(client, YADDR_START, 0x00); | |
383 | reg_write(client, YADDR_END, 0x2F); | |
384 | /* 0x838 == 2104 */ | |
385 | reg_write(client, X_OUTPUT_SIZE_MSB, 0x08); | |
386 | reg_write(client, X_OUTPUT_SIZE_LSB, 0x38); | |
387 | /* 0x618 == 1560 */ | |
388 | reg_write(client, Y_OUTPUT_SIZE_MSB, 0x06); | |
389 | reg_write(client, Y_OUTPUT_SIZE_LSB, 0x18); | |
390 | reg_write(client, X_EVEN_INC, 0x01); | |
391 | reg_write(client, X_ODD_INC, 0x03); | |
392 | reg_write(client, Y_EVEN_INC, 0x01); | |
393 | reg_write(client, Y_ODD_INC, 0x03); | |
394 | reg_write(client, HMODEADD, 0x00); | |
395 | reg_write(client, VMODEADD, 0x16); | |
396 | reg_write(client, VAPPLINE_START, 0x24); | |
397 | reg_write(client, VAPPLINE_END, 0x53); | |
398 | reg_write(client, SHUTTER, 0x00); | |
399 | reg_write(client, HADDAVE, 0x80); | |
400 | ||
401 | reg_write(client, LANESEL, 0x00); | |
402 | ||
403 | reg_write(client, GROUPED_PARAMETER_HOLD, 0x00); /* off */ | |
404 | ||
4bbc6d52 LP |
405 | ret = 0; |
406 | ||
407 | done: | |
408 | imx074_s_power(subdev, 0); | |
409 | return ret; | |
67826235 GL |
410 | } |
411 | ||
412 | static int imx074_probe(struct i2c_client *client, | |
413 | const struct i2c_device_id *did) | |
414 | { | |
415 | struct imx074 *priv; | |
67826235 | 416 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
25a34811 | 417 | struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); |
9aea470b | 418 | int ret; |
67826235 | 419 | |
25a34811 | 420 | if (!ssdd) { |
67826235 GL |
421 | dev_err(&client->dev, "IMX074: missing platform data!\n"); |
422 | return -EINVAL; | |
423 | } | |
424 | ||
425 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | |
426 | dev_warn(&adapter->dev, | |
427 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n"); | |
428 | return -EIO; | |
429 | } | |
430 | ||
70e176a5 | 431 | priv = devm_kzalloc(&client->dev, sizeof(struct imx074), GFP_KERNEL); |
67826235 GL |
432 | if (!priv) |
433 | return -ENOMEM; | |
434 | ||
435 | v4l2_i2c_subdev_init(&priv->subdev, client, &imx074_subdev_ops); | |
436 | ||
67826235 GL |
437 | priv->fmt = &imx074_colour_fmts[0]; |
438 | ||
9aea470b GL |
439 | priv->clk = v4l2_clk_get(&client->dev, "mclk"); |
440 | if (IS_ERR(priv->clk)) | |
441 | return PTR_ERR(priv->clk); | |
442 | ||
443 | ret = imx074_video_probe(client); | |
444 | if (ret < 0) | |
445 | v4l2_clk_put(priv->clk); | |
446 | ||
447 | return ret; | |
67826235 GL |
448 | } |
449 | ||
450 | static int imx074_remove(struct i2c_client *client) | |
451 | { | |
25a34811 | 452 | struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); |
9aea470b | 453 | struct imx074 *priv = to_imx074(client); |
67826235 | 454 | |
9aea470b | 455 | v4l2_clk_put(priv->clk); |
25a34811 GL |
456 | if (ssdd->free_bus) |
457 | ssdd->free_bus(ssdd); | |
67826235 GL |
458 | |
459 | return 0; | |
460 | } | |
461 | ||
462 | static const struct i2c_device_id imx074_id[] = { | |
463 | { "imx074", 0 }, | |
464 | { } | |
465 | }; | |
466 | MODULE_DEVICE_TABLE(i2c, imx074_id); | |
467 | ||
468 | static struct i2c_driver imx074_i2c_driver = { | |
469 | .driver = { | |
470 | .name = "imx074", | |
471 | }, | |
472 | .probe = imx074_probe, | |
473 | .remove = imx074_remove, | |
474 | .id_table = imx074_id, | |
475 | }; | |
476 | ||
c6e8d86f | 477 | module_i2c_driver(imx074_i2c_driver); |
67826235 GL |
478 | |
479 | MODULE_DESCRIPTION("Sony IMX074 Camera driver"); | |
480 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); | |
481 | MODULE_LICENSE("GPL v2"); |