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