18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci /* 38c2ecf20Sopenharmony_ci tda9840 - i2c-driver for the tda9840 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 tda9840 is a stereo/dual sound processor with digital 98c2ecf20Sopenharmony_ci identification. It can be found at address 0x84 on the i2c-bus. 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci For detailed information download the specifications directly 128c2ecf20Sopenharmony_ci from SGS Thomson at http://www.st.com 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci */ 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/module.h> 188c2ecf20Sopenharmony_ci#include <linux/ioctl.h> 198c2ecf20Sopenharmony_ci#include <linux/slab.h> 208c2ecf20Sopenharmony_ci#include <linux/i2c.h> 218c2ecf20Sopenharmony_ci#include <media/v4l2-device.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michael Hunold <michael@mihu.de>"); 248c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("tda9840 driver"); 258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic int debug; 288c2ecf20Sopenharmony_cimodule_param(debug, int, 0644); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Debug level (0-1)"); 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define SWITCH 0x00 338c2ecf20Sopenharmony_ci#define LEVEL_ADJUST 0x02 348c2ecf20Sopenharmony_ci#define STEREO_ADJUST 0x03 358c2ecf20Sopenharmony_ci#define TEST 0x04 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define TDA9840_SET_MUTE 0x00 388c2ecf20Sopenharmony_ci#define TDA9840_SET_MONO 0x10 398c2ecf20Sopenharmony_ci#define TDA9840_SET_STEREO 0x2a 408c2ecf20Sopenharmony_ci#define TDA9840_SET_LANG1 0x12 418c2ecf20Sopenharmony_ci#define TDA9840_SET_LANG2 0x1e 428c2ecf20Sopenharmony_ci#define TDA9840_SET_BOTH 0x1a 438c2ecf20Sopenharmony_ci#define TDA9840_SET_BOTH_R 0x16 448c2ecf20Sopenharmony_ci#define TDA9840_SET_EXTERNAL 0x7a 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cistatic void tda9840_write(struct v4l2_subdev *sd, u8 reg, u8 val) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(sd); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci if (i2c_smbus_write_byte_data(client, reg, val)) 528c2ecf20Sopenharmony_ci v4l2_dbg(1, debug, sd, "error writing %02x to %02x\n", 538c2ecf20Sopenharmony_ci val, reg); 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic int tda9840_status(struct v4l2_subdev *sd) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(sd); 598c2ecf20Sopenharmony_ci int rc; 608c2ecf20Sopenharmony_ci u8 byte; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci rc = i2c_master_recv(client, &byte, 1); 638c2ecf20Sopenharmony_ci if (rc != 1) { 648c2ecf20Sopenharmony_ci v4l2_dbg(1, debug, sd, 658c2ecf20Sopenharmony_ci "i2c_master_recv() failed\n"); 668c2ecf20Sopenharmony_ci if (rc < 0) 678c2ecf20Sopenharmony_ci return rc; 688c2ecf20Sopenharmony_ci return -EIO; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci if (byte & 0x80) { 728c2ecf20Sopenharmony_ci v4l2_dbg(1, debug, sd, 738c2ecf20Sopenharmony_ci "TDA9840_DETECT: register contents invalid\n"); 748c2ecf20Sopenharmony_ci return -EINVAL; 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci v4l2_dbg(1, debug, sd, "TDA9840_DETECT: byte: 0x%02x\n", byte); 788c2ecf20Sopenharmony_ci return byte & 0x60; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic int tda9840_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *t) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci int stat = tda9840_status(sd); 848c2ecf20Sopenharmony_ci int byte; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci if (t->index) 878c2ecf20Sopenharmony_ci return -EINVAL; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci stat = stat < 0 ? 0 : stat; 908c2ecf20Sopenharmony_ci if (stat == 0 || stat == 0x60) /* mono input */ 918c2ecf20Sopenharmony_ci byte = TDA9840_SET_MONO; 928c2ecf20Sopenharmony_ci else if (stat == 0x40) /* stereo input */ 938c2ecf20Sopenharmony_ci byte = (t->audmode == V4L2_TUNER_MODE_MONO) ? 948c2ecf20Sopenharmony_ci TDA9840_SET_MONO : TDA9840_SET_STEREO; 958c2ecf20Sopenharmony_ci else { /* bilingual */ 968c2ecf20Sopenharmony_ci switch (t->audmode) { 978c2ecf20Sopenharmony_ci case V4L2_TUNER_MODE_LANG1_LANG2: 988c2ecf20Sopenharmony_ci byte = TDA9840_SET_BOTH; 998c2ecf20Sopenharmony_ci break; 1008c2ecf20Sopenharmony_ci case V4L2_TUNER_MODE_LANG2: 1018c2ecf20Sopenharmony_ci byte = TDA9840_SET_LANG2; 1028c2ecf20Sopenharmony_ci break; 1038c2ecf20Sopenharmony_ci default: 1048c2ecf20Sopenharmony_ci byte = TDA9840_SET_LANG1; 1058c2ecf20Sopenharmony_ci break; 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci v4l2_dbg(1, debug, sd, "TDA9840_SWITCH: 0x%02x\n", byte); 1098c2ecf20Sopenharmony_ci tda9840_write(sd, SWITCH, byte); 1108c2ecf20Sopenharmony_ci return 0; 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistatic int tda9840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *t) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci int stat = tda9840_status(sd); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci if (stat < 0) 1188c2ecf20Sopenharmony_ci return stat; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci t->rxsubchans = V4L2_TUNER_SUB_MONO; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci switch (stat & 0x60) { 1238c2ecf20Sopenharmony_ci case 0x00: 1248c2ecf20Sopenharmony_ci t->rxsubchans = V4L2_TUNER_SUB_MONO; 1258c2ecf20Sopenharmony_ci break; 1268c2ecf20Sopenharmony_ci case 0x20: 1278c2ecf20Sopenharmony_ci t->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; 1288c2ecf20Sopenharmony_ci break; 1298c2ecf20Sopenharmony_ci case 0x40: 1308c2ecf20Sopenharmony_ci t->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO; 1318c2ecf20Sopenharmony_ci break; 1328c2ecf20Sopenharmony_ci default: /* Incorrect detect */ 1338c2ecf20Sopenharmony_ci t->rxsubchans = V4L2_TUNER_MODE_MONO; 1348c2ecf20Sopenharmony_ci break; 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci return 0; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */ 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_tuner_ops tda9840_tuner_ops = { 1428c2ecf20Sopenharmony_ci .s_tuner = tda9840_s_tuner, 1438c2ecf20Sopenharmony_ci .g_tuner = tda9840_g_tuner, 1448c2ecf20Sopenharmony_ci}; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops tda9840_ops = { 1478c2ecf20Sopenharmony_ci .tuner = &tda9840_tuner_ops, 1488c2ecf20Sopenharmony_ci}; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */ 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic int tda9840_probe(struct i2c_client *client, 1538c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci struct v4l2_subdev *sd; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci /* let's see whether this adapter can support what we need */ 1588c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, 1598c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_READ_BYTE_DATA | 1608c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) 1618c2ecf20Sopenharmony_ci return -EIO; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci v4l_info(client, "chip found @ 0x%x (%s)\n", 1648c2ecf20Sopenharmony_ci client->addr << 1, client->adapter->name); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL); 1678c2ecf20Sopenharmony_ci if (sd == NULL) 1688c2ecf20Sopenharmony_ci return -ENOMEM; 1698c2ecf20Sopenharmony_ci v4l2_i2c_subdev_init(sd, client, &tda9840_ops); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci /* set initial values for level & stereo - adjustment, mode */ 1728c2ecf20Sopenharmony_ci tda9840_write(sd, LEVEL_ADJUST, 0); 1738c2ecf20Sopenharmony_ci tda9840_write(sd, STEREO_ADJUST, 0); 1748c2ecf20Sopenharmony_ci tda9840_write(sd, SWITCH, TDA9840_SET_STEREO); 1758c2ecf20Sopenharmony_ci return 0; 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_cistatic int tda9840_remove(struct i2c_client *client) 1798c2ecf20Sopenharmony_ci{ 1808c2ecf20Sopenharmony_ci struct v4l2_subdev *sd = i2c_get_clientdata(client); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci v4l2_device_unregister_subdev(sd); 1838c2ecf20Sopenharmony_ci return 0; 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_cistatic const struct i2c_device_id tda9840_id[] = { 1878c2ecf20Sopenharmony_ci { "tda9840", 0 }, 1888c2ecf20Sopenharmony_ci { } 1898c2ecf20Sopenharmony_ci}; 1908c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tda9840_id); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic struct i2c_driver tda9840_driver = { 1938c2ecf20Sopenharmony_ci .driver = { 1948c2ecf20Sopenharmony_ci .name = "tda9840", 1958c2ecf20Sopenharmony_ci }, 1968c2ecf20Sopenharmony_ci .probe = tda9840_probe, 1978c2ecf20Sopenharmony_ci .remove = tda9840_remove, 1988c2ecf20Sopenharmony_ci .id_table = tda9840_id, 1998c2ecf20Sopenharmony_ci}; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cimodule_i2c_driver(tda9840_driver); 202