18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2005-2006 Micronas USA Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/init.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/i2c.h>
98c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
108c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("OmniVision ov7640 sensor driver");
148c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic const u8 initial_registers[] = {
178c2ecf20Sopenharmony_ci	0x12, 0x80,
188c2ecf20Sopenharmony_ci	0x12, 0x54,
198c2ecf20Sopenharmony_ci	0x14, 0x24,
208c2ecf20Sopenharmony_ci	0x15, 0x01,
218c2ecf20Sopenharmony_ci	0x28, 0x20,
228c2ecf20Sopenharmony_ci	0x75, 0x82,
238c2ecf20Sopenharmony_ci	0xFF, 0xFF, /* Terminator (reg 0xFF is unused) */
248c2ecf20Sopenharmony_ci};
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic int write_regs(struct i2c_client *client, const u8 *regs)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	int i;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	for (i = 0; regs[i] != 0xFF; i += 2)
318c2ecf20Sopenharmony_ci		if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
328c2ecf20Sopenharmony_ci			return -1;
338c2ecf20Sopenharmony_ci	return 0;
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops ov7640_ops;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int ov7640_probe(struct i2c_client *client,
418c2ecf20Sopenharmony_ci			const struct i2c_device_id *id)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
448c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
478c2ecf20Sopenharmony_ci		return -ENODEV;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
508c2ecf20Sopenharmony_ci	if (sd == NULL)
518c2ecf20Sopenharmony_ci		return -ENOMEM;
528c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &ov7640_ops);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	client->flags = I2C_CLIENT_SCCB;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	v4l_info(client, "chip found @ 0x%02x (%s)\n",
578c2ecf20Sopenharmony_ci			client->addr << 1, client->adapter->name);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (write_regs(client, initial_registers) < 0) {
608c2ecf20Sopenharmony_ci		v4l_err(client, "error initializing OV7640\n");
618c2ecf20Sopenharmony_ci		return -ENODEV;
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	return 0;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int ov7640_remove(struct i2c_client *client)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	return 0;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic const struct i2c_device_id ov7640_id[] = {
788c2ecf20Sopenharmony_ci	{ "ov7640", 0 },
798c2ecf20Sopenharmony_ci	{ }
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ov7640_id);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic struct i2c_driver ov7640_driver = {
848c2ecf20Sopenharmony_ci	.driver = {
858c2ecf20Sopenharmony_ci		.name	= "ov7640",
868c2ecf20Sopenharmony_ci	},
878c2ecf20Sopenharmony_ci	.probe = ov7640_probe,
888c2ecf20Sopenharmony_ci	.remove = ov7640_remove,
898c2ecf20Sopenharmony_ci	.id_table = ov7640_id,
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_cimodule_i2c_driver(ov7640_driver);
92