adm1177.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ADM1177 Hot Swap Controller and Digital Power Monitor with Soft Start Pin
  4. *
  5. * Copyright 2015-2019 Analog Devices Inc.
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/device.h>
  9. #include <linux/hwmon.h>
  10. #include <linux/i2c.h>
  11. #include <linux/init.h>
  12. #include <linux/math64.h>
  13. #include <linux/minmax.h>
  14. #include <linux/module.h>
  15. #include <linux/regulator/consumer.h>
  16. /* Command Byte Operations */
  17. #define ADM1177_CMD_V_CONT BIT(0)
  18. #define ADM1177_CMD_I_CONT BIT(2)
  19. #define ADM1177_CMD_VRANGE BIT(4)
  20. /* Extended Register */
  21. #define ADM1177_REG_ALERT_TH 2
  22. #define ADM1177_BITS 12
  23. /**
  24. * struct adm1177_state - driver instance specific data
  25. * @client: pointer to i2c client
  26. * @r_sense_uohm: current sense resistor value
  27. * @alert_threshold_ua: current limit for shutdown
  28. * @vrange_high: internal voltage divider
  29. */
  30. struct adm1177_state {
  31. struct i2c_client *client;
  32. u32 r_sense_uohm;
  33. u64 alert_threshold_ua;
  34. bool vrange_high;
  35. };
  36. static int adm1177_read_raw(struct adm1177_state *st, u8 num, u8 *data)
  37. {
  38. return i2c_master_recv(st->client, data, num);
  39. }
  40. static int adm1177_write_cmd(struct adm1177_state *st, u8 cmd)
  41. {
  42. return i2c_smbus_write_byte(st->client, cmd);
  43. }
  44. static int adm1177_write_alert_thr(struct adm1177_state *st,
  45. u64 alert_threshold_ua)
  46. {
  47. u64 val;
  48. int ret;
  49. val = 0xFFULL * alert_threshold_ua * st->r_sense_uohm;
  50. val = div_u64(val, 105840000U);
  51. val = div_u64(val, 1000U);
  52. if (val > 0xFF)
  53. val = 0xFF;
  54. ret = i2c_smbus_write_byte_data(st->client, ADM1177_REG_ALERT_TH,
  55. val);
  56. if (ret)
  57. return ret;
  58. st->alert_threshold_ua = alert_threshold_ua;
  59. return 0;
  60. }
  61. static int adm1177_read(struct device *dev, enum hwmon_sensor_types type,
  62. u32 attr, int channel, long *val)
  63. {
  64. struct adm1177_state *st = dev_get_drvdata(dev);
  65. u8 data[3];
  66. long dummy;
  67. int ret;
  68. switch (type) {
  69. case hwmon_curr:
  70. switch (attr) {
  71. case hwmon_curr_input:
  72. ret = adm1177_read_raw(st, 3, data);
  73. if (ret < 0)
  74. return ret;
  75. dummy = (data[1] << 4) | (data[2] & 0xF);
  76. /*
  77. * convert to milliamperes
  78. * ((105.84mV / 4096) x raw) / senseResistor(ohm)
  79. */
  80. *val = div_u64((105840000ull * dummy),
  81. 4096 * st->r_sense_uohm);
  82. return 0;
  83. case hwmon_curr_max:
  84. *val = div_u64(st->alert_threshold_ua, 1000);
  85. return 0;
  86. default:
  87. return -EOPNOTSUPP;
  88. }
  89. case hwmon_in:
  90. ret = adm1177_read_raw(st, 3, data);
  91. if (ret < 0)
  92. return ret;
  93. dummy = (data[0] << 4) | (data[2] >> 4);
  94. /*
  95. * convert to millivolts based on resistor devision
  96. * (V_fullscale / 4096) * raw
  97. */
  98. if (st->vrange_high)
  99. dummy *= 26350;
  100. else
  101. dummy *= 6650;
  102. *val = DIV_ROUND_CLOSEST(dummy, 4096);
  103. return 0;
  104. default:
  105. return -EOPNOTSUPP;
  106. }
  107. }
  108. static int adm1177_write(struct device *dev, enum hwmon_sensor_types type,
  109. u32 attr, int channel, long val)
  110. {
  111. struct adm1177_state *st = dev_get_drvdata(dev);
  112. switch (type) {
  113. case hwmon_curr:
  114. switch (attr) {
  115. case hwmon_curr_max:
  116. val = clamp_val(val, 0,
  117. div_u64(105840000ULL, st->r_sense_uohm));
  118. return adm1177_write_alert_thr(st, (u64)val * 1000);
  119. default:
  120. return -EOPNOTSUPP;
  121. }
  122. default:
  123. return -EOPNOTSUPP;
  124. }
  125. }
  126. static umode_t adm1177_is_visible(const void *data,
  127. enum hwmon_sensor_types type,
  128. u32 attr, int channel)
  129. {
  130. const struct adm1177_state *st = data;
  131. switch (type) {
  132. case hwmon_in:
  133. switch (attr) {
  134. case hwmon_in_input:
  135. return 0444;
  136. }
  137. break;
  138. case hwmon_curr:
  139. switch (attr) {
  140. case hwmon_curr_input:
  141. if (st->r_sense_uohm)
  142. return 0444;
  143. return 0;
  144. case hwmon_curr_max:
  145. if (st->r_sense_uohm)
  146. return 0644;
  147. return 0;
  148. }
  149. break;
  150. default:
  151. break;
  152. }
  153. return 0;
  154. }
  155. static const struct hwmon_channel_info * const adm1177_info[] = {
  156. HWMON_CHANNEL_INFO(curr,
  157. HWMON_C_INPUT | HWMON_C_MAX),
  158. HWMON_CHANNEL_INFO(in,
  159. HWMON_I_INPUT),
  160. NULL
  161. };
  162. static const struct hwmon_ops adm1177_hwmon_ops = {
  163. .is_visible = adm1177_is_visible,
  164. .read = adm1177_read,
  165. .write = adm1177_write,
  166. };
  167. static const struct hwmon_chip_info adm1177_chip_info = {
  168. .ops = &adm1177_hwmon_ops,
  169. .info = adm1177_info,
  170. };
  171. static int adm1177_probe(struct i2c_client *client)
  172. {
  173. struct device *dev = &client->dev;
  174. struct device *hwmon_dev;
  175. struct adm1177_state *st;
  176. u64 alert_threshold_ua;
  177. u32 prop;
  178. int ret;
  179. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  180. if (!st)
  181. return -ENOMEM;
  182. st->client = client;
  183. ret = devm_regulator_get_enable_optional(&client->dev, "vref");
  184. if (ret == -EPROBE_DEFER)
  185. return -EPROBE_DEFER;
  186. if (device_property_read_u32(dev, "shunt-resistor-micro-ohms",
  187. &st->r_sense_uohm))
  188. st->r_sense_uohm = 0;
  189. if (!device_property_read_u32(dev, "adi,shutdown-threshold-microamp",
  190. &prop)) {
  191. alert_threshold_ua = prop;
  192. } else if (st->r_sense_uohm) {
  193. /*
  194. * set maximum default value from datasheet based on
  195. * shunt-resistor
  196. */
  197. alert_threshold_ua = div_u64(105840000000ULL,
  198. st->r_sense_uohm);
  199. } else {
  200. alert_threshold_ua = 0;
  201. }
  202. st->vrange_high = device_property_read_bool(dev,
  203. "adi,vrange-high-enable");
  204. if (alert_threshold_ua && st->r_sense_uohm) {
  205. ret = adm1177_write_alert_thr(st, alert_threshold_ua);
  206. if (ret)
  207. return ret;
  208. }
  209. ret = adm1177_write_cmd(st, ADM1177_CMD_V_CONT |
  210. ADM1177_CMD_I_CONT |
  211. (st->vrange_high ? 0 : ADM1177_CMD_VRANGE));
  212. if (ret)
  213. return ret;
  214. hwmon_dev =
  215. devm_hwmon_device_register_with_info(dev, client->name, st,
  216. &adm1177_chip_info, NULL);
  217. return PTR_ERR_OR_ZERO(hwmon_dev);
  218. }
  219. static const struct i2c_device_id adm1177_id[] = {
  220. {"adm1177"},
  221. {}
  222. };
  223. MODULE_DEVICE_TABLE(i2c, adm1177_id);
  224. static const struct of_device_id adm1177_dt_ids[] = {
  225. { .compatible = "adi,adm1177" },
  226. {},
  227. };
  228. MODULE_DEVICE_TABLE(of, adm1177_dt_ids);
  229. static struct i2c_driver adm1177_driver = {
  230. .driver = {
  231. .name = "adm1177",
  232. .of_match_table = adm1177_dt_ids,
  233. },
  234. .probe = adm1177_probe,
  235. .id_table = adm1177_id,
  236. };
  237. module_i2c_driver(adm1177_driver);
  238. MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
  239. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  240. MODULE_DESCRIPTION("Analog Devices ADM1177 ADC driver");
  241. MODULE_LICENSE("GPL v2");