regmap-i2c.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Register map access API - I2C support
  3. *
  4. * Copyright 2011 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
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/regmap.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. static int regmap_i2c_write(void *context, const void *data, size_t count)
  17. {
  18. struct device *dev = context;
  19. struct i2c_client *i2c = to_i2c_client(dev);
  20. int ret;
  21. ret = i2c_master_send(i2c, data, count);
  22. if (ret == count)
  23. return 0;
  24. else if (ret < 0)
  25. return ret;
  26. else
  27. return -EIO;
  28. }
  29. static int regmap_i2c_gather_write(void *context,
  30. const void *reg, size_t reg_size,
  31. const void *val, size_t val_size)
  32. {
  33. struct device *dev = context;
  34. struct i2c_client *i2c = to_i2c_client(dev);
  35. struct i2c_msg xfer[2];
  36. int ret;
  37. /* If the I2C controller can't do a gather tell the core, it
  38. * will substitute in a linear write for us.
  39. */
  40. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_NOSTART))
  41. return -ENOTSUPP;
  42. xfer[0].addr = i2c->addr;
  43. xfer[0].flags = 0;
  44. xfer[0].len = reg_size;
  45. xfer[0].buf = (void *)reg;
  46. xfer[1].addr = i2c->addr;
  47. xfer[1].flags = I2C_M_NOSTART;
  48. xfer[1].len = val_size;
  49. xfer[1].buf = (void *)val;
  50. ret = i2c_transfer(i2c->adapter, xfer, 2);
  51. if (ret == 2)
  52. return 0;
  53. if (ret < 0)
  54. return ret;
  55. else
  56. return -EIO;
  57. }
  58. static int regmap_i2c_read(void *context,
  59. const void *reg, size_t reg_size,
  60. void *val, size_t val_size)
  61. {
  62. struct device *dev = context;
  63. struct i2c_client *i2c = to_i2c_client(dev);
  64. struct i2c_msg xfer[2];
  65. int ret;
  66. xfer[0].addr = i2c->addr;
  67. xfer[0].flags = 0;
  68. xfer[0].len = reg_size;
  69. xfer[0].buf = (void *)reg;
  70. xfer[1].addr = i2c->addr;
  71. xfer[1].flags = I2C_M_RD;
  72. xfer[1].len = val_size;
  73. xfer[1].buf = val;
  74. ret = i2c_transfer(i2c->adapter, xfer, 2);
  75. if (ret == 2)
  76. return 0;
  77. else if (ret < 0)
  78. return ret;
  79. else
  80. return -EIO;
  81. }
  82. static struct regmap_bus regmap_i2c = {
  83. .write = regmap_i2c_write,
  84. .gather_write = regmap_i2c_gather_write,
  85. .read = regmap_i2c_read,
  86. };
  87. /**
  88. * regmap_init_i2c(): Initialise register map
  89. *
  90. * @i2c: Device that will be interacted with
  91. * @config: Configuration for register map
  92. *
  93. * The return value will be an ERR_PTR() on error or a valid pointer to
  94. * a struct regmap.
  95. */
  96. struct regmap *regmap_init_i2c(struct i2c_client *i2c,
  97. const struct regmap_config *config)
  98. {
  99. return regmap_init(&i2c->dev, &regmap_i2c, &i2c->dev, config);
  100. }
  101. EXPORT_SYMBOL_GPL(regmap_init_i2c);
  102. /**
  103. * devm_regmap_init_i2c(): Initialise managed register map
  104. *
  105. * @i2c: Device that will be interacted with
  106. * @config: Configuration for register map
  107. *
  108. * The return value will be an ERR_PTR() on error or a valid pointer
  109. * to a struct regmap. The regmap will be automatically freed by the
  110. * device management code.
  111. */
  112. struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
  113. const struct regmap_config *config)
  114. {
  115. return devm_regmap_init(&i2c->dev, &regmap_i2c, &i2c->dev, config);
  116. }
  117. EXPORT_SYMBOL_GPL(devm_regmap_init_i2c);
  118. MODULE_LICENSE("GPL");