Merge tag 'bcachefs-2024-10-05' of git://evilpiepirate.org/bcachefs
[linux-2.6-block.git] / drivers / media / i2c / tea6420.c
CommitLineData
74ba9207 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2 /*
3 tea6420 - i2c-driver for the tea6420 by SGS Thomson
4
5 Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
a832781c 6 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
1da177e4
LT
7
8 The tea6420 is a bus controlled audio-matrix with 5 stereo inputs,
9 4 stereo outputs and gain control for each output.
3ad2f3fb 10 It is cascadable, i.e. it can be found at the addresses 0x98
1da177e4
LT
11 and 0x9a on the i2c-bus.
12
f8a7647d 13 For detailed information download the specifications directly
1da177e4
LT
14 from SGS Thomson at http://www.st.com
15
1da177e4
LT
16 */
17
a8733ca5 18
1da177e4
LT
19#include <linux/module.h>
20#include <linux/ioctl.h>
5a0e3ad6 21#include <linux/slab.h>
1da177e4 22#include <linux/i2c.h>
2a5b828d 23#include <media/v4l2-device.h>
1da177e4
LT
24#include "tea6420.h"
25
a832781c
HV
26MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
27MODULE_DESCRIPTION("tea6420 driver");
28MODULE_LICENSE("GPL");
29
30static int debug;
1da177e4 31module_param(debug, int, 0644);
f87086e3 32
a832781c 33MODULE_PARM_DESC(debug, "Debug level (0-1)");
1da177e4 34
1da177e4 35
1da177e4 36/* make a connection between the input 'i' and the output 'o'
1b8dac15 37 with gain 'g' (note: i = 6 means 'mute') */
5325b427
HV
38static int tea6420_s_routing(struct v4l2_subdev *sd,
39 u32 i, u32 o, u32 config)
1da177e4 40{
1b8dac15 41 struct i2c_client *client = v4l2_get_subdevdata(sd);
5325b427 42 int g = (o >> 4) & 0xf;
a832781c 43 u8 byte;
1da177e4
LT
44 int ret;
45
5325b427 46 o &= 0xf;
1b8dac15 47 v4l2_dbg(1, debug, sd, "i=%d, o=%d, g=%d\n", i, o, g);
1da177e4 48
c84e6036 49 /* check if the parameters are valid */
1da177e4 50 if (i < 1 || i > 6 || o < 1 || o > 4 || g < 0 || g > 6 || g % 2 != 0)
1b8dac15 51 return -EINVAL;
1da177e4 52
a832781c 53 byte = ((o - 1) << 5);
1da177e4
LT
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) {
1b8dac15 73 v4l2_dbg(1, debug, sd,
a832781c 74 "i2c_smbus_write_byte() failed, ret:%d\n", ret);
1da177e4
LT
75 return -EIO;
76 }
1da177e4
LT
77 return 0;
78}
79
2a5b828d
HV
80/* ----------------------------------------------------------------------- */
81
1b8dac15
HV
82static const struct v4l2_subdev_audio_ops tea6420_audio_ops = {
83 .s_routing = tea6420_s_routing,
2a5b828d
HV
84};
85
86static const struct v4l2_subdev_ops tea6420_ops = {
1b8dac15 87 .audio = &tea6420_audio_ops,
2a5b828d
HV
88};
89
8569336f 90static int tea6420_probe(struct i2c_client *client)
a832781c 91{
2a5b828d 92 struct v4l2_subdev *sd;
a832781c 93 int err, i;
1da177e4 94
a832781c
HV
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);
1da177e4 101
c02b211d 102 sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
1b8dac15
HV
103 if (sd == NULL)
104 return -ENOMEM;
105 v4l2_i2c_subdev_init(sd, client, &tea6420_ops);
106
1da177e4
LT
107 /* set initial values: set "mute"-input to all outputs at gain 0 */
108 err = 0;
5325b427
HV
109 for (i = 1; i < 5; i++)
110 err += tea6420_s_routing(sd, 6, i, 0);
1da177e4 111 if (err) {
a832781c 112 v4l_dbg(1, debug, client, "could not initialize tea6420\n");
1da177e4
LT
113 return -ENODEV;
114 }
2a5b828d
HV
115 return 0;
116}
117
ed5c2f5f 118static void tea6420_remove(struct i2c_client *client)
2a5b828d
HV
119{
120 struct v4l2_subdev *sd = i2c_get_clientdata(client);
121
122 v4l2_device_unregister_subdev(sd);
1da177e4
LT
123}
124
a832781c 125static const struct i2c_device_id tea6420_id[] = {
cc4cbd4b 126 { "tea6420" },
a832781c 127 { }
1da177e4 128};
a832781c 129MODULE_DEVICE_TABLE(i2c, tea6420_id);
1da177e4 130
e2c52ba6
HV
131static struct i2c_driver tea6420_driver = {
132 .driver = {
e2c52ba6
HV
133 .name = "tea6420",
134 },
aaeb31c0 135 .probe = tea6420_probe,
e2c52ba6
HV
136 .remove = tea6420_remove,
137 .id_table = tea6420_id,
1da177e4 138};
e2c52ba6 139
c6e8d86f 140module_i2c_driver(tea6420_driver);