Advertisement
Guest User

mxc_ov2640_driver_test

a guest
Feb 8th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 37.98 KB | None | 0 0
  1. /*
  2.  * Copyright 2005-2012 Freescale Semiconductor, Inc. All Rights Reserved.
  3.  */
  4.  
  5. /*
  6.  * The code contained herein is licensed under the GNU General Public
  7.  * License. You may obtain a copy of the GNU General Public License
  8.  * Version 2 or later at the following locations:
  9.  *
  10.  * http://www.opensource.org/licenses/gpl-license.html
  11.  * http://www.gnu.org/copyleft/gpl.html
  12.  */
  13.  
  14. /*!
  15.  * @file ov2640.c
  16.  *
  17.  * @brief ov2640 camera driver functions
  18.  *
  19.  * @ingroup Camera
  20.  */
  21.  
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/ctype.h>
  26. #include <linux/types.h>
  27. #include <linux/delay.h>
  28. #include <linux/device.h>
  29. #include <linux/i2c.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <linux/fsl_devices.h>
  32.  
  33. #include <media/v4l2-int-device.h>
  34. #include "mxc_v4l2_capture.h"
  35.  
  36. #define MIN_FPS 5
  37. #define MAX_FPS 30
  38. #define DEFAULT_FPS 30
  39.  
  40. #define OV2640_XCLK_MIN 6000000
  41. #define OV2640_XCLK_MAX 27000000
  42.  
  43.  
  44. #define PID_H   0x0A  // VAL = 0x26
  45. #define PID_L   0x0B  // VAL = 0x41 ..or  42?
  46.  
  47.  
  48. enum ov2640_mode {
  49.     ov2640_mode_1600_1120_yuv,
  50.     ov2640_mode_800_600_yuv,
  51.     ov2640_mode_1600_1120_rgb,
  52.     ov2640_mode_800_600_rgb
  53.     };
  54.  
  55. struct reg_value {
  56.     u8 reg;
  57.     u8 value;
  58.     int delay_ms;
  59. };
  60.  
  61. static int ov2640_mode_width[4]     = { 1600, 800, 1600, 800 };
  62. static int ov2640_mode_height[4]    = { 1120, 600, 1120, 600 };
  63. static int ov2640_mode_pixelformat[4] = { V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_UYVY,
  64.                     V4L2_PIX_FMT_RGB565, V4L2_PIX_FMT_RGB565 };
  65.  
  66.  
  67. static struct reg_value ov2640_setting_1600_1120[] = {
  68. #ifdef CONFIG_MACH_MX25_3DS                              
  69.     {0xff, 0x01, 0}, {0x12, 0x80, 5}, {0xff, 0x00, 0}, {0x2c, 0xff, 0},
  70.     {0x2e, 0xdf, 0}, {0xff, 0x01, 0}, {0x3c, 0x32, 0}, {0x11, 0x00, 0},
  71.     {0x09, 0x02, 0}, {0x04, 0x28, 0}, {0x13, 0xe5, 0}, {0x14, 0x48, 0},
  72.     {0x2c, 0x0c, 0}, {0x33, 0x78, 0}, {0x3a, 0x33, 0}, {0x3b, 0xfb, 0},
  73.     {0x3e, 0x00, 0}, {0x43, 0x11, 0}, {0x16, 0x10, 0}, {0x39, 0x02, 0},
  74.     {0x35, 0x58, 0}, {0x22, 0x0a, 0}, {0x37, 0x40, 0}, {0x23, 0x00, 0},
  75.     {0x34, 0xa0, 0}, {0x36, 0x1a, 0}, {0x06, 0x02, 0}, {0x07, 0xc0, 0},
  76.     {0x0d, 0xb7, 0}, {0x0e, 0x01, 0}, {0x4c, 0x00, 0}, {0x4a, 0x81, 0},
  77.     {0x21, 0x99, 0}, {0x24, 0x40, 0}, {0x25, 0x38, 0}, {0x26, 0x82, 0},
  78.     {0x5c, 0x00, 0}, {0x63, 0x00, 0}, {0x46, 0x3f, 0}, {0x61, 0x70, 0},
  79.     {0x62, 0x80, 0}, {0x7c, 0x05, 0}, {0x20, 0x80, 0}, {0x28, 0x30, 0},
  80.     {0x6c, 0x00, 0}, {0x6d, 0x80, 0}, {0x6e, 0x00, 0}, {0x70, 0x02, 0},
  81.     {0x71, 0x94, 0}, {0x73, 0xc1, 0}, {0x3d, 0x34, 0}, {0x5a, 0x57, 0},
  82.     {0x4f, 0xbb, 0}, {0x50, 0x9c, 0}, {0xff, 0x00, 0}, {0xe5, 0x7f, 0},
  83.     {0xf9, 0xc0, 0}, {0x41, 0x24, 0}, {0xe0, 0x14, 0}, {0x76, 0xff, 0},
  84.     {0x33, 0xa0, 0}, {0x42, 0x20, 0}, {0x43, 0x18, 0}, {0x4c, 0x00, 0},
  85.     {0x87, 0xd0, 0}, {0x88, 0x3f, 0}, {0xd7, 0x01, 0}, {0xd9, 0x10, 0},
  86.     {0xd3, 0x82, 0}, {0xc8, 0x08, 0}, {0xc9, 0x80, 0}, {0x7c, 0x00, 0},
  87.     {0x7d, 0x00, 0}, {0x7c, 0x03, 0}, {0x7d, 0x48, 0}, {0x7d, 0x48, 0},
  88.     {0x7c, 0x08, 0}, {0x7d, 0x20, 0}, {0x7d, 0x10, 0}, {0x7d, 0x0e, 0},
  89.     {0x90, 0x00, 0}, {0x91, 0x0e, 0}, {0x91, 0x1a, 0}, {0x91, 0x31, 0},
  90.     {0x91, 0x5a, 0}, {0x91, 0x69, 0}, {0x91, 0x75, 0}, {0x91, 0x7e, 0},
  91.     {0x91, 0x88, 0}, {0x91, 0x8f, 0}, {0x91, 0x96, 0}, {0x91, 0xa3, 0},
  92.     {0x91, 0xaf, 0}, {0x91, 0xc4, 0}, {0x91, 0xd7, 0}, {0x91, 0xe8, 0},
  93.     {0x91, 0x20, 0}, {0x92, 0x00, 0}, {0x93, 0x06, 0}, {0x93, 0xe3, 0},
  94.     {0x93, 0x05, 0}, {0x93, 0x05, 0}, {0x93, 0x00, 0}, {0x93, 0x04, 0},
  95.     {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0},
  96.     {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x96, 0x00, 0},
  97.     {0x97, 0x08, 0}, {0x97, 0x19, 0}, {0x97, 0x02, 0}, {0x97, 0x0c, 0},
  98.     {0x97, 0x24, 0}, {0x97, 0x30, 0}, {0x97, 0x28, 0}, {0x97, 0x26, 0},
  99.     {0x97, 0x02, 0}, {0x97, 0x98, 0}, {0x97, 0x80, 0}, {0x97, 0x00, 0},
  100.     {0x97, 0x00, 0}, {0xc3, 0xed, 0}, {0xa4, 0x00, 0}, {0xa8, 0x00, 0},
  101.     {0xc5, 0x11, 0}, {0xc6, 0x51, 0}, {0xbf, 0x80, 0}, {0xc7, 0x10, 0},
  102.     {0xb6, 0x66, 0}, {0xb8, 0xa5, 0}, {0xb7, 0x64, 0}, {0xb9, 0x7c, 0},
  103.     {0xb3, 0xaf, 0}, {0xb4, 0x97, 0}, {0xb5, 0xff, 0}, {0xb0, 0xc5, 0},
  104.     {0xb1, 0x94, 0}, {0xb2, 0x0f, 0}, {0xc4, 0x5c, 0}, {0xc0, 0xc8, 0},
  105.     {0xc1, 0x96, 0}, {0x86, 0x1d, 0}, {0x50, 0x00, 0}, {0x51, 0x90, 0},
  106.     {0x52, 0x2c, 0}, {0x53, 0x00, 0}, {0x54, 0x00, 0}, {0x55, 0x88, 0},
  107.     {0x57, 0x00, 0}, {0x5a, 0x90, 0}, {0x5b, 0x2c, 0}, {0x5c, 0x05, 0},
  108.     {0xc3, 0xed, 0}, {0x7f, 0x00, 0}, {0xda, 0x00, 0}, {0xe5, 0x1f, 0},
  109.     {0xe1, 0x77, 0}, {0xe0, 0x00, 0}, {0xdd, 0x7f, 0}, {0x05, 0x00, 0},
  110.     {0xff, 0x00, 0}, {0xe0, 0x04, 0}, {0xc0, 0xc8, 0}, {0xc1, 0x96, 0},
  111.     {0x86, 0x3d, 0}, {0x50, 0x00, 0}, {0x51, 0x90, 0}, {0x52, 0x2c, 0},
  112.     {0x53, 0x00, 0}, {0x54, 0x00, 0}, {0x55, 0x88, 0}, {0x57, 0x00, 0},
  113.     {0x5a, 0x40, 0}, {0x5b, 0xf0, 0}, {0x5c, 0x01, 0}, {0xd3, 0x82, 0},
  114.     {0xe0, 0x00, 1000}
  115. #else
  116.     {0xff, 0x1, 0}, {0x12, 0x80, 1}, {0xff, 0, 0}, {0x2c, 0xff, 0},
  117.     {0x2e, 0xdf, 0}, {0xff, 0x1, 0}, {0x3c, 0x32, 0}, {0x11, 0x01, 0},
  118.     {0x09, 0x00, 0}, {0x04, 0x28, 0}, {0x13, 0xe5, 0}, {0x14, 0x48, 0},
  119.     {0x2c, 0x0c, 0}, {0x33, 0x78, 0}, {0x3a, 0x33, 0}, {0x3b, 0xfb, 0},
  120.     {0x3e, 0x00, 0}, {0x43, 0x11, 0}, {0x16, 0x10, 0}, {0x39, 0x82, 0},
  121.     {0x35, 0x88, 0}, {0x22, 0x0a, 0}, {0x37, 0x40, 0}, {0x23, 0x00, 0},
  122.     {0x34, 0xa0, 0}, {0x36, 0x1a, 0}, {0x06, 0x02, 0}, {0x07, 0xc0, 0},
  123.     {0x0d, 0xb7, 0}, {0x0e, 0x01, 0}, {0x4c, 0x00, 0}, {0x4a, 0x81, 0},
  124.     {0x21, 0x99, 0}, {0x24, 0x40, 0}, {0x25, 0x38, 0}, {0x26, 0x82, 0},
  125.     {0x5c, 0x00, 0}, {0x63, 0x00, 0}, {0x46, 0x3f, 0}, {0x0c, 0x3c, 0},
  126.     {0x5d, 0x55, 0}, {0x5e, 0x7d, 0}, {0x5f, 0x7d, 0}, {0x60, 0x55, 0},
  127.     {0x61, 0x70, 0}, {0x62, 0x80, 0}, {0x7c, 0x05, 0}, {0x20, 0x80, 0},
  128.     {0x28, 0x30, 0}, {0x6c, 0x00, 0}, {0x6d, 0x80, 0}, {0x6e, 00, 0},
  129.     {0x70, 0x02, 0}, {0x71, 0x94, 0}, {0x73, 0xc1, 0}, {0x3d, 0x34, 0},
  130.     {0x5a, 0x57, 0}, {0x4f, 0xbb, 0}, {0x50, 0x9c, 0}, {0xff, 0x00, 0},
  131.     {0xe5, 0x7f, 0}, {0xf9, 0xc0, 0}, {0x41, 0x24, 0}, {0x44, 0x06, 0},
  132.     {0xe0, 0x14, 0}, {0x76, 0xff, 0}, {0x33, 0xa0, 0}, {0x42, 0x20, 0},
  133.     {0x43, 0x18, 0}, {0x4c, 0x00, 0}, {0x87, 0xd0, 0}, {0xd7, 0x03, 0},
  134.     {0xd9, 0x10, 0}, {0xd3, 0x82, 0}, {0xc8, 0x08, 0}, {0xc9, 0x80, 0},
  135.     {0x7c, 0x00, 0}, {0x7d, 0x00, 0}, {0x7c, 0x03, 0}, {0x7d, 0x48, 0},
  136.     {0x7d, 0x48, 0}, {0x7c, 0x08, 0}, {0x7d, 0x20, 0}, {0x7d, 0x10, 0},
  137.     {0x7d, 0x0e, 0}, {0x90, 0x00, 0}, {0x91, 0x0e, 0}, {0x91, 0x1a, 0},
  138.     {0x91, 0x31, 0}, {0x91, 0x5a, 0}, {0x91, 0x69, 0}, {0x91, 0x75, 0},
  139.     {0x91, 0x7e, 0}, {0x91, 0x88, 0}, {0x91, 0x8f, 0}, {0x91, 0x96, 0},
  140.     {0x91, 0xa3, 0}, {0x91, 0xaf, 0}, {0x91, 0xc4, 0}, {0x91, 0xd7, 0},
  141.     {0x91, 0xe8, 0}, {0x91, 0x20, 0}, {0x92, 0x00, 0}, {0x93, 0x06, 0},
  142.     {0x93, 0xe3, 0}, {0x93, 0x03, 0}, {0x93, 0x03, 0}, {0x93, 0x00, 0},
  143.     {0x93, 0x02, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0},
  144.     {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0},
  145.     {0x96, 0x00, 0}, {0x97, 0x08, 0}, {0x97, 0x19, 0}, {0x97, 0x02, 0},
  146.     {0x97, 0x0c, 0}, {0x97, 0x24, 0}, {0x97, 0x30, 0}, {0x97, 0x28, 0},
  147.     {0x97, 0x26, 0}, {0x97, 0x02, 0}, {0x97, 0x98, 0}, {0x97, 0x80, 0},
  148.     {0x97, 0x00, 0}, {0x97, 0x00, 0}, {0xa4, 0x00, 0}, {0xa8, 0x00, 0},
  149.     {0xc5, 0x11, 0}, {0xc6, 0x51, 0}, {0xbf, 0x80, 0}, {0xc7, 0x10, 0},
  150.     {0xb6, 0x66, 0}, {0xb8, 0xa5, 0}, {0xb7, 0x64, 0}, {0xb9, 0x7c, 0},
  151.     {0xb3, 0xaf, 0}, {0xb4, 0x97, 0}, {0xb5, 0xff, 0}, {0xb0, 0xc5, 0},
  152.     {0xb1, 0x94, 0}, {0xb2, 0x0f, 0}, {0xc4, 0x5c, 0}, {0xa6, 0x00, 0},
  153.     {0xa7, 0x20, 0}, {0xa7, 0xd8, 0}, {0xa7, 0x1b, 0}, {0xa7, 0x31, 0},
  154.     {0xa7, 0x00, 0}, {0xa7, 0x18, 0}, {0xa7, 0x20, 0}, {0xa7, 0xd8, 0},
  155.     {0xa7, 0x19, 0}, {0xa7, 0x31, 0}, {0xa7, 0x00, 0}, {0xa7, 0x18, 0},
  156.     {0xa7, 0x20, 0}, {0xa7, 0xd8, 0}, {0xa7, 0x19, 0}, {0xa7, 0x31, 0},
  157.     {0xa7, 0x00, 0}, {0xa7, 0x18, 0}, {0xc0, 0xc8, 0}, {0xc1, 0x96, 0},
  158.     {0x86, 0x3d, 0}, {0x50, 0x00, 0}, {0x51, 0x90, 0}, {0x52, 0x18, 0},
  159.     {0x53, 0x00, 0}, {0x54, 0x00, 0}, {0x55, 0x88, 0}, {0x57, 0x00, 0},
  160.     {0x5a, 0x90, 0}, {0x5b, 0x18, 0}, {0x5c, 0x05, 0}, {0xc3, 0xef, 0},
  161.     {0x7f, 0x00, 0}, {0xda, 0x01, 0}, {0xe5, 0x1f, 0}, {0xe1, 0x67, 0},
  162.     {0xe0, 0x00, 0}, {0xdd, 0x7f, 0}, {0x05, 0x00, 0}
  163. #endif
  164. };
  165.  
  166. static struct reg_value ov2640_setting_800_600[] = {
  167. #ifdef CONFIG_MACH_MX25_3DS
  168.     {0xff, 0x01, 0}, {0x12, 0x80, 5}, {0xff, 0x00, 0}, {0x2c, 0xff, 0},
  169.     {0x2e, 0xdf, 0}, {0xff, 0x01, 0}, {0x3c, 0x32, 0}, {0x11, 0x00, 0},
  170.     {0x09, 0x02, 0}, {0x04, 0x28, 0}, {0x13, 0xe5, 0}, {0x14, 0x48, 0},
  171.     {0x2c, 0x0c, 0}, {0x33, 0x78, 0}, {0x3a, 0x33, 0}, {0x3b, 0xfb, 0},
  172.     {0x3e, 0x00, 0}, {0x43, 0x11, 0}, {0x16, 0x10, 0}, {0x39, 0x92, 0},
  173.     {0x35, 0xda, 0}, {0x22, 0x1a, 0}, {0x37, 0xc3, 0}, {0x23, 0x00, 0},
  174.     {0x34, 0xc0, 0}, {0x36, 0x1a, 0}, {0x06, 0x88, 0}, {0x07, 0xc0, 0},
  175.     {0x0d, 0x87, 0}, {0x0e, 0x41, 0}, {0x4c, 0x00, 0},
  176.     {0x48, 0x00, 0}, {0x5b, 0x00, 0}, {0x42, 0x03, 0}, {0x4a, 0x81, 0},
  177.     {0x21, 0x99, 0}, {0x24, 0x40, 0}, {0x25, 0x38, 0}, {0x26, 0x82, 0},
  178.     {0x5c, 0x00, 0}, {0x63, 0x00, 0}, {0x46, 0x22, 0}, {0x0c, 0x3c, 0},
  179.     {0x61, 0x70, 0}, {0x62, 0x80, 0}, {0x7c, 0x05, 0}, {0x20, 0x80, 0},
  180.     {0x28, 0x30, 0}, {0x6c, 0x00, 0}, {0x6d, 0x80, 0}, {0x6e, 0x00, 0},
  181.     {0x70, 0x02, 0}, {0x71, 0x94, 0}, {0x73, 0xc1, 0}, {0x12, 0x40, 0},
  182.     {0x17, 0x11, 0}, {0x18, 0x43, 0}, {0x19, 0x00, 0}, {0x1a, 0x4b, 0},
  183.     {0x32, 0x09, 0}, {0x37, 0xc0, 0}, {0x4f, 0xca, 0}, {0x50, 0xa8, 0},
  184.     {0x5a, 0x23, 0}, {0x6d, 0x00, 0}, {0x3d, 0x38, 0}, {0xff, 0x00, 0},
  185.     {0xe5, 0x7f, 0}, {0xf9, 0xc0, 0}, {0x41, 0x24, 0}, {0xe0, 0x14, 0},
  186.     {0x76, 0xff, 0}, {0x33, 0xa0, 0}, {0x42, 0x20, 0}, {0x43, 0x18, 0},
  187.     {0x4c, 0x00, 0}, {0x87, 0xd5, 0}, {0x88, 0x3f, 0}, {0xd7, 0x01, 0},
  188.     {0xd9, 0x10, 0}, {0xd3, 0x82, 0}, {0xc8, 0x08, 0}, {0xc9, 0x80, 0},
  189.     {0x7c, 0x00, 0}, {0x7d, 0x00, 0}, {0x7c, 0x03, 0}, {0x7d, 0x48, 0},
  190.     {0x7d, 0x48, 0}, {0x7c, 0x08, 0}, {0x7d, 0x20, 0}, {0x7d, 0x10, 0},
  191.     {0x7d, 0x0e, 0}, {0x90, 0x00, 0}, {0x91, 0x0e, 0}, {0x91, 0x1a, 0},
  192.     {0x91, 0x31, 0}, {0x91, 0x5a, 0}, {0x91, 0x69, 0}, {0x91, 0x75, 0},
  193.     {0x91, 0x7e, 0}, {0x91, 0x88, 0}, {0x91, 0x8f, 0}, {0x91, 0x96, 0},
  194.     {0x91, 0xa3, 0}, {0x91, 0xaf, 0}, {0x91, 0xc4, 0}, {0x91, 0xd7, 0},
  195.     {0x91, 0xe8, 0}, {0x91, 0x20, 0}, {0x92, 0x00, 0}, {0x93, 0x06, 0},
  196.     {0x93, 0xe3, 0}, {0x93, 0x05, 0}, {0x93, 0x05, 0}, {0x93, 0x00, 0},
  197.     {0x93, 0x04, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0},
  198.     {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0},
  199.     {0x96, 0x00, 0}, {0x97, 0x08, 0}, {0x97, 0x19, 0}, {0x97, 0x02, 0},
  200.     {0x97, 0x0c, 0}, {0x97, 0x24, 0}, {0x97, 0x30, 0}, {0x97, 0x28, 0},
  201.     {0x97, 0x26, 0}, {0x97, 0x02, 0}, {0x97, 0x98, 0}, {0x97, 0x80, 0},
  202.     {0x97, 0x00, 0}, {0x97, 0x00, 0}, {0xc3, 0xed, 0}, {0xa4, 0x00, 0},
  203.     {0xa8, 0x00, 0}, {0xc5, 0x11, 0}, {0xc6, 0x51, 0}, {0xbf, 0x80, 0},
  204.     {0xc7, 0x10, 0}, {0xb6, 0x66, 0}, {0xb8, 0xa5, 0}, {0xb7, 0x64, 0},
  205.     {0xb9, 0x7c, 0}, {0xb3, 0xaf, 0}, {0xb4, 0x97, 0}, {0xb5, 0xff, 0},
  206.     {0xb0, 0xc5, 0}, {0xb1, 0x94, 0}, {0xb2, 0x0f, 0}, {0xc4, 0x5c, 0},
  207.     {0xc0, 0x64, 0}, {0xc1, 0x4b, 0}, {0x8c, 0x00, 0}, {0x86, 0x3d, 0},
  208.     {0x50, 0x00, 0}, {0x51, 0xc8, 0}, {0x52, 0x96, 0}, {0x53, 0x00, 0},
  209.     {0x54, 0x00, 0}, {0x55, 0x00, 0}, {0x5a, 0xc8, 0}, {0x5b, 0x96, 0},
  210.     {0x5c, 0x00, 0}, {0xd3, 0x82, 0}, {0xc3, 0xed, 0}, {0x7f, 0x00, 0},
  211.     {0xda, 0x00, 0}, {0xe5, 0x1f, 0}, {0xe1, 0x67, 0}, {0xe0, 0x00, 0},
  212.     {0xdd, 0x7f, 0}, {0x05, 0x00, 0}, {0xff, 0x00, 0}, {0xe0, 0x04, 0},
  213.     {0xc0, 0x64, 0}, {0xc1, 0x4b, 0}, {0x8c, 0x00, 0}, {0x86, 0x3d, 0},
  214.     {0x50, 0x00, 0}, {0x51, 0xc8, 0}, {0x52, 0x96, 0}, {0x53, 0x00, 0},
  215.     {0x54, 0x00, 0}, {0x55, 0x00, 0}, {0x5a, 0xa0, 0}, {0x5b, 0x78, 0},
  216.     {0x5c, 0x00, 0}, {0xd3, 0x82, 0}, {0xe0, 0x00, 1000}
  217. #else
  218.     {0xff, 0, 0}, {0xff, 1, 0}, {0x12, 0x80, 1}, {0xff, 00, 0},
  219.     {0x2c, 0xff, 0}, {0x2e, 0xdf, 0}, {0xff, 0x1, 0}, {0x3c, 0x32, 0},
  220.     {0x11, 0x01, 0}, {0x09, 0x00, 0}, {0x04, 0x28, 0}, {0x13, 0xe5, 0},
  221.     {0x14, 0x48, 0}, {0x2c, 0x0c, 0}, {0x33, 0x78, 0}, {0x33, 0x33, 0},
  222.     {0x3b, 0xfb, 0}, {0x3e, 0x00, 0}, {0x43, 0x11, 0}, {0x16, 0x10, 0},
  223.     {0x39, 0x92, 0}, {0x35, 0xda, 0}, {0x22, 0x1a, 0}, {0x37, 0xc3, 0},
  224.     {0x23, 0x00, 0}, {0x34, 0xc0, 0}, {0x36, 0x1a, 0}, {0x06, 0x88, 0},
  225.     {0x07, 0xc0, 0}, {0x0d, 0x87, 0}, {0x0e, 0x41, 0}, {0x4c, 0x00, 0},
  226.     {0x4a, 0x81, 0}, {0x21, 0x99, 0}, {0x24, 0x40, 0}, {0x25, 0x38, 0},
  227.     {0x26, 0x82, 0}, {0x5c, 0x00, 0}, {0x63, 0x00, 0}, {0x46, 0x22, 0},
  228.     {0x0c, 0x3c, 0}, {0x5d, 0x55, 0}, {0x5e, 0x7d, 0}, {0x5f, 0x7d, 0},
  229.     {0x60, 0x55, 0}, {0x61, 0x70, 0}, {0x62, 0x80, 0}, {0x7c, 0x05, 0},
  230.     {0x20, 0x80, 0}, {0x28, 0x30, 0}, {0x6c, 0x00, 0}, {0x6d, 0x80, 0},
  231.     {0x6e, 00, 0}, {0x70, 0x02, 0}, {0x71, 0x94, 0}, {0x73, 0xc1, 0},
  232.     {0x12, 0x40, 0}, {0x17, 0x11, 0}, {0x18, 0x43, 0}, {0x19, 0x00, 0},
  233.     {0x1a, 0x4b, 0}, {0x32, 0x09, 0}, {0x37, 0xc0, 0}, {0x4f, 0xca, 0},
  234.     {0x50, 0xa8, 0}, {0x6d, 0x00, 0}, {0x3d, 0x38, 0}, {0xff, 0x00, 0},
  235.     {0xe5, 0x7f, 0}, {0xf9, 0xc0, 0}, {0x41, 0x24, 0}, {0x44, 0x06, 0},
  236.     {0xe0, 0x14, 0}, {0x76, 0xff, 0}, {0x33, 0xa0, 0}, {0x42, 0x20, 0},
  237.     {0x43, 0x18, 0}, {0x4c, 0x00, 0}, {0x87, 0xd0, 0}, {0x88, 0x3f, 0},
  238.     {0xd7, 0x03, 0}, {0xd9, 0x10, 0}, {0xd3, 0x82, 0}, {0xc8, 0x08, 0},
  239.     {0xc9, 0x80, 0}, {0x7c, 0x00, 0}, {0x7d, 0x00, 0}, {0x7c, 0x03, 0},
  240.     {0x7d, 0x48, 0}, {0x7d, 0x48, 0}, {0x7c, 0x08, 0}, {0x7d, 0x20, 0},
  241.     {0x7d, 0x10, 0}, {0x7d, 0x0e, 0}, {0x90, 0x00, 0}, {0x91, 0x0e, 0},
  242.     {0x91, 0x1a, 0}, {0x91, 0x31, 0}, {0x91, 0x5a, 0}, {0x91, 0x69, 0},
  243.     {0x91, 0x75, 0}, {0x91, 0x7e, 0}, {0x91, 0x88, 0}, {0x91, 0x8f, 0},
  244.     {0x91, 0x96, 0}, {0x91, 0xa3, 0}, {0x91, 0xaf, 0}, {0x91, 0xc4, 0},
  245.     {0x91, 0xd7, 0}, {0x91, 0xe8, 0}, {0x91, 0x20, 0}, {0x92, 0x00, 0},
  246.     {0x93, 0x06, 0}, {0x93, 0xe3, 0}, {0x93, 0x03, 0}, {0x93, 0x03, 0},
  247.     {0x93, 0x00, 0}, {0x93, 0x02, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0},
  248.     {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0}, {0x93, 0x00, 0},
  249.     {0x93, 0x00, 0}, {0x96, 0x00, 0}, {0x97, 0x08, 0}, {0x97, 0x19, 0},
  250.     {0x97, 0x02, 0}, {0x97, 0x0c, 0}, {0x97, 0x24, 0}, {0x97, 0x30, 0},
  251.     {0x97, 0x28, 0}, {0x97, 0x26, 0}, {0x97, 0x02, 0}, {0x97, 0x98, 0},
  252.     {0x97, 0x80, 0}, {0x97, 0x00, 0}, {0x97, 0x00, 0}, {0xa4, 0x00, 0},
  253.     {0xa8, 0x00, 0}, {0xc5, 0x11, 0}, {0xc6, 0x51, 0}, {0xbf, 0x80, 0},
  254.     {0xc7, 0x10, 0}, {0xb6, 0x66, 0}, {0xb8, 0xa5, 0}, {0xb7, 0x64, 0},
  255.     {0xb9, 0x7c, 0}, {0xb3, 0xaf, 0}, {0xb4, 0x97, 0}, {0xb5, 0xff, 0},
  256.     {0xb0, 0xc5, 0}, {0xb1, 0x94, 0}, {0xb2, 0x0f, 0}, {0xc4, 0x5c, 0},
  257.     {0xa6, 0x00, 0}, {0xa7, 0x20, 0}, {0xa7, 0xd8, 0}, {0xa7, 0x1b, 0},
  258.     {0xa7, 0x31, 0}, {0xa7, 0x00, 0}, {0xa7, 0x18, 0}, {0xa7, 0x20, 0},
  259.     {0xa7, 0xd8, 0}, {0xa7, 0x19, 0}, {0xa7, 0x31, 0}, {0xa7, 0x00, 0},
  260.     {0xa7, 0x18, 0}, {0xa7, 0x20, 0}, {0xa7, 0xd8, 0}, {0xa7, 0x19, 0},
  261.     {0xa7, 0x31, 0}, {0xa7, 0x00, 0}, {0xa7, 0x18, 0}, {0xc0, 0x64, 0},
  262.     {0xc1, 0x4b, 0}, {0x86, 0x1d, 0}, {0x50, 0x00, 0}, {0x51, 0xc8, 0},
  263.     {0x52, 0x96, 0}, {0x53, 0x00, 0}, {0x54, 0x00, 0}, {0x55, 0x00, 0},
  264.     {0x57, 0x00, 0}, {0x5a, 0xc8, 0}, {0x5b, 0x96, 0}, {0x5c, 0x00, 0},
  265.     {0xc3, 0xef, 0}, {0x7f, 0x00, 0}, {0xda, 0x01, 0}, {0xe5, 0x1f, 0},
  266.     {0xe1, 0x67, 0}, {0xe0, 0x00, 0}, {0xdd, 0x7f, 0}, {0x05, 0x00, 0}
  267. #endif
  268. };
  269.  
  270. /*!
  271.  * Maintains the information on the current state of the sesor.
  272.  */
  273. struct sensor {
  274.     const struct ov2640_platform_data *platform_data;
  275.     struct v4l2_int_device *v4l2_int_device;
  276.     struct i2c_client *i2c_client;
  277.     struct v4l2_pix_format pix;
  278.     struct v4l2_captureparm streamcap;
  279.     bool on;
  280.  
  281.     /* control settings */
  282.     int brightness;
  283.     int hue;
  284.     int contrast;
  285.     int saturation;
  286.     int red;
  287.     int green;
  288.     int blue;
  289.     int ae_mode;
  290.  
  291.     u32 csi;
  292.     u32 mclk;
  293.  
  294. } ov2640_data;
  295.  
  296. static struct regulator *io_regulator;
  297. static struct regulator *core_regulator;
  298. static struct regulator *analog_regulator;
  299. static struct regulator *gpo_regulator;
  300.  
  301.  
  302. /* list of image formats supported by this sensor */
  303. /*
  304. const static struct v4l2_fmtdesc ov2640_formats[] = {
  305.     {
  306.         .description = "YUYV (YUV 4:2:2), packed",
  307.         .pixelformat = V4L2_PIX_FMT_UYVY,
  308.     },
  309. };
  310.  */
  311.  
  312. static int ov2640_init_mode(struct sensor *s)
  313. {
  314.     int ret = -1;
  315.     struct reg_value *setting;
  316.     int i, num;
  317.  
  318.     pr_debug("In ov2640:ov2640_init_mode capturemode is %d\n",
  319.         s->streamcap.capturemode);
  320.  
  321.     if (s->streamcap.capturemode & V4L2_MODE_HIGHQUALITY) {
  322.         s->pix.width = 1600;
  323.         s->pix.height = 1120;
  324.         setting = ov2640_setting_1600_1120;
  325.         num = ARRAY_SIZE(ov2640_setting_1600_1120);
  326.     } else {
  327.         s->pix.width = 800;
  328.         s->pix.height = 600;
  329.         setting = ov2640_setting_800_600;
  330.         num = ARRAY_SIZE(ov2640_setting_800_600);
  331.     }
  332.  
  333.     for (i = 0; i < num; i++) {
  334.         ret = i2c_smbus_write_byte_data(s->i2c_client,
  335.                         setting[i].reg,
  336.                         setting[i].value);
  337.         if (ret < 0) {
  338.             pr_err("write reg error: reg=%x, val=%x\n",
  339.                    setting[i].reg, setting[i].value);
  340.             return ret;
  341.         }
  342.         if (setting[i].delay_ms > 0)
  343.             msleep(setting[i].delay_ms);
  344.     }
  345.  
  346.     return ret;
  347. }
  348.  
  349. /* At present only support change to 15fps(only for SVGA mode) */
  350. static int ov2640_set_fps(struct sensor *s, int fps)
  351. {
  352.     int ret = 0;
  353.  
  354.     if (i2c_smbus_write_byte_data(s->i2c_client, 0xff, 0x01) < 0) {
  355.         pr_err("in %s,change to sensor addr failed\n", __func__);
  356.         ret = -EPERM;
  357.     }
  358.  
  359.     /* change the camera framerate to 15fps(only for SVGA mode) */
  360.     if (i2c_smbus_write_byte_data(s->i2c_client, 0x11, 0x01) < 0) {
  361.         pr_err("change camera to 15fps failed\n");
  362.         ret = -EPERM;
  363.     }
  364.  
  365.     return ret;
  366. }
  367.  
  368. static int ov2640_set_format(struct sensor *s, int format)
  369. {
  370.     int ret = 0;
  371.  
  372.     if (i2c_smbus_write_byte_data(s->i2c_client, 0xff, 0x00) < 0)
  373.         ret = -EPERM;
  374.  
  375.     if (format == V4L2_PIX_FMT_RGB565) {
  376.          pr_err(" set RGB\n");
  377.         /* set RGB565 format */
  378.         if (i2c_smbus_write_byte_data(s->i2c_client, 0xda, 0x08) < 0)
  379.             ret = -EPERM;
  380.  
  381.         if (i2c_smbus_write_byte_data(s->i2c_client, 0xd7, 0x03) < 0)
  382.             ret = -EPERM;
  383.     } else if (format == V4L2_PIX_FMT_YUV420) {  
  384.         pr_err(" set YUV\n");
  385.         /* set YUV420 format */
  386.         if (i2c_smbus_write_byte_data(s->i2c_client, 0xda, 0x00) < 0)
  387.             ret = -EPERM;
  388.  
  389.         if (i2c_smbus_write_byte_data(s->i2c_client, 0xd7, 0x1b) < 0)
  390.             ret = -EPERM;
  391.     } else {
  392.         pr_debug("format not supported\n");
  393.     }
  394.  
  395.     return ret;
  396. }
  397.  
  398. /* --------------- IOCTL functions from v4l2_int_ioctl_desc --------------- */
  399.  
  400. /*!
  401.  * ioctl_g_ifparm - V4L2 sensor interface handler for vidioc_int_g_ifparm_num
  402.  * s: pointer to standard V4L2 device structure
  403.  * p: pointer to standard V4L2 vidioc_int_g_ifparm_num ioctl structure
  404.  *
  405.  * Gets slave interface parameters.
  406.  * Calculates the required xclk value to support the requested
  407.  * clock parameters in p.  This value is returned in the p
  408.  * parameter.
  409.  *
  410.  * vidioc_int_g_ifparm returns platform-specific information about the
  411.  * interface settings used by the sensor.
  412.  *
  413.  * Given the image capture format in pix, the nominal frame period in
  414.  * timeperframe, calculate the required xclk frequency.
  415.  *
  416.  * Called on open.
  417.  */
  418. static int ioctl_g_ifparm(struct v4l2_int_device *s, struct v4l2_ifparm *p)
  419. {
  420.     pr_debug("In ov2640:ioctl_g_ifparm\n");
  421.  
  422.     if (s == NULL) {
  423.         pr_err("   ERROR!! no slave device set!\n");
  424.         return -1;
  425.     }
  426.  
  427.     memset(p, 0, sizeof(*p));
  428.     p->u.bt656.clock_curr = ov2640_data.mclk;
  429.     p->if_type = V4L2_IF_TYPE_BT656;
  430.     p->u.bt656.mode = V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT;
  431.     p->u.bt656.clock_min = OV2640_XCLK_MIN;
  432.     p->u.bt656.clock_max = OV2640_XCLK_MAX;
  433.  
  434.     return 0;
  435. }
  436.  
  437. /*!
  438.  * Sets the camera power.
  439.  *
  440.  * s  pointer to the camera device
  441.  * on if 1, power is to be turned on.  0 means power is to be turned off
  442.  *
  443.  * ioctl_s_power - V4L2 sensor interface handler for vidioc_int_s_power_num
  444.  * @s: pointer to standard V4L2 device structure
  445.  * @on: power state to which device is to be set
  446.  *
  447.  * Sets devices power state to requrested state, if possible.
  448.  * This is called on open, close, suspend and resume.
  449.  */
  450. static int ioctl_s_power(struct v4l2_int_device *s, int on)
  451. {
  452.     struct sensor *sensor = s->priv;
  453.  
  454.     pr_debug("In ov2640:ioctl_s_power\n");
  455.  
  456.     if (on && !sensor->on) {
  457.         if (io_regulator)
  458.             if (regulator_enable(io_regulator) != 0)
  459.                 return -EIO;
  460.         if (core_regulator)
  461.             if (regulator_enable(core_regulator) != 0)
  462.                 return -EIO;
  463.         if (gpo_regulator)
  464.             if (regulator_enable(gpo_regulator) != 0)
  465.                 return -EIO;
  466.         if (analog_regulator)
  467.             if (regulator_enable(analog_regulator) != 0)
  468.                 return -EIO;
  469.     } else if (!on && sensor->on) {
  470.         if (analog_regulator)
  471.             regulator_disable(analog_regulator);
  472.         if (core_regulator)
  473.             regulator_disable(core_regulator);
  474.         if (io_regulator)
  475.             regulator_disable(io_regulator);
  476.         if (gpo_regulator)
  477.             regulator_disable(gpo_regulator);
  478.     }
  479.  
  480.     sensor->on = on;
  481.  
  482.     return 0;
  483. }
  484.  
  485. /*!
  486.  * ioctl_g_parm - V4L2 sensor interface handler for VIDIOC_G_PARM ioctl
  487.  * @s: pointer to standard V4L2 device structure
  488.  * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
  489.  *
  490.  * Returns the sensor's video CAPTURE parameters.
  491.  */
  492. static int ioctl_g_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
  493. {
  494.     struct sensor *sensor = s->priv;
  495.     struct v4l2_captureparm *cparm = &a->parm.capture;
  496.     int ret = 0;
  497.  
  498.     pr_debug("In ov2640:ioctl_g_parm\n");
  499.  
  500.     switch (a->type) {
  501.     /* This is the only case currently handled. */
  502.     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  503.         pr_debug("   type is V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  504.         memset(a, 0, sizeof(*a));
  505.         a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  506.         cparm->capability = sensor->streamcap.capability;
  507.         cparm->timeperframe = sensor->streamcap.timeperframe;
  508.         cparm->capturemode = sensor->streamcap.capturemode;
  509.         ret = 0;
  510.         break;
  511.  
  512.     /* These are all the possible cases. */
  513.     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  514.     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  515.     case V4L2_BUF_TYPE_VBI_CAPTURE:
  516.     case V4L2_BUF_TYPE_VBI_OUTPUT:
  517.     case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  518.     case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  519.         pr_err("   type is not V4L2_BUF_TYPE_VIDEO_CAPTURE " \
  520.             "but %d\n", a->type);
  521.         ret = -EINVAL;
  522.         break;
  523.  
  524.     default:
  525.         pr_err("   type is unknown - %d\n", a->type);
  526.         ret = -EINVAL;
  527.         break;
  528.     }
  529.  
  530.     return ret;
  531. }
  532.  
  533. /*!
  534.  * ioctl_s_parm - V4L2 sensor interface handler for VIDIOC_S_PARM ioctl
  535.  * @s: pointer to standard V4L2 device structure
  536.  * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
  537.  *
  538.  * Configures the sensor to use the input parameters, if possible.  If
  539.  * not possible, reverts to the old parameters and returns the
  540.  * appropriate error code.
  541.  */
  542. static int ioctl_s_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
  543. {
  544.     struct sensor *sensor = s->priv;
  545.     struct v4l2_fract *timeperframe = &a->parm.capture.timeperframe;
  546.     u32 tgt_fps;    /* target frames per secound */
  547.     int ret = 0;
  548.  
  549.     pr_debug("In ov2640:ioctl_s_parm\n");
  550.  
  551.     switch (a->type) {
  552.     /* This is the only case currently handled. */
  553.     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  554.         pr_debug("   type is V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  555.  
  556.         /* Check that the new frame rate is allowed. */
  557.         if ((timeperframe->numerator == 0)
  558.             || (timeperframe->denominator == 0)) {
  559.             timeperframe->denominator = DEFAULT_FPS;
  560.             timeperframe->numerator = 1;
  561.         }
  562.         tgt_fps = timeperframe->denominator
  563.               / timeperframe->numerator;
  564.  
  565.         if (tgt_fps > MAX_FPS) {
  566.             timeperframe->denominator = MAX_FPS;
  567.             timeperframe->numerator = 1;
  568.         } else if (tgt_fps < MIN_FPS) {
  569.             timeperframe->denominator = MIN_FPS;
  570.             timeperframe->numerator = 1;
  571.         }
  572.         sensor->streamcap.timeperframe = *timeperframe;
  573.         sensor->streamcap.capturemode =
  574.                 (u32)a->parm.capture.capturemode;
  575.  
  576.         ret = ov2640_init_mode(sensor);
  577.         if (tgt_fps == 15)
  578.             ov2640_set_fps(sensor, tgt_fps);
  579.         break;
  580.  
  581.     /* These are all the possible cases. */
  582.     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  583.     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  584.     case V4L2_BUF_TYPE_VBI_CAPTURE:
  585.     case V4L2_BUF_TYPE_VBI_OUTPUT:
  586.     case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  587.     case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  588.         pr_err("   type is not V4L2_BUF_TYPE_VIDEO_CAPTURE " \
  589.             "but %d\n", a->type);
  590.         ret = -EINVAL;
  591.         break;
  592.  
  593.     default:
  594.         pr_err("   type is unknown - %d\n", a->type);
  595.         ret = -EINVAL;
  596.         break;
  597.     }
  598.  
  599.     return ret;
  600. }
  601.  
  602. /*!
  603.  * ioctl_s_fmt_cap - V4L2 sensor interface handler for ioctl_s_fmt_cap
  604.  *           set camera output format and resolution format
  605.  *
  606.  * @s: pointer to standard V4L2 device structure
  607.  * @arg: pointer to parameter, according this to set camera
  608.  *
  609.  * Returns 0 if set succeed, else return -1
  610.  */
  611. static int ioctl_s_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
  612. {
  613.     struct sensor *sensor = s->priv;
  614.     u32 format = f->fmt.pix.pixelformat;
  615.     int size = 0, ret = 0;
  616.  
  617.     size = f->fmt.pix.width * f->fmt.pix.height;
  618.     switch (format) {
  619.     case V4L2_PIX_FMT_RGB565:
  620.         if (size > 640 * 480)
  621.             sensor->streamcap.capturemode = V4L2_MODE_HIGHQUALITY;
  622.         else
  623.             sensor->streamcap.capturemode = 0;
  624.         ret = ov2640_init_mode(sensor);
  625.  
  626.         ret = ov2640_set_format(sensor, V4L2_PIX_FMT_RGB565);
  627.         break;
  628.     case V4L2_PIX_FMT_UYVY:
  629.         if (size > 640 * 480)
  630.             sensor->streamcap.capturemode = V4L2_MODE_HIGHQUALITY;
  631.         else
  632.             sensor->streamcap.capturemode = 0;
  633.         ret = ov2640_init_mode(sensor);
  634.         break;
  635.     case V4L2_PIX_FMT_YUV420:
  636.         if (size > 640 * 480)
  637.             sensor->streamcap.capturemode = V4L2_MODE_HIGHQUALITY;
  638.         else
  639.             sensor->streamcap.capturemode = 0;
  640.         ret = ov2640_init_mode(sensor);
  641.  
  642.         /* YUYV: width * 2, YY: width */
  643.         ret = ov2640_set_format(sensor, V4L2_PIX_FMT_YUV420);
  644.         break;
  645.     default:
  646.         pr_debug("case not supported\n");
  647.         break;
  648.     }
  649.  
  650.     return ret;
  651. }
  652.  
  653. /*!
  654.  * ioctl_g_fmt_cap - V4L2 sensor interface handler for ioctl_g_fmt_cap
  655.  * @s: pointer to standard V4L2 device structure
  656.  * @f: pointer to standard V4L2 v4l2_format structure
  657.  *
  658.  * Returns the sensor's current pixel format in the v4l2_format
  659.  * parameter.
  660.  */
  661. static int ioctl_g_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
  662. {
  663.     struct sensor *sensor = s->priv;
  664.  
  665.     pr_debug("In ov2640:ioctl_g_fmt_cap.\n");
  666.  
  667.     f->fmt.pix = sensor->pix;
  668.  
  669.     return 0;
  670. }
  671.  
  672. /*!
  673.  * ioctl_g_ctrl - V4L2 sensor interface handler for VIDIOC_G_CTRL ioctl
  674.  * @s: pointer to standard V4L2 device structure
  675.  * @vc: standard V4L2 VIDIOC_G_CTRL ioctl structure
  676.  *
  677.  * If the requested control is supported, returns the control's current
  678.  * value from the video_control[] array.  Otherwise, returns -EINVAL
  679.  * if the control is not supported.
  680.  */
  681. static int ioctl_g_ctrl(struct v4l2_int_device *s, struct v4l2_control *vc)
  682. {
  683.     int ret = 0;
  684.  
  685.     pr_debug("In ov2640:ioctl_g_ctrl\n");
  686.  
  687.     switch (vc->id) {
  688.     case V4L2_CID_BRIGHTNESS:
  689.         vc->value = ov2640_data.brightness;
  690.         break;
  691.     case V4L2_CID_HUE:
  692.         vc->value = ov2640_data.hue;
  693.         break;
  694.     case V4L2_CID_CONTRAST:
  695.         vc->value = ov2640_data.contrast;
  696.         break;
  697.     case V4L2_CID_SATURATION:
  698.         vc->value = ov2640_data.saturation;
  699.         break;
  700.     case V4L2_CID_RED_BALANCE:
  701.         vc->value = ov2640_data.red;
  702.         break;
  703.     case V4L2_CID_BLUE_BALANCE:
  704.         vc->value = ov2640_data.blue;
  705.         break;
  706.     case V4L2_CID_EXPOSURE:
  707.         vc->value = ov2640_data.ae_mode;
  708.         break;
  709.     default:
  710.         ret = -EINVAL;
  711.     }
  712.  
  713.     return ret;
  714. }
  715.  
  716. /*!
  717.  * ioctl_s_ctrl - V4L2 sensor interface handler for VIDIOC_S_CTRL ioctl
  718.  * @s: pointer to standard V4L2 device structure
  719.  * @vc: standard V4L2 VIDIOC_S_CTRL ioctl structure
  720.  *
  721.  * If the requested control is supported, sets the control's current
  722.  * value in HW (and updates the video_control[] array).  Otherwise,
  723.  * returns -EINVAL if the control is not supported.
  724.  */
  725. static int ioctl_s_ctrl(struct v4l2_int_device *s, struct v4l2_control *vc)
  726. {
  727.     int retval = 0;
  728.  
  729.     pr_debug("In ov2640:ioctl_s_ctrl %d\n", vc->id);
  730.  
  731.     switch (vc->id) {
  732.     case V4L2_CID_BRIGHTNESS:
  733.         pr_debug("   V4L2_CID_BRIGHTNESS\n");
  734.         break;
  735.     case V4L2_CID_CONTRAST:
  736.         pr_debug("   V4L2_CID_CONTRAST\n");
  737.         break;
  738.     case V4L2_CID_SATURATION:
  739.         pr_debug("   V4L2_CID_SATURATION\n");
  740.         break;
  741.     case V4L2_CID_HUE:
  742.         pr_debug("   V4L2_CID_HUE\n");
  743.         break;
  744.     case V4L2_CID_AUTO_WHITE_BALANCE:
  745.         pr_debug(
  746.             "   V4L2_CID_AUTO_WHITE_BALANCE\n");
  747.         break;
  748.     case V4L2_CID_DO_WHITE_BALANCE:
  749.         pr_debug(
  750.             "   V4L2_CID_DO_WHITE_BALANCE\n");
  751.         break;
  752.     case V4L2_CID_RED_BALANCE:
  753.         pr_debug("   V4L2_CID_RED_BALANCE\n");
  754.         break;
  755.     case V4L2_CID_BLUE_BALANCE:
  756.         pr_debug("   V4L2_CID_BLUE_BALANCE\n");
  757.         break;
  758.     case V4L2_CID_GAMMA:
  759.         pr_debug("   V4L2_CID_GAMMA\n");
  760.         break;
  761.     case V4L2_CID_EXPOSURE:
  762.         pr_debug("   V4L2_CID_EXPOSURE\n");
  763.         break;
  764.     case V4L2_CID_AUTOGAIN:
  765.         pr_debug("   V4L2_CID_AUTOGAIN\n");
  766.         break;
  767.     case V4L2_CID_GAIN:
  768.         pr_debug("   V4L2_CID_GAIN\n");
  769.         break;
  770.     case V4L2_CID_HFLIP:
  771.         pr_debug("   V4L2_CID_HFLIP\n");
  772.         break;
  773.     case V4L2_CID_VFLIP:
  774.         pr_debug("   V4L2_CID_VFLIP\n");
  775.         break;
  776.     default:
  777.         pr_debug("   Default case\n");
  778.         retval = -EPERM;
  779.         break;
  780.     }
  781.  
  782.     return retval;
  783. }
  784.  
  785. /*!
  786.  * ioctl_init - V4L2 sensor interface handler for VIDIOC_INT_INIT
  787.  * @s: pointer to standard V4L2 device structure
  788.  */
  789. static int ioctl_init(struct v4l2_int_device *s)
  790. {
  791.     pr_debug("In ov2640:ioctl_init\n");
  792.  
  793.     return 0;
  794. }
  795.  
  796. /*!
  797.  * ioctl_dev_init - V4L2 sensor interface handler for vidioc_int_dev_init_num
  798.  * @s: pointer to standard V4L2 device structure
  799.  *
  800.  * Initialise the device when slave attaches to the master.
  801.  */
  802. static int ioctl_dev_init(struct v4l2_int_device *s)
  803. {
  804.     struct sensor *sensor = s->priv;
  805.     u32 tgt_xclk;   /* target xclk */
  806.  
  807.     pr_debug("In ov2640:ioctl_dev_init\n");
  808.  
  809.     ov2640_data.on = true;
  810.  
  811.     tgt_xclk = ov2640_data.mclk;
  812.     tgt_xclk = min(tgt_xclk, (u32)OV2640_XCLK_MAX);
  813.     tgt_xclk = max(tgt_xclk, (u32)OV2640_XCLK_MIN);
  814.     ov2640_data.mclk = tgt_xclk;
  815.  
  816.     pr_debug("   Setting mclk to %d MHz\n",
  817.         tgt_xclk / 1000000);
  818.     //set_mclk_rate(&ov2640_data.mclk, 0);
  819.  
  820.     return ov2640_init_mode(sensor);
  821. }
  822.  
  823. /*!
  824.  * ioctl_dev_exit - V4L2 sensor interface handler for vidioc_int_dev_exit_num
  825.  * @s: pointer to standard V4L2 device structure
  826.  *
  827.  * Delinitialise the device when slave detaches to the master.
  828.  */
  829. static int ioctl_dev_exit(struct v4l2_int_device *s)
  830. {
  831.     pr_debug("In ov2640:ioctl_dev_exit\n");
  832.  
  833.     return 0;
  834. }
  835.  
  836.  
  837. /*!
  838.  * ioctl_enum_framesizes - V4L2 sensor interface handler for
  839.  *                         VIDIOC_ENUM_FRAMESIZES ioctl
  840.  * @s: pointer to standard V4L2 device structure
  841.  * @fsize: standard V4L2 VIDIOC_ENUM_FRAMESIZES ioctl structure
  842.  *
  843.  * Return 0 if successful, otherwise -EINVAL.
  844.  */
  845. static int ioctl_enum_framesizes(struct v4l2_int_device *s,
  846.                                  struct v4l2_frmsizeenum *fsize)
  847.     {
  848.         if (fsize->index > ov2640_mode_800_600_rgb)
  849.                 return -EINVAL;
  850.        
  851.     fsize->discrete.width   = ov2640_mode_width[fsize->index];
  852.     fsize->discrete.height  = ov2640_mode_height[fsize->index];
  853.     fsize->pixel_format = ov2640_mode_pixelformat[fsize->index];
  854.        
  855.         return 0;
  856.     }
  857.  
  858.       /*!
  859.  * ioctl_g_chip_ident - V4L2 sensor interface handler for
  860.  *                      VIDIOC_DBG_G_CHIP_IDENT ioctl
  861.  * @s: pointer to standard V4L2 device structure
  862.  * @id: pointer to int
  863.  *
  864.  * Return 0.
  865.  */
  866.   static int ioctl_g_chip_ident(struct v4l2_int_device *s, int *id)
  867. {
  868.         ((struct v4l2_dbg_chip_ident *)id)->match.type =
  869.                                         V4L2_CHIP_MATCH_I2C_DRIVER;
  870.         strcpy(((struct v4l2_dbg_chip_ident *)id)->match.name,
  871.                 "ov2640_camera");
  872.  
  873.         return 0;
  874. }                        
  875.  
  876. /*!
  877.  * This structure defines all the ioctls for this module and links them to the
  878.  * enumeration.
  879.  */
  880. static struct v4l2_int_ioctl_desc ov2640_ioctl_desc[] = {
  881.     {vidioc_int_dev_init_num, (v4l2_int_ioctl_func *)ioctl_dev_init},
  882.     {vidioc_int_dev_exit_num, (v4l2_int_ioctl_func *)ioctl_dev_exit},
  883.     {vidioc_int_s_power_num, (v4l2_int_ioctl_func *)ioctl_s_power},
  884.     {vidioc_int_g_ifparm_num, (v4l2_int_ioctl_func *)ioctl_g_ifparm},
  885. /*  {vidioc_int_g_needs_reset_num,
  886.                 (v4l2_int_ioctl_func *)ioctl_g_needs_reset}, */
  887. /*  {vidioc_int_reset_num, (v4l2_int_ioctl_func *)ioctl_reset}, */
  888.     {vidioc_int_init_num, (v4l2_int_ioctl_func *)ioctl_init},
  889. /*  {vidioc_int_enum_fmt_cap_num,
  890.                 (v4l2_int_ioctl_func *)ioctl_enum_fmt_cap}, */
  891. /*  {vidioc_int_try_fmt_cap_num,
  892.                 (v4l2_int_ioctl_func *)ioctl_try_fmt_cap}, */
  893.     {vidioc_int_g_fmt_cap_num, (v4l2_int_ioctl_func *)ioctl_g_fmt_cap},
  894.     {vidioc_int_s_fmt_cap_num, (v4l2_int_ioctl_func *)ioctl_s_fmt_cap},
  895.     {vidioc_int_g_parm_num, (v4l2_int_ioctl_func *)ioctl_g_parm},
  896.     {vidioc_int_s_parm_num, (v4l2_int_ioctl_func *)ioctl_s_parm},
  897. /*  {vidioc_int_queryctrl_num, (v4l2_int_ioctl_func *)ioctl_queryctrl}, */
  898.     {vidioc_int_g_ctrl_num, (v4l2_int_ioctl_func *)ioctl_g_ctrl},
  899.     {vidioc_int_s_ctrl_num, (v4l2_int_ioctl_func *)ioctl_s_ctrl},
  900.     {vidioc_int_enum_framesizes_num,
  901.             (v4l2_int_ioctl_func *) ioctl_enum_framesizes},
  902.     {vidioc_int_g_chip_ident_num,
  903.                 (v4l2_int_ioctl_func *) ioctl_g_chip_ident},
  904.     } ;
  905.  
  906. static struct v4l2_int_slave ov2640_slave = {
  907.     .ioctls = ov2640_ioctl_desc,
  908.     .num_ioctls = ARRAY_SIZE(ov2640_ioctl_desc),
  909. };
  910.  
  911. static struct v4l2_int_device ov2640_int_device = {
  912.     .module = THIS_MODULE,
  913.     .name = "ov2640",
  914.     .type = v4l2_int_type_slave,
  915.     .u = {
  916.         .slave = &ov2640_slave,
  917.         },
  918. };
  919.  
  920. /*!
  921.  * ov2640 I2C attach function
  922.  * Function set in i2c_driver struct.
  923.  * Called by insmod ov2640_camera.ko.
  924.  *
  925.  * @param client            struct i2c_client*
  926.  * @return  Error code indicating success or failure
  927.  */
  928. static int ov2640_probe(struct i2c_client *client,
  929.             const struct i2c_device_id *id)
  930. {
  931.     int retval;
  932.     s32 pid_hi, pid_low;
  933.     struct fsl_mxc_camera_platform_data *plat_data = client->dev.platform_data;
  934.  
  935.     pr_debug("In ov2640_probe (RH_BT565)\n");
  936.  
  937.     /* Set initial values for the sensor struct. */
  938.     memset(&ov2640_data, 0, sizeof(ov2640_data));
  939.     ov2640_data.i2c_client = client;
  940.     ov2640_data.mclk = 24000000;
  941.     ov2640_data.mclk = plat_data->mclk;
  942.     ov2640_data.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  943.     ov2640_data.pix.width = 800;
  944.     ov2640_data.pix.height = 600;
  945.     ov2640_data.streamcap.capability = V4L2_MODE_HIGHQUALITY
  946.                        | V4L2_CAP_TIMEPERFRAME;
  947.     ov2640_data.streamcap.capturemode = 0;
  948.     ov2640_data.streamcap.timeperframe.denominator = DEFAULT_FPS;
  949.     ov2640_data.streamcap.timeperframe.numerator = 1;
  950.  
  951.         if (plat_data->io_init)
  952.             plat_data->io_init();
  953.                        
  954.         pid_hi = i2c_smbus_read_byte_data(client, PID_H);
  955.         pid_low = i2c_smbus_read_byte_data(client, PID_L);
  956.         pr_err("Read Id: 0x%02x%02x", pid_hi, pid_low);    
  957.  
  958.     if (pid_hi != 0x26)
  959.         pr_err("chip not found\n");
  960.     else
  961.         pr_err("chip found\n");    
  962.                    
  963.     if (plat_data->io_regulator) {
  964.         io_regulator =
  965.             regulator_get(&client->dev, plat_data->io_regulator);
  966.         if (!IS_ERR(io_regulator)) {
  967.             regulator_set_voltage(io_regulator, 2800000, 2800000);
  968.             if (regulator_enable(io_regulator) != 0) {
  969.                 pr_err("%s:io set voltage error\n", __func__);
  970.                 goto err1;
  971.             } else {
  972.                 dev_dbg(&client->dev,
  973.                     "%s:io set voltage ok\n", __func__);
  974.             }
  975.         } else
  976.             io_regulator = NULL;
  977.     }
  978.  
  979.     if (plat_data->core_regulator) {
  980.         core_regulator =
  981.             regulator_get(&client->dev, plat_data->core_regulator);
  982.         if (!IS_ERR(core_regulator)) {
  983.             regulator_set_voltage(core_regulator,
  984.                      1300000, 1300000);
  985.             if (regulator_enable(core_regulator) != 0) {
  986.                 pr_err("%s:core set voltage error\n", __func__);
  987.                 goto err2;
  988.             } else {
  989.                 dev_dbg(&client->dev,
  990.                     "%s:core set voltage ok\n", __func__);
  991.             }
  992.         } else
  993.             core_regulator = NULL;
  994.     }
  995.  
  996.     if (plat_data->analog_regulator) {
  997.         analog_regulator =
  998.             regulator_get(&client->dev, plat_data->analog_regulator);
  999.         if (!IS_ERR(analog_regulator)) {
  1000.             regulator_set_voltage(analog_regulator, 2000000, 2000000);
  1001.             if (regulator_enable(analog_regulator) != 0) {
  1002.                 pr_err("%s:analog set voltage error\n",
  1003.                      __func__);
  1004.                 goto err3;
  1005.             } else {
  1006.                 dev_dbg(&client->dev,
  1007.                     "%s:analog set voltage ok\n", __func__);
  1008.             }
  1009.         } else
  1010.             analog_regulator = NULL;
  1011.     }
  1012.  
  1013.     if (plat_data->gpo_regulator) {
  1014.         gpo_regulator =
  1015.             regulator_get(&client->dev, plat_data->gpo_regulator);
  1016.         if (!IS_ERR(gpo_regulator)) {
  1017.             if (regulator_enable(gpo_regulator) != 0) {
  1018.                 pr_err("%s:gpo3 set voltage error\n", __func__);
  1019.                 goto err4;
  1020.             } else {
  1021.                 dev_dbg(&client->dev,
  1022.                     "%s:gpo3 set voltage ok\n", __func__);
  1023.             }
  1024.         } else
  1025.             gpo_regulator = NULL;
  1026.     }
  1027.  
  1028.     /* This function attaches this structure to the /dev/video0 device.
  1029.      * The pointer in priv points to the ov2640_data structure here.*/
  1030.     ov2640_int_device.priv = &ov2640_data;
  1031.     retval = v4l2_int_device_register(&ov2640_int_device);
  1032.  
  1033.     return retval;
  1034.  
  1035. err4:
  1036.     if (analog_regulator) {
  1037.         regulator_disable(analog_regulator);
  1038.         regulator_put(analog_regulator);
  1039.     }
  1040. err3:
  1041.     if (core_regulator) {
  1042.         regulator_disable(core_regulator);
  1043.         regulator_put(core_regulator);
  1044.     }
  1045. err2:
  1046.     if (io_regulator) {
  1047.         regulator_disable(io_regulator);
  1048.         regulator_put(io_regulator);
  1049.     }
  1050. err1:
  1051.     return -1;
  1052. }
  1053.  
  1054. /*!
  1055.  * ov2640 I2C detach function
  1056.  * Called on rmmod ov2640_camera.ko
  1057.  *
  1058.  * @param client            struct i2c_client*
  1059.  * @return  Error code indicating success or failure
  1060.  */
  1061. static int ov2640_remove(struct i2c_client *client)
  1062. {
  1063.     pr_debug("In ov2640_remove\n");
  1064.  
  1065.     v4l2_int_device_unregister(&ov2640_int_device);
  1066.  
  1067.     if (gpo_regulator) {
  1068.         regulator_disable(gpo_regulator);
  1069.         regulator_put(gpo_regulator);
  1070.     }
  1071.  
  1072.     if (analog_regulator) {
  1073.         regulator_disable(analog_regulator);
  1074.         regulator_put(analog_regulator);
  1075.     }
  1076.  
  1077.     if (core_regulator) {
  1078.         regulator_disable(core_regulator);
  1079.         regulator_put(core_regulator);
  1080.     }
  1081.  
  1082.     if (io_regulator) {
  1083.         regulator_disable(io_regulator);
  1084.         regulator_put(io_regulator);
  1085.     }
  1086.  
  1087.     return 0;
  1088. }
  1089.  
  1090. static const struct i2c_device_id ov2640_id[] = {
  1091.     {"ov2640", 0},
  1092.     {},
  1093. };
  1094.  
  1095. MODULE_DEVICE_TABLE(i2c, ov2640_id);
  1096.  
  1097. static struct i2c_driver ov2640_i2c_driver = {
  1098.     .driver = {
  1099.            .owner = THIS_MODULE,
  1100.            .name = "ov2640",
  1101.           },
  1102.     .probe = ov2640_probe,
  1103.     .remove = ov2640_remove,
  1104.     .id_table = ov2640_id,
  1105. /* To add power management add .suspend and .resume functions */
  1106. };
  1107.  
  1108. /*!
  1109.  * ov2640 init function
  1110.  * Called by insmod ov2640_camera.ko.
  1111.  *
  1112.  * @return  Error code indicating success or failure
  1113.  */
  1114. static __init int ov2640_init(void)
  1115. {
  1116.     u8 err;
  1117.  
  1118.     pr_debug("In ov2640_init\n");
  1119.  
  1120.     err = i2c_add_driver(&ov2640_i2c_driver);
  1121.     if (err != 0)
  1122.         pr_err("%s:driver registration failed, error=%d \n",
  1123.             __func__, err);
  1124.  
  1125.     return err;
  1126. }
  1127.  
  1128. /*!
  1129.  * OV2640 cleanup function
  1130.  * Called on rmmod ov2640_camera.ko
  1131.  *
  1132.  * @return  Error code indicating success or failure
  1133.  */
  1134. static void __exit ov2640_clean(void)
  1135. {
  1136.     pr_debug("In ov2640_clean\n");
  1137.     i2c_del_driver(&ov2640_i2c_driver);
  1138. }
  1139.  
  1140. module_init(ov2640_init);
  1141. module_exit(ov2640_clean);
  1142.  
  1143. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1144. MODULE_DESCRIPTION("OV2640 Camera Driver");
  1145. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement