Merge tag 'for-6.5-rc5-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-2.6-microblaze.git] / drivers / media / i2c / tea6420.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2  /*
3     tea6420 - i2c-driver for the tea6420 by SGS Thomson
4
5     Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
6     Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
7
8     The tea6420 is a bus controlled audio-matrix with 5 stereo inputs,
9     4 stereo outputs and gain control for each output.
10     It is cascadable, i.e. it can be found at the addresses 0x98
11     and 0x9a on the i2c-bus.
12
13     For detailed information download the specifications directly
14     from SGS Thomson at http://www.st.com
15
16   */
17
18
19 #include <linux/module.h>
20 #include <linux/ioctl.h>
21 #include <linux/slab.h>
22 #include <linux/i2c.h>
23 #include <media/v4l2-device.h>
24 #include "tea6420.h"
25
26 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
27 MODULE_DESCRIPTION("tea6420 driver");
28 MODULE_LICENSE("GPL");
29
30 static int debug;
31 module_param(debug, int, 0644);
32
33 MODULE_PARM_DESC(debug, "Debug level (0-1)");
34
35
36 /* make a connection between the input 'i' and the output 'o'
37    with gain 'g' (note: i = 6 means 'mute') */
38 static int tea6420_s_routing(struct v4l2_subdev *sd,
39                              u32 i, u32 o, u32 config)
40 {
41         struct i2c_client *client = v4l2_get_subdevdata(sd);
42         int g = (o >> 4) & 0xf;
43         u8 byte;
44         int ret;
45
46         o &= 0xf;
47         v4l2_dbg(1, debug, sd, "i=%d, o=%d, g=%d\n", i, o, g);
48
49         /* check if the parameters are valid */
50         if (i < 1 || i > 6 || o < 1 || o > 4 || g < 0 || g > 6 || g % 2 != 0)
51                 return -EINVAL;
52
53         byte = ((o - 1) << 5);
54         byte |= (i - 1);
55
56         /* to understand this, have a look at the tea6420-specs (p.5) */
57         switch (g) {
58         case 0:
59                 byte |= (3 << 3);
60                 break;
61         case 2:
62                 byte |= (2 << 3);
63                 break;
64         case 4:
65                 byte |= (1 << 3);
66                 break;
67         case 6:
68                 break;
69         }
70
71         ret = i2c_smbus_write_byte(client, byte);
72         if (ret) {
73                 v4l2_dbg(1, debug, sd,
74                         "i2c_smbus_write_byte() failed, ret:%d\n", ret);
75                 return -EIO;
76         }
77         return 0;
78 }
79
80 /* ----------------------------------------------------------------------- */
81
82 static const struct v4l2_subdev_audio_ops tea6420_audio_ops = {
83         .s_routing = tea6420_s_routing,
84 };
85
86 static const struct v4l2_subdev_ops tea6420_ops = {
87         .audio = &tea6420_audio_ops,
88 };
89
90 static int tea6420_probe(struct i2c_client *client)
91 {
92         struct v4l2_subdev *sd;
93         int err, i;
94
95         /* let's see whether this adapter can support what we need */
96         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE))
97                 return -EIO;
98
99         v4l_info(client, "chip found @ 0x%x (%s)\n",
100                         client->addr << 1, client->adapter->name);
101
102         sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
103         if (sd == NULL)
104                 return -ENOMEM;
105         v4l2_i2c_subdev_init(sd, client, &tea6420_ops);
106
107         /* set initial values: set "mute"-input to all outputs at gain 0 */
108         err = 0;
109         for (i = 1; i < 5; i++)
110                 err += tea6420_s_routing(sd, 6, i, 0);
111         if (err) {
112                 v4l_dbg(1, debug, client, "could not initialize tea6420\n");
113                 return -ENODEV;
114         }
115         return 0;
116 }
117
118 static void tea6420_remove(struct i2c_client *client)
119 {
120         struct v4l2_subdev *sd = i2c_get_clientdata(client);
121
122         v4l2_device_unregister_subdev(sd);
123 }
124
125 static const struct i2c_device_id tea6420_id[] = {
126         { "tea6420", 0 },
127         { }
128 };
129 MODULE_DEVICE_TABLE(i2c, tea6420_id);
130
131 static struct i2c_driver tea6420_driver = {
132         .driver = {
133                 .name   = "tea6420",
134         },
135         .probe          = tea6420_probe,
136         .remove         = tea6420_remove,
137         .id_table       = tea6420_id,
138 };
139
140 module_i2c_driver(tea6420_driver);