xref: /openbmc/linux/drivers/media/i2c/tea6420.c (revision ac73d4bf)
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 			  const struct i2c_device_id *id)
92 {
93 	struct v4l2_subdev *sd;
94 	int err, i;
95 
96 	/* let's see whether this adapter can support what we need */
97 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE))
98 		return -EIO;
99 
100 	v4l_info(client, "chip found @ 0x%x (%s)\n",
101 			client->addr << 1, client->adapter->name);
102 
103 	sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
104 	if (sd == NULL)
105 		return -ENOMEM;
106 	v4l2_i2c_subdev_init(sd, client, &tea6420_ops);
107 
108 	/* set initial values: set "mute"-input to all outputs at gain 0 */
109 	err = 0;
110 	for (i = 1; i < 5; i++)
111 		err += tea6420_s_routing(sd, 6, i, 0);
112 	if (err) {
113 		v4l_dbg(1, debug, client, "could not initialize tea6420\n");
114 		return -ENODEV;
115 	}
116 	return 0;
117 }
118 
119 static void tea6420_remove(struct i2c_client *client)
120 {
121 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
122 
123 	v4l2_device_unregister_subdev(sd);
124 }
125 
126 static const struct i2c_device_id tea6420_id[] = {
127 	{ "tea6420", 0 },
128 	{ }
129 };
130 MODULE_DEVICE_TABLE(i2c, tea6420_id);
131 
132 static struct i2c_driver tea6420_driver = {
133 	.driver = {
134 		.name	= "tea6420",
135 	},
136 	.probe		= tea6420_probe,
137 	.remove		= tea6420_remove,
138 	.id_table	= tea6420_id,
139 };
140 
141 module_i2c_driver(tea6420_driver);
142