174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2cb7a01acSMauro Carvalho Chehab /*
3cb7a01acSMauro Carvalho Chehab tda9840 - i2c-driver for the tda9840 by SGS Thomson
4cb7a01acSMauro Carvalho Chehab
5cb7a01acSMauro Carvalho Chehab Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
6cb7a01acSMauro Carvalho Chehab Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
7cb7a01acSMauro Carvalho Chehab
8cb7a01acSMauro Carvalho Chehab The tda9840 is a stereo/dual sound processor with digital
9cb7a01acSMauro Carvalho Chehab identification. It can be found at address 0x84 on the i2c-bus.
10cb7a01acSMauro Carvalho Chehab
11f8a7647dSMauro Carvalho Chehab For detailed information download the specifications directly
12cb7a01acSMauro Carvalho Chehab from SGS Thomson at http://www.st.com
13cb7a01acSMauro Carvalho Chehab
14cb7a01acSMauro Carvalho Chehab */
15cb7a01acSMauro Carvalho Chehab
16cb7a01acSMauro Carvalho Chehab
17cb7a01acSMauro Carvalho Chehab #include <linux/module.h>
18cb7a01acSMauro Carvalho Chehab #include <linux/ioctl.h>
19cb7a01acSMauro Carvalho Chehab #include <linux/slab.h>
20cb7a01acSMauro Carvalho Chehab #include <linux/i2c.h>
21cb7a01acSMauro Carvalho Chehab #include <media/v4l2-device.h>
22cb7a01acSMauro Carvalho Chehab
23cb7a01acSMauro Carvalho Chehab MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
24cb7a01acSMauro Carvalho Chehab MODULE_DESCRIPTION("tda9840 driver");
25cb7a01acSMauro Carvalho Chehab MODULE_LICENSE("GPL");
26cb7a01acSMauro Carvalho Chehab
27cb7a01acSMauro Carvalho Chehab static int debug;
28cb7a01acSMauro Carvalho Chehab module_param(debug, int, 0644);
29cb7a01acSMauro Carvalho Chehab
30cb7a01acSMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Debug level (0-1)");
31cb7a01acSMauro Carvalho Chehab
32cb7a01acSMauro Carvalho Chehab #define SWITCH 0x00
33cb7a01acSMauro Carvalho Chehab #define LEVEL_ADJUST 0x02
34cb7a01acSMauro Carvalho Chehab #define STEREO_ADJUST 0x03
35cb7a01acSMauro Carvalho Chehab #define TEST 0x04
36cb7a01acSMauro Carvalho Chehab
37cb7a01acSMauro Carvalho Chehab #define TDA9840_SET_MUTE 0x00
38cb7a01acSMauro Carvalho Chehab #define TDA9840_SET_MONO 0x10
39cb7a01acSMauro Carvalho Chehab #define TDA9840_SET_STEREO 0x2a
40cb7a01acSMauro Carvalho Chehab #define TDA9840_SET_LANG1 0x12
41cb7a01acSMauro Carvalho Chehab #define TDA9840_SET_LANG2 0x1e
42cb7a01acSMauro Carvalho Chehab #define TDA9840_SET_BOTH 0x1a
43cb7a01acSMauro Carvalho Chehab #define TDA9840_SET_BOTH_R 0x16
44cb7a01acSMauro Carvalho Chehab #define TDA9840_SET_EXTERNAL 0x7a
45cb7a01acSMauro Carvalho Chehab
46cb7a01acSMauro Carvalho Chehab
tda9840_write(struct v4l2_subdev * sd,u8 reg,u8 val)47cb7a01acSMauro Carvalho Chehab static void tda9840_write(struct v4l2_subdev *sd, u8 reg, u8 val)
48cb7a01acSMauro Carvalho Chehab {
49cb7a01acSMauro Carvalho Chehab struct i2c_client *client = v4l2_get_subdevdata(sd);
50cb7a01acSMauro Carvalho Chehab
51cb7a01acSMauro Carvalho Chehab if (i2c_smbus_write_byte_data(client, reg, val))
52cb7a01acSMauro Carvalho Chehab v4l2_dbg(1, debug, sd, "error writing %02x to %02x\n",
53cb7a01acSMauro Carvalho Chehab val, reg);
54cb7a01acSMauro Carvalho Chehab }
55cb7a01acSMauro Carvalho Chehab
tda9840_status(struct v4l2_subdev * sd)56cb7a01acSMauro Carvalho Chehab static int tda9840_status(struct v4l2_subdev *sd)
57cb7a01acSMauro Carvalho Chehab {
58cb7a01acSMauro Carvalho Chehab struct i2c_client *client = v4l2_get_subdevdata(sd);
59afdb4ca2SMauro Carvalho Chehab int rc;
60cb7a01acSMauro Carvalho Chehab u8 byte;
61cb7a01acSMauro Carvalho Chehab
62afdb4ca2SMauro Carvalho Chehab rc = i2c_master_recv(client, &byte, 1);
63afdb4ca2SMauro Carvalho Chehab if (rc != 1) {
64cb7a01acSMauro Carvalho Chehab v4l2_dbg(1, debug, sd,
65cb7a01acSMauro Carvalho Chehab "i2c_master_recv() failed\n");
66afdb4ca2SMauro Carvalho Chehab if (rc < 0)
67afdb4ca2SMauro Carvalho Chehab return rc;
68cb7a01acSMauro Carvalho Chehab return -EIO;
69cb7a01acSMauro Carvalho Chehab }
70cb7a01acSMauro Carvalho Chehab
71cb7a01acSMauro Carvalho Chehab if (byte & 0x80) {
72cb7a01acSMauro Carvalho Chehab v4l2_dbg(1, debug, sd,
73cb7a01acSMauro Carvalho Chehab "TDA9840_DETECT: register contents invalid\n");
74cb7a01acSMauro Carvalho Chehab return -EINVAL;
75cb7a01acSMauro Carvalho Chehab }
76cb7a01acSMauro Carvalho Chehab
77cb7a01acSMauro Carvalho Chehab v4l2_dbg(1, debug, sd, "TDA9840_DETECT: byte: 0x%02x\n", byte);
78cb7a01acSMauro Carvalho Chehab return byte & 0x60;
79cb7a01acSMauro Carvalho Chehab }
80cb7a01acSMauro Carvalho Chehab
tda9840_s_tuner(struct v4l2_subdev * sd,const struct v4l2_tuner * t)812f73c7c5SHans Verkuil static int tda9840_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *t)
82cb7a01acSMauro Carvalho Chehab {
83cb7a01acSMauro Carvalho Chehab int stat = tda9840_status(sd);
84cb7a01acSMauro Carvalho Chehab int byte;
85cb7a01acSMauro Carvalho Chehab
86cb7a01acSMauro Carvalho Chehab if (t->index)
87cb7a01acSMauro Carvalho Chehab return -EINVAL;
88cb7a01acSMauro Carvalho Chehab
89cb7a01acSMauro Carvalho Chehab stat = stat < 0 ? 0 : stat;
90cb7a01acSMauro Carvalho Chehab if (stat == 0 || stat == 0x60) /* mono input */
91cb7a01acSMauro Carvalho Chehab byte = TDA9840_SET_MONO;
92cb7a01acSMauro Carvalho Chehab else if (stat == 0x40) /* stereo input */
93cb7a01acSMauro Carvalho Chehab byte = (t->audmode == V4L2_TUNER_MODE_MONO) ?
94cb7a01acSMauro Carvalho Chehab TDA9840_SET_MONO : TDA9840_SET_STEREO;
95cb7a01acSMauro Carvalho Chehab else { /* bilingual */
96cb7a01acSMauro Carvalho Chehab switch (t->audmode) {
97cb7a01acSMauro Carvalho Chehab case V4L2_TUNER_MODE_LANG1_LANG2:
98cb7a01acSMauro Carvalho Chehab byte = TDA9840_SET_BOTH;
99cb7a01acSMauro Carvalho Chehab break;
100cb7a01acSMauro Carvalho Chehab case V4L2_TUNER_MODE_LANG2:
101cb7a01acSMauro Carvalho Chehab byte = TDA9840_SET_LANG2;
102cb7a01acSMauro Carvalho Chehab break;
103cb7a01acSMauro Carvalho Chehab default:
104cb7a01acSMauro Carvalho Chehab byte = TDA9840_SET_LANG1;
105cb7a01acSMauro Carvalho Chehab break;
106cb7a01acSMauro Carvalho Chehab }
107cb7a01acSMauro Carvalho Chehab }
108cb7a01acSMauro Carvalho Chehab v4l2_dbg(1, debug, sd, "TDA9840_SWITCH: 0x%02x\n", byte);
109cb7a01acSMauro Carvalho Chehab tda9840_write(sd, SWITCH, byte);
110cb7a01acSMauro Carvalho Chehab return 0;
111cb7a01acSMauro Carvalho Chehab }
112cb7a01acSMauro Carvalho Chehab
tda9840_g_tuner(struct v4l2_subdev * sd,struct v4l2_tuner * t)113cb7a01acSMauro Carvalho Chehab static int tda9840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *t)
114cb7a01acSMauro Carvalho Chehab {
115cb7a01acSMauro Carvalho Chehab int stat = tda9840_status(sd);
116cb7a01acSMauro Carvalho Chehab
117cb7a01acSMauro Carvalho Chehab if (stat < 0)
118cb7a01acSMauro Carvalho Chehab return stat;
119cb7a01acSMauro Carvalho Chehab
120cb7a01acSMauro Carvalho Chehab t->rxsubchans = V4L2_TUNER_SUB_MONO;
121cb7a01acSMauro Carvalho Chehab
122cb7a01acSMauro Carvalho Chehab switch (stat & 0x60) {
123cb7a01acSMauro Carvalho Chehab case 0x00:
124cb7a01acSMauro Carvalho Chehab t->rxsubchans = V4L2_TUNER_SUB_MONO;
125cb7a01acSMauro Carvalho Chehab break;
126cb7a01acSMauro Carvalho Chehab case 0x20:
127cb7a01acSMauro Carvalho Chehab t->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
128cb7a01acSMauro Carvalho Chehab break;
129cb7a01acSMauro Carvalho Chehab case 0x40:
130cb7a01acSMauro Carvalho Chehab t->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
131cb7a01acSMauro Carvalho Chehab break;
132cb7a01acSMauro Carvalho Chehab default: /* Incorrect detect */
133cb7a01acSMauro Carvalho Chehab t->rxsubchans = V4L2_TUNER_MODE_MONO;
134cb7a01acSMauro Carvalho Chehab break;
135cb7a01acSMauro Carvalho Chehab }
136cb7a01acSMauro Carvalho Chehab return 0;
137cb7a01acSMauro Carvalho Chehab }
138cb7a01acSMauro Carvalho Chehab
139cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */
140cb7a01acSMauro Carvalho Chehab
141cb7a01acSMauro Carvalho Chehab static const struct v4l2_subdev_tuner_ops tda9840_tuner_ops = {
142cb7a01acSMauro Carvalho Chehab .s_tuner = tda9840_s_tuner,
143cb7a01acSMauro Carvalho Chehab .g_tuner = tda9840_g_tuner,
144cb7a01acSMauro Carvalho Chehab };
145cb7a01acSMauro Carvalho Chehab
146cb7a01acSMauro Carvalho Chehab static const struct v4l2_subdev_ops tda9840_ops = {
147cb7a01acSMauro Carvalho Chehab .tuner = &tda9840_tuner_ops,
148cb7a01acSMauro Carvalho Chehab };
149cb7a01acSMauro Carvalho Chehab
150cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */
151cb7a01acSMauro Carvalho Chehab
tda9840_probe(struct i2c_client * client)1522cd4987dSUwe Kleine-König static int tda9840_probe(struct i2c_client *client)
153cb7a01acSMauro Carvalho Chehab {
154cb7a01acSMauro Carvalho Chehab struct v4l2_subdev *sd;
155cb7a01acSMauro Carvalho Chehab
156cb7a01acSMauro Carvalho Chehab /* let's see whether this adapter can support what we need */
157cb7a01acSMauro Carvalho Chehab if (!i2c_check_functionality(client->adapter,
158cb7a01acSMauro Carvalho Chehab I2C_FUNC_SMBUS_READ_BYTE_DATA |
159cb7a01acSMauro Carvalho Chehab I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
160cb7a01acSMauro Carvalho Chehab return -EIO;
161cb7a01acSMauro Carvalho Chehab
162cb7a01acSMauro Carvalho Chehab v4l_info(client, "chip found @ 0x%x (%s)\n",
163cb7a01acSMauro Carvalho Chehab client->addr << 1, client->adapter->name);
164cb7a01acSMauro Carvalho Chehab
165c02b211dSLaurent Pinchart sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
166cb7a01acSMauro Carvalho Chehab if (sd == NULL)
167cb7a01acSMauro Carvalho Chehab return -ENOMEM;
168cb7a01acSMauro Carvalho Chehab v4l2_i2c_subdev_init(sd, client, &tda9840_ops);
169cb7a01acSMauro Carvalho Chehab
170cb7a01acSMauro Carvalho Chehab /* set initial values for level & stereo - adjustment, mode */
171cb7a01acSMauro Carvalho Chehab tda9840_write(sd, LEVEL_ADJUST, 0);
172cb7a01acSMauro Carvalho Chehab tda9840_write(sd, STEREO_ADJUST, 0);
173cb7a01acSMauro Carvalho Chehab tda9840_write(sd, SWITCH, TDA9840_SET_STEREO);
174cb7a01acSMauro Carvalho Chehab return 0;
175cb7a01acSMauro Carvalho Chehab }
176cb7a01acSMauro Carvalho Chehab
tda9840_remove(struct i2c_client * client)177ed5c2f5fSUwe Kleine-König static void tda9840_remove(struct i2c_client *client)
178cb7a01acSMauro Carvalho Chehab {
179cb7a01acSMauro Carvalho Chehab struct v4l2_subdev *sd = i2c_get_clientdata(client);
180cb7a01acSMauro Carvalho Chehab
181cb7a01acSMauro Carvalho Chehab v4l2_device_unregister_subdev(sd);
182cb7a01acSMauro Carvalho Chehab }
183cb7a01acSMauro Carvalho Chehab
184cb7a01acSMauro Carvalho Chehab static const struct i2c_device_id tda9840_id[] = {
185*cc4cbd4bSUwe Kleine-König { "tda9840" },
186cb7a01acSMauro Carvalho Chehab { }
187cb7a01acSMauro Carvalho Chehab };
188cb7a01acSMauro Carvalho Chehab MODULE_DEVICE_TABLE(i2c, tda9840_id);
189cb7a01acSMauro Carvalho Chehab
190cb7a01acSMauro Carvalho Chehab static struct i2c_driver tda9840_driver = {
191cb7a01acSMauro Carvalho Chehab .driver = {
192cb7a01acSMauro Carvalho Chehab .name = "tda9840",
193cb7a01acSMauro Carvalho Chehab },
194aaeb31c0SUwe Kleine-König .probe = tda9840_probe,
195cb7a01acSMauro Carvalho Chehab .remove = tda9840_remove,
196cb7a01acSMauro Carvalho Chehab .id_table = tda9840_id,
197cb7a01acSMauro Carvalho Chehab };
198cb7a01acSMauro Carvalho Chehab
199cb7a01acSMauro Carvalho Chehab module_i2c_driver(tda9840_driver);
200