ov7640.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. const struct i2c_device_id *id)
  39. {
  40. struct i2c_adapter *adapter = client->adapter;
  41. struct v4l2_subdev *sd;
  42. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  43. return -ENODEV;
  44. sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
  45. if (sd == NULL)
  46. return -ENOMEM;
  47. v4l2_i2c_subdev_init(sd, client, &ov7640_ops);
  48. client->flags = I2C_CLIENT_SCCB;
  49. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  50. client->addr << 1, client->adapter->name);
  51. if (write_regs(client, regval_init, ARRAY_SIZE(regval_init)) < 0) {
  52. v4l_err(client, "error initializing OV7640\n");
  53. return -ENODEV;
  54. }
  55. return 0;
  56. }
  57. static void ov7640_remove(struct i2c_client *client)
  58. {
  59. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  60. v4l2_device_unregister_subdev(sd);
  61. }
  62. static const struct i2c_device_id ov7640_id[] = {
  63. { "ov7640", 0 },
  64. { }
  65. };
  66. MODULE_DEVICE_TABLE(i2c, ov7640_id);
  67. static struct i2c_driver ov7640_driver = {
  68. .driver = {
  69. .name = "ov7640",
  70. },
  71. .probe = ov7640_probe,
  72. .remove = ov7640_remove,
  73. .id_table = ov7640_id,
  74. };
  75. module_i2c_driver(ov7640_driver);