ov7640.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2005-2006 Micronas USA Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software Foundation,
  15. * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/videodev2.h>
  21. #include <media/v4l2-device.h>
  22. #include <media/v4l2-chip-ident.h>
  23. #include <linux/slab.h>
  24. MODULE_DESCRIPTION("OmniVision ov7640 sensor driver");
  25. MODULE_LICENSE("GPL v2");
  26. static const u8 initial_registers[] = {
  27. 0x12, 0x80,
  28. 0x12, 0x54,
  29. 0x14, 0x24,
  30. 0x15, 0x01,
  31. 0x28, 0x20,
  32. 0x75, 0x82,
  33. 0xFF, 0xFF, /* Terminator (reg 0xFF is unused) */
  34. };
  35. static int write_regs(struct i2c_client *client, const u8 *regs)
  36. {
  37. int i;
  38. for (i = 0; regs[i] != 0xFF; i += 2)
  39. if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
  40. return -1;
  41. return 0;
  42. }
  43. /* ----------------------------------------------------------------------- */
  44. static const struct v4l2_subdev_ops ov7640_ops;
  45. static int ov7640_probe(struct i2c_client *client,
  46. const struct i2c_device_id *id)
  47. {
  48. struct i2c_adapter *adapter = client->adapter;
  49. struct v4l2_subdev *sd;
  50. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  51. return -ENODEV;
  52. sd = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
  53. if (sd == NULL)
  54. return -ENOMEM;
  55. v4l2_i2c_subdev_init(sd, client, &ov7640_ops);
  56. client->flags = I2C_CLIENT_SCCB;
  57. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  58. client->addr << 1, client->adapter->name);
  59. if (write_regs(client, initial_registers) < 0) {
  60. v4l_err(client, "error initializing OV7640\n");
  61. kfree(sd);
  62. return -ENODEV;
  63. }
  64. return 0;
  65. }
  66. static int ov7640_remove(struct i2c_client *client)
  67. {
  68. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  69. v4l2_device_unregister_subdev(sd);
  70. kfree(sd);
  71. return 0;
  72. }
  73. static const struct i2c_device_id ov7640_id[] = {
  74. { "ov7640", 0 },
  75. { }
  76. };
  77. MODULE_DEVICE_TABLE(i2c, ov7640_id);
  78. static struct i2c_driver ov7640_driver = {
  79. .driver = {
  80. .owner = THIS_MODULE,
  81. .name = "ov7640",
  82. },
  83. .probe = ov7640_probe,
  84. .remove = ov7640_remove,
  85. .id_table = ov7640_id,
  86. };
  87. module_i2c_driver(ov7640_driver);