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