18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cirrus Logic cs3308 8-Channel Analog Volume Control
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2012 Steven Toth <stoth@kernellabs.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Derived from cs5345.c Copyright (C) 2007 Hans Verkuil
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/i2c.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
178c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("i2c device driver for cs3308 8-channel volume control");
208c2ecf20Sopenharmony_ciMODULE_AUTHOR("Devin Heitmueller");
218c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic inline int cs3308_write(struct v4l2_subdev *sd, u8 reg, u8 value)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client, reg, value);
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic inline int cs3308_read(struct v4l2_subdev *sd, u8 reg)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	return i2c_smbus_read_byte_data(client, reg);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
388c2ecf20Sopenharmony_cistatic int cs3308_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	reg->val = cs3308_read(sd, reg->reg & 0xffff);
418c2ecf20Sopenharmony_ci	reg->size = 1;
428c2ecf20Sopenharmony_ci	return 0;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int cs3308_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	cs3308_write(sd, reg->reg & 0xffff, reg->val & 0xff);
488c2ecf20Sopenharmony_ci	return 0;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci#endif
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops cs3308_core_ops = {
558c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
568c2ecf20Sopenharmony_ci	.g_register = cs3308_g_register,
578c2ecf20Sopenharmony_ci	.s_register = cs3308_s_register,
588c2ecf20Sopenharmony_ci#endif
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops cs3308_ops = {
628c2ecf20Sopenharmony_ci	.core = &cs3308_core_ops,
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int cs3308_probe(struct i2c_client *client,
688c2ecf20Sopenharmony_ci			const struct i2c_device_id *id)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
718c2ecf20Sopenharmony_ci	unsigned i;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* Check if the adapter supports the needed features */
748c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
758c2ecf20Sopenharmony_ci		return -EIO;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if ((i2c_smbus_read_byte_data(client, 0x1c) & 0xf0) != 0xe0)
788c2ecf20Sopenharmony_ci		return -ENODEV;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	v4l_info(client, "chip found @ 0x%x (%s)\n",
818c2ecf20Sopenharmony_ci		 client->addr << 1, client->adapter->name);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	sd = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
848c2ecf20Sopenharmony_ci	if (sd == NULL)
858c2ecf20Sopenharmony_ci		return -ENOMEM;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &cs3308_ops);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* Set some reasonable defaults */
908c2ecf20Sopenharmony_ci	cs3308_write(sd, 0x0d, 0x00); /* Power up all channels */
918c2ecf20Sopenharmony_ci	cs3308_write(sd, 0x0e, 0x00); /* Master Power */
928c2ecf20Sopenharmony_ci	cs3308_write(sd, 0x0b, 0x00); /* Device Configuration */
938c2ecf20Sopenharmony_ci	/* Set volume for each channel */
948c2ecf20Sopenharmony_ci	for (i = 1; i <= 8; i++)
958c2ecf20Sopenharmony_ci		cs3308_write(sd, i, 0xd2);
968c2ecf20Sopenharmony_ci	cs3308_write(sd, 0x0a, 0x00); /* Unmute all channels */
978c2ecf20Sopenharmony_ci	return 0;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int cs3308_remove(struct i2c_client *client)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
1078c2ecf20Sopenharmony_ci	kfree(sd);
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic const struct i2c_device_id cs3308_id[] = {
1148c2ecf20Sopenharmony_ci	{ "cs3308", 0 },
1158c2ecf20Sopenharmony_ci	{ }
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, cs3308_id);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic struct i2c_driver cs3308_driver = {
1208c2ecf20Sopenharmony_ci	.driver = {
1218c2ecf20Sopenharmony_ci		.name   = "cs3308",
1228c2ecf20Sopenharmony_ci	},
1238c2ecf20Sopenharmony_ci	.probe          = cs3308_probe,
1248c2ecf20Sopenharmony_ci	.remove         = cs3308_remove,
1258c2ecf20Sopenharmony_ci	.id_table       = cs3308_id,
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cimodule_i2c_driver(cs3308_driver);
129