soc-cache.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * soc-cache.c -- ASoC register cache helpers
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <sound/soc.h>
  14. static unsigned int snd_soc_7_9_read(struct snd_soc_codec *codec,
  15. unsigned int reg)
  16. {
  17. u16 *cache = codec->reg_cache;
  18. if (reg >= codec->reg_cache_size)
  19. return -1;
  20. return cache[reg];
  21. }
  22. static int snd_soc_7_9_write(struct snd_soc_codec *codec, unsigned int reg,
  23. unsigned int value)
  24. {
  25. u16 *cache = codec->reg_cache;
  26. u8 data[2];
  27. int ret;
  28. BUG_ON(codec->volatile_register);
  29. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  30. data[1] = value & 0x00ff;
  31. if (reg < codec->reg_cache_size)
  32. cache[reg] = value;
  33. ret = codec->hw_write(codec->control_data, data, 2);
  34. if (ret == 2)
  35. return 0;
  36. if (ret < 0)
  37. return ret;
  38. else
  39. return -EIO;
  40. }
  41. static struct {
  42. int addr_bits;
  43. int data_bits;
  44. int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);
  45. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  46. } io_types[] = {
  47. { 7, 9, snd_soc_7_9_write, snd_soc_7_9_read },
  48. };
  49. /**
  50. * snd_soc_codec_set_cache_io: Set up standard I/O functions.
  51. *
  52. * @codec: CODEC to configure.
  53. * @type: Type of cache.
  54. * @addr_bits: Number of bits of register address data.
  55. * @data_bits: Number of bits of data per register.
  56. *
  57. * Register formats are frequently shared between many I2C and SPI
  58. * devices. In order to promote code reuse the ASoC core provides
  59. * some standard implementations of CODEC read and write operations
  60. * which can be set up using this function.
  61. *
  62. * The caller is responsible for allocating and initialising the
  63. * actual cache.
  64. *
  65. * Note that at present this code cannot be used by CODECs with
  66. * volatile registers.
  67. */
  68. int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
  69. int addr_bits, int data_bits)
  70. {
  71. int i;
  72. /* We don't support volatile registers yet - refactoring of
  73. * the hw_read operation will be required to do so. */
  74. if (codec->volatile_register) {
  75. printk(KERN_ERR "Volatile registers not yet supported\n");
  76. return -EINVAL;
  77. }
  78. for (i = 0; i < ARRAY_SIZE(io_types); i++)
  79. if (io_types[i].addr_bits == addr_bits &&
  80. io_types[i].data_bits == data_bits)
  81. break;
  82. if (i == ARRAY_SIZE(io_types)) {
  83. printk(KERN_ERR
  84. "No I/O functions for %d bit address %d bit data\n",
  85. addr_bits, data_bits);
  86. return -EINVAL;
  87. }
  88. codec->write = io_types[i].write;
  89. codec->read = io_types[i].read;
  90. return 0;
  91. }
  92. EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);