tea6420.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. tea6420 - i2c-driver for the tea6420 by SGS Thomson
  4. Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
  5. Copyright (C) 2008 Hans Verkuil <hverkuil@kernel.org>
  6. The tea6420 is a bus controlled audio-matrix with 5 stereo inputs,
  7. 4 stereo outputs and gain control for each output.
  8. It is cascadable, i.e. it can be found at the addresses 0x98
  9. and 0x9a on the i2c-bus.
  10. For detailed information download the specifications directly
  11. from SGS Thomson at http://www.st.com
  12. */
  13. #include <linux/module.h>
  14. #include <linux/ioctl.h>
  15. #include <linux/slab.h>
  16. #include <linux/i2c.h>
  17. #include <media/v4l2-device.h>
  18. #include "tea6420.h"
  19. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  20. MODULE_DESCRIPTION("tea6420 driver");
  21. MODULE_LICENSE("GPL");
  22. static int debug;
  23. module_param(debug, int, 0644);
  24. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  25. /* make a connection between the input 'i' and the output 'o'
  26. with gain 'g' (note: i = 6 means 'mute') */
  27. static int tea6420_s_routing(struct v4l2_subdev *sd,
  28. u32 i, u32 o, u32 config)
  29. {
  30. struct i2c_client *client = v4l2_get_subdevdata(sd);
  31. int g = (o >> 4) & 0xf;
  32. u8 byte;
  33. int ret;
  34. o &= 0xf;
  35. v4l2_dbg(1, debug, sd, "i=%d, o=%d, g=%d\n", i, o, g);
  36. /* check if the parameters are valid */
  37. if (i < 1 || i > 6 || o < 1 || o > 4 || g < 0 || g > 6 || g % 2 != 0)
  38. return -EINVAL;
  39. byte = ((o - 1) << 5);
  40. byte |= (i - 1);
  41. /* to understand this, have a look at the tea6420-specs (p.5) */
  42. switch (g) {
  43. case 0:
  44. byte |= (3 << 3);
  45. break;
  46. case 2:
  47. byte |= (2 << 3);
  48. break;
  49. case 4:
  50. byte |= (1 << 3);
  51. break;
  52. case 6:
  53. break;
  54. }
  55. ret = i2c_smbus_write_byte(client, byte);
  56. if (ret) {
  57. v4l2_dbg(1, debug, sd,
  58. "i2c_smbus_write_byte() failed, ret:%d\n", ret);
  59. return -EIO;
  60. }
  61. return 0;
  62. }
  63. /* ----------------------------------------------------------------------- */
  64. static const struct v4l2_subdev_audio_ops tea6420_audio_ops = {
  65. .s_routing = tea6420_s_routing,
  66. };
  67. static const struct v4l2_subdev_ops tea6420_ops = {
  68. .audio = &tea6420_audio_ops,
  69. };
  70. static int tea6420_probe(struct i2c_client *client)
  71. {
  72. struct v4l2_subdev *sd;
  73. int err, i;
  74. /* let's see whether this adapter can support what we need */
  75. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE))
  76. return -EIO;
  77. v4l_info(client, "chip found @ 0x%x (%s)\n",
  78. client->addr << 1, client->adapter->name);
  79. sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
  80. if (sd == NULL)
  81. return -ENOMEM;
  82. v4l2_i2c_subdev_init(sd, client, &tea6420_ops);
  83. /* set initial values: set "mute"-input to all outputs at gain 0 */
  84. err = 0;
  85. for (i = 1; i < 5; i++)
  86. err += tea6420_s_routing(sd, 6, i, 0);
  87. if (err) {
  88. v4l_dbg(1, debug, client, "could not initialize tea6420\n");
  89. return -ENODEV;
  90. }
  91. return 0;
  92. }
  93. static void tea6420_remove(struct i2c_client *client)
  94. {
  95. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  96. v4l2_device_unregister_subdev(sd);
  97. }
  98. static const struct i2c_device_id tea6420_id[] = {
  99. { "tea6420" },
  100. { }
  101. };
  102. MODULE_DEVICE_TABLE(i2c, tea6420_id);
  103. static struct i2c_driver tea6420_driver = {
  104. .driver = {
  105. .name = "tea6420",
  106. },
  107. .probe = tea6420_probe,
  108. .remove = tea6420_remove,
  109. .id_table = tea6420_id,
  110. };
  111. module_i2c_driver(tea6420_driver);