uda1342.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. const struct i2c_device_id *id)
  43. {
  44. struct i2c_adapter *adapter = client->adapter;
  45. struct v4l2_subdev *sd;
  46. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  47. return -ENODEV;
  48. dev_dbg(&client->dev, "initializing UDA1342 at address %d on %s\n",
  49. client->addr, adapter->name);
  50. sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
  51. if (sd == NULL)
  52. return -ENOMEM;
  53. v4l2_i2c_subdev_init(sd, client, &uda1342_ops);
  54. write_reg(client, 0x00, 0x8000); /* reset registers */
  55. write_reg(client, 0x00, 0x1241); /* select input 1 */
  56. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  57. client->addr << 1, client->adapter->name);
  58. return 0;
  59. }
  60. static int uda1342_remove(struct i2c_client *client)
  61. {
  62. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  63. v4l2_device_unregister_subdev(sd);
  64. return 0;
  65. }
  66. static const struct i2c_device_id uda1342_id[] = {
  67. { "uda1342", 0 },
  68. { }
  69. };
  70. MODULE_DEVICE_TABLE(i2c, uda1342_id);
  71. static struct i2c_driver uda1342_driver = {
  72. .driver = {
  73. .name = "uda1342",
  74. },
  75. .probe = uda1342_probe,
  76. .remove = uda1342_remove,
  77. .id_table = uda1342_id,
  78. };
  79. module_i2c_driver(uda1342_driver);
  80. MODULE_LICENSE("GPL v2");