hmc425a.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * HMC425A and similar Gain Amplifiers
  4. *
  5. * Copyright 2020 Analog Devices Inc.
  6. */
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/iio/iio.h>
  11. #include <linux/iio/sysfs.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/property.h>
  17. #include <linux/slab.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/sysfs.h>
  20. enum hmc425a_type {
  21. ID_HMC425A,
  22. };
  23. struct hmc425a_chip_info {
  24. const char *name;
  25. const struct iio_chan_spec *channels;
  26. unsigned int num_channels;
  27. unsigned int num_gpios;
  28. int gain_min;
  29. int gain_max;
  30. int default_gain;
  31. };
  32. struct hmc425a_state {
  33. struct regulator *reg;
  34. struct mutex lock; /* protect sensor state */
  35. struct hmc425a_chip_info *chip_info;
  36. struct gpio_descs *gpios;
  37. enum hmc425a_type type;
  38. u32 gain;
  39. };
  40. static int hmc425a_write(struct iio_dev *indio_dev, u32 value)
  41. {
  42. struct hmc425a_state *st = iio_priv(indio_dev);
  43. DECLARE_BITMAP(values, BITS_PER_TYPE(value));
  44. values[0] = value;
  45. gpiod_set_array_value_cansleep(st->gpios->ndescs, st->gpios->desc,
  46. NULL, values);
  47. return 0;
  48. }
  49. static int hmc425a_read_raw(struct iio_dev *indio_dev,
  50. struct iio_chan_spec const *chan, int *val,
  51. int *val2, long m)
  52. {
  53. struct hmc425a_state *st = iio_priv(indio_dev);
  54. int code, gain = 0;
  55. int ret;
  56. mutex_lock(&st->lock);
  57. switch (m) {
  58. case IIO_CHAN_INFO_HARDWAREGAIN:
  59. code = st->gain;
  60. switch (st->type) {
  61. case ID_HMC425A:
  62. gain = ~code * -500;
  63. break;
  64. }
  65. *val = gain / 1000;
  66. *val2 = (gain % 1000) * 1000;
  67. ret = IIO_VAL_INT_PLUS_MICRO_DB;
  68. break;
  69. default:
  70. ret = -EINVAL;
  71. }
  72. mutex_unlock(&st->lock);
  73. return ret;
  74. };
  75. static int hmc425a_write_raw(struct iio_dev *indio_dev,
  76. struct iio_chan_spec const *chan, int val,
  77. int val2, long mask)
  78. {
  79. struct hmc425a_state *st = iio_priv(indio_dev);
  80. struct hmc425a_chip_info *inf = st->chip_info;
  81. int code = 0, gain;
  82. int ret;
  83. if (val < 0)
  84. gain = (val * 1000) - (val2 / 1000);
  85. else
  86. gain = (val * 1000) + (val2 / 1000);
  87. if (gain > inf->gain_max || gain < inf->gain_min)
  88. return -EINVAL;
  89. switch (st->type) {
  90. case ID_HMC425A:
  91. code = ~((abs(gain) / 500) & 0x3F);
  92. break;
  93. }
  94. mutex_lock(&st->lock);
  95. switch (mask) {
  96. case IIO_CHAN_INFO_HARDWAREGAIN:
  97. st->gain = code;
  98. ret = hmc425a_write(indio_dev, st->gain);
  99. break;
  100. default:
  101. ret = -EINVAL;
  102. }
  103. mutex_unlock(&st->lock);
  104. return ret;
  105. }
  106. static int hmc425a_write_raw_get_fmt(struct iio_dev *indio_dev,
  107. struct iio_chan_spec const *chan,
  108. long mask)
  109. {
  110. switch (mask) {
  111. case IIO_CHAN_INFO_HARDWAREGAIN:
  112. return IIO_VAL_INT_PLUS_MICRO_DB;
  113. default:
  114. return -EINVAL;
  115. }
  116. }
  117. static const struct iio_info hmc425a_info = {
  118. .read_raw = &hmc425a_read_raw,
  119. .write_raw = &hmc425a_write_raw,
  120. .write_raw_get_fmt = &hmc425a_write_raw_get_fmt,
  121. };
  122. #define HMC425A_CHAN(_channel) \
  123. { \
  124. .type = IIO_VOLTAGE, \
  125. .output = 1, \
  126. .indexed = 1, \
  127. .channel = _channel, \
  128. .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
  129. }
  130. static const struct iio_chan_spec hmc425a_channels[] = {
  131. HMC425A_CHAN(0),
  132. };
  133. /* Match table for of_platform binding */
  134. static const struct of_device_id hmc425a_of_match[] = {
  135. { .compatible = "adi,hmc425a", .data = (void *)ID_HMC425A },
  136. {},
  137. };
  138. MODULE_DEVICE_TABLE(of, hmc425a_of_match);
  139. static void hmc425a_reg_disable(void *data)
  140. {
  141. struct hmc425a_state *st = data;
  142. regulator_disable(st->reg);
  143. }
  144. static struct hmc425a_chip_info hmc425a_chip_info_tbl[] = {
  145. [ID_HMC425A] = {
  146. .name = "hmc425a",
  147. .channels = hmc425a_channels,
  148. .num_channels = ARRAY_SIZE(hmc425a_channels),
  149. .num_gpios = 6,
  150. .gain_min = -31500,
  151. .gain_max = 0,
  152. .default_gain = -0x40, /* set default gain -31.5db*/
  153. },
  154. };
  155. static int hmc425a_probe(struct platform_device *pdev)
  156. {
  157. struct iio_dev *indio_dev;
  158. struct hmc425a_state *st;
  159. int ret;
  160. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
  161. if (!indio_dev)
  162. return -ENOMEM;
  163. st = iio_priv(indio_dev);
  164. st->type = (uintptr_t)device_get_match_data(&pdev->dev);
  165. st->chip_info = &hmc425a_chip_info_tbl[st->type];
  166. indio_dev->num_channels = st->chip_info->num_channels;
  167. indio_dev->channels = st->chip_info->channels;
  168. indio_dev->name = st->chip_info->name;
  169. st->gain = st->chip_info->default_gain;
  170. st->gpios = devm_gpiod_get_array(&pdev->dev, "ctrl", GPIOD_OUT_LOW);
  171. if (IS_ERR(st->gpios))
  172. return dev_err_probe(&pdev->dev, PTR_ERR(st->gpios),
  173. "failed to get gpios\n");
  174. if (st->gpios->ndescs != st->chip_info->num_gpios) {
  175. dev_err(&pdev->dev, "%d GPIOs needed to operate\n",
  176. st->chip_info->num_gpios);
  177. return -ENODEV;
  178. }
  179. st->reg = devm_regulator_get(&pdev->dev, "vcc-supply");
  180. if (IS_ERR(st->reg))
  181. return PTR_ERR(st->reg);
  182. ret = regulator_enable(st->reg);
  183. if (ret)
  184. return ret;
  185. ret = devm_add_action_or_reset(&pdev->dev, hmc425a_reg_disable, st);
  186. if (ret)
  187. return ret;
  188. mutex_init(&st->lock);
  189. indio_dev->info = &hmc425a_info;
  190. indio_dev->modes = INDIO_DIRECT_MODE;
  191. return devm_iio_device_register(&pdev->dev, indio_dev);
  192. }
  193. static struct platform_driver hmc425a_driver = {
  194. .driver = {
  195. .name = KBUILD_MODNAME,
  196. .of_match_table = hmc425a_of_match,
  197. },
  198. .probe = hmc425a_probe,
  199. };
  200. module_platform_driver(hmc425a_driver);
  201. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  202. MODULE_DESCRIPTION("Analog Devices HMC425A and similar GPIO control Gain Amplifiers");
  203. MODULE_LICENSE("GPL v2");