Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for RJ54N1CB0C CMOS Image Sensor from Micron |
| 3 | * |
| 4 | * Copyright (C) 2009, Guennadi Liakhovetski <g.liakhovetski@gmx.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/delay.h> |
| 12 | #include <linux/i2c.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/videodev2.h> |
| 15 | |
| 16 | #include <media/v4l2-subdev.h> |
| 17 | #include <media/v4l2-chip-ident.h> |
| 18 | #include <media/soc_camera.h> |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 19 | #include <media/soc_mediabus.h> |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 20 | |
| 21 | #define RJ54N1_DEV_CODE 0x0400 |
| 22 | #define RJ54N1_DEV_CODE2 0x0401 |
| 23 | #define RJ54N1_OUT_SEL 0x0403 |
| 24 | #define RJ54N1_XY_OUTPUT_SIZE_S_H 0x0404 |
| 25 | #define RJ54N1_X_OUTPUT_SIZE_S_L 0x0405 |
| 26 | #define RJ54N1_Y_OUTPUT_SIZE_S_L 0x0406 |
| 27 | #define RJ54N1_XY_OUTPUT_SIZE_P_H 0x0407 |
| 28 | #define RJ54N1_X_OUTPUT_SIZE_P_L 0x0408 |
| 29 | #define RJ54N1_Y_OUTPUT_SIZE_P_L 0x0409 |
| 30 | #define RJ54N1_LINE_LENGTH_PCK_S_H 0x040a |
| 31 | #define RJ54N1_LINE_LENGTH_PCK_S_L 0x040b |
| 32 | #define RJ54N1_LINE_LENGTH_PCK_P_H 0x040c |
| 33 | #define RJ54N1_LINE_LENGTH_PCK_P_L 0x040d |
| 34 | #define RJ54N1_RESIZE_N 0x040e |
| 35 | #define RJ54N1_RESIZE_N_STEP 0x040f |
| 36 | #define RJ54N1_RESIZE_STEP 0x0410 |
| 37 | #define RJ54N1_RESIZE_HOLD_H 0x0411 |
| 38 | #define RJ54N1_RESIZE_HOLD_L 0x0412 |
| 39 | #define RJ54N1_H_OBEN_OFS 0x0413 |
| 40 | #define RJ54N1_V_OBEN_OFS 0x0414 |
| 41 | #define RJ54N1_RESIZE_CONTROL 0x0415 |
| 42 | #define RJ54N1_INC_USE_SEL_H 0x0425 |
| 43 | #define RJ54N1_INC_USE_SEL_L 0x0426 |
| 44 | #define RJ54N1_MIRROR_STILL_MODE 0x0427 |
| 45 | #define RJ54N1_INIT_START 0x0428 |
| 46 | #define RJ54N1_SCALE_1_2_LEV 0x0429 |
| 47 | #define RJ54N1_SCALE_4_LEV 0x042a |
| 48 | #define RJ54N1_Y_GAIN 0x04d8 |
| 49 | #define RJ54N1_APT_GAIN_UP 0x04fa |
| 50 | #define RJ54N1_RA_SEL_UL 0x0530 |
| 51 | #define RJ54N1_BYTE_SWAP 0x0531 |
| 52 | #define RJ54N1_OUT_SIGPO 0x053b |
| 53 | #define RJ54N1_FRAME_LENGTH_S_H 0x0595 |
| 54 | #define RJ54N1_FRAME_LENGTH_S_L 0x0596 |
| 55 | #define RJ54N1_FRAME_LENGTH_P_H 0x0597 |
| 56 | #define RJ54N1_FRAME_LENGTH_P_L 0x0598 |
| 57 | #define RJ54N1_IOC 0x05ef |
| 58 | #define RJ54N1_TG_BYPASS 0x0700 |
| 59 | #define RJ54N1_PLL_L 0x0701 |
| 60 | #define RJ54N1_PLL_N 0x0702 |
| 61 | #define RJ54N1_PLL_EN 0x0704 |
| 62 | #define RJ54N1_RATIO_TG 0x0706 |
| 63 | #define RJ54N1_RATIO_T 0x0707 |
| 64 | #define RJ54N1_RATIO_R 0x0708 |
| 65 | #define RJ54N1_RAMP_TGCLK_EN 0x0709 |
| 66 | #define RJ54N1_OCLK_DSP 0x0710 |
| 67 | #define RJ54N1_RATIO_OP 0x0711 |
| 68 | #define RJ54N1_RATIO_O 0x0712 |
| 69 | #define RJ54N1_OCLK_SEL_EN 0x0713 |
| 70 | #define RJ54N1_CLK_RST 0x0717 |
| 71 | #define RJ54N1_RESET_STANDBY 0x0718 |
| 72 | |
| 73 | #define E_EXCLK (1 << 7) |
| 74 | #define SOFT_STDBY (1 << 4) |
| 75 | #define SEN_RSTX (1 << 2) |
| 76 | #define TG_RSTX (1 << 1) |
| 77 | #define DSP_RSTX (1 << 0) |
| 78 | |
| 79 | #define RESIZE_HOLD_SEL (1 << 2) |
| 80 | #define RESIZE_GO (1 << 1) |
| 81 | |
| 82 | #define RJ54N1_COLUMN_SKIP 0 |
| 83 | #define RJ54N1_ROW_SKIP 0 |
| 84 | #define RJ54N1_MAX_WIDTH 1600 |
| 85 | #define RJ54N1_MAX_HEIGHT 1200 |
| 86 | |
| 87 | /* I2C addresses: 0x50, 0x51, 0x60, 0x61 */ |
| 88 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 89 | /* RJ54N1CB0C has only one fixed colorspace per pixelcode */ |
| 90 | struct rj54n1_datafmt { |
| 91 | enum v4l2_mbus_pixelcode code; |
| 92 | enum v4l2_colorspace colorspace; |
| 93 | }; |
| 94 | |
| 95 | /* Find a data format by a pixel code in an array */ |
| 96 | static const struct rj54n1_datafmt *rj54n1_find_datafmt( |
| 97 | enum v4l2_mbus_pixelcode code, const struct rj54n1_datafmt *fmt, |
| 98 | int n) |
| 99 | { |
| 100 | int i; |
| 101 | for (i = 0; i < n; i++) |
| 102 | if (fmt[i].code == code) |
| 103 | return fmt + i; |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | static const struct rj54n1_datafmt rj54n1_colour_fmts[] = { |
| 109 | {V4L2_MBUS_FMT_YUYV8_2X8_LE, V4L2_COLORSPACE_JPEG}, |
| 110 | {V4L2_MBUS_FMT_YVYU8_2X8_LE, V4L2_COLORSPACE_JPEG}, |
| 111 | {V4L2_MBUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB}, |
| 112 | {V4L2_MBUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB}, |
| 113 | {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB}, |
| 114 | {V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE, V4L2_COLORSPACE_SRGB}, |
| 115 | {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE, V4L2_COLORSPACE_SRGB}, |
| 116 | {V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE, V4L2_COLORSPACE_SRGB}, |
| 117 | {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB}, |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | struct rj54n1_clock_div { |
| 121 | u8 ratio_tg; |
| 122 | u8 ratio_t; |
| 123 | u8 ratio_r; |
| 124 | u8 ratio_op; |
| 125 | u8 ratio_o; |
| 126 | }; |
| 127 | |
| 128 | struct rj54n1 { |
| 129 | struct v4l2_subdev subdev; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 130 | const struct rj54n1_datafmt *fmt; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 131 | struct v4l2_rect rect; /* Sensor window */ |
| 132 | unsigned short width; /* Output window */ |
| 133 | unsigned short height; |
| 134 | unsigned short resize; /* Sensor * 1024 / resize = Output */ |
| 135 | struct rj54n1_clock_div clk_div; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 136 | unsigned short scale; |
| 137 | u8 bank; |
| 138 | }; |
| 139 | |
| 140 | struct rj54n1_reg_val { |
| 141 | u16 reg; |
| 142 | u8 val; |
| 143 | }; |
| 144 | |
| 145 | const static struct rj54n1_reg_val bank_4[] = { |
| 146 | {0x417, 0}, |
| 147 | {0x42c, 0}, |
| 148 | {0x42d, 0xf0}, |
| 149 | {0x42e, 0}, |
| 150 | {0x42f, 0x50}, |
| 151 | {0x430, 0xf5}, |
| 152 | {0x431, 0x16}, |
| 153 | {0x432, 0x20}, |
| 154 | {0x433, 0}, |
| 155 | {0x434, 0xc8}, |
| 156 | {0x43c, 8}, |
| 157 | {0x43e, 0x90}, |
| 158 | {0x445, 0x83}, |
| 159 | {0x4ba, 0x58}, |
| 160 | {0x4bb, 4}, |
| 161 | {0x4bc, 0x20}, |
| 162 | {0x4db, 4}, |
| 163 | {0x4fe, 2}, |
| 164 | }; |
| 165 | |
| 166 | const static struct rj54n1_reg_val bank_5[] = { |
| 167 | {0x514, 0}, |
| 168 | {0x516, 0}, |
| 169 | {0x518, 0}, |
| 170 | {0x51a, 0}, |
| 171 | {0x51d, 0xff}, |
| 172 | {0x56f, 0x28}, |
| 173 | {0x575, 0x40}, |
| 174 | {0x5bc, 0x48}, |
| 175 | {0x5c1, 6}, |
| 176 | {0x5e5, 0x11}, |
| 177 | {0x5e6, 0x43}, |
| 178 | {0x5e7, 0x33}, |
| 179 | {0x5e8, 0x21}, |
| 180 | {0x5e9, 0x30}, |
| 181 | {0x5ea, 0x0}, |
| 182 | {0x5eb, 0xa5}, |
| 183 | {0x5ec, 0xff}, |
| 184 | {0x5fe, 2}, |
| 185 | }; |
| 186 | |
| 187 | const static struct rj54n1_reg_val bank_7[] = { |
| 188 | {0x70a, 0}, |
| 189 | {0x714, 0xff}, |
| 190 | {0x715, 0xff}, |
| 191 | {0x716, 0x1f}, |
| 192 | {0x7FE, 0x02}, |
| 193 | }; |
| 194 | |
| 195 | const static struct rj54n1_reg_val bank_8[] = { |
| 196 | {0x800, 0x00}, |
| 197 | {0x801, 0x01}, |
| 198 | {0x802, 0x61}, |
| 199 | {0x805, 0x00}, |
| 200 | {0x806, 0x00}, |
| 201 | {0x807, 0x00}, |
| 202 | {0x808, 0x00}, |
| 203 | {0x809, 0x01}, |
| 204 | {0x80A, 0x61}, |
| 205 | {0x80B, 0x00}, |
| 206 | {0x80C, 0x01}, |
| 207 | {0x80D, 0x00}, |
| 208 | {0x80E, 0x00}, |
| 209 | {0x80F, 0x00}, |
| 210 | {0x810, 0x00}, |
| 211 | {0x811, 0x01}, |
| 212 | {0x812, 0x61}, |
| 213 | {0x813, 0x00}, |
| 214 | {0x814, 0x11}, |
| 215 | {0x815, 0x00}, |
| 216 | {0x816, 0x41}, |
| 217 | {0x817, 0x00}, |
| 218 | {0x818, 0x51}, |
| 219 | {0x819, 0x01}, |
| 220 | {0x81A, 0x1F}, |
| 221 | {0x81B, 0x00}, |
| 222 | {0x81C, 0x01}, |
| 223 | {0x81D, 0x00}, |
| 224 | {0x81E, 0x11}, |
| 225 | {0x81F, 0x00}, |
| 226 | {0x820, 0x41}, |
| 227 | {0x821, 0x00}, |
| 228 | {0x822, 0x51}, |
| 229 | {0x823, 0x00}, |
| 230 | {0x824, 0x00}, |
| 231 | {0x825, 0x00}, |
| 232 | {0x826, 0x47}, |
| 233 | {0x827, 0x01}, |
| 234 | {0x828, 0x4F}, |
| 235 | {0x829, 0x00}, |
| 236 | {0x82A, 0x00}, |
| 237 | {0x82B, 0x00}, |
| 238 | {0x82C, 0x30}, |
| 239 | {0x82D, 0x00}, |
| 240 | {0x82E, 0x40}, |
| 241 | {0x82F, 0x00}, |
| 242 | {0x830, 0xB3}, |
| 243 | {0x831, 0x00}, |
| 244 | {0x832, 0xE3}, |
| 245 | {0x833, 0x00}, |
| 246 | {0x834, 0x00}, |
| 247 | {0x835, 0x00}, |
| 248 | {0x836, 0x00}, |
| 249 | {0x837, 0x00}, |
| 250 | {0x838, 0x00}, |
| 251 | {0x839, 0x01}, |
| 252 | {0x83A, 0x61}, |
| 253 | {0x83B, 0x00}, |
| 254 | {0x83C, 0x01}, |
| 255 | {0x83D, 0x00}, |
| 256 | {0x83E, 0x00}, |
| 257 | {0x83F, 0x00}, |
| 258 | {0x840, 0x00}, |
| 259 | {0x841, 0x01}, |
| 260 | {0x842, 0x61}, |
| 261 | {0x843, 0x00}, |
| 262 | {0x844, 0x1D}, |
| 263 | {0x845, 0x00}, |
| 264 | {0x846, 0x00}, |
| 265 | {0x847, 0x00}, |
| 266 | {0x848, 0x00}, |
| 267 | {0x849, 0x01}, |
| 268 | {0x84A, 0x1F}, |
| 269 | {0x84B, 0x00}, |
| 270 | {0x84C, 0x05}, |
| 271 | {0x84D, 0x00}, |
| 272 | {0x84E, 0x19}, |
| 273 | {0x84F, 0x01}, |
| 274 | {0x850, 0x21}, |
| 275 | {0x851, 0x01}, |
| 276 | {0x852, 0x5D}, |
| 277 | {0x853, 0x00}, |
| 278 | {0x854, 0x00}, |
| 279 | {0x855, 0x00}, |
| 280 | {0x856, 0x19}, |
| 281 | {0x857, 0x01}, |
| 282 | {0x858, 0x21}, |
| 283 | {0x859, 0x00}, |
| 284 | {0x85A, 0x00}, |
| 285 | {0x85B, 0x00}, |
| 286 | {0x85C, 0x00}, |
| 287 | {0x85D, 0x00}, |
| 288 | {0x85E, 0x00}, |
| 289 | {0x85F, 0x00}, |
| 290 | {0x860, 0xB3}, |
| 291 | {0x861, 0x00}, |
| 292 | {0x862, 0xE3}, |
| 293 | {0x863, 0x00}, |
| 294 | {0x864, 0x00}, |
| 295 | {0x865, 0x00}, |
| 296 | {0x866, 0x00}, |
| 297 | {0x867, 0x00}, |
| 298 | {0x868, 0x00}, |
| 299 | {0x869, 0xE2}, |
| 300 | {0x86A, 0x00}, |
| 301 | {0x86B, 0x01}, |
| 302 | {0x86C, 0x06}, |
| 303 | {0x86D, 0x00}, |
| 304 | {0x86E, 0x00}, |
| 305 | {0x86F, 0x00}, |
| 306 | {0x870, 0x60}, |
| 307 | {0x871, 0x8C}, |
| 308 | {0x872, 0x10}, |
| 309 | {0x873, 0x00}, |
| 310 | {0x874, 0xE0}, |
| 311 | {0x875, 0x00}, |
| 312 | {0x876, 0x27}, |
| 313 | {0x877, 0x01}, |
| 314 | {0x878, 0x00}, |
| 315 | {0x879, 0x00}, |
| 316 | {0x87A, 0x00}, |
| 317 | {0x87B, 0x03}, |
| 318 | {0x87C, 0x00}, |
| 319 | {0x87D, 0x00}, |
| 320 | {0x87E, 0x00}, |
| 321 | {0x87F, 0x00}, |
| 322 | {0x880, 0x00}, |
| 323 | {0x881, 0x00}, |
| 324 | {0x882, 0x00}, |
| 325 | {0x883, 0x00}, |
| 326 | {0x884, 0x00}, |
| 327 | {0x885, 0x00}, |
| 328 | {0x886, 0xF8}, |
| 329 | {0x887, 0x00}, |
| 330 | {0x888, 0x03}, |
| 331 | {0x889, 0x00}, |
| 332 | {0x88A, 0x64}, |
| 333 | {0x88B, 0x00}, |
| 334 | {0x88C, 0x03}, |
| 335 | {0x88D, 0x00}, |
| 336 | {0x88E, 0xB1}, |
| 337 | {0x88F, 0x00}, |
| 338 | {0x890, 0x03}, |
| 339 | {0x891, 0x01}, |
| 340 | {0x892, 0x1D}, |
| 341 | {0x893, 0x00}, |
| 342 | {0x894, 0x03}, |
| 343 | {0x895, 0x01}, |
| 344 | {0x896, 0x4B}, |
| 345 | {0x897, 0x00}, |
| 346 | {0x898, 0xE5}, |
| 347 | {0x899, 0x00}, |
| 348 | {0x89A, 0x01}, |
| 349 | {0x89B, 0x00}, |
| 350 | {0x89C, 0x01}, |
| 351 | {0x89D, 0x04}, |
| 352 | {0x89E, 0xC8}, |
| 353 | {0x89F, 0x00}, |
| 354 | {0x8A0, 0x01}, |
| 355 | {0x8A1, 0x01}, |
| 356 | {0x8A2, 0x61}, |
| 357 | {0x8A3, 0x00}, |
| 358 | {0x8A4, 0x01}, |
| 359 | {0x8A5, 0x00}, |
| 360 | {0x8A6, 0x00}, |
| 361 | {0x8A7, 0x00}, |
| 362 | {0x8A8, 0x00}, |
| 363 | {0x8A9, 0x00}, |
| 364 | {0x8AA, 0x7F}, |
| 365 | {0x8AB, 0x03}, |
| 366 | {0x8AC, 0x00}, |
| 367 | {0x8AD, 0x00}, |
| 368 | {0x8AE, 0x00}, |
| 369 | {0x8AF, 0x00}, |
| 370 | {0x8B0, 0x00}, |
| 371 | {0x8B1, 0x00}, |
| 372 | {0x8B6, 0x00}, |
| 373 | {0x8B7, 0x01}, |
| 374 | {0x8B8, 0x00}, |
| 375 | {0x8B9, 0x00}, |
| 376 | {0x8BA, 0x02}, |
| 377 | {0x8BB, 0x00}, |
| 378 | {0x8BC, 0xFF}, |
| 379 | {0x8BD, 0x00}, |
| 380 | {0x8FE, 0x02}, |
| 381 | }; |
| 382 | |
| 383 | const static struct rj54n1_reg_val bank_10[] = { |
| 384 | {0x10bf, 0x69} |
| 385 | }; |
| 386 | |
| 387 | /* Clock dividers - these are default register values, divider = register + 1 */ |
| 388 | const static struct rj54n1_clock_div clk_div = { |
| 389 | .ratio_tg = 3 /* default: 5 */, |
| 390 | .ratio_t = 4 /* default: 1 */, |
| 391 | .ratio_r = 4 /* default: 0 */, |
| 392 | .ratio_op = 1 /* default: 5 */, |
| 393 | .ratio_o = 9 /* default: 0 */, |
| 394 | }; |
| 395 | |
| 396 | static struct rj54n1 *to_rj54n1(const struct i2c_client *client) |
| 397 | { |
| 398 | return container_of(i2c_get_clientdata(client), struct rj54n1, subdev); |
| 399 | } |
| 400 | |
| 401 | static int reg_read(struct i2c_client *client, const u16 reg) |
| 402 | { |
| 403 | struct rj54n1 *rj54n1 = to_rj54n1(client); |
| 404 | int ret; |
| 405 | |
| 406 | /* set bank */ |
| 407 | if (rj54n1->bank != reg >> 8) { |
| 408 | dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8); |
| 409 | ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8); |
| 410 | if (ret < 0) |
| 411 | return ret; |
| 412 | rj54n1->bank = reg >> 8; |
| 413 | } |
| 414 | return i2c_smbus_read_byte_data(client, reg & 0xff); |
| 415 | } |
| 416 | |
| 417 | static int reg_write(struct i2c_client *client, const u16 reg, |
| 418 | const u8 data) |
| 419 | { |
| 420 | struct rj54n1 *rj54n1 = to_rj54n1(client); |
| 421 | int ret; |
| 422 | |
| 423 | /* set bank */ |
| 424 | if (rj54n1->bank != reg >> 8) { |
| 425 | dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8); |
| 426 | ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8); |
| 427 | if (ret < 0) |
| 428 | return ret; |
| 429 | rj54n1->bank = reg >> 8; |
| 430 | } |
| 431 | dev_dbg(&client->dev, "[0x%x] = 0x%x\n", reg & 0xff, data); |
| 432 | return i2c_smbus_write_byte_data(client, reg & 0xff, data); |
| 433 | } |
| 434 | |
| 435 | static int reg_set(struct i2c_client *client, const u16 reg, |
| 436 | const u8 data, const u8 mask) |
| 437 | { |
| 438 | int ret; |
| 439 | |
| 440 | ret = reg_read(client, reg); |
| 441 | if (ret < 0) |
| 442 | return ret; |
| 443 | return reg_write(client, reg, (ret & ~mask) | (data & mask)); |
| 444 | } |
| 445 | |
| 446 | static int reg_write_multiple(struct i2c_client *client, |
| 447 | const struct rj54n1_reg_val *rv, const int n) |
| 448 | { |
| 449 | int i, ret; |
| 450 | |
| 451 | for (i = 0; i < n; i++) { |
| 452 | ret = reg_write(client, rv->reg, rv->val); |
| 453 | if (ret < 0) |
| 454 | return ret; |
| 455 | rv++; |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 461 | static int rj54n1_enum_fmt(struct v4l2_subdev *sd, int index, |
| 462 | enum v4l2_mbus_pixelcode *code) |
| 463 | { |
| 464 | if ((unsigned int)index >= ARRAY_SIZE(rj54n1_colour_fmts)) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | *code = rj54n1_colour_fmts[index].code; |
| 468 | return 0; |
| 469 | } |
| 470 | |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 471 | static int rj54n1_s_stream(struct v4l2_subdev *sd, int enable) |
| 472 | { |
| 473 | /* TODO: start / stop streaming */ |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static int rj54n1_set_bus_param(struct soc_camera_device *icd, |
| 478 | unsigned long flags) |
| 479 | { |
| 480 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 481 | struct i2c_client *client = sd->priv; |
| 482 | /* Figures 2.5-1 to 2.5-3 - default falling pixclk edge */ |
| 483 | |
| 484 | if (flags & SOCAM_PCLK_SAMPLE_RISING) |
| 485 | return reg_write(client, RJ54N1_OUT_SIGPO, 1 << 4); |
| 486 | else |
| 487 | return reg_write(client, RJ54N1_OUT_SIGPO, 0); |
| 488 | } |
| 489 | |
| 490 | static unsigned long rj54n1_query_bus_param(struct soc_camera_device *icd) |
| 491 | { |
| 492 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 493 | const unsigned long flags = |
| 494 | SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | |
| 495 | SOCAM_MASTER | SOCAM_DATAWIDTH_8 | |
| 496 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH | |
| 497 | SOCAM_DATA_ACTIVE_HIGH; |
| 498 | |
| 499 | return soc_camera_apply_sensor_flags(icl, flags); |
| 500 | } |
| 501 | |
| 502 | static int rj54n1_set_rect(struct i2c_client *client, |
| 503 | u16 reg_x, u16 reg_y, u16 reg_xy, |
| 504 | u32 width, u32 height) |
| 505 | { |
| 506 | int ret; |
| 507 | |
| 508 | ret = reg_write(client, reg_xy, |
| 509 | ((width >> 4) & 0x70) | |
| 510 | ((height >> 8) & 7)); |
| 511 | |
| 512 | if (!ret) |
| 513 | ret = reg_write(client, reg_x, width & 0xff); |
| 514 | if (!ret) |
| 515 | ret = reg_write(client, reg_y, height & 0xff); |
| 516 | |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Some commands, specifically certain initialisation sequences, require |
| 522 | * a commit operation. |
| 523 | */ |
| 524 | static int rj54n1_commit(struct i2c_client *client) |
| 525 | { |
| 526 | int ret = reg_write(client, RJ54N1_INIT_START, 1); |
| 527 | msleep(10); |
| 528 | if (!ret) |
| 529 | ret = reg_write(client, RJ54N1_INIT_START, 0); |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | static int rj54n1_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a) |
| 534 | { |
| 535 | struct i2c_client *client = sd->priv; |
| 536 | struct rj54n1 *rj54n1 = to_rj54n1(client); |
| 537 | |
| 538 | a->c = rj54n1->rect; |
| 539 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static int rj54n1_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a) |
| 545 | { |
| 546 | a->bounds.left = RJ54N1_COLUMN_SKIP; |
| 547 | a->bounds.top = RJ54N1_ROW_SKIP; |
| 548 | a->bounds.width = RJ54N1_MAX_WIDTH; |
| 549 | a->bounds.height = RJ54N1_MAX_HEIGHT; |
| 550 | a->defrect = a->bounds; |
| 551 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 552 | a->pixelaspect.numerator = 1; |
| 553 | a->pixelaspect.denominator = 1; |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 558 | static int rj54n1_g_fmt(struct v4l2_subdev *sd, |
| 559 | struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 560 | { |
| 561 | struct i2c_client *client = sd->priv; |
| 562 | struct rj54n1 *rj54n1 = to_rj54n1(client); |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 563 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 564 | mf->code = rj54n1->fmt->code; |
| 565 | mf->colorspace = rj54n1->fmt->colorspace; |
| 566 | mf->field = V4L2_FIELD_NONE; |
| 567 | mf->width = rj54n1->width; |
| 568 | mf->height = rj54n1->height; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * The actual geometry configuration routine. It scales the input window into |
| 575 | * the output one, updates the window sizes and returns an error or the resize |
| 576 | * coefficient on success. Note: we only use the "Fixed Scaling" on this camera. |
| 577 | */ |
| 578 | static int rj54n1_sensor_scale(struct v4l2_subdev *sd, u32 *in_w, u32 *in_h, |
| 579 | u32 *out_w, u32 *out_h) |
| 580 | { |
| 581 | struct i2c_client *client = sd->priv; |
| 582 | unsigned int skip, resize, input_w = *in_w, input_h = *in_h, |
| 583 | output_w = *out_w, output_h = *out_h; |
| 584 | u16 inc_sel; |
| 585 | int ret; |
| 586 | |
| 587 | ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_S_L, |
| 588 | RJ54N1_Y_OUTPUT_SIZE_S_L, |
| 589 | RJ54N1_XY_OUTPUT_SIZE_S_H, output_w, output_h); |
| 590 | if (!ret) |
| 591 | ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_P_L, |
| 592 | RJ54N1_Y_OUTPUT_SIZE_P_L, |
| 593 | RJ54N1_XY_OUTPUT_SIZE_P_H, output_w, output_h); |
| 594 | |
| 595 | if (ret < 0) |
| 596 | return ret; |
| 597 | |
| 598 | if (output_w > input_w || output_h > input_h) { |
| 599 | input_w = output_w; |
| 600 | input_h = output_h; |
| 601 | |
| 602 | resize = 1024; |
| 603 | } else { |
| 604 | unsigned int resize_x, resize_y; |
| 605 | resize_x = input_w * 1024 / output_w; |
| 606 | resize_y = input_h * 1024 / output_h; |
| 607 | |
| 608 | resize = min(resize_x, resize_y); |
| 609 | |
| 610 | /* Prohibited value ranges */ |
| 611 | switch (resize) { |
| 612 | case 2040 ... 2047: |
| 613 | resize = 2039; |
| 614 | break; |
| 615 | case 4080 ... 4095: |
| 616 | resize = 4079; |
| 617 | break; |
| 618 | case 8160 ... 8191: |
| 619 | resize = 8159; |
| 620 | break; |
| 621 | case 16320 ... 16383: |
| 622 | resize = 16319; |
| 623 | } |
| 624 | |
| 625 | input_w = output_w * resize / 1024; |
| 626 | input_h = output_h * resize / 1024; |
| 627 | } |
| 628 | |
| 629 | /* Set scaling */ |
| 630 | ret = reg_write(client, RJ54N1_RESIZE_HOLD_L, resize & 0xff); |
| 631 | if (!ret) |
| 632 | ret = reg_write(client, RJ54N1_RESIZE_HOLD_H, resize >> 8); |
| 633 | |
| 634 | if (ret < 0) |
| 635 | return ret; |
| 636 | |
| 637 | /* |
| 638 | * Configure a skipping bitmask. The sensor will select a skipping value |
| 639 | * among set bits automatically. |
| 640 | */ |
| 641 | skip = min(resize / 1024, (unsigned)15); |
| 642 | inc_sel = 1 << skip; |
| 643 | |
| 644 | if (inc_sel <= 2) |
| 645 | inc_sel = 0xc; |
| 646 | else if (resize & 1023 && skip < 15) |
| 647 | inc_sel |= 1 << (skip + 1); |
| 648 | |
| 649 | ret = reg_write(client, RJ54N1_INC_USE_SEL_L, inc_sel & 0xfc); |
| 650 | if (!ret) |
| 651 | ret = reg_write(client, RJ54N1_INC_USE_SEL_H, inc_sel >> 8); |
| 652 | |
| 653 | /* Start resizing */ |
| 654 | if (!ret) |
| 655 | ret = reg_write(client, RJ54N1_RESIZE_CONTROL, |
| 656 | RESIZE_HOLD_SEL | RESIZE_GO | 1); |
| 657 | |
| 658 | if (ret < 0) |
| 659 | return ret; |
| 660 | |
| 661 | dev_dbg(&client->dev, "resize %u, skip %u\n", resize, skip); |
| 662 | |
| 663 | /* Constant taken from manufacturer's example */ |
| 664 | msleep(230); |
| 665 | |
| 666 | ret = reg_write(client, RJ54N1_RESIZE_CONTROL, RESIZE_HOLD_SEL | 1); |
| 667 | if (ret < 0) |
| 668 | return ret; |
| 669 | |
| 670 | *in_w = input_w; |
| 671 | *in_h = input_h; |
| 672 | *out_w = output_w; |
| 673 | *out_h = output_h; |
| 674 | |
| 675 | return resize; |
| 676 | } |
| 677 | |
| 678 | static int rj54n1_set_clock(struct i2c_client *client) |
| 679 | { |
| 680 | struct rj54n1 *rj54n1 = to_rj54n1(client); |
| 681 | int ret; |
| 682 | |
| 683 | /* Enable external clock */ |
| 684 | ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK | SOFT_STDBY); |
| 685 | /* Leave stand-by */ |
| 686 | if (!ret) |
| 687 | ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK); |
| 688 | |
| 689 | if (!ret) |
| 690 | ret = reg_write(client, RJ54N1_PLL_L, 2); |
| 691 | if (!ret) |
| 692 | ret = reg_write(client, RJ54N1_PLL_N, 0x31); |
| 693 | |
| 694 | /* TGCLK dividers */ |
| 695 | if (!ret) |
| 696 | ret = reg_write(client, RJ54N1_RATIO_TG, |
| 697 | rj54n1->clk_div.ratio_tg); |
| 698 | if (!ret) |
| 699 | ret = reg_write(client, RJ54N1_RATIO_T, |
| 700 | rj54n1->clk_div.ratio_t); |
| 701 | if (!ret) |
| 702 | ret = reg_write(client, RJ54N1_RATIO_R, |
| 703 | rj54n1->clk_div.ratio_r); |
| 704 | |
| 705 | /* Enable TGCLK & RAMP */ |
| 706 | if (!ret) |
| 707 | ret = reg_write(client, RJ54N1_RAMP_TGCLK_EN, 3); |
| 708 | |
| 709 | /* Disable clock output */ |
| 710 | if (!ret) |
| 711 | ret = reg_write(client, RJ54N1_OCLK_DSP, 0); |
| 712 | |
| 713 | /* Set divisors */ |
| 714 | if (!ret) |
| 715 | ret = reg_write(client, RJ54N1_RATIO_OP, |
| 716 | rj54n1->clk_div.ratio_op); |
| 717 | if (!ret) |
| 718 | ret = reg_write(client, RJ54N1_RATIO_O, |
| 719 | rj54n1->clk_div.ratio_o); |
| 720 | |
| 721 | /* Enable OCLK */ |
| 722 | if (!ret) |
| 723 | ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1); |
| 724 | |
| 725 | /* Use PLL for Timing Generator, write 2 to reserved bits */ |
| 726 | if (!ret) |
| 727 | ret = reg_write(client, RJ54N1_TG_BYPASS, 2); |
| 728 | |
| 729 | /* Take sensor out of reset */ |
| 730 | if (!ret) |
| 731 | ret = reg_write(client, RJ54N1_RESET_STANDBY, |
| 732 | E_EXCLK | SEN_RSTX); |
| 733 | /* Enable PLL */ |
| 734 | if (!ret) |
| 735 | ret = reg_write(client, RJ54N1_PLL_EN, 1); |
| 736 | |
| 737 | /* Wait for PLL to stabilise */ |
| 738 | msleep(10); |
| 739 | |
| 740 | /* Enable clock to frequency divider */ |
| 741 | if (!ret) |
| 742 | ret = reg_write(client, RJ54N1_CLK_RST, 1); |
| 743 | |
| 744 | if (!ret) |
| 745 | ret = reg_read(client, RJ54N1_CLK_RST); |
| 746 | if (ret != 1) { |
| 747 | dev_err(&client->dev, |
| 748 | "Resetting RJ54N1CB0C clock failed: %d!\n", ret); |
| 749 | return -EIO; |
| 750 | } |
| 751 | /* Start the PLL */ |
| 752 | ret = reg_set(client, RJ54N1_OCLK_DSP, 1, 1); |
| 753 | |
| 754 | /* Enable OCLK */ |
| 755 | if (!ret) |
| 756 | ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1); |
| 757 | |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | static int rj54n1_reg_init(struct i2c_client *client) |
| 762 | { |
| 763 | int ret = rj54n1_set_clock(client); |
| 764 | |
| 765 | if (!ret) |
| 766 | ret = reg_write_multiple(client, bank_7, ARRAY_SIZE(bank_7)); |
| 767 | if (!ret) |
| 768 | ret = reg_write_multiple(client, bank_10, ARRAY_SIZE(bank_10)); |
| 769 | |
| 770 | /* Set binning divisors */ |
| 771 | if (!ret) |
| 772 | ret = reg_write(client, RJ54N1_SCALE_1_2_LEV, 3 | (7 << 4)); |
| 773 | if (!ret) |
| 774 | ret = reg_write(client, RJ54N1_SCALE_4_LEV, 0xf); |
| 775 | |
| 776 | /* Switch to fixed resize mode */ |
| 777 | if (!ret) |
| 778 | ret = reg_write(client, RJ54N1_RESIZE_CONTROL, |
| 779 | RESIZE_HOLD_SEL | 1); |
| 780 | |
| 781 | /* Set gain */ |
| 782 | if (!ret) |
| 783 | ret = reg_write(client, RJ54N1_Y_GAIN, 0x84); |
| 784 | |
| 785 | /* Mirror the image back: default is upside down and left-to-right... */ |
| 786 | if (!ret) |
| 787 | ret = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 3, 3); |
| 788 | |
| 789 | if (!ret) |
| 790 | ret = reg_write_multiple(client, bank_4, ARRAY_SIZE(bank_4)); |
| 791 | if (!ret) |
| 792 | ret = reg_write_multiple(client, bank_5, ARRAY_SIZE(bank_5)); |
| 793 | if (!ret) |
| 794 | ret = reg_write_multiple(client, bank_8, ARRAY_SIZE(bank_8)); |
| 795 | |
| 796 | if (!ret) |
| 797 | ret = reg_write(client, RJ54N1_RESET_STANDBY, |
| 798 | E_EXCLK | DSP_RSTX | SEN_RSTX); |
| 799 | |
| 800 | /* Commit init */ |
| 801 | if (!ret) |
| 802 | ret = rj54n1_commit(client); |
| 803 | |
| 804 | /* Take DSP, TG, sensor out of reset */ |
| 805 | if (!ret) |
| 806 | ret = reg_write(client, RJ54N1_RESET_STANDBY, |
| 807 | E_EXCLK | DSP_RSTX | TG_RSTX | SEN_RSTX); |
| 808 | |
| 809 | if (!ret) |
| 810 | ret = reg_write(client, 0x7fe, 2); |
| 811 | |
| 812 | /* Constant taken from manufacturer's example */ |
| 813 | msleep(700); |
| 814 | |
| 815 | return ret; |
| 816 | } |
| 817 | |
| 818 | /* FIXME: streaming output only up to 800x600 is functional */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 819 | static int rj54n1_try_fmt(struct v4l2_subdev *sd, |
| 820 | struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 821 | { |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 822 | struct i2c_client *client = sd->priv; |
| 823 | struct rj54n1 *rj54n1 = to_rj54n1(client); |
| 824 | const struct rj54n1_datafmt *fmt; |
| 825 | int align = mf->code == V4L2_MBUS_FMT_SBGGR10_1X10 || |
| 826 | mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE || |
| 827 | mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE || |
| 828 | mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE || |
| 829 | mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 830 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 831 | dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n", |
| 832 | __func__, mf->code, mf->width, mf->height); |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 833 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 834 | fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts, |
| 835 | ARRAY_SIZE(rj54n1_colour_fmts)); |
| 836 | if (!fmt) { |
| 837 | fmt = rj54n1->fmt; |
| 838 | mf->code = fmt->code; |
| 839 | } |
| 840 | |
| 841 | mf->field = V4L2_FIELD_NONE; |
| 842 | mf->colorspace = fmt->colorspace; |
| 843 | |
| 844 | v4l_bound_align_image(&mf->width, 112, RJ54N1_MAX_WIDTH, align, |
| 845 | &mf->height, 84, RJ54N1_MAX_HEIGHT, align, 0); |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 850 | static int rj54n1_s_fmt(struct v4l2_subdev *sd, |
| 851 | struct v4l2_mbus_framefmt *mf) |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 852 | { |
| 853 | struct i2c_client *client = sd->priv; |
| 854 | struct rj54n1 *rj54n1 = to_rj54n1(client); |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 855 | const struct rj54n1_datafmt *fmt; |
| 856 | unsigned int output_w, output_h, max_w, max_h, |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 857 | input_w = rj54n1->rect.width, input_h = rj54n1->rect.height; |
| 858 | int ret; |
| 859 | |
| 860 | /* |
| 861 | * The host driver can call us without .try_fmt(), so, we have to take |
| 862 | * care ourseleves |
| 863 | */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 864 | ret = rj54n1_try_fmt(sd, mf); |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 865 | |
| 866 | /* |
| 867 | * Verify if the sensor has just been powered on. TODO: replace this |
| 868 | * with proper PM, when a suitable API is available. |
| 869 | */ |
| 870 | if (!ret) |
| 871 | ret = reg_read(client, RJ54N1_RESET_STANDBY); |
| 872 | if (ret < 0) |
| 873 | return ret; |
| 874 | |
| 875 | if (!(ret & E_EXCLK)) { |
| 876 | ret = rj54n1_reg_init(client); |
| 877 | if (ret < 0) |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | /* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */ |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 882 | switch (mf->code) { |
| 883 | case V4L2_MBUS_FMT_YUYV8_2X8_LE: |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 884 | ret = reg_write(client, RJ54N1_OUT_SEL, 0); |
| 885 | if (!ret) |
| 886 | ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8); |
| 887 | break; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 888 | case V4L2_MBUS_FMT_YVYU8_2X8_LE: |
| 889 | ret = reg_write(client, RJ54N1_OUT_SEL, 0); |
| 890 | if (!ret) |
| 891 | ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8); |
| 892 | break; |
| 893 | case V4L2_MBUS_FMT_RGB565_2X8_LE: |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 894 | ret = reg_write(client, RJ54N1_OUT_SEL, 0x11); |
| 895 | if (!ret) |
| 896 | ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8); |
| 897 | break; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 898 | case V4L2_MBUS_FMT_RGB565_2X8_BE: |
| 899 | ret = reg_write(client, RJ54N1_OUT_SEL, 0x11); |
| 900 | if (!ret) |
| 901 | ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8); |
| 902 | break; |
| 903 | case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE: |
| 904 | ret = reg_write(client, RJ54N1_OUT_SEL, 4); |
| 905 | if (!ret) |
| 906 | ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8); |
| 907 | if (!ret) |
| 908 | ret = reg_write(client, RJ54N1_RA_SEL_UL, 0); |
| 909 | break; |
| 910 | case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE: |
| 911 | ret = reg_write(client, RJ54N1_OUT_SEL, 4); |
| 912 | if (!ret) |
| 913 | ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8); |
| 914 | if (!ret) |
| 915 | ret = reg_write(client, RJ54N1_RA_SEL_UL, 8); |
| 916 | break; |
| 917 | case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE: |
| 918 | ret = reg_write(client, RJ54N1_OUT_SEL, 4); |
| 919 | if (!ret) |
| 920 | ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8); |
| 921 | if (!ret) |
| 922 | ret = reg_write(client, RJ54N1_RA_SEL_UL, 0); |
| 923 | break; |
| 924 | case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE: |
| 925 | ret = reg_write(client, RJ54N1_OUT_SEL, 4); |
| 926 | if (!ret) |
| 927 | ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8); |
| 928 | if (!ret) |
| 929 | ret = reg_write(client, RJ54N1_RA_SEL_UL, 8); |
| 930 | break; |
| 931 | case V4L2_MBUS_FMT_SBGGR10_1X10: |
| 932 | ret = reg_write(client, RJ54N1_OUT_SEL, 5); |
| 933 | break; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 934 | default: |
| 935 | ret = -EINVAL; |
| 936 | } |
| 937 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 938 | /* Special case: a raw mode with 10 bits of data per clock tick */ |
| 939 | if (!ret) |
| 940 | ret = reg_set(client, RJ54N1_OCLK_SEL_EN, |
| 941 | (mf->code == V4L2_MBUS_FMT_SBGGR10_1X10) << 1, 2); |
| 942 | |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 943 | if (ret < 0) |
| 944 | return ret; |
| 945 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 946 | /* Supported scales 1:1 >= scale > 1:16 */ |
| 947 | max_w = mf->width * (16 * 1024 - 1) / 1024; |
| 948 | if (input_w > max_w) |
| 949 | input_w = max_w; |
| 950 | max_h = mf->height * (16 * 1024 - 1) / 1024; |
| 951 | if (input_h > max_h) |
| 952 | input_h = max_h; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 953 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 954 | output_w = mf->width; |
| 955 | output_h = mf->height; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 956 | |
| 957 | ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h); |
| 958 | if (ret < 0) |
| 959 | return ret; |
| 960 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 961 | fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts, |
| 962 | ARRAY_SIZE(rj54n1_colour_fmts)); |
| 963 | |
| 964 | rj54n1->fmt = fmt; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 965 | rj54n1->resize = ret; |
| 966 | rj54n1->rect.width = input_w; |
| 967 | rj54n1->rect.height = input_h; |
| 968 | rj54n1->width = output_w; |
| 969 | rj54n1->height = output_h; |
| 970 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 971 | mf->width = output_w; |
| 972 | mf->height = output_h; |
| 973 | mf->field = V4L2_FIELD_NONE; |
| 974 | mf->colorspace = fmt->colorspace; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 975 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 976 | return 0; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | static int rj54n1_g_chip_ident(struct v4l2_subdev *sd, |
| 980 | struct v4l2_dbg_chip_ident *id) |
| 981 | { |
| 982 | struct i2c_client *client = sd->priv; |
| 983 | |
| 984 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
| 985 | return -EINVAL; |
| 986 | |
| 987 | if (id->match.addr != client->addr) |
| 988 | return -ENODEV; |
| 989 | |
| 990 | id->ident = V4L2_IDENT_RJ54N1CB0C; |
| 991 | id->revision = 0; |
| 992 | |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 997 | static int rj54n1_g_register(struct v4l2_subdev *sd, |
| 998 | struct v4l2_dbg_register *reg) |
| 999 | { |
| 1000 | struct i2c_client *client = sd->priv; |
| 1001 | |
| 1002 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || |
| 1003 | reg->reg < 0x400 || reg->reg > 0x1fff) |
| 1004 | /* Registers > 0x0800 are only available from Sharp support */ |
| 1005 | return -EINVAL; |
| 1006 | |
| 1007 | if (reg->match.addr != client->addr) |
| 1008 | return -ENODEV; |
| 1009 | |
| 1010 | reg->size = 1; |
| 1011 | reg->val = reg_read(client, reg->reg); |
| 1012 | |
| 1013 | if (reg->val > 0xff) |
| 1014 | return -EIO; |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | static int rj54n1_s_register(struct v4l2_subdev *sd, |
| 1020 | struct v4l2_dbg_register *reg) |
| 1021 | { |
| 1022 | struct i2c_client *client = sd->priv; |
| 1023 | |
| 1024 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || |
| 1025 | reg->reg < 0x400 || reg->reg > 0x1fff) |
| 1026 | /* Registers >= 0x0800 are only available from Sharp support */ |
| 1027 | return -EINVAL; |
| 1028 | |
| 1029 | if (reg->match.addr != client->addr) |
| 1030 | return -ENODEV; |
| 1031 | |
| 1032 | if (reg_write(client, reg->reg, reg->val) < 0) |
| 1033 | return -EIO; |
| 1034 | |
| 1035 | return 0; |
| 1036 | } |
| 1037 | #endif |
| 1038 | |
| 1039 | static const struct v4l2_queryctrl rj54n1_controls[] = { |
| 1040 | { |
| 1041 | .id = V4L2_CID_VFLIP, |
| 1042 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 1043 | .name = "Flip Vertically", |
| 1044 | .minimum = 0, |
| 1045 | .maximum = 1, |
| 1046 | .step = 1, |
| 1047 | .default_value = 0, |
| 1048 | }, { |
| 1049 | .id = V4L2_CID_HFLIP, |
| 1050 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 1051 | .name = "Flip Horizontally", |
| 1052 | .minimum = 0, |
| 1053 | .maximum = 1, |
| 1054 | .step = 1, |
| 1055 | .default_value = 0, |
| 1056 | }, { |
| 1057 | .id = V4L2_CID_GAIN, |
| 1058 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 1059 | .name = "Gain", |
| 1060 | .minimum = 0, |
| 1061 | .maximum = 127, |
| 1062 | .step = 1, |
| 1063 | .default_value = 66, |
| 1064 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 1065 | }, |
| 1066 | }; |
| 1067 | |
| 1068 | static struct soc_camera_ops rj54n1_ops = { |
| 1069 | .set_bus_param = rj54n1_set_bus_param, |
| 1070 | .query_bus_param = rj54n1_query_bus_param, |
| 1071 | .controls = rj54n1_controls, |
| 1072 | .num_controls = ARRAY_SIZE(rj54n1_controls), |
| 1073 | }; |
| 1074 | |
| 1075 | static int rj54n1_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 1076 | { |
| 1077 | struct i2c_client *client = sd->priv; |
| 1078 | int data; |
| 1079 | |
| 1080 | switch (ctrl->id) { |
| 1081 | case V4L2_CID_VFLIP: |
| 1082 | data = reg_read(client, RJ54N1_MIRROR_STILL_MODE); |
| 1083 | if (data < 0) |
| 1084 | return -EIO; |
| 1085 | ctrl->value = !(data & 1); |
| 1086 | break; |
| 1087 | case V4L2_CID_HFLIP: |
| 1088 | data = reg_read(client, RJ54N1_MIRROR_STILL_MODE); |
| 1089 | if (data < 0) |
| 1090 | return -EIO; |
| 1091 | ctrl->value = !(data & 2); |
| 1092 | break; |
| 1093 | case V4L2_CID_GAIN: |
| 1094 | data = reg_read(client, RJ54N1_Y_GAIN); |
| 1095 | if (data < 0) |
| 1096 | return -EIO; |
| 1097 | |
| 1098 | ctrl->value = data / 2; |
| 1099 | break; |
| 1100 | } |
| 1101 | |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | static int rj54n1_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 1106 | { |
| 1107 | int data; |
| 1108 | struct i2c_client *client = sd->priv; |
| 1109 | const struct v4l2_queryctrl *qctrl; |
| 1110 | |
| 1111 | qctrl = soc_camera_find_qctrl(&rj54n1_ops, ctrl->id); |
| 1112 | if (!qctrl) |
| 1113 | return -EINVAL; |
| 1114 | |
| 1115 | switch (ctrl->id) { |
| 1116 | case V4L2_CID_VFLIP: |
| 1117 | if (ctrl->value) |
| 1118 | data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 1); |
| 1119 | else |
| 1120 | data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 1, 1); |
| 1121 | if (data < 0) |
| 1122 | return -EIO; |
| 1123 | break; |
| 1124 | case V4L2_CID_HFLIP: |
| 1125 | if (ctrl->value) |
| 1126 | data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 2); |
| 1127 | else |
| 1128 | data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 2, 2); |
| 1129 | if (data < 0) |
| 1130 | return -EIO; |
| 1131 | break; |
| 1132 | case V4L2_CID_GAIN: |
| 1133 | if (ctrl->value > qctrl->maximum || |
| 1134 | ctrl->value < qctrl->minimum) |
| 1135 | return -EINVAL; |
| 1136 | else if (reg_write(client, RJ54N1_Y_GAIN, ctrl->value * 2) < 0) |
| 1137 | return -EIO; |
| 1138 | break; |
| 1139 | } |
| 1140 | |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
| 1144 | static struct v4l2_subdev_core_ops rj54n1_subdev_core_ops = { |
| 1145 | .g_ctrl = rj54n1_g_ctrl, |
| 1146 | .s_ctrl = rj54n1_s_ctrl, |
| 1147 | .g_chip_ident = rj54n1_g_chip_ident, |
| 1148 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1149 | .g_register = rj54n1_g_register, |
| 1150 | .s_register = rj54n1_s_register, |
| 1151 | #endif |
| 1152 | }; |
| 1153 | |
| 1154 | static struct v4l2_subdev_video_ops rj54n1_subdev_video_ops = { |
| 1155 | .s_stream = rj54n1_s_stream, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 1156 | .s_mbus_fmt = rj54n1_s_fmt, |
| 1157 | .g_mbus_fmt = rj54n1_g_fmt, |
| 1158 | .try_mbus_fmt = rj54n1_try_fmt, |
| 1159 | .enum_mbus_fmt = rj54n1_enum_fmt, |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 1160 | .g_crop = rj54n1_g_crop, |
| 1161 | .cropcap = rj54n1_cropcap, |
| 1162 | }; |
| 1163 | |
| 1164 | static struct v4l2_subdev_ops rj54n1_subdev_ops = { |
| 1165 | .core = &rj54n1_subdev_core_ops, |
| 1166 | .video = &rj54n1_subdev_video_ops, |
| 1167 | }; |
| 1168 | |
| 1169 | static int rj54n1_pin_config(struct i2c_client *client) |
| 1170 | { |
| 1171 | /* |
| 1172 | * Experimentally found out IOCTRL wired to 0. TODO: add to platform |
| 1173 | * data: 0 or 1 << 7. |
| 1174 | */ |
| 1175 | return reg_write(client, RJ54N1_IOC, 0); |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * Interface active, can use i2c. If it fails, it can indeed mean, that |
| 1180 | * this wasn't our capture interface, so, we wait for the right one |
| 1181 | */ |
| 1182 | static int rj54n1_video_probe(struct soc_camera_device *icd, |
| 1183 | struct i2c_client *client) |
| 1184 | { |
| 1185 | int data1, data2; |
| 1186 | int ret; |
| 1187 | |
| 1188 | /* This could be a BUG_ON() or a WARN_ON(), or remove it completely */ |
| 1189 | if (!icd->dev.parent || |
| 1190 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) |
| 1191 | return -ENODEV; |
| 1192 | |
| 1193 | /* Read out the chip version register */ |
| 1194 | data1 = reg_read(client, RJ54N1_DEV_CODE); |
| 1195 | data2 = reg_read(client, RJ54N1_DEV_CODE2); |
| 1196 | |
| 1197 | if (data1 != 0x51 || data2 != 0x10) { |
| 1198 | ret = -ENODEV; |
| 1199 | dev_info(&client->dev, "No RJ54N1CB0C found, read 0x%x:0x%x\n", |
| 1200 | data1, data2); |
| 1201 | goto ei2c; |
| 1202 | } |
| 1203 | |
| 1204 | ret = rj54n1_pin_config(client); |
| 1205 | if (ret < 0) |
| 1206 | goto ei2c; |
| 1207 | |
| 1208 | dev_info(&client->dev, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n", |
| 1209 | data1, data2); |
| 1210 | |
| 1211 | ei2c: |
| 1212 | return ret; |
| 1213 | } |
| 1214 | |
| 1215 | static int rj54n1_probe(struct i2c_client *client, |
| 1216 | const struct i2c_device_id *did) |
| 1217 | { |
| 1218 | struct rj54n1 *rj54n1; |
| 1219 | struct soc_camera_device *icd = client->dev.platform_data; |
| 1220 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 1221 | struct soc_camera_link *icl; |
| 1222 | int ret; |
| 1223 | |
| 1224 | if (!icd) { |
| 1225 | dev_err(&client->dev, "RJ54N1CB0C: missing soc-camera data!\n"); |
| 1226 | return -EINVAL; |
| 1227 | } |
| 1228 | |
| 1229 | icl = to_soc_camera_link(icd); |
| 1230 | if (!icl) { |
| 1231 | dev_err(&client->dev, "RJ54N1CB0C: missing platform data!\n"); |
| 1232 | return -EINVAL; |
| 1233 | } |
| 1234 | |
| 1235 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 1236 | dev_warn(&adapter->dev, |
| 1237 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n"); |
| 1238 | return -EIO; |
| 1239 | } |
| 1240 | |
| 1241 | rj54n1 = kzalloc(sizeof(struct rj54n1), GFP_KERNEL); |
| 1242 | if (!rj54n1) |
| 1243 | return -ENOMEM; |
| 1244 | |
| 1245 | v4l2_i2c_subdev_init(&rj54n1->subdev, client, &rj54n1_subdev_ops); |
| 1246 | |
| 1247 | icd->ops = &rj54n1_ops; |
| 1248 | |
| 1249 | rj54n1->clk_div = clk_div; |
| 1250 | rj54n1->rect.left = RJ54N1_COLUMN_SKIP; |
| 1251 | rj54n1->rect.top = RJ54N1_ROW_SKIP; |
| 1252 | rj54n1->rect.width = RJ54N1_MAX_WIDTH; |
| 1253 | rj54n1->rect.height = RJ54N1_MAX_HEIGHT; |
| 1254 | rj54n1->width = RJ54N1_MAX_WIDTH; |
| 1255 | rj54n1->height = RJ54N1_MAX_HEIGHT; |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame^] | 1256 | rj54n1->fmt = &rj54n1_colour_fmts[0]; |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 1257 | rj54n1->resize = 1024; |
| 1258 | |
| 1259 | ret = rj54n1_video_probe(icd, client); |
| 1260 | if (ret < 0) { |
| 1261 | icd->ops = NULL; |
| 1262 | i2c_set_clientdata(client, NULL); |
| 1263 | kfree(rj54n1); |
| 1264 | return ret; |
| 1265 | } |
| 1266 | |
Guennadi Liakhovetski | 8f37cf2 | 2009-10-05 12:54:04 -0300 | [diff] [blame] | 1267 | return ret; |
| 1268 | } |
| 1269 | |
| 1270 | static int rj54n1_remove(struct i2c_client *client) |
| 1271 | { |
| 1272 | struct rj54n1 *rj54n1 = to_rj54n1(client); |
| 1273 | struct soc_camera_device *icd = client->dev.platform_data; |
| 1274 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 1275 | |
| 1276 | icd->ops = NULL; |
| 1277 | if (icl->free_bus) |
| 1278 | icl->free_bus(icl); |
| 1279 | i2c_set_clientdata(client, NULL); |
| 1280 | client->driver = NULL; |
| 1281 | kfree(rj54n1); |
| 1282 | |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | static const struct i2c_device_id rj54n1_id[] = { |
| 1287 | { "rj54n1cb0c", 0 }, |
| 1288 | { } |
| 1289 | }; |
| 1290 | MODULE_DEVICE_TABLE(i2c, rj54n1_id); |
| 1291 | |
| 1292 | static struct i2c_driver rj54n1_i2c_driver = { |
| 1293 | .driver = { |
| 1294 | .name = "rj54n1cb0c", |
| 1295 | }, |
| 1296 | .probe = rj54n1_probe, |
| 1297 | .remove = rj54n1_remove, |
| 1298 | .id_table = rj54n1_id, |
| 1299 | }; |
| 1300 | |
| 1301 | static int __init rj54n1_mod_init(void) |
| 1302 | { |
| 1303 | return i2c_add_driver(&rj54n1_i2c_driver); |
| 1304 | } |
| 1305 | |
| 1306 | static void __exit rj54n1_mod_exit(void) |
| 1307 | { |
| 1308 | i2c_del_driver(&rj54n1_i2c_driver); |
| 1309 | } |
| 1310 | |
| 1311 | module_init(rj54n1_mod_init); |
| 1312 | module_exit(rj54n1_mod_exit); |
| 1313 | |
| 1314 | MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver"); |
| 1315 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |
| 1316 | MODULE_LICENSE("GPL v2"); |