xref: /openbmc/linux/drivers/media/i2c/cs3308.c (revision 8ffdff6a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Cirrus Logic cs3308 8-Channel Analog Volume Control
4  *
5  * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
6  * Copyright (C) 2012 Steven Toth <stoth@kernellabs.com>
7  *
8  * Derived from cs5345.c Copyright (C) 2007 Hans Verkuil
9  */
10 
11 
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/i2c.h>
15 #include <linux/slab.h>
16 #include <linux/videodev2.h>
17 #include <media/v4l2-device.h>
18 
19 MODULE_DESCRIPTION("i2c device driver for cs3308 8-channel volume control");
20 MODULE_AUTHOR("Devin Heitmueller");
21 MODULE_LICENSE("GPL");
22 
23 static inline int cs3308_write(struct v4l2_subdev *sd, u8 reg, u8 value)
24 {
25 	struct i2c_client *client = v4l2_get_subdevdata(sd);
26 
27 	return i2c_smbus_write_byte_data(client, reg, value);
28 }
29 
30 static inline int cs3308_read(struct v4l2_subdev *sd, u8 reg)
31 {
32 	struct i2c_client *client = v4l2_get_subdevdata(sd);
33 
34 	return i2c_smbus_read_byte_data(client, reg);
35 }
36 
37 #ifdef CONFIG_VIDEO_ADV_DEBUG
38 static int cs3308_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
39 {
40 	reg->val = cs3308_read(sd, reg->reg & 0xffff);
41 	reg->size = 1;
42 	return 0;
43 }
44 
45 static int cs3308_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
46 {
47 	cs3308_write(sd, reg->reg & 0xffff, reg->val & 0xff);
48 	return 0;
49 }
50 #endif
51 
52 /* ----------------------------------------------------------------------- */
53 
54 static const struct v4l2_subdev_core_ops cs3308_core_ops = {
55 #ifdef CONFIG_VIDEO_ADV_DEBUG
56 	.g_register = cs3308_g_register,
57 	.s_register = cs3308_s_register,
58 #endif
59 };
60 
61 static const struct v4l2_subdev_ops cs3308_ops = {
62 	.core = &cs3308_core_ops,
63 };
64 
65 /* ----------------------------------------------------------------------- */
66 
67 static int cs3308_probe(struct i2c_client *client,
68 			const struct i2c_device_id *id)
69 {
70 	struct v4l2_subdev *sd;
71 	unsigned i;
72 
73 	/* Check if the adapter supports the needed features */
74 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
75 		return -EIO;
76 
77 	if ((i2c_smbus_read_byte_data(client, 0x1c) & 0xf0) != 0xe0)
78 		return -ENODEV;
79 
80 	v4l_info(client, "chip found @ 0x%x (%s)\n",
81 		 client->addr << 1, client->adapter->name);
82 
83 	sd = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
84 	if (sd == NULL)
85 		return -ENOMEM;
86 
87 	v4l2_i2c_subdev_init(sd, client, &cs3308_ops);
88 
89 	/* Set some reasonable defaults */
90 	cs3308_write(sd, 0x0d, 0x00); /* Power up all channels */
91 	cs3308_write(sd, 0x0e, 0x00); /* Master Power */
92 	cs3308_write(sd, 0x0b, 0x00); /* Device Configuration */
93 	/* Set volume for each channel */
94 	for (i = 1; i <= 8; i++)
95 		cs3308_write(sd, i, 0xd2);
96 	cs3308_write(sd, 0x0a, 0x00); /* Unmute all channels */
97 	return 0;
98 }
99 
100 /* ----------------------------------------------------------------------- */
101 
102 static int cs3308_remove(struct i2c_client *client)
103 {
104 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
105 
106 	v4l2_device_unregister_subdev(sd);
107 	kfree(sd);
108 	return 0;
109 }
110 
111 /* ----------------------------------------------------------------------- */
112 
113 static const struct i2c_device_id cs3308_id[] = {
114 	{ "cs3308", 0 },
115 	{ }
116 };
117 MODULE_DEVICE_TABLE(i2c, cs3308_id);
118 
119 static struct i2c_driver cs3308_driver = {
120 	.driver = {
121 		.name   = "cs3308",
122 	},
123 	.probe          = cs3308_probe,
124 	.remove         = cs3308_remove,
125 	.id_table       = cs3308_id,
126 };
127 
128 module_i2c_driver(cs3308_driver);
129