adm1177.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/module.h>
  13. #include <linux/regulator/consumer.h>
  14. /* Command Byte Operations */
  15. #define ADM1177_CMD_V_CONT BIT(0)
  16. #define ADM1177_CMD_I_CONT BIT(2)
  17. #define ADM1177_CMD_VRANGE BIT(4)
  18. /* Extended Register */
  19. #define ADM1177_REG_ALERT_TH 2
  20. #define ADM1177_BITS 12
  21. /**
  22. * struct adm1177_state - driver instance specific data
  23. * @client: pointer to i2c client
  24. * @reg: regulator info for the power supply of the device
  25. * @r_sense_uohm: current sense resistor value
  26. * @alert_threshold_ua: current limit for shutdown
  27. * @vrange_high: internal voltage divider
  28. */
  29. struct adm1177_state {
  30. struct i2c_client *client;
  31. struct regulator *reg;
  32. u32 r_sense_uohm;
  33. u32 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. u32 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_alarm:
  84. *val = st->alert_threshold_ua;
  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_alarm:
  116. adm1177_write_alert_thr(st, val);
  117. return 0;
  118. default:
  119. return -EOPNOTSUPP;
  120. }
  121. default:
  122. return -EOPNOTSUPP;
  123. }
  124. }
  125. static umode_t adm1177_is_visible(const void *data,
  126. enum hwmon_sensor_types type,
  127. u32 attr, int channel)
  128. {
  129. const struct adm1177_state *st = data;
  130. switch (type) {
  131. case hwmon_in:
  132. switch (attr) {
  133. case hwmon_in_input:
  134. return 0444;
  135. }
  136. break;
  137. case hwmon_curr:
  138. switch (attr) {
  139. case hwmon_curr_input:
  140. if (st->r_sense_uohm)
  141. return 0444;
  142. return 0;
  143. case hwmon_curr_max_alarm:
  144. if (st->r_sense_uohm)
  145. return 0644;
  146. return 0;
  147. }
  148. break;
  149. default:
  150. break;
  151. }
  152. return 0;
  153. }
  154. static const struct hwmon_channel_info *adm1177_info[] = {
  155. HWMON_CHANNEL_INFO(curr,
  156. HWMON_C_INPUT | HWMON_C_MAX_ALARM),
  157. HWMON_CHANNEL_INFO(in,
  158. HWMON_I_INPUT),
  159. NULL
  160. };
  161. static const struct hwmon_ops adm1177_hwmon_ops = {
  162. .is_visible = adm1177_is_visible,
  163. .read = adm1177_read,
  164. .write = adm1177_write,
  165. };
  166. static const struct hwmon_chip_info adm1177_chip_info = {
  167. .ops = &adm1177_hwmon_ops,
  168. .info = adm1177_info,
  169. };
  170. static void adm1177_remove(void *data)
  171. {
  172. struct adm1177_state *st = data;
  173. regulator_disable(st->reg);
  174. }
  175. static int adm1177_probe(struct i2c_client *client)
  176. {
  177. struct device *dev = &client->dev;
  178. struct device *hwmon_dev;
  179. struct adm1177_state *st;
  180. u32 alert_threshold_ua;
  181. int ret;
  182. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  183. if (!st)
  184. return -ENOMEM;
  185. st->client = client;
  186. st->reg = devm_regulator_get_optional(&client->dev, "vref");
  187. if (IS_ERR(st->reg)) {
  188. if (PTR_ERR(st->reg) == -EPROBE_DEFER)
  189. return -EPROBE_DEFER;
  190. st->reg = NULL;
  191. } else {
  192. ret = regulator_enable(st->reg);
  193. if (ret)
  194. return ret;
  195. ret = devm_add_action_or_reset(&client->dev, adm1177_remove,
  196. st);
  197. if (ret)
  198. return ret;
  199. }
  200. if (device_property_read_u32(dev, "shunt-resistor-micro-ohms",
  201. &st->r_sense_uohm))
  202. st->r_sense_uohm = 0;
  203. if (device_property_read_u32(dev, "adi,shutdown-threshold-microamp",
  204. &alert_threshold_ua)) {
  205. if (st->r_sense_uohm)
  206. /*
  207. * set maximum default value from datasheet based on
  208. * shunt-resistor
  209. */
  210. alert_threshold_ua = div_u64(105840000000,
  211. st->r_sense_uohm);
  212. else
  213. alert_threshold_ua = 0;
  214. }
  215. st->vrange_high = device_property_read_bool(dev,
  216. "adi,vrange-high-enable");
  217. if (alert_threshold_ua && st->r_sense_uohm)
  218. adm1177_write_alert_thr(st, alert_threshold_ua);
  219. ret = adm1177_write_cmd(st, ADM1177_CMD_V_CONT |
  220. ADM1177_CMD_I_CONT |
  221. (st->vrange_high ? 0 : ADM1177_CMD_VRANGE));
  222. if (ret)
  223. return ret;
  224. hwmon_dev =
  225. devm_hwmon_device_register_with_info(dev, client->name, st,
  226. &adm1177_chip_info, NULL);
  227. return PTR_ERR_OR_ZERO(hwmon_dev);
  228. }
  229. static const struct i2c_device_id adm1177_id[] = {
  230. {"adm1177", 0},
  231. {}
  232. };
  233. MODULE_DEVICE_TABLE(i2c, adm1177_id);
  234. static const struct of_device_id adm1177_dt_ids[] = {
  235. { .compatible = "adi,adm1177" },
  236. {},
  237. };
  238. MODULE_DEVICE_TABLE(of, adm1177_dt_ids);
  239. static struct i2c_driver adm1177_driver = {
  240. .class = I2C_CLASS_HWMON,
  241. .driver = {
  242. .name = "adm1177",
  243. .of_match_table = adm1177_dt_ids,
  244. },
  245. .probe_new = adm1177_probe,
  246. .id_table = adm1177_id,
  247. };
  248. module_i2c_driver(adm1177_driver);
  249. MODULE_AUTHOR("Beniamin Bia <[email protected]>");
  250. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  251. MODULE_DESCRIPTION("Analog Devices ADM1177 ADC driver");
  252. MODULE_LICENSE("GPL v2");