ov7640.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <linux/slab.h>
  23. MODULE_DESCRIPTION("OmniVision ov7640 sensor driver");
  24. MODULE_LICENSE("GPL v2");
  25. static const u8 initial_registers[] = {
  26. 0x12, 0x80,
  27. 0x12, 0x54,
  28. 0x14, 0x24,
  29. 0x15, 0x01,
  30. 0x28, 0x20,
  31. 0x75, 0x82,
  32. 0xFF, 0xFF, /* Terminator (reg 0xFF is unused) */
  33. };
  34. static int write_regs(struct i2c_client *client, const u8 *regs)
  35. {
  36. int i;
  37. for (i = 0; regs[i] != 0xFF; i += 2)
  38. if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
  39. return -1;
  40. return 0;
  41. }
  42. /* ----------------------------------------------------------------------- */
  43. static const struct v4l2_subdev_ops ov7640_ops;
  44. static int ov7640_probe(struct i2c_client *client,
  45. const struct i2c_device_id *id)
  46. {
  47. struct i2c_adapter *adapter = client->adapter;
  48. struct v4l2_subdev *sd;
  49. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  50. return -ENODEV;
  51. sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
  52. if (sd == NULL)
  53. return -ENOMEM;
  54. v4l2_i2c_subdev_init(sd, client, &ov7640_ops);
  55. client->flags = I2C_CLIENT_SCCB;
  56. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  57. client->addr << 1, client->adapter->name);
  58. if (write_regs(client, initial_registers) < 0) {
  59. v4l_err(client, "error initializing OV7640\n");
  60. return -ENODEV;
  61. }
  62. return 0;
  63. }
  64. static int ov7640_remove(struct i2c_client *client)
  65. {
  66. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  67. v4l2_device_unregister_subdev(sd);
  68. return 0;
  69. }
  70. static const struct i2c_device_id ov7640_id[] = {
  71. { "ov7640", 0 },
  72. { }
  73. };
  74. MODULE_DEVICE_TABLE(i2c, ov7640_id);
  75. static struct i2c_driver ov7640_driver = {
  76. .driver = {
  77. .name = "ov7640",
  78. },
  79. .probe = ov7640_probe,
  80. .remove = ov7640_remove,
  81. .id_table = ov7640_id,
  82. };
  83. module_i2c_driver(ov7640_driver);