tda9840.c 4.8 KB

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