sigmadsp-i2c.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Load Analog Devices SigmaStudio firmware files
  3. *
  4. * Copyright 2009-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/export.h>
  9. #include <linux/i2c.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <asm/unaligned.h>
  13. #include "sigmadsp.h"
  14. static int sigmadsp_write_i2c(void *control_data,
  15. unsigned int addr, const uint8_t data[], size_t len)
  16. {
  17. uint8_t *buf;
  18. int ret;
  19. buf = kzalloc(2 + len, GFP_KERNEL | GFP_DMA);
  20. if (!buf)
  21. return -ENOMEM;
  22. put_unaligned_be16(addr, buf);
  23. memcpy(buf + 2, data, len);
  24. ret = i2c_master_send(control_data, buf, len + 2);
  25. kfree(buf);
  26. if (ret < 0)
  27. return ret;
  28. return 0;
  29. }
  30. static int sigmadsp_read_i2c(void *control_data,
  31. unsigned int addr, uint8_t data[], size_t len)
  32. {
  33. struct i2c_client *client = control_data;
  34. struct i2c_msg msgs[2];
  35. uint8_t buf[2];
  36. int ret;
  37. put_unaligned_be16(addr, buf);
  38. msgs[0].addr = client->addr;
  39. msgs[0].len = sizeof(buf);
  40. msgs[0].buf = buf;
  41. msgs[0].flags = 0;
  42. msgs[1].addr = client->addr;
  43. msgs[1].len = len;
  44. msgs[1].buf = data;
  45. msgs[1].flags = I2C_M_RD;
  46. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  47. if (ret < 0)
  48. return ret;
  49. else if (ret != ARRAY_SIZE(msgs))
  50. return -EIO;
  51. return 0;
  52. }
  53. /**
  54. * devm_sigmadsp_init_i2c() - Initialize SigmaDSP instance
  55. * @client: The parent I2C device
  56. * @ops: The sigmadsp_ops to use for this instance
  57. * @firmware_name: Name of the firmware file to load
  58. *
  59. * Allocates a SigmaDSP instance and loads the specified firmware file.
  60. *
  61. * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
  62. */
  63. struct sigmadsp *devm_sigmadsp_init_i2c(struct i2c_client *client,
  64. const struct sigmadsp_ops *ops, const char *firmware_name)
  65. {
  66. struct sigmadsp *sigmadsp;
  67. sigmadsp = devm_sigmadsp_init(&client->dev, ops, firmware_name);
  68. if (IS_ERR(sigmadsp))
  69. return sigmadsp;
  70. sigmadsp->control_data = client;
  71. sigmadsp->write = sigmadsp_write_i2c;
  72. sigmadsp->read = sigmadsp_read_i2c;
  73. return sigmadsp;
  74. }
  75. EXPORT_SYMBOL_GPL(devm_sigmadsp_init_i2c);
  76. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  77. MODULE_DESCRIPTION("SigmaDSP I2C firmware loader");
  78. MODULE_LICENSE("GPL");