Merge branch 'work.regset' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / drivers / hwmon / adm1177.c
blobd314223a404a4b42936317657b7412a54f6ff08f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ADM1177 Hot Swap Controller and Digital Power Monitor with Soft Start Pin
5 * Copyright 2015-2019 Analog Devices Inc.
6 */
8 #include <linux/bits.h>
9 #include <linux/device.h>
10 #include <linux/hwmon.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #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)
21 /* Extended Register */
22 #define ADM1177_REG_ALERT_TH 2
24 #define ADM1177_BITS 12
26 /**
27 * struct adm1177_state - driver instance specific data
28 * @client pointer to i2c client
29 * @reg regulator info for the the power supply of the device
30 * @r_sense_uohm current sense resistor value
31 * @alert_threshold_ua current limit for shutdown
32 * @vrange_high internal voltage divider
34 struct adm1177_state {
35 struct i2c_client *client;
36 struct regulator *reg;
37 u32 r_sense_uohm;
38 u32 alert_threshold_ua;
39 bool vrange_high;
42 static int adm1177_read_raw(struct adm1177_state *st, u8 num, u8 *data)
44 return i2c_master_recv(st->client, data, num);
47 static int adm1177_write_cmd(struct adm1177_state *st, u8 cmd)
49 return i2c_smbus_write_byte(st->client, cmd);
52 static int adm1177_write_alert_thr(struct adm1177_state *st,
53 u32 alert_threshold_ua)
55 u64 val;
56 int ret;
58 val = 0xFFULL * alert_threshold_ua * st->r_sense_uohm;
59 val = div_u64(val, 105840000U);
60 val = div_u64(val, 1000U);
61 if (val > 0xFF)
62 val = 0xFF;
64 ret = i2c_smbus_write_byte_data(st->client, ADM1177_REG_ALERT_TH,
65 val);
66 if (ret)
67 return ret;
69 st->alert_threshold_ua = alert_threshold_ua;
70 return 0;
73 static int adm1177_read(struct device *dev, enum hwmon_sensor_types type,
74 u32 attr, int channel, long *val)
76 struct adm1177_state *st = dev_get_drvdata(dev);
77 u8 data[3];
78 long dummy;
79 int ret;
81 switch (type) {
82 case hwmon_curr:
83 switch (attr) {
84 case hwmon_curr_input:
85 ret = adm1177_read_raw(st, 3, data);
86 if (ret < 0)
87 return ret;
88 dummy = (data[1] << 4) | (data[2] & 0xF);
90 * convert to milliamperes
91 * ((105.84mV / 4096) x raw) / senseResistor(ohm)
93 *val = div_u64((105840000ull * dummy),
94 4096 * st->r_sense_uohm);
95 return 0;
96 case hwmon_curr_max_alarm:
97 *val = st->alert_threshold_ua;
98 return 0;
99 default:
100 return -EOPNOTSUPP;
102 case hwmon_in:
103 ret = adm1177_read_raw(st, 3, data);
104 if (ret < 0)
105 return ret;
106 dummy = (data[0] << 4) | (data[2] >> 4);
108 * convert to millivolts based on resistor devision
109 * (V_fullscale / 4096) * raw
111 if (st->vrange_high)
112 dummy *= 26350;
113 else
114 dummy *= 6650;
116 *val = DIV_ROUND_CLOSEST(dummy, 4096);
117 return 0;
118 default:
119 return -EOPNOTSUPP;
123 static int adm1177_write(struct device *dev, enum hwmon_sensor_types type,
124 u32 attr, int channel, long val)
126 struct adm1177_state *st = dev_get_drvdata(dev);
128 switch (type) {
129 case hwmon_curr:
130 switch (attr) {
131 case hwmon_curr_max_alarm:
132 adm1177_write_alert_thr(st, val);
133 return 0;
134 default:
135 return -EOPNOTSUPP;
137 default:
138 return -EOPNOTSUPP;
142 static umode_t adm1177_is_visible(const void *data,
143 enum hwmon_sensor_types type,
144 u32 attr, int channel)
146 const struct adm1177_state *st = data;
148 switch (type) {
149 case hwmon_in:
150 switch (attr) {
151 case hwmon_in_input:
152 return 0444;
154 break;
155 case hwmon_curr:
156 switch (attr) {
157 case hwmon_curr_input:
158 if (st->r_sense_uohm)
159 return 0444;
160 return 0;
161 case hwmon_curr_max_alarm:
162 if (st->r_sense_uohm)
163 return 0644;
164 return 0;
166 break;
167 default:
168 break;
170 return 0;
173 static const struct hwmon_channel_info *adm1177_info[] = {
174 HWMON_CHANNEL_INFO(curr,
175 HWMON_C_INPUT | HWMON_C_MAX_ALARM),
176 HWMON_CHANNEL_INFO(in,
177 HWMON_I_INPUT),
178 NULL
181 static const struct hwmon_ops adm1177_hwmon_ops = {
182 .is_visible = adm1177_is_visible,
183 .read = adm1177_read,
184 .write = adm1177_write,
187 static const struct hwmon_chip_info adm1177_chip_info = {
188 .ops = &adm1177_hwmon_ops,
189 .info = adm1177_info,
192 static void adm1177_remove(void *data)
194 struct adm1177_state *st = data;
196 regulator_disable(st->reg);
199 static int adm1177_probe(struct i2c_client *client,
200 const struct i2c_device_id *id)
202 struct device *dev = &client->dev;
203 struct device *hwmon_dev;
204 struct adm1177_state *st;
205 u32 alert_threshold_ua;
206 int ret;
208 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
209 if (!st)
210 return -ENOMEM;
212 st->client = client;
214 st->reg = devm_regulator_get_optional(&client->dev, "vref");
215 if (IS_ERR(st->reg)) {
216 if (PTR_ERR(st->reg) == -EPROBE_DEFER)
217 return -EPROBE_DEFER;
219 st->reg = NULL;
220 } else {
221 ret = regulator_enable(st->reg);
222 if (ret)
223 return ret;
224 ret = devm_add_action_or_reset(&client->dev, adm1177_remove,
225 st);
226 if (ret)
227 return ret;
230 if (device_property_read_u32(dev, "shunt-resistor-micro-ohms",
231 &st->r_sense_uohm))
232 st->r_sense_uohm = 0;
233 if (device_property_read_u32(dev, "adi,shutdown-threshold-microamp",
234 &alert_threshold_ua)) {
235 if (st->r_sense_uohm)
237 * set maximum default value from datasheet based on
238 * shunt-resistor
240 alert_threshold_ua = div_u64(105840000000,
241 st->r_sense_uohm);
242 else
243 alert_threshold_ua = 0;
245 st->vrange_high = device_property_read_bool(dev,
246 "adi,vrange-high-enable");
247 if (alert_threshold_ua && st->r_sense_uohm)
248 adm1177_write_alert_thr(st, alert_threshold_ua);
250 ret = adm1177_write_cmd(st, ADM1177_CMD_V_CONT |
251 ADM1177_CMD_I_CONT |
252 (st->vrange_high ? 0 : ADM1177_CMD_VRANGE));
253 if (ret)
254 return ret;
256 hwmon_dev =
257 devm_hwmon_device_register_with_info(dev, client->name, st,
258 &adm1177_chip_info, NULL);
259 return PTR_ERR_OR_ZERO(hwmon_dev);
262 static const struct i2c_device_id adm1177_id[] = {
263 {"adm1177", 0},
266 MODULE_DEVICE_TABLE(i2c, adm1177_id);
268 static const struct of_device_id adm1177_dt_ids[] = {
269 { .compatible = "adi,adm1177" },
272 MODULE_DEVICE_TABLE(of, adm1177_dt_ids);
274 static struct i2c_driver adm1177_driver = {
275 .class = I2C_CLASS_HWMON,
276 .driver = {
277 .name = "adm1177",
278 .of_match_table = adm1177_dt_ids,
280 .probe = adm1177_probe,
281 .id_table = adm1177_id,
283 module_i2c_driver(adm1177_driver);
285 MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
286 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
287 MODULE_DESCRIPTION("Analog Devices ADM1177 ADC driver");
288 MODULE_LICENSE("GPL v2");