ov7640.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005-2006 Micronas USA Inc.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/i2c.h>
  8. #include <linux/videodev2.h>
  9. #include <media/v4l2-device.h>
  10. #include <linux/slab.h>
  11. MODULE_DESCRIPTION("OmniVision ov7640 sensor driver");
  12. MODULE_LICENSE("GPL v2");
  13. struct reg_val {
  14. u8 reg;
  15. u8 val;
  16. };
  17. static const struct reg_val regval_init[] = {
  18. {0x12, 0x80},
  19. {0x12, 0x54},
  20. {0x14, 0x24},
  21. {0x15, 0x01},
  22. {0x28, 0x20},
  23. {0x75, 0x82},
  24. };
  25. static int write_regs(struct i2c_client *client,
  26. const struct reg_val *rv, int len)
  27. {
  28. while (--len >= 0) {
  29. if (i2c_smbus_write_byte_data(client, rv->reg, rv->val) < 0)
  30. return -1;
  31. rv++;
  32. }
  33. return 0;
  34. }
  35. /* ----------------------------------------------------------------------- */
  36. static const struct v4l2_subdev_ops ov7640_ops;
  37. static int ov7640_probe(struct i2c_client *client)
  38. {
  39. struct i2c_adapter *adapter = client->adapter;
  40. struct v4l2_subdev *sd;
  41. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  42. return -ENODEV;
  43. sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
  44. if (sd == NULL)
  45. return -ENOMEM;
  46. v4l2_i2c_subdev_init(sd, client, &ov7640_ops);
  47. client->flags = I2C_CLIENT_SCCB;
  48. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  49. client->addr << 1, client->adapter->name);
  50. if (write_regs(client, regval_init, ARRAY_SIZE(regval_init)) < 0) {
  51. v4l_err(client, "error initializing OV7640\n");
  52. return -ENODEV;
  53. }
  54. return 0;
  55. }
  56. static void ov7640_remove(struct i2c_client *client)
  57. {
  58. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  59. v4l2_device_unregister_subdev(sd);
  60. }
  61. static const struct i2c_device_id ov7640_id[] = {
  62. { "ov7640" },
  63. { }
  64. };
  65. MODULE_DEVICE_TABLE(i2c, ov7640_id);
  66. static struct i2c_driver ov7640_driver = {
  67. .driver = {
  68. .name = "ov7640",
  69. },
  70. .probe = ov7640_probe,
  71. .remove = ov7640_remove,
  72. .id_table = ov7640_id,
  73. };
  74. module_i2c_driver(ov7640_driver);