18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci /*
38c2ecf20Sopenharmony_ci    tea6420 - i2c-driver for the tea6420 by SGS Thomson
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci    Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
68c2ecf20Sopenharmony_ci    Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci    The tea6420 is a bus controlled audio-matrix with 5 stereo inputs,
98c2ecf20Sopenharmony_ci    4 stereo outputs and gain control for each output.
108c2ecf20Sopenharmony_ci    It is cascadable, i.e. it can be found at the addresses 0x98
118c2ecf20Sopenharmony_ci    and 0x9a on the i2c-bus.
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci    For detailed information download the specifications directly
148c2ecf20Sopenharmony_ci    from SGS Thomson at http://www.st.com
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci  */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/ioctl.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include <linux/i2c.h>
238c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
248c2ecf20Sopenharmony_ci#include "tea6420.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
278c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("tea6420 driver");
288c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic int debug;
318c2ecf20Sopenharmony_cimodule_param(debug, int, 0644);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Debug level (0-1)");
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* make a connection between the input 'i' and the output 'o'
378c2ecf20Sopenharmony_ci   with gain 'g' (note: i = 6 means 'mute') */
388c2ecf20Sopenharmony_cistatic int tea6420_s_routing(struct v4l2_subdev *sd,
398c2ecf20Sopenharmony_ci			     u32 i, u32 o, u32 config)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
428c2ecf20Sopenharmony_ci	int g = (o >> 4) & 0xf;
438c2ecf20Sopenharmony_ci	u8 byte;
448c2ecf20Sopenharmony_ci	int ret;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	o &= 0xf;
478c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "i=%d, o=%d, g=%d\n", i, o, g);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	/* check if the parameters are valid */
508c2ecf20Sopenharmony_ci	if (i < 1 || i > 6 || o < 1 || o > 4 || g < 0 || g > 6 || g % 2 != 0)
518c2ecf20Sopenharmony_ci		return -EINVAL;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	byte = ((o - 1) << 5);
548c2ecf20Sopenharmony_ci	byte |= (i - 1);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	/* to understand this, have a look at the tea6420-specs (p.5) */
578c2ecf20Sopenharmony_ci	switch (g) {
588c2ecf20Sopenharmony_ci	case 0:
598c2ecf20Sopenharmony_ci		byte |= (3 << 3);
608c2ecf20Sopenharmony_ci		break;
618c2ecf20Sopenharmony_ci	case 2:
628c2ecf20Sopenharmony_ci		byte |= (2 << 3);
638c2ecf20Sopenharmony_ci		break;
648c2ecf20Sopenharmony_ci	case 4:
658c2ecf20Sopenharmony_ci		byte |= (1 << 3);
668c2ecf20Sopenharmony_ci		break;
678c2ecf20Sopenharmony_ci	case 6:
688c2ecf20Sopenharmony_ci		break;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte(client, byte);
728c2ecf20Sopenharmony_ci	if (ret) {
738c2ecf20Sopenharmony_ci		v4l2_dbg(1, debug, sd,
748c2ecf20Sopenharmony_ci			"i2c_smbus_write_byte() failed, ret:%d\n", ret);
758c2ecf20Sopenharmony_ci		return -EIO;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci	return 0;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_audio_ops tea6420_audio_ops = {
838c2ecf20Sopenharmony_ci	.s_routing = tea6420_s_routing,
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops tea6420_ops = {
878c2ecf20Sopenharmony_ci	.audio = &tea6420_audio_ops,
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic int tea6420_probe(struct i2c_client *client,
918c2ecf20Sopenharmony_ci			  const struct i2c_device_id *id)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
948c2ecf20Sopenharmony_ci	int err, i;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* let's see whether this adapter can support what we need */
978c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE))
988c2ecf20Sopenharmony_ci		return -EIO;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	v4l_info(client, "chip found @ 0x%x (%s)\n",
1018c2ecf20Sopenharmony_ci			client->addr << 1, client->adapter->name);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
1048c2ecf20Sopenharmony_ci	if (sd == NULL)
1058c2ecf20Sopenharmony_ci		return -ENOMEM;
1068c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &tea6420_ops);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* set initial values: set "mute"-input to all outputs at gain 0 */
1098c2ecf20Sopenharmony_ci	err = 0;
1108c2ecf20Sopenharmony_ci	for (i = 1; i < 5; i++)
1118c2ecf20Sopenharmony_ci		err += tea6420_s_routing(sd, 6, i, 0);
1128c2ecf20Sopenharmony_ci	if (err) {
1138c2ecf20Sopenharmony_ci		v4l_dbg(1, debug, client, "could not initialize tea6420\n");
1148c2ecf20Sopenharmony_ci		return -ENODEV;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci	return 0;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int tea6420_remove(struct i2c_client *client)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
1248c2ecf20Sopenharmony_ci	return 0;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic const struct i2c_device_id tea6420_id[] = {
1288c2ecf20Sopenharmony_ci	{ "tea6420", 0 },
1298c2ecf20Sopenharmony_ci	{ }
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tea6420_id);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic struct i2c_driver tea6420_driver = {
1348c2ecf20Sopenharmony_ci	.driver = {
1358c2ecf20Sopenharmony_ci		.name	= "tea6420",
1368c2ecf20Sopenharmony_ci	},
1378c2ecf20Sopenharmony_ci	.probe		= tea6420_probe,
1388c2ecf20Sopenharmony_ci	.remove		= tea6420_remove,
1398c2ecf20Sopenharmony_ci	.id_table	= tea6420_id,
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cimodule_i2c_driver(tea6420_driver);
143