cs3308.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Cirrus Logic cs3308 8-Channel Analog Volume Control
  3. *
  4. * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
  5. * Copyright (C) 2012 Steven Toth <stoth@kernellabs.com>
  6. *
  7. * Derived from cs5345.c Copyright (C) 2007 Hans Verkuil
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/i2c.h>
  22. #include <linux/slab.h>
  23. #include <linux/videodev2.h>
  24. #include <media/v4l2-device.h>
  25. MODULE_DESCRIPTION("i2c device driver for cs3308 8-channel volume control");
  26. MODULE_AUTHOR("Devin Heitmueller");
  27. MODULE_LICENSE("GPL");
  28. static inline int cs3308_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  29. {
  30. struct i2c_client *client = v4l2_get_subdevdata(sd);
  31. return i2c_smbus_write_byte_data(client, reg, value);
  32. }
  33. static inline int cs3308_read(struct v4l2_subdev *sd, u8 reg)
  34. {
  35. struct i2c_client *client = v4l2_get_subdevdata(sd);
  36. return i2c_smbus_read_byte_data(client, reg);
  37. }
  38. #ifdef CONFIG_VIDEO_ADV_DEBUG
  39. static int cs3308_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  40. {
  41. reg->val = cs3308_read(sd, reg->reg & 0xffff);
  42. reg->size = 1;
  43. return 0;
  44. }
  45. static int cs3308_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  46. {
  47. cs3308_write(sd, reg->reg & 0xffff, reg->val & 0xff);
  48. return 0;
  49. }
  50. #endif
  51. /* ----------------------------------------------------------------------- */
  52. static const struct v4l2_subdev_core_ops cs3308_core_ops = {
  53. #ifdef CONFIG_VIDEO_ADV_DEBUG
  54. .g_register = cs3308_g_register,
  55. .s_register = cs3308_s_register,
  56. #endif
  57. };
  58. static const struct v4l2_subdev_ops cs3308_ops = {
  59. .core = &cs3308_core_ops,
  60. };
  61. /* ----------------------------------------------------------------------- */
  62. static int cs3308_probe(struct i2c_client *client,
  63. const struct i2c_device_id *id)
  64. {
  65. struct v4l2_subdev *sd;
  66. unsigned i;
  67. /* Check if the adapter supports the needed features */
  68. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  69. return -EIO;
  70. if ((i2c_smbus_read_byte_data(client, 0x1c) & 0xf0) != 0xe0)
  71. return -ENODEV;
  72. v4l_info(client, "chip found @ 0x%x (%s)\n",
  73. client->addr << 1, client->adapter->name);
  74. sd = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
  75. if (sd == NULL)
  76. return -ENOMEM;
  77. v4l2_i2c_subdev_init(sd, client, &cs3308_ops);
  78. /* Set some reasonable defaults */
  79. cs3308_write(sd, 0x0d, 0x00); /* Power up all channels */
  80. cs3308_write(sd, 0x0e, 0x00); /* Master Power */
  81. cs3308_write(sd, 0x0b, 0x00); /* Device Configuration */
  82. /* Set volume for each channel */
  83. for (i = 1; i <= 8; i++)
  84. cs3308_write(sd, i, 0xd2);
  85. cs3308_write(sd, 0x0a, 0x00); /* Unmute all channels */
  86. return 0;
  87. }
  88. /* ----------------------------------------------------------------------- */
  89. static int cs3308_remove(struct i2c_client *client)
  90. {
  91. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  92. v4l2_device_unregister_subdev(sd);
  93. kfree(sd);
  94. return 0;
  95. }
  96. /* ----------------------------------------------------------------------- */
  97. static const struct i2c_device_id cs3308_id[] = {
  98. { "cs3308", 0 },
  99. { }
  100. };
  101. MODULE_DEVICE_TABLE(i2c, cs3308_id);
  102. static struct i2c_driver cs3308_driver = {
  103. .driver = {
  104. .name = "cs3308",
  105. },
  106. .probe = cs3308_probe,
  107. .remove = cs3308_remove,
  108. .id_table = cs3308_id,
  109. };
  110. module_i2c_driver(cs3308_driver);