uda1342.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005-2006 Micronas USA Inc.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/i2c.h>
  8. #include <linux/videodev2.h>
  9. #include <media/v4l2-device.h>
  10. #include <media/i2c/uda1342.h>
  11. #include <linux/slab.h>
  12. static int write_reg(struct i2c_client *client, int reg, int value)
  13. {
  14. /* UDA1342 wants MSB first, but SMBus sends LSB first */
  15. i2c_smbus_write_word_data(client, reg, swab16(value));
  16. return 0;
  17. }
  18. static int uda1342_s_routing(struct v4l2_subdev *sd,
  19. u32 input, u32 output, u32 config)
  20. {
  21. struct i2c_client *client = v4l2_get_subdevdata(sd);
  22. switch (input) {
  23. case UDA1342_IN1:
  24. write_reg(client, 0x00, 0x1241); /* select input 1 */
  25. break;
  26. case UDA1342_IN2:
  27. write_reg(client, 0x00, 0x1441); /* select input 2 */
  28. break;
  29. default:
  30. v4l2_err(sd, "input %d not supported\n", input);
  31. break;
  32. }
  33. return 0;
  34. }
  35. static const struct v4l2_subdev_audio_ops uda1342_audio_ops = {
  36. .s_routing = uda1342_s_routing,
  37. };
  38. static const struct v4l2_subdev_ops uda1342_ops = {
  39. .audio = &uda1342_audio_ops,
  40. };
  41. static int uda1342_probe(struct i2c_client *client)
  42. {
  43. struct i2c_adapter *adapter = client->adapter;
  44. struct v4l2_subdev *sd;
  45. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  46. return -ENODEV;
  47. dev_dbg(&client->dev, "initializing UDA1342 at address %d on %s\n",
  48. client->addr, adapter->name);
  49. sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
  50. if (sd == NULL)
  51. return -ENOMEM;
  52. v4l2_i2c_subdev_init(sd, client, &uda1342_ops);
  53. write_reg(client, 0x00, 0x8000); /* reset registers */
  54. write_reg(client, 0x00, 0x1241); /* select input 1 */
  55. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  56. client->addr << 1, client->adapter->name);
  57. return 0;
  58. }
  59. static void uda1342_remove(struct i2c_client *client)
  60. {
  61. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  62. v4l2_device_unregister_subdev(sd);
  63. }
  64. static const struct i2c_device_id uda1342_id[] = {
  65. { "uda1342" },
  66. { }
  67. };
  68. MODULE_DEVICE_TABLE(i2c, uda1342_id);
  69. static struct i2c_driver uda1342_driver = {
  70. .driver = {
  71. .name = "uda1342",
  72. },
  73. .probe = uda1342_probe,
  74. .remove = uda1342_remove,
  75. .id_table = uda1342_id,
  76. };
  77. module_i2c_driver(uda1342_driver);
  78. MODULE_DESCRIPTION("Philips UDA1342 audio codec driver");
  79. MODULE_LICENSE("GPL v2");