Linux-libre 3.10.103-gnu
[librecmc/linux-libre.git] / drivers / media / i2c / soc_camera / imx074.c
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>
15 #include <linux/v4l2-mediabus.h>
16 #include <linux/slab.h>
17 #include <linux/videodev2.h>
18 #include <linux/module.h>
19
20 #include <media/soc_camera.h>
21 #include <media/v4l2-subdev.h>
22 #include <media/v4l2-chip-ident.h>
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;
80 };
81
82 static const struct imx074_datafmt imx074_colour_fmts[] = {
83         {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
84 };
85
86 static struct imx074 *to_imx074(const struct i2c_client *client)
87 {
88         return container_of(i2c_get_clientdata(client), struct imx074, subdev);
89 }
90
91 /* Find a data format by a pixel code in an array */
92 static const struct imx074_datafmt *imx074_find_datafmt(enum v4l2_mbus_pixelcode code)
93 {
94         int i;
95
96         for (i = 0; i < ARRAY_SIZE(imx074_colour_fmts); i++)
97                 if (imx074_colour_fmts[i].code == code)
98                         return imx074_colour_fmts + i;
99
100         return NULL;
101 }
102
103 static int reg_write(struct i2c_client *client, const u16 addr, const u8 data)
104 {
105         struct i2c_adapter *adap = client->adapter;
106         struct i2c_msg msg;
107         unsigned char tx[3];
108         int ret;
109
110         msg.addr = client->addr;
111         msg.buf = tx;
112         msg.len = 3;
113         msg.flags = 0;
114
115         tx[0] = addr >> 8;
116         tx[1] = addr & 0xff;
117         tx[2] = data;
118
119         ret = i2c_transfer(adap, &msg, 1);
120
121         mdelay(2);
122
123         return ret == 1 ? 0 : -EIO;
124 }
125
126 static int reg_read(struct i2c_client *client, const u16 addr)
127 {
128         u8 buf[2] = {addr >> 8, addr & 0xff};
129         int ret;
130         struct i2c_msg msgs[] = {
131                 {
132                         .addr  = client->addr,
133                         .flags = 0,
134                         .len   = 2,
135                         .buf   = buf,
136                 }, {
137                         .addr  = client->addr,
138                         .flags = I2C_M_RD,
139                         .len   = 2,
140                         .buf   = buf,
141                 },
142         };
143
144         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
145         if (ret < 0) {
146                 dev_warn(&client->dev, "Reading register %x from %x failed\n",
147                          addr, client->addr);
148                 return ret;
149         }
150
151         return buf[0] & 0xff; /* no sign-extension */
152 }
153
154 static int imx074_try_fmt(struct v4l2_subdev *sd,
155                           struct v4l2_mbus_framefmt *mf)
156 {
157         const struct imx074_datafmt *fmt = imx074_find_datafmt(mf->code);
158
159         dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code);
160
161         if (!fmt) {
162                 mf->code        = imx074_colour_fmts[0].code;
163                 mf->colorspace  = imx074_colour_fmts[0].colorspace;
164         }
165
166         mf->width       = IMX074_WIDTH;
167         mf->height      = IMX074_HEIGHT;
168         mf->field       = V4L2_FIELD_NONE;
169
170         return 0;
171 }
172
173 static int imx074_s_fmt(struct v4l2_subdev *sd,
174                         struct v4l2_mbus_framefmt *mf)
175 {
176         struct i2c_client *client = v4l2_get_subdevdata(sd);
177         struct imx074 *priv = to_imx074(client);
178
179         dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code);
180
181         /* MIPI CSI could have changed the format, double-check */
182         if (!imx074_find_datafmt(mf->code))
183                 return -EINVAL;
184
185         imx074_try_fmt(sd, mf);
186
187         priv->fmt = imx074_find_datafmt(mf->code);
188
189         return 0;
190 }
191
192 static int imx074_g_fmt(struct v4l2_subdev *sd,
193                         struct v4l2_mbus_framefmt *mf)
194 {
195         struct i2c_client *client = v4l2_get_subdevdata(sd);
196         struct imx074 *priv = to_imx074(client);
197
198         const struct imx074_datafmt *fmt = priv->fmt;
199
200         mf->code        = fmt->code;
201         mf->colorspace  = fmt->colorspace;
202         mf->width       = IMX074_WIDTH;
203         mf->height      = IMX074_HEIGHT;
204         mf->field       = V4L2_FIELD_NONE;
205
206         return 0;
207 }
208
209 static int imx074_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
210 {
211         struct v4l2_rect *rect = &a->c;
212
213         a->type         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
214         rect->top       = 0;
215         rect->left      = 0;
216         rect->width     = IMX074_WIDTH;
217         rect->height    = IMX074_HEIGHT;
218
219         return 0;
220 }
221
222 static int imx074_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
223 {
224         a->bounds.left                  = 0;
225         a->bounds.top                   = 0;
226         a->bounds.width                 = IMX074_WIDTH;
227         a->bounds.height                = IMX074_HEIGHT;
228         a->defrect                      = a->bounds;
229         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
230         a->pixelaspect.numerator        = 1;
231         a->pixelaspect.denominator      = 1;
232
233         return 0;
234 }
235
236 static int imx074_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
237                            enum v4l2_mbus_pixelcode *code)
238 {
239         if ((unsigned int)index >= ARRAY_SIZE(imx074_colour_fmts))
240                 return -EINVAL;
241
242         *code = imx074_colour_fmts[index].code;
243         return 0;
244 }
245
246 static int imx074_s_stream(struct v4l2_subdev *sd, int enable)
247 {
248         struct i2c_client *client = v4l2_get_subdevdata(sd);
249
250         /* MODE_SELECT: stream or standby */
251         return reg_write(client, MODE_SELECT, !!enable);
252 }
253
254 static int imx074_g_chip_ident(struct v4l2_subdev *sd,
255                                struct v4l2_dbg_chip_ident *id)
256 {
257         struct i2c_client *client = v4l2_get_subdevdata(sd);
258
259         if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
260                 return -EINVAL;
261
262         if (id->match.addr != client->addr)
263                 return -ENODEV;
264
265         id->ident       = V4L2_IDENT_IMX074;
266         id->revision    = 0;
267
268         return 0;
269 }
270
271 static int imx074_s_power(struct v4l2_subdev *sd, int on)
272 {
273         struct i2c_client *client = v4l2_get_subdevdata(sd);
274         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
275
276         return soc_camera_set_power(&client->dev, ssdd, on);
277 }
278
279 static int imx074_g_mbus_config(struct v4l2_subdev *sd,
280                                 struct v4l2_mbus_config *cfg)
281 {
282         cfg->type = V4L2_MBUS_CSI2;
283         cfg->flags = V4L2_MBUS_CSI2_2_LANE |
284                 V4L2_MBUS_CSI2_CHANNEL_0 |
285                 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
286
287         return 0;
288 }
289
290 static struct v4l2_subdev_video_ops imx074_subdev_video_ops = {
291         .s_stream       = imx074_s_stream,
292         .s_mbus_fmt     = imx074_s_fmt,
293         .g_mbus_fmt     = imx074_g_fmt,
294         .try_mbus_fmt   = imx074_try_fmt,
295         .enum_mbus_fmt  = imx074_enum_fmt,
296         .g_crop         = imx074_g_crop,
297         .cropcap        = imx074_cropcap,
298         .g_mbus_config  = imx074_g_mbus_config,
299 };
300
301 static struct v4l2_subdev_core_ops imx074_subdev_core_ops = {
302         .g_chip_ident   = imx074_g_chip_ident,
303         .s_power        = imx074_s_power,
304 };
305
306 static struct v4l2_subdev_ops imx074_subdev_ops = {
307         .core   = &imx074_subdev_core_ops,
308         .video  = &imx074_subdev_video_ops,
309 };
310
311 static int imx074_video_probe(struct i2c_client *client)
312 {
313         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
314         int ret;
315         u16 id;
316
317         ret = imx074_s_power(subdev, 1);
318         if (ret < 0)
319                 return ret;
320
321         /* Read sensor Model ID */
322         ret = reg_read(client, 0);
323         if (ret < 0)
324                 goto done;
325
326         id = ret << 8;
327
328         ret = reg_read(client, 1);
329         if (ret < 0)
330                 goto done;
331
332         id |= ret;
333
334         dev_info(&client->dev, "Chip ID 0x%04x detected\n", id);
335
336         if (id != 0x74) {
337                 ret = -ENODEV;
338                 goto done;
339         }
340
341         /* PLL Setting EXTCLK=24MHz, 22.5times */
342         reg_write(client, PLL_MULTIPLIER, 0x2D);
343         reg_write(client, PRE_PLL_CLK_DIV, 0x02);
344         reg_write(client, PLSTATIM, 0x4B);
345
346         /* 2-lane mode */
347         reg_write(client, 0x3024, 0x00);
348
349         reg_write(client, IMAGE_ORIENTATION, 0x00);
350
351         /* select RAW mode:
352          * 0x08+0x08 = top 8 bits
353          * 0x0a+0x08 = compressed 8-bits
354          * 0x0a+0x0a = 10 bits
355          */
356         reg_write(client, 0x0112, 0x08);
357         reg_write(client, 0x0113, 0x08);
358
359         /* Base setting for High frame mode */
360         reg_write(client, VNDMY_ABLMGSHLMT, 0x80);
361         reg_write(client, Y_OPBADDR_START_DI, 0x08);
362         reg_write(client, 0x3015, 0x37);
363         reg_write(client, 0x301C, 0x01);
364         reg_write(client, 0x302C, 0x05);
365         reg_write(client, 0x3031, 0x26);
366         reg_write(client, 0x3041, 0x60);
367         reg_write(client, 0x3051, 0x24);
368         reg_write(client, 0x3053, 0x34);
369         reg_write(client, 0x3057, 0xC0);
370         reg_write(client, 0x305C, 0x09);
371         reg_write(client, 0x305D, 0x07);
372         reg_write(client, 0x3060, 0x30);
373         reg_write(client, 0x3065, 0x00);
374         reg_write(client, 0x30AA, 0x08);
375         reg_write(client, 0x30AB, 0x1C);
376         reg_write(client, 0x30B0, 0x32);
377         reg_write(client, 0x30B2, 0x83);
378         reg_write(client, 0x30D3, 0x04);
379         reg_write(client, 0x3106, 0x78);
380         reg_write(client, 0x310C, 0x82);
381         reg_write(client, 0x3304, 0x05);
382         reg_write(client, 0x3305, 0x04);
383         reg_write(client, 0x3306, 0x11);
384         reg_write(client, 0x3307, 0x02);
385         reg_write(client, 0x3308, 0x0C);
386         reg_write(client, 0x3309, 0x06);
387         reg_write(client, 0x330A, 0x08);
388         reg_write(client, 0x330B, 0x04);
389         reg_write(client, 0x330C, 0x08);
390         reg_write(client, 0x330D, 0x06);
391         reg_write(client, 0x330E, 0x01);
392         reg_write(client, 0x3381, 0x00);
393
394         /* V : 1/2V-addition (1,3), H : 1/2H-averaging (1,3) -> Full HD */
395         /* 1608 = 1560 + 48 (black lines) */
396         reg_write(client, FRAME_LENGTH_LINES_HI, 0x06);
397         reg_write(client, FRAME_LENGTH_LINES_LO, 0x48);
398         reg_write(client, YADDR_START, 0x00);
399         reg_write(client, YADDR_END, 0x2F);
400         /* 0x838 == 2104 */
401         reg_write(client, X_OUTPUT_SIZE_MSB, 0x08);
402         reg_write(client, X_OUTPUT_SIZE_LSB, 0x38);
403         /* 0x618 == 1560 */
404         reg_write(client, Y_OUTPUT_SIZE_MSB, 0x06);
405         reg_write(client, Y_OUTPUT_SIZE_LSB, 0x18);
406         reg_write(client, X_EVEN_INC, 0x01);
407         reg_write(client, X_ODD_INC, 0x03);
408         reg_write(client, Y_EVEN_INC, 0x01);
409         reg_write(client, Y_ODD_INC, 0x03);
410         reg_write(client, HMODEADD, 0x00);
411         reg_write(client, VMODEADD, 0x16);
412         reg_write(client, VAPPLINE_START, 0x24);
413         reg_write(client, VAPPLINE_END, 0x53);
414         reg_write(client, SHUTTER, 0x00);
415         reg_write(client, HADDAVE, 0x80);
416
417         reg_write(client, LANESEL, 0x00);
418
419         reg_write(client, GROUPED_PARAMETER_HOLD, 0x00);        /* off */
420
421         ret = 0;
422
423 done:
424         imx074_s_power(subdev, 0);
425         return ret;
426 }
427
428 static int imx074_probe(struct i2c_client *client,
429                         const struct i2c_device_id *did)
430 {
431         struct imx074 *priv;
432         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
433         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
434
435         if (!ssdd) {
436                 dev_err(&client->dev, "IMX074: missing platform data!\n");
437                 return -EINVAL;
438         }
439
440         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
441                 dev_warn(&adapter->dev,
442                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
443                 return -EIO;
444         }
445
446         priv = devm_kzalloc(&client->dev, sizeof(struct imx074), GFP_KERNEL);
447         if (!priv)
448                 return -ENOMEM;
449
450         v4l2_i2c_subdev_init(&priv->subdev, client, &imx074_subdev_ops);
451
452         priv->fmt       = &imx074_colour_fmts[0];
453
454         return imx074_video_probe(client);
455 }
456
457 static int imx074_remove(struct i2c_client *client)
458 {
459         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
460
461         if (ssdd->free_bus)
462                 ssdd->free_bus(ssdd);
463
464         return 0;
465 }
466
467 static const struct i2c_device_id imx074_id[] = {
468         { "imx074", 0 },
469         { }
470 };
471 MODULE_DEVICE_TABLE(i2c, imx074_id);
472
473 static struct i2c_driver imx074_i2c_driver = {
474         .driver = {
475                 .name = "imx074",
476         },
477         .probe          = imx074_probe,
478         .remove         = imx074_remove,
479         .id_table       = imx074_id,
480 };
481
482 module_i2c_driver(imx074_i2c_driver);
483
484 MODULE_DESCRIPTION("Sony IMX074 Camera driver");
485 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
486 MODULE_LICENSE("GPL v2");