Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for MT9M001 CMOS Image Sensor from Micron |
| 3 | * |
| 4 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/videodev2.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/log2.h> |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 16 | |
Guennadi Liakhovetski | 32ca208 | 2011-07-26 11:46:02 -0300 | [diff] [blame] | 17 | #include <media/soc_camera.h> |
| 18 | #include <media/soc_mediabus.h> |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 19 | #include <media/v4l2-subdev.h> |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 20 | #include <media/v4l2-ctrls.h> |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 21 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 22 | /* |
| 23 | * mt9m001 i2c address 0x5d |
Peter Meerwald | 22cf83f | 2012-06-13 10:48:11 -0300 | [diff] [blame] | 24 | * The platform has to define struct i2c_board_info objects and link to them |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 25 | * from struct soc_camera_host_desc |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 26 | */ |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 27 | |
| 28 | /* mt9m001 selected register addresses */ |
| 29 | #define MT9M001_CHIP_VERSION 0x00 |
| 30 | #define MT9M001_ROW_START 0x01 |
| 31 | #define MT9M001_COLUMN_START 0x02 |
| 32 | #define MT9M001_WINDOW_HEIGHT 0x03 |
| 33 | #define MT9M001_WINDOW_WIDTH 0x04 |
| 34 | #define MT9M001_HORIZONTAL_BLANKING 0x05 |
| 35 | #define MT9M001_VERTICAL_BLANKING 0x06 |
| 36 | #define MT9M001_OUTPUT_CONTROL 0x07 |
| 37 | #define MT9M001_SHUTTER_WIDTH 0x09 |
| 38 | #define MT9M001_FRAME_RESTART 0x0b |
| 39 | #define MT9M001_SHUTTER_DELAY 0x0c |
| 40 | #define MT9M001_RESET 0x0d |
| 41 | #define MT9M001_READ_OPTIONS1 0x1e |
| 42 | #define MT9M001_READ_OPTIONS2 0x20 |
| 43 | #define MT9M001_GLOBAL_GAIN 0x35 |
| 44 | #define MT9M001_CHIP_ENABLE 0xF1 |
| 45 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 46 | #define MT9M001_MAX_WIDTH 1280 |
| 47 | #define MT9M001_MAX_HEIGHT 1024 |
| 48 | #define MT9M001_MIN_WIDTH 48 |
| 49 | #define MT9M001_MIN_HEIGHT 32 |
| 50 | #define MT9M001_COLUMN_SKIP 20 |
| 51 | #define MT9M001_ROW_SKIP 12 |
| 52 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 53 | /* MT9M001 has only one fixed colorspace per pixelcode */ |
| 54 | struct mt9m001_datafmt { |
| 55 | enum v4l2_mbus_pixelcode code; |
| 56 | enum v4l2_colorspace colorspace; |
| 57 | }; |
| 58 | |
| 59 | /* Find a data format by a pixel code in an array */ |
| 60 | static const struct mt9m001_datafmt *mt9m001_find_datafmt( |
| 61 | enum v4l2_mbus_pixelcode code, const struct mt9m001_datafmt *fmt, |
| 62 | int n) |
| 63 | { |
| 64 | int i; |
| 65 | for (i = 0; i < n; i++) |
| 66 | if (fmt[i].code == code) |
| 67 | return fmt + i; |
| 68 | |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | static const struct mt9m001_datafmt mt9m001_colour_fmts[] = { |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 73 | /* |
| 74 | * Order important: first natively supported, |
| 75 | * second supported with a GPIO extender |
| 76 | */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 77 | {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB}, |
| 78 | {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB}, |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 79 | }; |
| 80 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 81 | static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = { |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 82 | /* Order important - see above */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 83 | {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG}, |
Laurent Pinchart | 07670433 | 2010-09-28 07:01:44 -0300 | [diff] [blame] | 84 | {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG}, |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct mt9m001 { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 88 | struct v4l2_subdev subdev; |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 89 | struct v4l2_ctrl_handler hdl; |
| 90 | struct { |
| 91 | /* exposure/auto-exposure cluster */ |
| 92 | struct v4l2_ctrl *autoexposure; |
| 93 | struct v4l2_ctrl *exposure; |
| 94 | }; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 95 | struct v4l2_rect rect; /* Sensor window */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 96 | const struct mt9m001_datafmt *fmt; |
| 97 | const struct mt9m001_datafmt *fmts; |
| 98 | int num_fmts; |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 99 | unsigned int total_h; |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 100 | unsigned short y_skip_top; /* Lines to skip at the top */ |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 101 | }; |
| 102 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 103 | static struct mt9m001 *to_mt9m001(const struct i2c_client *client) |
| 104 | { |
| 105 | return container_of(i2c_get_clientdata(client), struct mt9m001, subdev); |
| 106 | } |
| 107 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 108 | static int reg_read(struct i2c_client *client, const u8 reg) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 109 | { |
Jonathan Cameron | 3f87704 | 2011-10-21 09:30:25 -0300 | [diff] [blame] | 110 | return i2c_smbus_read_word_swapped(client, reg); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 111 | } |
| 112 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 113 | static int reg_write(struct i2c_client *client, const u8 reg, |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 114 | const u16 data) |
| 115 | { |
Jonathan Cameron | 3f87704 | 2011-10-21 09:30:25 -0300 | [diff] [blame] | 116 | return i2c_smbus_write_word_swapped(client, reg, data); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 117 | } |
| 118 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 119 | static int reg_set(struct i2c_client *client, const u8 reg, |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 120 | const u16 data) |
| 121 | { |
| 122 | int ret; |
| 123 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 124 | ret = reg_read(client, reg); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 125 | if (ret < 0) |
| 126 | return ret; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 127 | return reg_write(client, reg, ret | data); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 128 | } |
| 129 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 130 | static int reg_clear(struct i2c_client *client, const u8 reg, |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 131 | const u16 data) |
| 132 | { |
| 133 | int ret; |
| 134 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 135 | ret = reg_read(client, reg); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 136 | if (ret < 0) |
| 137 | return ret; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 138 | return reg_write(client, reg, ret & ~data); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 139 | } |
| 140 | |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 141 | static int mt9m001_init(struct i2c_client *client) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 142 | { |
| 143 | int ret; |
| 144 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 145 | dev_dbg(&client->dev, "%s\n", __func__); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 146 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 147 | /* |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 148 | * We don't know, whether platform provides reset, issue a soft reset |
| 149 | * too. This returns all registers to their default values. |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 150 | */ |
| 151 | ret = reg_write(client, MT9M001_RESET, 1); |
| 152 | if (!ret) |
| 153 | ret = reg_write(client, MT9M001_RESET, 0); |
Stefan Herbrechtsmeier | 8103466 | 2008-08-14 12:04:11 -0300 | [diff] [blame] | 154 | |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 155 | /* Disable chip, synchronous option update */ |
| 156 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 157 | ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 158 | |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 159 | return ret; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 160 | } |
| 161 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 162 | static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 163 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 164 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 165 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 166 | /* Switch to master "normal" mode or stop sensor readout */ |
| 167 | if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 168 | return -EIO; |
| 169 | return 0; |
| 170 | } |
| 171 | |
Hans Verkuil | 4f99659 | 2012-09-05 05:10:48 -0300 | [diff] [blame] | 172 | static int mt9m001_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a) |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 173 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 174 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 175 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 176 | struct v4l2_rect rect = a->c; |
Guennadi Liakhovetski | ad5f2e8 | 2008-03-07 21:57:18 -0300 | [diff] [blame] | 177 | int ret; |
| 178 | const u16 hblank = 9, vblank = 25; |
| 179 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 180 | if (mt9m001->fmts == mt9m001_colour_fmts) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 181 | /* |
| 182 | * Bayer format - even number of rows for simplicity, |
| 183 | * but let the user play with the top row. |
| 184 | */ |
| 185 | rect.height = ALIGN(rect.height, 2); |
| 186 | |
| 187 | /* Datasheet requirement: see register description */ |
| 188 | rect.width = ALIGN(rect.width, 2); |
| 189 | rect.left = ALIGN(rect.left, 2); |
| 190 | |
| 191 | soc_camera_limit_side(&rect.left, &rect.width, |
| 192 | MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH); |
| 193 | |
| 194 | soc_camera_limit_side(&rect.top, &rect.height, |
| 195 | MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT); |
| 196 | |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 197 | mt9m001->total_h = rect.height + mt9m001->y_skip_top + vblank; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 198 | |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 199 | /* Blanking and start values - default... */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 200 | ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 201 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 202 | ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 203 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 204 | /* |
| 205 | * The caller provides a supported format, as verified per |
Guennadi Liakhovetski | 14178aa | 2011-09-21 15:16:30 -0300 | [diff] [blame] | 206 | * call to .try_mbus_fmt() |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 207 | */ |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 208 | if (!ret) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 209 | ret = reg_write(client, MT9M001_COLUMN_START, rect.left); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 210 | if (!ret) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 211 | ret = reg_write(client, MT9M001_ROW_START, rect.top); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 212 | if (!ret) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 213 | ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1); |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 214 | if (!ret) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 215 | ret = reg_write(client, MT9M001_WINDOW_HEIGHT, |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 216 | rect.height + mt9m001->y_skip_top - 1); |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 217 | if (!ret && v4l2_ctrl_g_ctrl(mt9m001->autoexposure) == V4L2_EXPOSURE_AUTO) |
| 218 | ret = reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 219 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 220 | if (!ret) |
| 221 | mt9m001->rect = rect; |
| 222 | |
Guennadi Liakhovetski | 1121164 | 2008-08-14 12:03:18 -0300 | [diff] [blame] | 223 | return ret; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 224 | } |
| 225 | |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 226 | static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a) |
| 227 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 228 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 229 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
| 230 | |
| 231 | a->c = mt9m001->rect; |
| 232 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a) |
| 238 | { |
| 239 | a->bounds.left = MT9M001_COLUMN_SKIP; |
| 240 | a->bounds.top = MT9M001_ROW_SKIP; |
| 241 | a->bounds.width = MT9M001_MAX_WIDTH; |
| 242 | a->bounds.height = MT9M001_MAX_HEIGHT; |
| 243 | a->defrect = a->bounds; |
| 244 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 245 | a->pixelaspect.numerator = 1; |
| 246 | a->pixelaspect.denominator = 1; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 251 | static int mt9m001_g_fmt(struct v4l2_subdev *sd, |
| 252 | struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 253 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 254 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 255 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 256 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 257 | mf->width = mt9m001->rect.width; |
| 258 | mf->height = mt9m001->rect.height; |
| 259 | mf->code = mt9m001->fmt->code; |
| 260 | mf->colorspace = mt9m001->fmt->colorspace; |
| 261 | mf->field = V4L2_FIELD_NONE; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 266 | static int mt9m001_s_fmt(struct v4l2_subdev *sd, |
| 267 | struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 268 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 269 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 270 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 271 | struct v4l2_crop a = { |
| 272 | .c = { |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 273 | .left = mt9m001->rect.left, |
| 274 | .top = mt9m001->rect.top, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 275 | .width = mf->width, |
| 276 | .height = mf->height, |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 277 | }, |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 278 | }; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 279 | int ret; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 280 | |
| 281 | /* No support for scaling so far, just crop. TODO: use skipping */ |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 282 | ret = mt9m001_s_crop(sd, &a); |
| 283 | if (!ret) { |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 284 | mf->width = mt9m001->rect.width; |
| 285 | mf->height = mt9m001->rect.height; |
| 286 | mt9m001->fmt = mt9m001_find_datafmt(mf->code, |
| 287 | mt9m001->fmts, mt9m001->num_fmts); |
| 288 | mf->colorspace = mt9m001->fmt->colorspace; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return ret; |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 292 | } |
| 293 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 294 | static int mt9m001_try_fmt(struct v4l2_subdev *sd, |
| 295 | struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 296 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 297 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 298 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 299 | const struct mt9m001_datafmt *fmt; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 300 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 301 | v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH, |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 302 | MT9M001_MAX_WIDTH, 1, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 303 | &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top, |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 304 | MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0); |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 305 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 306 | if (mt9m001->fmts == mt9m001_colour_fmts) |
| 307 | mf->height = ALIGN(mf->height - 1, 2); |
| 308 | |
| 309 | fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts, |
| 310 | mt9m001->num_fmts); |
| 311 | if (!fmt) { |
| 312 | fmt = mt9m001->fmt; |
| 313 | mf->code = fmt->code; |
| 314 | } |
| 315 | |
| 316 | mf->colorspace = fmt->colorspace; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 321 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 322 | static int mt9m001_g_register(struct v4l2_subdev *sd, |
| 323 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 324 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 325 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 326 | |
Hans Verkuil | 6be89da | 2013-05-29 06:59:50 -0300 | [diff] [blame^] | 327 | if (reg->reg > 0xff) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 328 | return -EINVAL; |
| 329 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 330 | reg->size = 2; |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 331 | reg->val = reg_read(client, reg->reg); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 332 | |
| 333 | if (reg->val > 0xffff) |
| 334 | return -EIO; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 339 | static int mt9m001_s_register(struct v4l2_subdev *sd, |
Hans Verkuil | 977ba3b | 2013-03-24 08:28:46 -0300 | [diff] [blame] | 340 | const struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 341 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 342 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 343 | |
Hans Verkuil | 6be89da | 2013-05-29 06:59:50 -0300 | [diff] [blame^] | 344 | if (reg->reg > 0xff) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 345 | return -EINVAL; |
| 346 | |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 347 | if (reg_write(client, reg->reg, reg->val) < 0) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 348 | return -EIO; |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | #endif |
| 353 | |
Laurent Pinchart | 4ec10ba | 2012-07-20 10:19:50 -0300 | [diff] [blame] | 354 | static int mt9m001_s_power(struct v4l2_subdev *sd, int on) |
| 355 | { |
| 356 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 357 | struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); |
Laurent Pinchart | 4ec10ba | 2012-07-20 10:19:50 -0300 | [diff] [blame] | 358 | |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 359 | return soc_camera_set_power(&client->dev, ssdd, on); |
Laurent Pinchart | 4ec10ba | 2012-07-20 10:19:50 -0300 | [diff] [blame] | 360 | } |
| 361 | |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 362 | static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 363 | { |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 364 | struct mt9m001 *mt9m001 = container_of(ctrl->handler, |
| 365 | struct mt9m001, hdl); |
| 366 | s32 min, max; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 367 | |
| 368 | switch (ctrl->id) { |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 369 | case V4L2_CID_EXPOSURE_AUTO: |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 370 | min = mt9m001->exposure->minimum; |
| 371 | max = mt9m001->exposure->maximum; |
| 372 | mt9m001->exposure->val = |
| 373 | (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min; |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 374 | break; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 379 | static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 380 | { |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 381 | struct mt9m001 *mt9m001 = container_of(ctrl->handler, |
| 382 | struct mt9m001, hdl); |
| 383 | struct v4l2_subdev *sd = &mt9m001->subdev; |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 384 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 385 | struct v4l2_ctrl *exp = mt9m001->exposure; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 386 | int data; |
| 387 | |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 388 | switch (ctrl->id) { |
| 389 | case V4L2_CID_VFLIP: |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 390 | if (ctrl->val) |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 391 | data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 392 | else |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 393 | data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 394 | if (data < 0) |
| 395 | return -EIO; |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 396 | return 0; |
| 397 | |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 398 | case V4L2_CID_GAIN: |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 399 | /* See Datasheet Table 7, Gain settings. */ |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 400 | if (ctrl->val <= ctrl->default_value) { |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 401 | /* Pack it into 0..1 step 0.125, register values 0..8 */ |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 402 | unsigned long range = ctrl->default_value - ctrl->minimum; |
| 403 | data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 404 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 405 | dev_dbg(&client->dev, "Setting gain %d\n", data); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 406 | data = reg_write(client, MT9M001_GLOBAL_GAIN, data); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 407 | if (data < 0) |
| 408 | return -EIO; |
| 409 | } else { |
| 410 | /* Pack it into 1.125..15 variable step, register values 9..67 */ |
| 411 | /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 412 | unsigned long range = ctrl->maximum - ctrl->default_value - 1; |
| 413 | unsigned long gain = ((ctrl->val - ctrl->default_value - 1) * |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 414 | 111 + range / 2) / range + 9; |
| 415 | |
| 416 | if (gain <= 32) |
| 417 | data = gain; |
| 418 | else if (gain <= 64) |
| 419 | data = ((gain - 32) * 16 + 16) / 32 + 80; |
| 420 | else |
| 421 | data = ((gain - 64) * 7 + 28) / 56 + 96; |
| 422 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 423 | dev_dbg(&client->dev, "Setting gain from %d to %d\n", |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 424 | reg_read(client, MT9M001_GLOBAL_GAIN), data); |
| 425 | data = reg_write(client, MT9M001_GLOBAL_GAIN, data); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 426 | if (data < 0) |
| 427 | return -EIO; |
| 428 | } |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 429 | return 0; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 430 | |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 431 | case V4L2_CID_EXPOSURE_AUTO: |
| 432 | if (ctrl->val == V4L2_EXPOSURE_MANUAL) { |
| 433 | unsigned long range = exp->maximum - exp->minimum; |
| 434 | unsigned long shutter = ((exp->val - exp->minimum) * 1048 + |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 435 | range / 2) / range + 1; |
| 436 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 437 | dev_dbg(&client->dev, |
| 438 | "Setting shutter width from %d to %lu\n", |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 439 | reg_read(client, MT9M001_SHUTTER_WIDTH), shutter); |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 440 | if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 441 | return -EIO; |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 442 | } else { |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 443 | const u16 vblank = 25; |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 444 | |
| 445 | mt9m001->total_h = mt9m001->rect.height + |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 446 | mt9m001->y_skip_top + vblank; |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 447 | if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 448 | return -EIO; |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 449 | } |
| 450 | return 0; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 451 | } |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 452 | return -EINVAL; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 453 | } |
| 454 | |
Guennadi Liakhovetski | 5d28d52 | 2009-12-11 11:15:05 -0300 | [diff] [blame] | 455 | /* |
| 456 | * Interface active, can use i2c. If it fails, it can indeed mean, that |
| 457 | * this wasn't our capture interface, so, we wait for the right one |
| 458 | */ |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 459 | static int mt9m001_video_probe(struct soc_camera_subdev_desc *ssdd, |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 460 | struct i2c_client *client) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 461 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 462 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 463 | s32 data; |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 464 | unsigned long flags; |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 465 | int ret; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 466 | |
Laurent Pinchart | 4bbc6d5 | 2012-07-18 10:54:04 -0300 | [diff] [blame] | 467 | ret = mt9m001_s_power(&mt9m001->subdev, 1); |
| 468 | if (ret < 0) |
| 469 | return ret; |
| 470 | |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 471 | /* Enable the chip */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 472 | data = reg_write(client, MT9M001_CHIP_ENABLE, 1); |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 473 | dev_dbg(&client->dev, "write: %d\n", data); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 474 | |
| 475 | /* Read out the chip version register */ |
Guennadi Liakhovetski | 9538e1c | 2009-04-24 12:57:01 -0300 | [diff] [blame] | 476 | data = reg_read(client, MT9M001_CHIP_VERSION); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 477 | |
| 478 | /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */ |
| 479 | switch (data) { |
| 480 | case 0x8411: |
| 481 | case 0x8421: |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 482 | mt9m001->fmts = mt9m001_colour_fmts; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 483 | break; |
| 484 | case 0x8431: |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 485 | mt9m001->fmts = mt9m001_monochrome_fmts; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 486 | break; |
| 487 | default: |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 488 | dev_err(&client->dev, |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 489 | "No MT9M001 chip detected, register read %x\n", data); |
Laurent Pinchart | 4bbc6d5 | 2012-07-18 10:54:04 -0300 | [diff] [blame] | 490 | ret = -ENODEV; |
| 491 | goto done; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 492 | } |
| 493 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 494 | mt9m001->num_fmts = 0; |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * This is a 10bit sensor, so by default we only allow 10bit. |
| 498 | * The platform may support different bus widths due to |
| 499 | * different routing of the data lines. |
| 500 | */ |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 501 | if (ssdd->query_bus_param) |
| 502 | flags = ssdd->query_bus_param(ssdd); |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 503 | else |
| 504 | flags = SOCAM_DATAWIDTH_10; |
| 505 | |
| 506 | if (flags & SOCAM_DATAWIDTH_10) |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 507 | mt9m001->num_fmts++; |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 508 | else |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 509 | mt9m001->fmts++; |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 510 | |
| 511 | if (flags & SOCAM_DATAWIDTH_8) |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 512 | mt9m001->num_fmts++; |
Sascha Hauer | 36034dc | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 513 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 514 | mt9m001->fmt = &mt9m001->fmts[0]; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 515 | |
Guennadi Liakhovetski | 85f8be6 | 2009-08-25 11:47:00 -0300 | [diff] [blame] | 516 | dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data, |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 517 | data == 0x8431 ? "C12STM" : "C12ST"); |
| 518 | |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 519 | ret = mt9m001_init(client); |
Laurent Pinchart | 4bbc6d5 | 2012-07-18 10:54:04 -0300 | [diff] [blame] | 520 | if (ret < 0) { |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 521 | dev_err(&client->dev, "Failed to initialise the camera\n"); |
Laurent Pinchart | 4bbc6d5 | 2012-07-18 10:54:04 -0300 | [diff] [blame] | 522 | goto done; |
| 523 | } |
Guennadi Liakhovetski | a4c56fd | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 524 | |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 525 | /* mt9m001_init() has reset the chip, returning registers to defaults */ |
Laurent Pinchart | 4bbc6d5 | 2012-07-18 10:54:04 -0300 | [diff] [blame] | 526 | ret = v4l2_ctrl_handler_setup(&mt9m001->hdl); |
| 527 | |
| 528 | done: |
| 529 | mt9m001_s_power(&mt9m001->subdev, 0); |
| 530 | return ret; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 531 | } |
| 532 | |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 533 | static void mt9m001_video_remove(struct soc_camera_subdev_desc *ssdd) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 534 | { |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 535 | if (ssdd->free_bus) |
| 536 | ssdd->free_bus(ssdd); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 537 | } |
| 538 | |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 539 | static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines) |
| 540 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 541 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 542 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
| 543 | |
| 544 | *lines = mt9m001->y_skip_top; |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 549 | static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = { |
| 550 | .g_volatile_ctrl = mt9m001_g_volatile_ctrl, |
| 551 | .s_ctrl = mt9m001_s_ctrl, |
| 552 | }; |
| 553 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 554 | static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 555 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 556 | .g_register = mt9m001_g_register, |
| 557 | .s_register = mt9m001_s_register, |
| 558 | #endif |
Laurent Pinchart | 4ec10ba | 2012-07-20 10:19:50 -0300 | [diff] [blame] | 559 | .s_power = mt9m001_s_power, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 560 | }; |
| 561 | |
Hans Verkuil | 3805f20 | 2010-05-08 17:55:00 -0300 | [diff] [blame] | 562 | static int mt9m001_enum_fmt(struct v4l2_subdev *sd, unsigned int index, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 563 | enum v4l2_mbus_pixelcode *code) |
| 564 | { |
Laurent Pinchart | c4ce6d1 | 2010-07-30 17:24:54 -0300 | [diff] [blame] | 565 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 566 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
| 567 | |
Hans Verkuil | 3805f20 | 2010-05-08 17:55:00 -0300 | [diff] [blame] | 568 | if (index >= mt9m001->num_fmts) |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 569 | return -EINVAL; |
| 570 | |
| 571 | *code = mt9m001->fmts[index].code; |
| 572 | return 0; |
| 573 | } |
| 574 | |
Guennadi Liakhovetski | 32ca208 | 2011-07-26 11:46:02 -0300 | [diff] [blame] | 575 | static int mt9m001_g_mbus_config(struct v4l2_subdev *sd, |
| 576 | struct v4l2_mbus_config *cfg) |
| 577 | { |
| 578 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 579 | struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); |
Guennadi Liakhovetski | 32ca208 | 2011-07-26 11:46:02 -0300 | [diff] [blame] | 580 | |
| 581 | /* MT9M001 has all capture_format parameters fixed */ |
| 582 | cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING | |
| 583 | V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH | |
| 584 | V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER; |
| 585 | cfg->type = V4L2_MBUS_PARALLEL; |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 586 | cfg->flags = soc_camera_apply_board_flags(ssdd, cfg); |
Guennadi Liakhovetski | 32ca208 | 2011-07-26 11:46:02 -0300 | [diff] [blame] | 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static int mt9m001_s_mbus_config(struct v4l2_subdev *sd, |
| 592 | const struct v4l2_mbus_config *cfg) |
| 593 | { |
Guennadi Liakhovetski | 14178aa | 2011-09-21 15:16:30 -0300 | [diff] [blame] | 594 | const struct i2c_client *client = v4l2_get_subdevdata(sd); |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 595 | struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); |
Guennadi Liakhovetski | 443f483 | 2011-09-09 07:06:50 -0300 | [diff] [blame] | 596 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
| 597 | unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample; |
Guennadi Liakhovetski | 32ca208 | 2011-07-26 11:46:02 -0300 | [diff] [blame] | 598 | |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 599 | if (ssdd->set_bus_param) |
| 600 | return ssdd->set_bus_param(ssdd, 1 << (bps - 1)); |
Guennadi Liakhovetski | 32ca208 | 2011-07-26 11:46:02 -0300 | [diff] [blame] | 601 | |
| 602 | /* |
| 603 | * Without board specific bus width settings we only support the |
| 604 | * sensors native bus width |
| 605 | */ |
| 606 | return bps == 10 ? 0 : -EINVAL; |
| 607 | } |
| 608 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 609 | static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = { |
| 610 | .s_stream = mt9m001_s_stream, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 611 | .s_mbus_fmt = mt9m001_s_fmt, |
| 612 | .g_mbus_fmt = mt9m001_g_fmt, |
| 613 | .try_mbus_fmt = mt9m001_try_fmt, |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 614 | .s_crop = mt9m001_s_crop, |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 615 | .g_crop = mt9m001_g_crop, |
| 616 | .cropcap = mt9m001_cropcap, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 617 | .enum_mbus_fmt = mt9m001_enum_fmt, |
Guennadi Liakhovetski | 32ca208 | 2011-07-26 11:46:02 -0300 | [diff] [blame] | 618 | .g_mbus_config = mt9m001_g_mbus_config, |
| 619 | .s_mbus_config = mt9m001_s_mbus_config, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 620 | }; |
| 621 | |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 622 | static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = { |
| 623 | .g_skip_top_lines = mt9m001_g_skip_top_lines, |
| 624 | }; |
| 625 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 626 | static struct v4l2_subdev_ops mt9m001_subdev_ops = { |
| 627 | .core = &mt9m001_subdev_core_ops, |
| 628 | .video = &mt9m001_subdev_video_ops, |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 629 | .sensor = &mt9m001_subdev_sensor_ops, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 630 | }; |
| 631 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 632 | static int mt9m001_probe(struct i2c_client *client, |
| 633 | const struct i2c_device_id *did) |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 634 | { |
| 635 | struct mt9m001 *mt9m001; |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 636 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 637 | struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 638 | int ret; |
| 639 | |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 640 | if (!ssdd) { |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 641 | dev_err(&client->dev, "MT9M001 driver needs platform data\n"); |
| 642 | return -EINVAL; |
| 643 | } |
| 644 | |
| 645 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 646 | dev_warn(&adapter->dev, |
| 647 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 648 | return -EIO; |
| 649 | } |
| 650 | |
Guennadi Liakhovetski | 70e176a | 2012-12-21 10:28:43 -0300 | [diff] [blame] | 651 | mt9m001 = devm_kzalloc(&client->dev, sizeof(struct mt9m001), GFP_KERNEL); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 652 | if (!mt9m001) |
| 653 | return -ENOMEM; |
| 654 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 655 | v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops); |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 656 | v4l2_ctrl_handler_init(&mt9m001->hdl, 4); |
| 657 | v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops, |
| 658 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 659 | v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops, |
| 660 | V4L2_CID_GAIN, 0, 127, 1, 64); |
| 661 | mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops, |
| 662 | V4L2_CID_EXPOSURE, 1, 255, 1, 255); |
| 663 | /* |
| 664 | * Simulated autoexposure. If enabled, we calculate shutter width |
| 665 | * ourselves in the driver based on vertical blanking and frame width |
| 666 | */ |
| 667 | mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl, |
| 668 | &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0, |
| 669 | V4L2_EXPOSURE_AUTO); |
| 670 | mt9m001->subdev.ctrl_handler = &mt9m001->hdl; |
Guennadi Liakhovetski | 70e176a | 2012-12-21 10:28:43 -0300 | [diff] [blame] | 671 | if (mt9m001->hdl.error) |
| 672 | return mt9m001->hdl.error; |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 673 | |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 674 | v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure, |
| 675 | V4L2_EXPOSURE_MANUAL, true); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 676 | |
| 677 | /* Second stage probe - when a capture adapter is there */ |
Guennadi Liakhovetski | 3253610 | 2009-12-11 11:14:46 -0300 | [diff] [blame] | 678 | mt9m001->y_skip_top = 0; |
Guennadi Liakhovetski | 6a6c878 | 2009-08-25 11:50:46 -0300 | [diff] [blame] | 679 | mt9m001->rect.left = MT9M001_COLUMN_SKIP; |
| 680 | mt9m001->rect.top = MT9M001_ROW_SKIP; |
| 681 | mt9m001->rect.width = MT9M001_MAX_WIDTH; |
| 682 | mt9m001->rect.height = MT9M001_MAX_HEIGHT; |
| 683 | |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 684 | ret = mt9m001_video_probe(ssdd, client); |
Guennadi Liakhovetski | 70e176a | 2012-12-21 10:28:43 -0300 | [diff] [blame] | 685 | if (ret) |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 686 | v4l2_ctrl_handler_free(&mt9m001->hdl); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 687 | |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | static int mt9m001_remove(struct i2c_client *client) |
| 692 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 693 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 694 | struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 695 | |
Hans Verkuil | 2dd7d29 | 2011-09-07 06:04:30 -0300 | [diff] [blame] | 696 | v4l2_device_unregister_subdev(&mt9m001->subdev); |
| 697 | v4l2_ctrl_handler_free(&mt9m001->hdl); |
Guennadi Liakhovetski | 25a3481 | 2012-12-21 08:11:48 -0300 | [diff] [blame] | 698 | mt9m001_video_remove(ssdd); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 703 | static const struct i2c_device_id mt9m001_id[] = { |
| 704 | { "mt9m001", 0 }, |
| 705 | { } |
| 706 | }; |
| 707 | MODULE_DEVICE_TABLE(i2c, mt9m001_id); |
| 708 | |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 709 | static struct i2c_driver mt9m001_i2c_driver = { |
| 710 | .driver = { |
| 711 | .name = "mt9m001", |
| 712 | }, |
| 713 | .probe = mt9m001_probe, |
| 714 | .remove = mt9m001_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 715 | .id_table = mt9m001_id, |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 716 | }; |
| 717 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 718 | module_i2c_driver(mt9m001_i2c_driver); |
Guennadi Liakhovetski | f523dd0 | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 719 | |
| 720 | MODULE_DESCRIPTION("Micron MT9M001 Camera driver"); |
| 721 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); |
| 722 | MODULE_LICENSE("GPL"); |