ov7640.c 2.3 KB

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