0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/bits.h>
0009 #include <linux/device.h>
0010 #include <linux/hwmon.h>
0011 #include <linux/i2c.h>
0012 #include <linux/init.h>
0013 #include <linux/module.h>
0014 #include <linux/regulator/consumer.h>
0015
0016
0017 #define ADM1177_CMD_V_CONT BIT(0)
0018 #define ADM1177_CMD_I_CONT BIT(2)
0019 #define ADM1177_CMD_VRANGE BIT(4)
0020
0021
0022 #define ADM1177_REG_ALERT_TH 2
0023
0024 #define ADM1177_BITS 12
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 struct adm1177_state {
0035 struct i2c_client *client;
0036 struct regulator *reg;
0037 u32 r_sense_uohm;
0038 u32 alert_threshold_ua;
0039 bool vrange_high;
0040 };
0041
0042 static int adm1177_read_raw(struct adm1177_state *st, u8 num, u8 *data)
0043 {
0044 return i2c_master_recv(st->client, data, num);
0045 }
0046
0047 static int adm1177_write_cmd(struct adm1177_state *st, u8 cmd)
0048 {
0049 return i2c_smbus_write_byte(st->client, cmd);
0050 }
0051
0052 static int adm1177_write_alert_thr(struct adm1177_state *st,
0053 u32 alert_threshold_ua)
0054 {
0055 u64 val;
0056 int ret;
0057
0058 val = 0xFFULL * alert_threshold_ua * st->r_sense_uohm;
0059 val = div_u64(val, 105840000U);
0060 val = div_u64(val, 1000U);
0061 if (val > 0xFF)
0062 val = 0xFF;
0063
0064 ret = i2c_smbus_write_byte_data(st->client, ADM1177_REG_ALERT_TH,
0065 val);
0066 if (ret)
0067 return ret;
0068
0069 st->alert_threshold_ua = alert_threshold_ua;
0070 return 0;
0071 }
0072
0073 static int adm1177_read(struct device *dev, enum hwmon_sensor_types type,
0074 u32 attr, int channel, long *val)
0075 {
0076 struct adm1177_state *st = dev_get_drvdata(dev);
0077 u8 data[3];
0078 long dummy;
0079 int ret;
0080
0081 switch (type) {
0082 case hwmon_curr:
0083 switch (attr) {
0084 case hwmon_curr_input:
0085 ret = adm1177_read_raw(st, 3, data);
0086 if (ret < 0)
0087 return ret;
0088 dummy = (data[1] << 4) | (data[2] & 0xF);
0089
0090
0091
0092
0093 *val = div_u64((105840000ull * dummy),
0094 4096 * st->r_sense_uohm);
0095 return 0;
0096 case hwmon_curr_max_alarm:
0097 *val = st->alert_threshold_ua;
0098 return 0;
0099 default:
0100 return -EOPNOTSUPP;
0101 }
0102 case hwmon_in:
0103 ret = adm1177_read_raw(st, 3, data);
0104 if (ret < 0)
0105 return ret;
0106 dummy = (data[0] << 4) | (data[2] >> 4);
0107
0108
0109
0110
0111 if (st->vrange_high)
0112 dummy *= 26350;
0113 else
0114 dummy *= 6650;
0115
0116 *val = DIV_ROUND_CLOSEST(dummy, 4096);
0117 return 0;
0118 default:
0119 return -EOPNOTSUPP;
0120 }
0121 }
0122
0123 static int adm1177_write(struct device *dev, enum hwmon_sensor_types type,
0124 u32 attr, int channel, long val)
0125 {
0126 struct adm1177_state *st = dev_get_drvdata(dev);
0127
0128 switch (type) {
0129 case hwmon_curr:
0130 switch (attr) {
0131 case hwmon_curr_max_alarm:
0132 adm1177_write_alert_thr(st, val);
0133 return 0;
0134 default:
0135 return -EOPNOTSUPP;
0136 }
0137 default:
0138 return -EOPNOTSUPP;
0139 }
0140 }
0141
0142 static umode_t adm1177_is_visible(const void *data,
0143 enum hwmon_sensor_types type,
0144 u32 attr, int channel)
0145 {
0146 const struct adm1177_state *st = data;
0147
0148 switch (type) {
0149 case hwmon_in:
0150 switch (attr) {
0151 case hwmon_in_input:
0152 return 0444;
0153 }
0154 break;
0155 case hwmon_curr:
0156 switch (attr) {
0157 case hwmon_curr_input:
0158 if (st->r_sense_uohm)
0159 return 0444;
0160 return 0;
0161 case hwmon_curr_max_alarm:
0162 if (st->r_sense_uohm)
0163 return 0644;
0164 return 0;
0165 }
0166 break;
0167 default:
0168 break;
0169 }
0170 return 0;
0171 }
0172
0173 static const struct hwmon_channel_info *adm1177_info[] = {
0174 HWMON_CHANNEL_INFO(curr,
0175 HWMON_C_INPUT | HWMON_C_MAX_ALARM),
0176 HWMON_CHANNEL_INFO(in,
0177 HWMON_I_INPUT),
0178 NULL
0179 };
0180
0181 static const struct hwmon_ops adm1177_hwmon_ops = {
0182 .is_visible = adm1177_is_visible,
0183 .read = adm1177_read,
0184 .write = adm1177_write,
0185 };
0186
0187 static const struct hwmon_chip_info adm1177_chip_info = {
0188 .ops = &adm1177_hwmon_ops,
0189 .info = adm1177_info,
0190 };
0191
0192 static void adm1177_remove(void *data)
0193 {
0194 struct adm1177_state *st = data;
0195
0196 regulator_disable(st->reg);
0197 }
0198
0199 static int adm1177_probe(struct i2c_client *client)
0200 {
0201 struct device *dev = &client->dev;
0202 struct device *hwmon_dev;
0203 struct adm1177_state *st;
0204 u32 alert_threshold_ua;
0205 int ret;
0206
0207 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
0208 if (!st)
0209 return -ENOMEM;
0210
0211 st->client = client;
0212
0213 st->reg = devm_regulator_get_optional(&client->dev, "vref");
0214 if (IS_ERR(st->reg)) {
0215 if (PTR_ERR(st->reg) == -EPROBE_DEFER)
0216 return -EPROBE_DEFER;
0217
0218 st->reg = NULL;
0219 } else {
0220 ret = regulator_enable(st->reg);
0221 if (ret)
0222 return ret;
0223 ret = devm_add_action_or_reset(&client->dev, adm1177_remove,
0224 st);
0225 if (ret)
0226 return ret;
0227 }
0228
0229 if (device_property_read_u32(dev, "shunt-resistor-micro-ohms",
0230 &st->r_sense_uohm))
0231 st->r_sense_uohm = 0;
0232 if (device_property_read_u32(dev, "adi,shutdown-threshold-microamp",
0233 &alert_threshold_ua)) {
0234 if (st->r_sense_uohm)
0235
0236
0237
0238
0239 alert_threshold_ua = div_u64(105840000000,
0240 st->r_sense_uohm);
0241 else
0242 alert_threshold_ua = 0;
0243 }
0244 st->vrange_high = device_property_read_bool(dev,
0245 "adi,vrange-high-enable");
0246 if (alert_threshold_ua && st->r_sense_uohm)
0247 adm1177_write_alert_thr(st, alert_threshold_ua);
0248
0249 ret = adm1177_write_cmd(st, ADM1177_CMD_V_CONT |
0250 ADM1177_CMD_I_CONT |
0251 (st->vrange_high ? 0 : ADM1177_CMD_VRANGE));
0252 if (ret)
0253 return ret;
0254
0255 hwmon_dev =
0256 devm_hwmon_device_register_with_info(dev, client->name, st,
0257 &adm1177_chip_info, NULL);
0258 return PTR_ERR_OR_ZERO(hwmon_dev);
0259 }
0260
0261 static const struct i2c_device_id adm1177_id[] = {
0262 {"adm1177", 0},
0263 {}
0264 };
0265 MODULE_DEVICE_TABLE(i2c, adm1177_id);
0266
0267 static const struct of_device_id adm1177_dt_ids[] = {
0268 { .compatible = "adi,adm1177" },
0269 {},
0270 };
0271 MODULE_DEVICE_TABLE(of, adm1177_dt_ids);
0272
0273 static struct i2c_driver adm1177_driver = {
0274 .class = I2C_CLASS_HWMON,
0275 .driver = {
0276 .name = "adm1177",
0277 .of_match_table = adm1177_dt_ids,
0278 },
0279 .probe_new = adm1177_probe,
0280 .id_table = adm1177_id,
0281 };
0282 module_i2c_driver(adm1177_driver);
0283
0284 MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
0285 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
0286 MODULE_DESCRIPTION("Analog Devices ADM1177 ADC driver");
0287 MODULE_LICENSE("GPL v2");