b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * GC032A CMOS Image Sensor driver |
| 4 | * |
| 5 | * Copyright (C) 2023 ASR Mirco Co., Ltd. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/gpio/consumer.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/regmap.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/media.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/of_graph.h> |
| 22 | #include <linux/regulator/consumer.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/videodev2.h> |
| 26 | #include <linux/version.h> |
| 27 | #include <media/media-entity.h> |
| 28 | #include <media/v4l2-common.h> |
| 29 | #include <media/v4l2-ctrls.h> |
| 30 | #include <media/v4l2-device.h> |
| 31 | #include <media/v4l2-event.h> |
| 32 | #include <media/v4l2-fwnode.h> |
| 33 | #include <media/v4l2-image-sizes.h> |
| 34 | #include <media/v4l2-mediabus.h> |
| 35 | #include <media/v4l2-subdev.h> |
| 36 | |
| 37 | #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x1) |
| 38 | #define DRIVER_NAME "gc032a" |
| 39 | #define GC032A_PIXEL_RATE (96 * 1000 * 1000) |
| 40 | |
| 41 | /* |
| 42 | * GC032A register definitions |
| 43 | */ |
| 44 | #define REG_SOFTWARE_STANDBY 0xf3 |
| 45 | |
| 46 | #define REG_SC_CHIP_ID_H 0xf0 |
| 47 | #define REG_SC_CHIP_ID_L 0xf1 |
| 48 | |
| 49 | #define REG_NULL 0xFFFF /* Array end token */ |
| 50 | |
| 51 | #define SENSOR_ID(_msb, _lsb) ((_msb) << 8 | (_lsb)) |
| 52 | #define GC032A_ID 0x232a |
| 53 | |
| 54 | /* gc isp nead extra 6 lines with raw sensor */ |
| 55 | #define EXTRA_LINES_RAW_SENSOR 6 |
| 56 | |
| 57 | struct sensor_register { |
| 58 | u16 addr; |
| 59 | u8 value; |
| 60 | }; |
| 61 | |
| 62 | enum cam_interface { |
| 63 | INF_SPI1LAN = 0x0, |
| 64 | INF_SPI2LAN, |
| 65 | INF_SPI4LAN, |
| 66 | INF_MIPI1LAN, |
| 67 | INF_MIPI2LAN, |
| 68 | INF_MIPI4LAN, |
| 69 | INF_DVP, |
| 70 | INF_MAX, |
| 71 | }; |
| 72 | |
| 73 | struct spi_param { |
| 74 | u8 spi_sdr; //0x0:no sdr 0x1:sdr |
| 75 | u8 spi_crc; //0x0:no crc 0x1:crc |
| 76 | u8 spi_manual_enable; //0x0:not enable 0x1:enable |
| 77 | u8 spi_manual_mode; |
| 78 | u8 spi_manual_height_enable; //0x0:not enable 0x1:enable |
| 79 | u8 spi_manual_width_enable; //0x0:not enable 0x1:enable |
| 80 | u16 spi_manual_height; |
| 81 | u16 spi_manual_width; |
| 82 | u8 spi_ignore_line_id; |
| 83 | }; |
| 84 | |
| 85 | struct gc032a_framesize { |
| 86 | u16 width; |
| 87 | u16 height; |
| 88 | u16 max_exp_lines; |
| 89 | struct v4l2_fract max_fps; |
| 90 | const struct sensor_register *regs; |
| 91 | }; |
| 92 | |
| 93 | struct gc032a_spi_config { |
| 94 | enum cam_interface inf; |
| 95 | union { |
| 96 | struct spi_param inf_spi; |
| 97 | } u; |
| 98 | }; |
| 99 | |
| 100 | struct gc032a_pll_ctrl { |
| 101 | u8 ctrl1; |
| 102 | u8 ctrl2; |
| 103 | u8 ctrl3; |
| 104 | }; |
| 105 | |
| 106 | struct gc032a_pixfmt { |
| 107 | u32 code; |
| 108 | /* Output format Register Value (REG_FORMAT_CTRL00) */ |
| 109 | struct sensor_register *format_ctrl_regs; |
| 110 | }; |
| 111 | |
| 112 | struct pll_ctrl_reg { |
| 113 | unsigned int div; |
| 114 | unsigned char reg; |
| 115 | }; |
| 116 | |
| 117 | static const char * const gc032a_supply_names[] = { |
| 118 | "avdd", /* Analog power */ |
| 119 | "iovdd", /* Digital I/O power */ |
| 120 | }; |
| 121 | |
| 122 | #define GC032A_NUM_SUPPLIES ARRAY_SIZE(gc032a_supply_names) |
| 123 | |
| 124 | struct gc032a { |
| 125 | struct v4l2_subdev sd; |
| 126 | struct media_pad pad; |
| 127 | struct v4l2_mbus_framefmt format; |
| 128 | struct gpio_desc *pwdn_gpio; |
| 129 | struct gpio_desc *power_gpio; |
| 130 | struct regulator_bulk_data supplies[GC032A_NUM_SUPPLIES]; |
| 131 | struct mutex lock; |
| 132 | struct i2c_client *client; |
| 133 | struct v4l2_ctrl_handler ctrls; |
| 134 | struct v4l2_ctrl *link_frequency; |
| 135 | const struct gc032a_framesize *frame_size; |
| 136 | int streaming; |
| 137 | bool power_on; |
| 138 | }; |
| 139 | |
| 140 | #define NUM_BR_LEVELS 9 |
| 141 | |
| 142 | #define GC032A_SET_QUICK_STREAM \ |
| 143 | _IOW('V', BASE_VIDIOC_PRIVATE + 0, __u32) |
| 144 | |
| 145 | extern void asr_camera_mclk_ctrl(int on); |
| 146 | /* yuv422 640 * 480 spi 2 lan */ |
| 147 | static const struct sensor_register gc032a_init_yuv422_640x480_spi2lan_crc_sdr[] = |
| 148 | { |
| 149 | {0xf3, 0x83}, /*System*/ |
| 150 | {0xf5, 0x0c}, |
| 151 | {0xf7, 0x13}, //{0xf7, 0x11}, |
| 152 | {0xf8, 0x0f}, //{0xf8, 0x07}, pclk 24M |
| 153 | {0xf9, 0x4e}, |
| 154 | {0xfa, 0x31}, //{0xfa, 0x10}, |
| 155 | {0xfc, 0x02}, |
| 156 | {0xfe, 0x02}, |
| 157 | {0x81, 0x03}, |
| 158 | {0xfe, 0x00}, /*Analog&Cisctl*/ |
| 159 | {0x03, 0x01}, |
| 160 | {0x04, 0xc2}, |
| 161 | {0x05, 0x01}, |
| 162 | {0x06, 0xa3}, |
| 163 | {0x07, 0x00}, |
| 164 | {0x08, 0x08}, |
| 165 | {0x0a, 0x04}, |
| 166 | {0x0c, 0x04}, |
| 167 | {0x0d, 0x01}, |
| 168 | {0x0e, 0xe8}, |
| 169 | {0x0f, 0x02}, |
| 170 | {0x10, 0x88}, |
| 171 | {0x17, 0x54}, |
| 172 | {0x19, 0x04}, |
| 173 | {0x1a, 0x0a}, |
| 174 | {0x1f, 0x40}, |
| 175 | {0x20, 0x30}, |
| 176 | {0x2e, 0x80}, |
| 177 | {0x2f, 0x2b}, |
| 178 | {0x30, 0x1a}, |
| 179 | {0xfe, 0x02}, |
| 180 | {0x03, 0x02}, //[4:0]post_tx_width |
| 181 | {0x06, 0x60}, //[5:4]stsbp_mode |
| 182 | {0x05, 0xd7}, //drv |
| 183 | {0x12, 0x89}, //[7:6]init_ramp_mode |
| 184 | {0xfe, 0x03}, /*SPI*/ |
| 185 | {0x51, 0x00}, //0x01 can not stream on |
| 186 | {0x52, 0xda}, |
| 187 | {0x53, 0xa4}, //no crc 0x24 crc 0xa4 |
| 188 | {0x54, 0x20}, |
| 189 | {0x55, 0x00}, |
| 190 | {0x59, 0x10}, // {0x59, 0x30}, |
| 191 | {0x5a, 0x00}, //0x01 type |
| 192 | {0x5b, 0x80}, |
| 193 | {0x5c, 0x02}, |
| 194 | {0x5d, 0xe0}, // e0 |
| 195 | {0x5e, 0x01}, |
| 196 | {0x64, 0x06}, |
| 197 | {0xfe, 0x00}, /*blk*/ |
| 198 | {0x18, 0x02}, |
| 199 | {0xfe, 0x02}, |
| 200 | {0x40, 0x22}, |
| 201 | {0x45, 0x00}, |
| 202 | {0x46, 0x00}, |
| 203 | {0x49, 0x20}, |
| 204 | {0x4b, 0x3c}, |
| 205 | {0x50, 0x20}, |
| 206 | {0x42, 0x10}, |
| 207 | {0xfe, 0x01}, /*isp*/ |
| 208 | {0x0a, 0xc5}, |
| 209 | {0x45, 0x00}, //[6]darksun_en |
| 210 | {0xfe, 0x00}, |
| 211 | {0x40, 0xff}, |
| 212 | {0x41, 0x25}, |
| 213 | {0x42, 0xcf}, |
| 214 | {0x43, 0x10}, |
| 215 | {0x44, 0x83}, |
| 216 | {0x46, 0x26}, |
| 217 | {0x49, 0x03}, |
| 218 | {0x4f, 0x01}, //[0]AEC_en |
| 219 | {0xde, 0x84}, |
| 220 | {0xfe, 0x02}, |
| 221 | {0x22, 0xf6}, //CISCTL_SUN_TH_R |
| 222 | {0xfe, 0x01}, /*Shading*/ |
| 223 | {0xc1, 0x3c}, |
| 224 | {0xc2, 0x50}, |
| 225 | {0xc3, 0x00}, |
| 226 | {0xc4, 0x32}, |
| 227 | {0xc5, 0x24}, |
| 228 | {0xc6, 0x16}, |
| 229 | {0xc7, 0x08}, |
| 230 | {0xc8, 0x08}, |
| 231 | {0xc9, 0x00}, |
| 232 | {0xca, 0x20}, |
| 233 | {0xdc, 0x8a}, |
| 234 | {0xdd, 0xa0}, |
| 235 | {0xde, 0xa6}, |
| 236 | {0xdf, 0x75}, |
| 237 | {0xfe, 0x01}, /*AWB*/ |
| 238 | {0x7c, 0x09}, |
| 239 | {0x65, 0x06}, |
| 240 | {0x7c, 0x08}, |
| 241 | {0x56, 0xf4}, |
| 242 | {0x66, 0x0f}, |
| 243 | {0x67, 0x84}, |
| 244 | {0x6b, 0x80}, |
| 245 | {0x6d, 0x12}, |
| 246 | {0x6e, 0xb0}, |
| 247 | {0x86, 0x00}, |
| 248 | {0x87, 0x00}, |
| 249 | {0x88, 0x00}, |
| 250 | {0x89, 0x00}, |
| 251 | {0x8a, 0x00}, |
| 252 | {0x8b, 0x00}, |
| 253 | {0x8c, 0x00}, |
| 254 | {0x8d, 0x00}, |
| 255 | {0x8e, 0x00}, |
| 256 | {0x8f, 0x00}, |
| 257 | {0x90, 0xef}, |
| 258 | {0x91, 0xe1}, |
| 259 | {0x92, 0x0c}, |
| 260 | {0x93, 0xef}, |
| 261 | {0x94, 0x65}, |
| 262 | {0x95, 0x1f}, |
| 263 | {0x96, 0x0c}, |
| 264 | {0x97, 0x2d}, |
| 265 | {0x98, 0x20}, |
| 266 | {0x99, 0xaa}, |
| 267 | {0x9a, 0x3f}, |
| 268 | {0x9b, 0x2c}, |
| 269 | {0x9c, 0x5f}, |
| 270 | {0x9d, 0x3e}, |
| 271 | {0x9e, 0xaa}, |
| 272 | {0x9f, 0x67}, |
| 273 | {0xa0, 0x60}, |
| 274 | {0xa1, 0x00}, |
| 275 | {0xa2, 0x00}, |
| 276 | {0xa3, 0x0a}, |
| 277 | {0xa4, 0xb6}, |
| 278 | {0xa5, 0xac}, |
| 279 | {0xa6, 0xc1}, |
| 280 | {0xa7, 0xac}, |
| 281 | {0xa8, 0x55}, |
| 282 | {0xa9, 0xc3}, |
| 283 | {0xaa, 0xa4}, |
| 284 | {0xab, 0xba}, |
| 285 | {0xac, 0xa8}, |
| 286 | {0xad, 0x55}, |
| 287 | {0xae, 0xc8}, |
| 288 | {0xaf, 0xb9}, |
| 289 | {0xb0, 0xd4}, |
| 290 | {0xb1, 0xc3}, |
| 291 | {0xb2, 0x55}, |
| 292 | {0xb3, 0xd8}, |
| 293 | {0xb4, 0xce}, |
| 294 | {0xb5, 0x00}, |
| 295 | {0xb6, 0x00}, |
| 296 | {0xb7, 0x05}, |
| 297 | {0xb8, 0xd6}, |
| 298 | {0xb9, 0x8c}, |
| 299 | {0xfe, 0x01}, /*CC*/ |
| 300 | {0xd0, 0x40}, //3a |
| 301 | {0xd1, 0xf8}, |
| 302 | {0xd2, 0x00}, |
| 303 | {0xd3, 0xfa}, |
| 304 | {0xd4, 0x45}, |
| 305 | {0xd5, 0x02}, |
| 306 | {0xd6, 0x30}, |
| 307 | {0xd7, 0xfa}, |
| 308 | {0xd8, 0x08}, |
| 309 | {0xd9, 0x08}, |
| 310 | {0xda, 0x58}, |
| 311 | {0xdb, 0x02}, |
| 312 | {0xfe, 0x00}, |
| 313 | {0xfe, 0x00}, /*Gamma*/ |
| 314 | {0xba, 0x00}, |
| 315 | {0xbb, 0x04}, |
| 316 | {0xbc, 0x0a}, |
| 317 | {0xbd, 0x0e}, |
| 318 | {0xbe, 0x22}, |
| 319 | {0xbf, 0x30}, |
| 320 | {0xc0, 0x3d}, |
| 321 | {0xc1, 0x4a}, |
| 322 | {0xc2, 0x5d}, |
| 323 | {0xc3, 0x6b}, |
| 324 | {0xc4, 0x7a}, |
| 325 | {0xc5, 0x85}, |
| 326 | {0xc6, 0x90}, |
| 327 | {0xc7, 0xa5}, |
| 328 | {0xc8, 0xb5}, |
| 329 | {0xc9, 0xc2}, |
| 330 | {0xca, 0xcc}, |
| 331 | {0xcb, 0xd5}, |
| 332 | {0xcc, 0xde}, |
| 333 | {0xcd, 0xea}, |
| 334 | {0xce, 0xf5}, |
| 335 | {0xcf, 0xff}, |
| 336 | {0xfe, 0x00}, /*Auto Gamma*/ |
| 337 | {0x5a, 0x08}, |
| 338 | {0x5b, 0x0f}, |
| 339 | {0x5c, 0x15}, |
| 340 | {0x5d, 0x1c}, |
| 341 | {0x5e, 0x28}, |
| 342 | {0x5f, 0x36}, |
| 343 | {0x60, 0x45}, |
| 344 | {0x61, 0x51}, |
| 345 | {0x62, 0x6a}, |
| 346 | {0x63, 0x7d}, |
| 347 | {0x64, 0x8d}, |
| 348 | {0x65, 0x98}, |
| 349 | {0x66, 0xa2}, |
| 350 | {0x67, 0xb5}, |
| 351 | {0x68, 0xc3}, |
| 352 | {0x69, 0xcd}, |
| 353 | {0x6a, 0xd4}, |
| 354 | {0x6b, 0xdc}, |
| 355 | {0x6c, 0xe3}, |
| 356 | {0x6d, 0xf0}, |
| 357 | {0x6e, 0xf9}, |
| 358 | {0x6f, 0xff}, |
| 359 | {0xfe, 0x00}, /*Gain*/ |
| 360 | {0x70, 0x50}, |
| 361 | {0xfe, 0x00}, /*AEC*/ |
| 362 | {0x4f, 0x01}, |
| 363 | {0xfe, 0x01}, |
| 364 | {0x44, 0x04}, |
| 365 | {0x1f, 0x30}, |
| 366 | {0x20, 0x40}, |
| 367 | {0x26, 0x4e}, |
| 368 | {0x27, 0x01}, |
| 369 | {0x28, 0xd4}, |
| 370 | {0x29, 0x03}, |
| 371 | {0x2a, 0x0c}, |
| 372 | {0x2b, 0x03}, |
| 373 | {0x2c, 0xe9}, |
| 374 | {0x2d, 0x07}, |
| 375 | {0x2e, 0xd2}, |
| 376 | {0x2f, 0x0b}, |
| 377 | {0x30, 0x6e}, |
| 378 | {0x31, 0x0e}, |
| 379 | {0x32, 0x70}, |
| 380 | {0x33, 0x12}, |
| 381 | {0x34, 0x0c}, |
| 382 | {0x3c, 0x10}, //[5:4] Max level setting |
| 383 | {0x3e, 0x20}, |
| 384 | {0x3f, 0x2d}, |
| 385 | {0x40, 0x40}, |
| 386 | {0x41, 0x5b}, |
| 387 | {0x42, 0x82}, |
| 388 | {0x43, 0xb7}, |
| 389 | {0x04, 0x0a}, |
| 390 | {0x02, 0x79}, |
| 391 | {0x03, 0xc0}, |
| 392 | {0xcc, 0x08}, /*measure window*/ |
| 393 | {0xcd, 0x08}, |
| 394 | {0xce, 0xa4}, |
| 395 | {0xcf, 0xec}, |
| 396 | {0xfe, 0x00}, /*DNDD*/ |
| 397 | {0x81, 0xb8}, //f8 |
| 398 | {0x82, 0x12}, |
| 399 | {0x83, 0x0a}, |
| 400 | {0x84, 0x01}, |
| 401 | {0x86, 0x50}, |
| 402 | {0x87, 0x18}, |
| 403 | {0x88, 0x10}, |
| 404 | {0x89, 0x70}, |
| 405 | {0x8a, 0x20}, |
| 406 | {0x8b, 0x10}, |
| 407 | {0x8c, 0x08}, |
| 408 | {0x8d, 0x0a}, |
| 409 | {0xfe, 0x00}, /*Intpee*/ |
| 410 | {0x8f, 0xaa}, |
| 411 | {0x90, 0x9c}, |
| 412 | {0x91, 0x52}, |
| 413 | {0x92, 0x03}, |
| 414 | {0x93, 0x03}, |
| 415 | {0x94, 0x08}, |
| 416 | {0x95, 0x44}, |
| 417 | {0x97, 0x00}, |
| 418 | {0x98, 0x00}, |
| 419 | {0xfe, 0x00}, /*ASDE*/ |
| 420 | {0xa1, 0x30}, |
| 421 | {0xa2, 0x41}, |
| 422 | {0xa4, 0x30}, |
| 423 | {0xa5, 0x20}, |
| 424 | {0xaa, 0x30}, |
| 425 | {0xac, 0x32}, |
| 426 | {0xfe, 0x00}, /*YCP*/ |
| 427 | {0xd1, 0x3c}, |
| 428 | {0xd2, 0x3c}, |
| 429 | {0xd3, 0x38}, |
| 430 | {0xd6, 0xf4}, |
| 431 | {0xd7, 0x1d}, |
| 432 | {0xdd, 0x73}, |
| 433 | {0xde, 0x84}, |
| 434 | #if 0 |
| 435 | /* sensor crop */ |
| 436 | {0x50, 0x01}, |
| 437 | // 320x240 |
| 438 | {0x55, 0x00}, // height |
| 439 | {0x56, 0xf0}, |
| 440 | {0x57, 0x01}, // width |
| 441 | {0x58, 0x40}, |
| 442 | |
| 443 | {0xfe, 0x03}, |
| 444 | {0x5b, 0x40}, //spi width |
| 445 | {0x5c, 0x01}, |
| 446 | {0x5d, 0xf0}, // spi height |
| 447 | {0x5e, 0x00}, |
| 448 | {0xfe, 0x00}, |
| 449 | |
| 450 | /* sensor crop */ |
| 451 | {0x50, 0x01}, |
| 452 | // 480x480 |
| 453 | {0x55, 0x01}, // height |
| 454 | {0x56, 0xe0}, |
| 455 | {0x57, 0x01}, // width |
| 456 | {0x58, 0xe0}, |
| 457 | |
| 458 | {0xfe, 0x03}, |
| 459 | {0x5b, 0xe0}, //spi width |
| 460 | {0x5c, 0x01}, |
| 461 | {0x5d, 0xe0}, // spi height |
| 462 | {0x5e, 0x01}, |
| 463 | {0xfe, 0x00}, |
| 464 | #endif |
| 465 | //{0x4c, 0x08}, //for color bar |
| 466 | {REG_NULL, 0x00}, |
| 467 | }; |
| 468 | |
| 469 | static const struct gc032a_framesize gc032a_framesizes[] = { |
| 470 | { |
| 471 | .width = 640, |
| 472 | .height = 480, |
| 473 | .max_fps = { |
| 474 | .numerator = 10000, |
| 475 | .denominator = 200000, |
| 476 | }, |
| 477 | .regs = gc032a_init_yuv422_640x480_spi2lan_crc_sdr, |
| 478 | .max_exp_lines = 488, |
| 479 | }, |
| 480 | }; |
| 481 | |
| 482 | static const struct gc032a_spi_config gc032a_spi[] = { |
| 483 | { |
| 484 | .inf = INF_SPI2LAN, |
| 485 | .u = { |
| 486 | .inf_spi = { |
| 487 | .spi_sdr = 0, //0x0:sdr |
| 488 | .spi_crc = 1, //0x1:crc |
| 489 | .spi_manual_enable = 0, //0x0:not enable 0x1:enable |
| 490 | .spi_manual_mode = 0, |
| 491 | .spi_manual_height_enable = 0, //0x0:not enable 0x1:enable |
| 492 | .spi_manual_width_enable = 0, //0x0:not enable 0x1:enable |
| 493 | .spi_manual_height = 0, |
| 494 | .spi_manual_width = 0, |
| 495 | .spi_ignore_line_id = 1, |
| 496 | }, |
| 497 | }, |
| 498 | }, |
| 499 | }; |
| 500 | |
| 501 | static const struct gc032a_pixfmt gc032a_formats[] = { |
| 502 | { |
| 503 | .code = MEDIA_BUS_FMT_YVYU8_2X8, |
| 504 | }, |
| 505 | }; |
| 506 | |
| 507 | static inline struct gc032a *to_gc032a(struct v4l2_subdev *sd) |
| 508 | { |
| 509 | return container_of(sd, struct gc032a, sd); |
| 510 | } |
| 511 | |
| 512 | /* sensor register write */ |
| 513 | static int gc032a_write(struct i2c_client *client, u8 reg, u8 val) |
| 514 | { |
| 515 | struct i2c_msg msg; |
| 516 | u8 buf[2]; |
| 517 | int ret; |
| 518 | |
| 519 | buf[0] = reg & 0xFF; |
| 520 | buf[1] = val; |
| 521 | |
| 522 | msg.addr = client->addr; |
| 523 | msg.flags = client->flags; |
| 524 | msg.buf = buf; |
| 525 | msg.len = sizeof(buf); |
| 526 | |
| 527 | ret = i2c_transfer(client->adapter, &msg, 1); |
| 528 | if (ret >= 0) |
| 529 | return 0; |
| 530 | |
| 531 | dev_err(&client->dev, |
| 532 | "gc032a write reg(0x%x val:0x%x) failed !\n", reg, val); |
| 533 | |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | /* sensor register read */ |
| 538 | static int gc032a_read(struct i2c_client *client, u8 reg, u8 *val) |
| 539 | { |
| 540 | struct i2c_msg msg[2]; |
| 541 | u8 buf[1]; |
| 542 | int ret; |
| 543 | |
| 544 | buf[0] = reg & 0xFF; |
| 545 | |
| 546 | msg[0].addr = client->addr; |
| 547 | msg[0].flags = client->flags; |
| 548 | msg[0].buf = buf; |
| 549 | msg[0].len = sizeof(buf); |
| 550 | |
| 551 | msg[1].addr = client->addr; |
| 552 | msg[1].flags = client->flags | I2C_M_RD; |
| 553 | msg[1].buf = buf; |
| 554 | msg[1].len = 1; |
| 555 | |
| 556 | ret = i2c_transfer(client->adapter, msg, 2); |
| 557 | if (ret >= 0) { |
| 558 | *val = buf[0]; |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | dev_err(&client->dev, |
| 563 | "gc032a read reg:0x%x failed !\n", reg); |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | static int gc032a_write_array(struct i2c_client *client, |
| 568 | const struct sensor_register *regs) |
| 569 | { |
| 570 | int i, ret = 0; |
| 571 | |
| 572 | i = 0; |
| 573 | while (regs[i].addr != REG_NULL) { |
| 574 | ret = gc032a_write(client, regs[i].addr, regs[i].value); |
| 575 | if (ret) { |
| 576 | dev_err(&client->dev, "%s failed !, i=%d\n", __func__,i); |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | i++; |
| 581 | } |
| 582 | |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | static void gc032a_get_default_format(struct v4l2_mbus_framefmt *format) |
| 587 | { |
| 588 | format->width = gc032a_framesizes[0].width; |
| 589 | format->height = gc032a_framesizes[0].height; |
| 590 | format->colorspace = V4L2_COLORSPACE_SRGB; //TODO |
| 591 | format->code = gc032a_formats[0].code; |
| 592 | format->field = V4L2_FIELD_NONE; |
| 593 | |
| 594 | format->reserved[0] = gc032a_spi[0].inf; |
| 595 | format->reserved[1] = gc032a_spi[0].u.inf_spi.spi_sdr; |
| 596 | format->reserved[2] = gc032a_spi[0].u.inf_spi.spi_crc; |
| 597 | format->reserved[3] = gc032a_spi[0].u.inf_spi.spi_manual_enable; |
| 598 | format->reserved[4] = gc032a_spi[0].u.inf_spi.spi_manual_mode; |
| 599 | format->reserved[5] = gc032a_spi[0].u.inf_spi.spi_manual_height_enable; |
| 600 | format->reserved[6] = gc032a_spi[0].u.inf_spi.spi_manual_width_enable; |
| 601 | format->reserved[7] = gc032a_spi[0].u.inf_spi.spi_manual_height; |
| 602 | format->reserved[8] = gc032a_spi[0].u.inf_spi.spi_manual_width; |
| 603 | format->reserved[9] = gc032a_spi[0].u.inf_spi.spi_ignore_line_id; |
| 604 | pr_debug("%s: %x %dx%d lane%d sdr%d crc%d line_id%d\n", __func__, |
| 605 | format->code, format->width, |
| 606 | format->height, format->reserved[0], |
| 607 | format->reserved[1], format->reserved[2], format->reserved[9]); |
| 608 | } |
| 609 | |
| 610 | static void gc032a_set_streaming(struct gc032a *gc032a, int on) |
| 611 | { |
| 612 | struct i2c_client *client = gc032a->client; |
| 613 | int ret; |
| 614 | |
| 615 | dev_dbg(&client->dev, "%s: on: %d\n", __func__, on); |
| 616 | |
| 617 | ret = gc032a_write(client, 0xfe, 0x03); //page select |
| 618 | if (ret) |
| 619 | dev_err(&client->dev, "gc032a write 0xfe, 0x03 failed ret = %d\n",ret ); |
| 620 | ret = gc032a_write(client, 0x51, on); |
| 621 | if (ret) |
| 622 | dev_err(&client->dev, "gc032a write 0x51, 0x%x failed ret = %d\n", on, ret); |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * V4L2 subdev video and pad level operations |
| 627 | */ |
| 628 | static int gc032a_enum_mbus_code(struct v4l2_subdev *sd, |
| 629 | struct v4l2_subdev_pad_config *cfg, |
| 630 | struct v4l2_subdev_mbus_code_enum *code) |
| 631 | { |
| 632 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 633 | |
| 634 | dev_dbg(&client->dev, "%s:\n", __func__); |
| 635 | |
| 636 | if (code->index >= ARRAY_SIZE(gc032a_formats)) |
| 637 | return -EINVAL; |
| 638 | |
| 639 | code->code = gc032a_formats[code->index].code; |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int gc032a_enum_frame_sizes(struct v4l2_subdev *sd, |
| 645 | struct v4l2_subdev_pad_config *cfg, |
| 646 | struct v4l2_subdev_frame_size_enum *fse) |
| 647 | { |
| 648 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 649 | int i = ARRAY_SIZE(gc032a_formats); |
| 650 | |
| 651 | dev_dbg(&client->dev, "%s:\n", __func__); |
| 652 | |
| 653 | if (fse->index >= ARRAY_SIZE(gc032a_framesizes)) |
| 654 | return -EINVAL; |
| 655 | |
| 656 | while (--i) |
| 657 | if (fse->code == gc032a_formats[i].code) |
| 658 | break; |
| 659 | |
| 660 | fse->code = gc032a_formats[i].code; |
| 661 | |
| 662 | fse->min_width = gc032a_framesizes[fse->index].width; |
| 663 | fse->max_width = fse->min_width; |
| 664 | fse->max_height = gc032a_framesizes[fse->index].height; |
| 665 | fse->min_height = fse->max_height; |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int gc032a_get_fmt(struct v4l2_subdev *sd, |
| 671 | struct v4l2_subdev_pad_config *cfg, |
| 672 | struct v4l2_subdev_format *fmt) |
| 673 | { |
| 674 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 675 | struct gc032a *gc032a = to_gc032a(sd); |
| 676 | |
| 677 | dev_dbg(&client->dev, "%s enter\n", __func__); |
| 678 | |
| 679 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 680 | #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API |
| 681 | struct v4l2_mbus_framefmt *mf; |
| 682 | |
| 683 | mf = v4l2_subdev_get_try_format(sd, cfg, 0); |
| 684 | mutex_lock(&gc032a->lock); |
| 685 | fmt->format = *mf; |
| 686 | mutex_unlock(&gc032a->lock); |
| 687 | return 0; |
| 688 | #else |
| 689 | return -ENOTTY; |
| 690 | #endif |
| 691 | } |
| 692 | |
| 693 | mutex_lock(&gc032a->lock); |
| 694 | fmt->format = gc032a->format; |
| 695 | mutex_unlock(&gc032a->lock); |
| 696 | |
| 697 | dev_dbg(&client->dev, "%s: %x %dx%d lane%d sdr%d crc%d line_id%d\n", __func__, |
| 698 | gc032a->format.code, gc032a->format.width, |
| 699 | gc032a->format.height, gc032a->format.reserved[0], |
| 700 | gc032a->format.reserved[1], gc032a->format.reserved[2], gc032a->format.reserved[9]); |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static void __gc032a_try_frame_size(struct v4l2_mbus_framefmt *mf, |
| 706 | const struct gc032a_framesize **size) |
| 707 | { |
| 708 | const struct gc032a_framesize *fsize = &gc032a_framesizes[0]; |
| 709 | const struct gc032a_framesize *match = NULL; |
| 710 | int i = ARRAY_SIZE(gc032a_framesizes); |
| 711 | unsigned int min_err = UINT_MAX; |
| 712 | |
| 713 | while (i--) { |
| 714 | unsigned int err = abs(fsize->width - mf->width) |
| 715 | + abs(fsize->height - mf->height); |
| 716 | if (err < min_err && fsize->regs[0].addr) { |
| 717 | min_err = err; |
| 718 | match = fsize; |
| 719 | } |
| 720 | fsize++; |
| 721 | } |
| 722 | |
| 723 | if (!match) |
| 724 | match = &gc032a_framesizes[0]; |
| 725 | |
| 726 | mf->width = match->width; |
| 727 | mf->height = match->height; |
| 728 | |
| 729 | if (size) |
| 730 | *size = match; |
| 731 | } |
| 732 | |
| 733 | static int gc032a_set_fmt(struct v4l2_subdev *sd, |
| 734 | struct v4l2_subdev_pad_config *cfg, |
| 735 | struct v4l2_subdev_format *fmt) |
| 736 | { |
| 737 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 738 | int index = ARRAY_SIZE(gc032a_formats); |
| 739 | struct v4l2_mbus_framefmt *mf = &fmt->format; |
| 740 | const struct gc032a_framesize *size = NULL; |
| 741 | struct gc032a *gc032a = to_gc032a(sd); |
| 742 | int ret = 0; |
| 743 | |
| 744 | dev_dbg(&client->dev, "%s enter, code:0x%x\n", __func__, mf->code); |
| 745 | |
| 746 | __gc032a_try_frame_size(mf, &size); |
| 747 | |
| 748 | while (--index >= 0) |
| 749 | if (gc032a_formats[index].code == mf->code) |
| 750 | break; |
| 751 | |
| 752 | if (index < 0) |
| 753 | return -EINVAL; |
| 754 | |
| 755 | mf->colorspace = V4L2_COLORSPACE_SRGB; |
| 756 | mf->code = gc032a_formats[index].code; |
| 757 | mf->field = V4L2_FIELD_NONE; |
| 758 | |
| 759 | mutex_lock(&gc032a->lock); |
| 760 | |
| 761 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 762 | #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API |
| 763 | mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad); |
| 764 | *mf = fmt->format; |
| 765 | #else |
| 766 | return -ENOTTY; |
| 767 | #endif |
| 768 | } else { |
| 769 | if (gc032a->streaming) { |
| 770 | mutex_unlock(&gc032a->lock); |
| 771 | return -EBUSY; |
| 772 | } |
| 773 | |
| 774 | gc032a->frame_size = size; |
| 775 | gc032a->format = fmt->format; |
| 776 | } |
| 777 | |
| 778 | mutex_unlock(&gc032a->lock); |
| 779 | return ret; |
| 780 | } |
| 781 | |
| 782 | static long gc032a_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) |
| 783 | { |
| 784 | struct gc032a *gc032a = to_gc032a(sd); |
| 785 | long ret = 0; |
| 786 | u32 stream = 0; |
| 787 | |
| 788 | switch (cmd) { |
| 789 | case GC032A_SET_QUICK_STREAM: |
| 790 | |
| 791 | stream = *((u32 *)arg); |
| 792 | |
| 793 | if (stream) |
| 794 | gc032a_set_streaming(gc032a, 1); |
| 795 | else |
| 796 | gc032a_set_streaming(gc032a, 0); |
| 797 | break; |
| 798 | default: |
| 799 | ret = -ENOIOCTLCMD; |
| 800 | break; |
| 801 | } |
| 802 | |
| 803 | return ret; |
| 804 | } |
| 805 | |
| 806 | #ifdef CONFIG_COMPAT |
| 807 | static long gc032a_compat_ioctl32(struct v4l2_subdev *sd, |
| 808 | unsigned int cmd, unsigned long arg) |
| 809 | { |
| 810 | void __user *up = compat_ptr(arg); |
| 811 | long ret; |
| 812 | u32 stream = 0; |
| 813 | |
| 814 | switch (cmd) { |
| 815 | case GC032A_SET_QUICK_STREAM: |
| 816 | ret = copy_from_user(&stream, up, sizeof(u32)); |
| 817 | if (!ret) |
| 818 | ret = gc032a_ioctl(sd, cmd, &stream); |
| 819 | break; |
| 820 | default: |
| 821 | ret = -ENOIOCTLCMD; |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | return ret; |
| 826 | } |
| 827 | #endif |
| 828 | |
| 829 | static int gc032a_s_stream(struct v4l2_subdev *sd, int on) |
| 830 | { |
| 831 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 832 | struct gc032a *gc032a = to_gc032a(sd); |
| 833 | |
| 834 | dev_dbg(&client->dev, "%s: on: %d\n", __func__, on); |
| 835 | |
| 836 | mutex_lock(&gc032a->lock); |
| 837 | on = !!on; |
| 838 | if (gc032a->streaming == on) |
| 839 | goto unlock; |
| 840 | |
| 841 | if (!on) { |
| 842 | /* Stop Streaming Sequence */ |
| 843 | gc032a_set_streaming(gc032a, 0); |
| 844 | gc032a->streaming = on; |
| 845 | goto unlock; |
| 846 | } |
| 847 | |
| 848 | gc032a_set_streaming(gc032a, 1); |
| 849 | gc032a->streaming = on; |
| 850 | |
| 851 | unlock: |
| 852 | mutex_unlock(&gc032a->lock); |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static int gc032a_set_test_pattern(struct gc032a *gc032a, int value) |
| 857 | { |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | static int gc032a_yuv_set_banding(struct gc032a *gc032a, int value) |
| 862 | { |
| 863 | struct i2c_client *client = gc032a->client; |
| 864 | int ret; |
| 865 | |
| 866 | dev_dbg(&client->dev, "%s: value: %d\n", __func__, value); |
| 867 | |
| 868 | switch (value) { |
| 869 | case V4L2_CID_POWER_LINE_FREQUENCY_AUTO: |
| 870 | gc032a_write(client, 0xfe, 0x00); |
| 871 | break; |
| 872 | case V4L2_CID_POWER_LINE_FREQUENCY_50HZ: |
| 873 | gc032a_write(client, 0xfe, 0x00); |
| 874 | break; |
| 875 | case V4L2_CID_POWER_LINE_FREQUENCY_60HZ: |
| 876 | gc032a_write(client, 0xfe, 0x00); |
| 877 | break; |
| 878 | default: |
| 879 | dev_err(&client->dev, "invalid banding mode %d", value); |
| 880 | ret = -EINVAL; |
| 881 | } |
| 882 | return ret; |
| 883 | } |
| 884 | |
| 885 | static int gc032a_yuv_set_brightness(struct gc032a *gc032a, int value) |
| 886 | { |
| 887 | struct i2c_client *client = gc032a->client; |
| 888 | static const u8 regs[NUM_BR_LEVELS + 1] = { |
| 889 | 0xfe, |
| 890 | 0x00, /* 1 */ |
| 891 | 0x00, /* 2 */ |
| 892 | 0x00, /* 3 */ |
| 893 | 0x00, /* 4 */ |
| 894 | 0x00, /* 5 */ |
| 895 | 0x00, /* 6 */ |
| 896 | 0x00, /* 7 */ |
| 897 | 0x00, /* 8 */ |
| 898 | 0x00, /* 9 */ |
| 899 | }; |
| 900 | int ret = 0; |
| 901 | |
| 902 | dev_dbg(&client->dev, "%s: value: %d\n", __func__, value); |
| 903 | |
| 904 | if (value > NUM_BR_LEVELS) |
| 905 | return -EINVAL; |
| 906 | |
| 907 | ret = gc032a_write(client, regs[0], regs[value]); |
| 908 | return ret; |
| 909 | } |
| 910 | |
| 911 | static int gc032a_set_exposure(struct gc032a *gc032a, int value) |
| 912 | { |
| 913 | struct i2c_client *client = gc032a->client; |
| 914 | int ret; |
| 915 | |
| 916 | dev_dbg(&client->dev, "%s: value: %d\n", __func__, value); |
| 917 | |
| 918 | ret = gc032a_write(client, 0xfe, 0x00); //page select |
| 919 | if (ret) |
| 920 | dev_err(&client->dev, "gc032a write 0xfe, 0x00 failed ret = %d\n",ret ); |
| 921 | |
| 922 | ret = gc032a_write(client, 0x04, value& 0xff); //exp_low |
| 923 | if (ret < 0) |
| 924 | dev_err(&client->dev, "%s: error!", __func__); |
| 925 | |
| 926 | ret = gc032a_write(client, 0x03, (value >> 8) & 0x0f); //exp_high |
| 927 | if (ret < 0) |
| 928 | dev_err(&client->dev, "%s: error!", __func__); |
| 929 | |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | static int gc032a_set_gain(struct gc032a *gc032a, int value) |
| 934 | { |
| 935 | struct i2c_client *client = gc032a->client; |
| 936 | int ret; |
| 937 | |
| 938 | dev_info(&client->dev, "%s: value: %d\n", __func__, value); |
| 939 | |
| 940 | ret = gc032a_write(client, 0xfe, 0x00); //page select |
| 941 | if (ret) |
| 942 | dev_err(&client->dev, "gc032a write 0xfe, 0x00 failed ret = %d\n",ret ); |
| 943 | ret = gc032a_write(client, 0x48, value); |
| 944 | if (ret < 0) |
| 945 | dev_err(&client->dev, "%s: error!", __func__); |
| 946 | |
| 947 | return ret; |
| 948 | } |
| 949 | |
| 950 | static int gc032a_s_ctrl(struct v4l2_ctrl *ctrl) |
| 951 | { |
| 952 | struct gc032a *gc032a = |
| 953 | container_of(ctrl->handler, struct gc032a, ctrls); |
| 954 | int ret = -EINVAL; |
| 955 | |
| 956 | mutex_lock(&gc032a->lock); |
| 957 | /* |
| 958 | * If the device is not powered up n ow postpone applying control's |
| 959 | * value to the hardware, until it is ready to accept commands. |
| 960 | */ |
| 961 | if (gc032a->power_on == 0) { |
| 962 | mutex_unlock(&gc032a->lock); |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | switch (ctrl->id) { |
| 967 | |
| 968 | case V4L2_CID_BRIGHTNESS: |
| 969 | ret = gc032a_yuv_set_brightness(gc032a, ctrl->val); |
| 970 | break; |
| 971 | case V4L2_CID_POWER_LINE_FREQUENCY: |
| 972 | ret = gc032a_yuv_set_banding(gc032a, ctrl->val); |
| 973 | break; |
| 974 | case V4L2_CID_EXPOSURE: |
| 975 | ret = gc032a_set_exposure(gc032a, ctrl->val); |
| 976 | break; |
| 977 | case V4L2_CID_GAIN: |
| 978 | ret = gc032a_set_gain(gc032a, ctrl->val); |
| 979 | break; |
| 980 | case V4L2_CID_TEST_PATTERN: |
| 981 | ret = gc032a_set_test_pattern(gc032a, ctrl->val); |
| 982 | break; |
| 983 | } |
| 984 | |
| 985 | mutex_unlock(&gc032a->lock); |
| 986 | return ret; |
| 987 | } |
| 988 | |
| 989 | static const struct v4l2_ctrl_ops gc032a_ctrl_ops = { |
| 990 | .s_ctrl = gc032a_s_ctrl, |
| 991 | }; |
| 992 | |
| 993 | static const char * const gc032a_test_pattern_menu[] = { |
| 994 | "Disabled", |
| 995 | "Vertical Color Bars", |
| 996 | }; |
| 997 | |
| 998 | /* ----------------------------------------------------------------------------- |
| 999 | * V4L2 subdev internal operations |
| 1000 | */ |
| 1001 | |
| 1002 | #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API |
| 1003 | static int gc032a_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) |
| 1004 | { |
| 1005 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 1006 | struct v4l2_mbus_framefmt *format = |
| 1007 | v4l2_subdev_get_try_format(sd, fh->pad, 0); |
| 1008 | |
| 1009 | dev_dbg(&client->dev, "%s:\n", __func__); |
| 1010 | |
| 1011 | gc032a_get_default_format(format); |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | #endif |
| 1016 | |
| 1017 | static int gc032a_g_mbus_config(struct v4l2_subdev *sd, |
| 1018 | struct v4l2_mbus_config *config) |
| 1019 | { |
| 1020 | config->type = V4L2_MBUS_PARALLEL; |
| 1021 | config->flags = V4L2_MBUS_PCLK_SAMPLE_RISING; |
| 1022 | |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | static int __gc032a_power_on(struct gc032a *gc032a) |
| 1027 | { |
| 1028 | int ret; |
| 1029 | struct device *dev = &gc032a->client->dev; |
| 1030 | |
| 1031 | if (gc032a->power_on) |
| 1032 | return 0; |
| 1033 | |
| 1034 | if (!IS_ERR(gc032a->power_gpio)) { |
| 1035 | gpiod_set_value_cansleep(gc032a->power_gpio, 1); |
| 1036 | usleep_range(2000, 5000); |
| 1037 | } else if (!IS_ERR(gc032a->supplies)) { |
| 1038 | ret = regulator_bulk_enable(GC032A_NUM_SUPPLIES, |
| 1039 | gc032a->supplies); |
| 1040 | if (ret < 0) |
| 1041 | dev_err(dev, "Failed to enable regulators\n"); |
| 1042 | |
| 1043 | usleep_range(2000, 5000); |
| 1044 | } |
| 1045 | |
| 1046 | asr_camera_mclk_ctrl(1); |
| 1047 | usleep_range(2000, 5000); |
| 1048 | |
| 1049 | if (!IS_ERR(gc032a->pwdn_gpio)) { |
| 1050 | gpiod_set_value_cansleep(gc032a->pwdn_gpio, 1); |
| 1051 | usleep_range(2000, 5000); |
| 1052 | } |
| 1053 | |
| 1054 | if (!IS_ERR(gc032a->pwdn_gpio)) { |
| 1055 | gpiod_set_value_cansleep(gc032a->pwdn_gpio, 0); |
| 1056 | usleep_range(2000, 5000); |
| 1057 | } |
| 1058 | |
| 1059 | gc032a->power_on = true; |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | static void __gc032a_power_off(struct gc032a *gc032a) |
| 1064 | { |
| 1065 | if (!gc032a->power_on) |
| 1066 | return; |
| 1067 | |
| 1068 | if (!IS_ERR(gc032a->pwdn_gpio)) { |
| 1069 | gpiod_set_value_cansleep(gc032a->pwdn_gpio, 1); |
| 1070 | usleep_range(2000, 5000); |
| 1071 | } |
| 1072 | |
| 1073 | asr_camera_mclk_ctrl(0); |
| 1074 | usleep_range(2000, 5000); |
| 1075 | |
| 1076 | if (!IS_ERR(gc032a->power_gpio)) { |
| 1077 | gpiod_set_value_cansleep(gc032a->power_gpio, 0); |
| 1078 | usleep_range(2000, 5000); |
| 1079 | } else if (!IS_ERR(gc032a->supplies)) { |
| 1080 | regulator_bulk_disable(GC032A_NUM_SUPPLIES, gc032a->supplies); |
| 1081 | usleep_range(2000, 5000); |
| 1082 | } |
| 1083 | |
| 1084 | if (!IS_ERR(gc032a->pwdn_gpio)) |
| 1085 | gpiod_set_value_cansleep(gc032a->pwdn_gpio, 0); |
| 1086 | usleep_range(7000, 10000); |
| 1087 | gc032a->power_on = false; |
| 1088 | } |
| 1089 | |
| 1090 | static int gc032a_power(struct v4l2_subdev *sd, int on) |
| 1091 | { |
| 1092 | int ret; |
| 1093 | struct gc032a *gc032a = to_gc032a(sd); |
| 1094 | struct i2c_client *client = gc032a->client; |
| 1095 | |
| 1096 | dev_dbg(&client->dev, "%s(%d) on(%d)\n", __func__, __LINE__, on); |
| 1097 | if (on) { |
| 1098 | __gc032a_power_on(gc032a); |
| 1099 | ret = gc032a_write_array(client, gc032a->frame_size->regs); |
| 1100 | if (ret) |
| 1101 | dev_err(&client->dev, "init error\n"); |
| 1102 | } else { |
| 1103 | __gc032a_power_off(gc032a); |
| 1104 | } |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | static int gc032a_enum_frame_interval(struct v4l2_subdev *sd, |
| 1109 | struct v4l2_subdev_pad_config *cfg, |
| 1110 | struct v4l2_subdev_frame_interval_enum *fie) |
| 1111 | { |
| 1112 | if (fie->index >= ARRAY_SIZE(gc032a_framesizes)) |
| 1113 | return -EINVAL; |
| 1114 | |
| 1115 | if (fie->code != MEDIA_BUS_FMT_YVYU8_2X8) |
| 1116 | return -EINVAL; |
| 1117 | |
| 1118 | fie->width = gc032a_framesizes[fie->index].width; |
| 1119 | fie->height = gc032a_framesizes[fie->index].height; |
| 1120 | fie->interval = gc032a_framesizes[fie->index].max_fps; |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | static const struct v4l2_subdev_core_ops gc032a_subdev_core_ops = { |
| 1125 | .log_status = v4l2_ctrl_subdev_log_status, |
| 1126 | .subscribe_event = v4l2_ctrl_subdev_subscribe_event, |
| 1127 | .unsubscribe_event = v4l2_event_subdev_unsubscribe, |
| 1128 | .ioctl = gc032a_ioctl, |
| 1129 | #ifdef CONFIG_COMPAT |
| 1130 | .compat_ioctl32 = gc032a_compat_ioctl32, |
| 1131 | #endif |
| 1132 | .s_power = gc032a_power, |
| 1133 | }; |
| 1134 | |
| 1135 | static const struct v4l2_subdev_video_ops gc032a_subdev_video_ops = { |
| 1136 | .s_stream = gc032a_s_stream, |
| 1137 | .g_mbus_config = gc032a_g_mbus_config, |
| 1138 | }; |
| 1139 | |
| 1140 | static const struct v4l2_subdev_pad_ops gc032a_subdev_pad_ops = { |
| 1141 | .enum_mbus_code = gc032a_enum_mbus_code, |
| 1142 | .enum_frame_size = gc032a_enum_frame_sizes, |
| 1143 | .enum_frame_interval = gc032a_enum_frame_interval, |
| 1144 | .get_fmt = gc032a_get_fmt, |
| 1145 | .set_fmt = gc032a_set_fmt, |
| 1146 | }; |
| 1147 | |
| 1148 | #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API |
| 1149 | static const struct v4l2_subdev_ops gc032a_subdev_ops = { |
| 1150 | .core = &gc032a_subdev_core_ops, |
| 1151 | .video = &gc032a_subdev_video_ops, |
| 1152 | .pad = &gc032a_subdev_pad_ops, |
| 1153 | }; |
| 1154 | |
| 1155 | static const struct v4l2_subdev_internal_ops gc032a_subdev_internal_ops = { |
| 1156 | .open = gc032a_open, |
| 1157 | }; |
| 1158 | #endif |
| 1159 | |
| 1160 | static int gc032a_detect(struct gc032a *gc032a) |
| 1161 | { |
| 1162 | struct i2c_client *client = gc032a->client; |
| 1163 | u8 pid, ver; |
| 1164 | int ret; |
| 1165 | |
| 1166 | dev_dbg(&client->dev, "%s:\n", __func__); |
| 1167 | |
| 1168 | /* Check sensor revision */ |
| 1169 | ret = gc032a_read(client, REG_SC_CHIP_ID_H, &pid); |
| 1170 | if (!ret) |
| 1171 | ret = gc032a_read(client, REG_SC_CHIP_ID_L, &ver); |
| 1172 | |
| 1173 | if (!ret) { |
| 1174 | unsigned short id; |
| 1175 | |
| 1176 | id = SENSOR_ID(pid, ver); |
| 1177 | if (id != GC032A_ID) { |
| 1178 | ret = -1; |
| 1179 | dev_err(&client->dev, |
| 1180 | "Sensor detection failed (%04X, %d)\n", id, ret); |
| 1181 | } else |
| 1182 | dev_info(&client->dev, "Found GC%04X sensor\n", id); |
| 1183 | } |
| 1184 | |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
| 1188 | static int gc032a_configure_regulators(struct gc032a *gc032a) |
| 1189 | { |
| 1190 | unsigned int i; |
| 1191 | |
| 1192 | for (i = 0; i < GC032A_NUM_SUPPLIES; i++) |
| 1193 | gc032a->supplies[i].supply = gc032a_supply_names[i]; |
| 1194 | |
| 1195 | return devm_regulator_bulk_get(&gc032a->client->dev, |
| 1196 | GC032A_NUM_SUPPLIES, |
| 1197 | gc032a->supplies); |
| 1198 | } |
| 1199 | |
| 1200 | static int gc032a_parse_of(struct gc032a *gc032a) |
| 1201 | { |
| 1202 | struct device *dev = &gc032a->client->dev; |
| 1203 | int ret; |
| 1204 | |
| 1205 | gc032a->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW); |
| 1206 | if (IS_ERR(gc032a->pwdn_gpio)) |
| 1207 | dev_err(dev, "Failed to get pwdn-gpios, maybe no used\n"); |
| 1208 | |
| 1209 | gc032a->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW); |
| 1210 | if (IS_ERR(gc032a->power_gpio)) { |
| 1211 | dev_err(dev, "Failed to get reset-gpios, maybe no used\n"); |
| 1212 | |
| 1213 | ret = gc032a_configure_regulators(gc032a); |
| 1214 | if (ret) |
| 1215 | dev_err(dev, "Failed to get power regulators\n"); |
| 1216 | } |
| 1217 | |
| 1218 | return __gc032a_power_on(gc032a); |
| 1219 | } |
| 1220 | |
| 1221 | static int gc032a_probe(struct i2c_client *client, |
| 1222 | const struct i2c_device_id *id) |
| 1223 | { |
| 1224 | struct device *dev = &client->dev; |
| 1225 | struct v4l2_subdev *sd; |
| 1226 | struct gc032a *gc032a; |
| 1227 | int ret; |
| 1228 | |
| 1229 | dev_info(dev, "driver version: %02x.%02x.%02x", |
| 1230 | DRIVER_VERSION >> 16, |
| 1231 | (DRIVER_VERSION & 0xff00) >> 8, |
| 1232 | DRIVER_VERSION & 0x00ff); |
| 1233 | |
| 1234 | gc032a = devm_kzalloc(&client->dev, sizeof(*gc032a), GFP_KERNEL); |
| 1235 | if (!gc032a) |
| 1236 | return -ENOMEM; |
| 1237 | |
| 1238 | gc032a->client = client; |
| 1239 | |
| 1240 | gc032a_parse_of(gc032a); |
| 1241 | |
| 1242 | v4l2_ctrl_handler_init(&gc032a->ctrls, 6); |
| 1243 | gc032a->link_frequency = |
| 1244 | v4l2_ctrl_new_std(&gc032a->ctrls, &gc032a_ctrl_ops, |
| 1245 | V4L2_CID_PIXEL_RATE, 0, |
| 1246 | GC032A_PIXEL_RATE, 1, |
| 1247 | GC032A_PIXEL_RATE); |
| 1248 | v4l2_ctrl_new_std_menu_items(&gc032a->ctrls, &gc032a_ctrl_ops, |
| 1249 | V4L2_CID_TEST_PATTERN, |
| 1250 | ARRAY_SIZE(gc032a_test_pattern_menu) - 1, |
| 1251 | 0, 0, gc032a_test_pattern_menu); |
| 1252 | v4l2_ctrl_new_std(&gc032a->ctrls, &gc032a_ctrl_ops, |
| 1253 | V4L2_CID_EXPOSURE, 1, 32, 1, 32); |
| 1254 | v4l2_ctrl_new_std(&gc032a->ctrls, &gc032a_ctrl_ops, |
| 1255 | V4L2_CID_GAIN, 16, 1023, 1, 16); |
| 1256 | v4l2_ctrl_new_std_menu(&gc032a->ctrls, &gc032a_ctrl_ops, |
| 1257 | V4L2_CID_POWER_LINE_FREQUENCY, |
| 1258 | V4L2_CID_POWER_LINE_FREQUENCY_60HZ, ~0x7, |
| 1259 | V4L2_CID_POWER_LINE_FREQUENCY_50HZ); |
| 1260 | |
| 1261 | v4l2_ctrl_new_std(&gc032a->ctrls, &gc032a_ctrl_ops, |
| 1262 | V4L2_CID_BRIGHTNESS, 0, 9, 1, 0); |
| 1263 | |
| 1264 | gc032a->sd.ctrl_handler = &gc032a->ctrls; |
| 1265 | |
| 1266 | if (gc032a->ctrls.error) { |
| 1267 | dev_err(&client->dev, "%s: control initialization error %d\n", |
| 1268 | __func__, gc032a->ctrls.error); |
| 1269 | return gc032a->ctrls.error; |
| 1270 | } |
| 1271 | |
| 1272 | sd = &gc032a->sd; |
| 1273 | //client->flags |= I2C_CLIENT_SCCB; |
| 1274 | #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API |
| 1275 | v4l2_i2c_subdev_init(sd, client, &gc032a_subdev_ops); |
| 1276 | |
| 1277 | sd->internal_ops = &gc032a_subdev_internal_ops; |
| 1278 | sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | |
| 1279 | V4L2_SUBDEV_FL_HAS_EVENTS; |
| 1280 | #endif |
| 1281 | |
| 1282 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 1283 | gc032a->pad.flags = MEDIA_PAD_FL_SOURCE; |
| 1284 | sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; |
| 1285 | ret = media_entity_pads_init(&sd->entity, 1, &gc032a->pad); |
| 1286 | if (ret < 0) { |
| 1287 | v4l2_ctrl_handler_free(&gc032a->ctrls); |
| 1288 | return ret; |
| 1289 | } |
| 1290 | #endif |
| 1291 | |
| 1292 | mutex_init(&gc032a->lock); |
| 1293 | |
| 1294 | gc032a_get_default_format(&gc032a->format); |
| 1295 | gc032a->frame_size = &gc032a_framesizes[0]; |
| 1296 | |
| 1297 | ret = gc032a_detect(gc032a); |
| 1298 | if (ret < 0) |
| 1299 | goto error; |
| 1300 | ret = gc032a_write_array(client, gc032a->frame_size->regs); |
| 1301 | if (ret) |
| 1302 | dev_err(&client->dev, "init error\n"); |
| 1303 | snprintf(sd->name, sizeof(sd->name), "m_%s %s", |
| 1304 | DRIVER_NAME, dev_name(sd->dev)); |
| 1305 | |
| 1306 | ret = v4l2_async_register_subdev_sensor_common(sd); |
| 1307 | if (ret) |
| 1308 | goto error; |
| 1309 | |
| 1310 | dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name); |
| 1311 | return 0; |
| 1312 | |
| 1313 | error: |
| 1314 | v4l2_ctrl_handler_free(&gc032a->ctrls); |
| 1315 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 1316 | media_entity_cleanup(&sd->entity); |
| 1317 | #endif |
| 1318 | mutex_destroy(&gc032a->lock); |
| 1319 | __gc032a_power_off(gc032a); |
| 1320 | return ret; |
| 1321 | } |
| 1322 | |
| 1323 | static int gc032a_remove(struct i2c_client *client) |
| 1324 | { |
| 1325 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 1326 | struct gc032a *gc032a = to_gc032a(sd); |
| 1327 | |
| 1328 | dev_dbg(&client->dev, "gc032a_remove...\n"); |
| 1329 | |
| 1330 | v4l2_ctrl_handler_free(&gc032a->ctrls); |
| 1331 | v4l2_async_unregister_subdev(sd); |
| 1332 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 1333 | media_entity_cleanup(&sd->entity); |
| 1334 | #endif |
| 1335 | mutex_destroy(&gc032a->lock); |
| 1336 | |
| 1337 | __gc032a_power_off(gc032a); |
| 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | static const struct i2c_device_id gc032a_id[] = { |
| 1343 | { "gc032a", 0 }, |
| 1344 | { /* sentinel */ }, |
| 1345 | }; |
| 1346 | MODULE_DEVICE_TABLE(i2c, gc032a_id); |
| 1347 | |
| 1348 | #if IS_ENABLED(CONFIG_OF) |
| 1349 | static const struct of_device_id gc032a_of_match[] = { |
| 1350 | { .compatible = "galaxycore,gc032a", }, |
| 1351 | { /* sentinel */ }, |
| 1352 | }; |
| 1353 | MODULE_DEVICE_TABLE(of, gc032a_of_match); |
| 1354 | #endif |
| 1355 | |
| 1356 | static struct i2c_driver gc032a_i2c_driver = { |
| 1357 | .driver = { |
| 1358 | .name = DRIVER_NAME, |
| 1359 | .of_match_table = of_match_ptr(gc032a_of_match), |
| 1360 | }, |
| 1361 | .probe = gc032a_probe, |
| 1362 | .remove = gc032a_remove, |
| 1363 | .id_table = gc032a_id, |
| 1364 | }; |
| 1365 | |
| 1366 | static int __init sensor_mod_init(void) |
| 1367 | { |
| 1368 | return i2c_add_driver(&gc032a_i2c_driver); |
| 1369 | } |
| 1370 | |
| 1371 | static void __exit sensor_mod_exit(void) |
| 1372 | { |
| 1373 | i2c_del_driver(&gc032a_i2c_driver); |
| 1374 | } |
| 1375 | |
| 1376 | device_initcall_sync(sensor_mod_init); |
| 1377 | module_exit(sensor_mod_exit); |
| 1378 | //module_i2c_driver(gc032a_i2c_driver); |
| 1379 | |
| 1380 | MODULE_AUTHOR("ASR Inc."); |
| 1381 | MODULE_DESCRIPTION("GC032A CMOS Image Sensor driver"); |
| 1382 | MODULE_LICENSE("GPL v2"); |