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