max34408.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IIO driver for Maxim MAX34409/34408 ADC, 4-Channels/2-Channels, 8bits, I2C
  4. *
  5. * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX34408-MAX34409.pdf
  6. *
  7. * TODO: ALERT interrupt, Overcurrent delay, Shutdown delay
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/init.h>
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/property.h>
  15. #include <linux/regmap.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/types.h>
  18. #define MAX34408_STATUS_REG 0x0
  19. #define MAX34408_CONTROL_REG 0x1
  20. #define MAX34408_OCDELAY_REG 0x2
  21. #define MAX34408_SDDELAY_REG 0x3
  22. #define MAX34408_ADC1_REG 0x4
  23. #define MAX34408_ADC2_REG 0x5
  24. /* ADC3 & ADC4 always returns 0x0 on 34408 */
  25. #define MAX34409_ADC3_REG 0x6
  26. #define MAX34409_ADC4_REG 0x7
  27. #define MAX34408_OCT1_REG 0x8
  28. #define MAX34408_OCT2_REG 0x9
  29. #define MAX34409_OCT3_REG 0xA
  30. #define MAX34409_OCT4_REG 0xB
  31. #define MAX34408_DID_REG 0xC
  32. #define MAX34408_DCYY_REG 0xD
  33. #define MAX34408_DCWW_REG 0xE
  34. /* Bit masks for status register */
  35. #define MAX34408_STATUS_OC_MSK GENMASK(1, 0)
  36. #define MAX34409_STATUS_OC_MSK GENMASK(3, 0)
  37. #define MAX34408_STATUS_SHTDN BIT(4)
  38. #define MAX34408_STATUS_ENA BIT(5)
  39. /* Bit masks for control register */
  40. #define MAX34408_CONTROL_AVG0 BIT(0)
  41. #define MAX34408_CONTROL_AVG1 BIT(1)
  42. #define MAX34408_CONTROL_AVG2 BIT(2)
  43. #define MAX34408_CONTROL_ALERT BIT(3)
  44. #define MAX34408_DEFAULT_AVG 0x4
  45. /* Bit masks for over current delay */
  46. #define MAX34408_OCDELAY_OCD_MSK GENMASK(6, 0)
  47. #define MAX34408_OCDELAY_RESET BIT(7)
  48. /* Bit masks for shutdown delay */
  49. #define MAX34408_SDDELAY_SHD_MSK GENMASK(6, 0)
  50. #define MAX34408_SDDELAY_RESET BIT(7)
  51. #define MAX34408_DEFAULT_RSENSE 1000
  52. /**
  53. * struct max34408_data - max34408/max34409 specific data.
  54. * @regmap: device register map.
  55. * @dev: max34408 device.
  56. * @lock: lock for protecting access to device hardware registers, mostly
  57. * for read modify write cycles for control registers.
  58. * @input_rsense: Rsense values in uOhm, will be overwritten by
  59. * values from channel nodes.
  60. */
  61. struct max34408_data {
  62. struct regmap *regmap;
  63. struct device *dev;
  64. struct mutex lock;
  65. u32 input_rsense[4];
  66. };
  67. static const struct regmap_config max34408_regmap_config = {
  68. .reg_bits = 8,
  69. .val_bits = 8,
  70. .max_register = MAX34408_DCWW_REG,
  71. };
  72. struct max34408_adc_model_data {
  73. const char *model_name;
  74. const struct iio_chan_spec *channels;
  75. const int num_channels;
  76. };
  77. #define MAX34008_CHANNEL(_index, _address) \
  78. { \
  79. .type = IIO_CURRENT, \
  80. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  81. BIT(IIO_CHAN_INFO_SCALE) | \
  82. BIT(IIO_CHAN_INFO_OFFSET), \
  83. .channel = (_index), \
  84. .address = (_address), \
  85. .indexed = 1, \
  86. }
  87. static const struct iio_chan_spec max34408_channels[] = {
  88. MAX34008_CHANNEL(0, MAX34408_ADC1_REG),
  89. MAX34008_CHANNEL(1, MAX34408_ADC2_REG),
  90. };
  91. static const struct iio_chan_spec max34409_channels[] = {
  92. MAX34008_CHANNEL(0, MAX34408_ADC1_REG),
  93. MAX34008_CHANNEL(1, MAX34408_ADC2_REG),
  94. MAX34008_CHANNEL(2, MAX34409_ADC3_REG),
  95. MAX34008_CHANNEL(3, MAX34409_ADC4_REG),
  96. };
  97. static int max34408_read_adc_avg(struct max34408_data *max34408,
  98. const struct iio_chan_spec *chan, int *val)
  99. {
  100. unsigned int ctrl;
  101. int rc;
  102. guard(mutex)(&max34408->lock);
  103. rc = regmap_read(max34408->regmap, MAX34408_CONTROL_REG, (u32 *)&ctrl);
  104. if (rc)
  105. return rc;
  106. /* set averaging (0b100) default values*/
  107. rc = regmap_write(max34408->regmap, MAX34408_CONTROL_REG,
  108. MAX34408_DEFAULT_AVG);
  109. if (rc) {
  110. dev_err(max34408->dev,
  111. "Error (%d) writing control register\n", rc);
  112. return rc;
  113. }
  114. rc = regmap_read(max34408->regmap, chan->address, val);
  115. if (rc)
  116. return rc;
  117. /* back to old values */
  118. rc = regmap_write(max34408->regmap, MAX34408_CONTROL_REG, ctrl);
  119. if (rc)
  120. dev_err(max34408->dev,
  121. "Error (%d) writing control register\n", rc);
  122. return rc;
  123. }
  124. static int max34408_read_raw(struct iio_dev *indio_dev,
  125. struct iio_chan_spec const *chan,
  126. int *val, int *val2, long mask)
  127. {
  128. struct max34408_data *max34408 = iio_priv(indio_dev);
  129. int rc;
  130. switch (mask) {
  131. case IIO_CHAN_INFO_RAW:
  132. rc = max34408_read_adc_avg(max34408, chan, val);
  133. if (rc)
  134. return rc;
  135. return IIO_VAL_INT;
  136. case IIO_CHAN_INFO_SCALE:
  137. /*
  138. * calcluate current for 8bit ADC with Rsense
  139. * value.
  140. * 10 mV * 1000 / Rsense uOhm = max current
  141. * (max current * adc val * 1000) / (2^8 - 1) mA
  142. */
  143. *val = 10000 / max34408->input_rsense[chan->channel];
  144. *val2 = 8;
  145. return IIO_VAL_FRACTIONAL_LOG2;
  146. default:
  147. return -EINVAL;
  148. }
  149. }
  150. static const struct iio_info max34408_info = {
  151. .read_raw = max34408_read_raw,
  152. };
  153. static const struct max34408_adc_model_data max34408_model_data = {
  154. .model_name = "max34408",
  155. .channels = max34408_channels,
  156. .num_channels = 2,
  157. };
  158. static const struct max34408_adc_model_data max34409_model_data = {
  159. .model_name = "max34409",
  160. .channels = max34409_channels,
  161. .num_channels = 4,
  162. };
  163. static int max34408_probe(struct i2c_client *client)
  164. {
  165. const struct max34408_adc_model_data *model_data;
  166. struct device *dev = &client->dev;
  167. struct max34408_data *max34408;
  168. struct fwnode_handle *node;
  169. struct iio_dev *indio_dev;
  170. struct regmap *regmap;
  171. int rc, i = 0;
  172. model_data = i2c_get_match_data(client);
  173. if (!model_data)
  174. return -EINVAL;
  175. regmap = devm_regmap_init_i2c(client, &max34408_regmap_config);
  176. if (IS_ERR(regmap)) {
  177. dev_err_probe(dev, PTR_ERR(regmap),
  178. "regmap_init failed\n");
  179. return PTR_ERR(regmap);
  180. }
  181. indio_dev = devm_iio_device_alloc(dev, sizeof(*max34408));
  182. if (!indio_dev)
  183. return -ENOMEM;
  184. max34408 = iio_priv(indio_dev);
  185. max34408->regmap = regmap;
  186. max34408->dev = dev;
  187. mutex_init(&max34408->lock);
  188. device_for_each_child_node(dev, node) {
  189. fwnode_property_read_u32(node, "maxim,rsense-val-micro-ohms",
  190. &max34408->input_rsense[i]);
  191. i++;
  192. }
  193. /* disable ALERT and averaging */
  194. rc = regmap_write(max34408->regmap, MAX34408_CONTROL_REG, 0x0);
  195. if (rc)
  196. return rc;
  197. indio_dev->channels = model_data->channels;
  198. indio_dev->num_channels = model_data->num_channels;
  199. indio_dev->name = model_data->model_name;
  200. indio_dev->info = &max34408_info;
  201. indio_dev->modes = INDIO_DIRECT_MODE;
  202. return devm_iio_device_register(dev, indio_dev);
  203. }
  204. static const struct of_device_id max34408_of_match[] = {
  205. {
  206. .compatible = "maxim,max34408",
  207. .data = &max34408_model_data,
  208. },
  209. {
  210. .compatible = "maxim,max34409",
  211. .data = &max34409_model_data,
  212. },
  213. { }
  214. };
  215. MODULE_DEVICE_TABLE(of, max34408_of_match);
  216. static const struct i2c_device_id max34408_id[] = {
  217. { "max34408", (kernel_ulong_t)&max34408_model_data },
  218. { "max34409", (kernel_ulong_t)&max34409_model_data },
  219. { }
  220. };
  221. MODULE_DEVICE_TABLE(i2c, max34408_id);
  222. static struct i2c_driver max34408_driver = {
  223. .driver = {
  224. .name = "max34408",
  225. .of_match_table = max34408_of_match,
  226. },
  227. .probe = max34408_probe,
  228. .id_table = max34408_id,
  229. };
  230. module_i2c_driver(max34408_driver);
  231. MODULE_AUTHOR("Ivan Mikhaylov <fr0st61te@gmail.com>");
  232. MODULE_DESCRIPTION("Maxim MAX34408/34409 ADC driver");
  233. MODULE_LICENSE("GPL");