| 1 | |
| 2 | |
| 3 | * Driver for Gateworks System Controller Hardware Monitor module
|
| 4 | *
|
| 5 | * Copyright (C) 2020 Gateworks Corporation
|
| 6 |
|
| 7 | #include <linux/hwmon.h>
|
| 8 | #include <linux/hwmon-sysfs.h>
|
| 9 | #include <linux/mfd/gsc.h>
|
| 10 | #include <linux/module.h>
|
| 11 | #include <linux/of.h>
|
| 12 | #include <linux/platform_device.h>
|
| 13 | #include <linux/regmap.h>
|
| 14 | #include <linux/slab.h>
|
| 15 |
|
| 16 | #include <linux/platform_data/gsc_hwmon.h>
|
| 17 |
|
| 18 | #define GSC_HWMON_MAX_TEMP_CH 16
|
| 19 | #define GSC_HWMON_MAX_IN_CH 16
|
| 20 | #define GSC_HWMON_MAX_FAN_CH 16
|
| 21 |
|
| 22 | #define GSC_HWMON_RESOLUTION 12
|
| 23 | #define GSC_HWMON_VREF 2500
|
| 24 |
|
| 25 | struct gsc_hwmon_data {
|
| 26 | struct gsc_dev *gsc;
|
| 27 | struct gsc_hwmon_platform_data *pdata;
|
| 28 | struct regmap *regmap;
|
| 29 | const struct gsc_hwmon_channel *temp_ch[GSC_HWMON_MAX_TEMP_CH];
|
| 30 | const struct gsc_hwmon_channel *in_ch[GSC_HWMON_MAX_IN_CH];
|
| 31 | const struct gsc_hwmon_channel *fan_ch[GSC_HWMON_MAX_FAN_CH];
|
| 32 | u32 temp_config[GSC_HWMON_MAX_TEMP_CH + 1];
|
| 33 | u32 in_config[GSC_HWMON_MAX_IN_CH + 1];
|
| 34 | u32 fan_config[GSC_HWMON_MAX_FAN_CH + 1];
|
| 35 | struct hwmon_channel_info temp_info;
|
| 36 | struct hwmon_channel_info in_info;
|
| 37 | struct hwmon_channel_info fan_info;
|
| 38 | const struct hwmon_channel_info *info[4];
|
| 39 | struct hwmon_chip_info chip;
|
| 40 | };
|
| 41 |
|
| 42 | static const struct regmap_bus gsc_hwmon_regmap_bus = {
|
| 43 | .reg_read = gsc_read,
|
| 44 | .reg_write = gsc_write,
|
| 45 | };
|
| 46 |
|
| 47 | static const struct regmap_config gsc_hwmon_regmap_config = {
|
| 48 | .reg_bits = 8,
|
| 49 | .val_bits = 8,
|
| 50 | };
|
| 51 |
|
| 52 | static ssize_t pwm_auto_point_temp_show(struct device *dev,
|
| 53 | struct device_attribute *devattr,
|
| 54 | char *buf)
|
| 55 | {
|
| 56 | struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
|
| 57 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
| 58 | u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
|
| 59 | u8 regs[2];
|
| 60 | int ret;
|
| 61 |
|
| 62 | ret = regmap_bulk_read(hwmon->regmap, reg, regs, 2);
|
| 63 | if (ret)
|
| 64 | return ret;
|
| 65 |
|
| 66 | ret = regs[0] | regs[1] << 8;
|
| 67 | return sprintf(buf, "%d\n", ret * 100);
|
| 68 | }
|
| 69 |
|
| 70 | static ssize_t pwm_auto_point_temp_store(struct device *dev,
|
| 71 | struct device_attribute *devattr,
|
| 72 | const char *buf, size_t count)
|
| 73 | {
|
| 74 | struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
|
| 75 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
| 76 | u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
|
| 77 | u8 regs[2];
|
| 78 | long temp;
|
| 79 | int err;
|
| 80 |
|
| 81 | if (kstrtol(buf, 10, &temp))
|
| 82 | return -EINVAL;
|
| 83 |
|
| 84 | temp = clamp_val(temp, 0, 100000);
|
| 85 | temp = DIV_ROUND_CLOSEST(temp, 100);
|
| 86 |
|
| 87 | regs[0] = temp & 0xff;
|
| 88 | regs[1] = (temp >> 8) & 0xff;
|
| 89 | err = regmap_bulk_write(hwmon->regmap, reg, regs, 2);
|
| 90 | if (err)
|
| 91 | return err;
|
| 92 |
|
| 93 | return count;
|
| 94 | }
|
| 95 |
|
| 96 | static ssize_t pwm_auto_point_pwm_show(struct device *dev,
|
| 97 | struct device_attribute *devattr,
|
| 98 | char *buf)
|
| 99 | {
|
| 100 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
| 101 |
|
| 102 | return sprintf(buf, "%d\n", 255 * (50 + (attr->index * 10)) / 100);
|
| 103 | }
|
| 104 |
|
| 105 | static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm_auto_point_pwm, 0);
|
| 106 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_auto_point_temp, 0);
|
| 107 |
|
| 108 | static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_pwm, pwm_auto_point_pwm, 1);
|
| 109 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp, pwm_auto_point_temp, 1);
|
| 110 |
|
| 111 | static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm_auto_point_pwm, 2);
|
| 112 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp, pwm_auto_point_temp, 2);
|
| 113 |
|
| 114 | static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point4_pwm, pwm_auto_point_pwm, 3);
|
| 115 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp, pwm_auto_point_temp, 3);
|
| 116 |
|
| 117 | static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm_auto_point_pwm, 4);
|
| 118 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point5_temp, pwm_auto_point_temp, 4);
|
| 119 |
|
| 120 | static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point6_pwm, pwm_auto_point_pwm, 5);
|
| 121 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point6_temp, pwm_auto_point_temp, 5);
|
| 122 |
|
| 123 | static struct attribute *gsc_hwmon_attributes[] = {
|
| 124 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
|
| 125 | &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
|
| 126 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
|
| 127 | &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
|
| 128 | &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
|
| 129 | &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
|
| 130 | &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
|
| 131 | &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
|
| 132 | &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
|
| 133 | &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
|
| 134 | &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
|
| 135 | &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
|
| 136 | NULL
|
| 137 | };
|
| 138 |
|
| 139 | static const struct attribute_group gsc_hwmon_group = {
|
| 140 | .attrs = gsc_hwmon_attributes,
|
| 141 | };
|
| 142 | __ATTRIBUTE_GROUPS(gsc_hwmon);
|
| 143 |
|
| 144 | static int
|
| 145 | gsc_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
|
| 146 | int channel, long *val)
|
| 147 | {
|
| 148 | struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
|
| 149 | const struct gsc_hwmon_channel *ch;
|
| 150 | int sz, ret;
|
| 151 | long tmp;
|
| 152 | u8 buf[3];
|
| 153 |
|
| 154 | switch (type) {
|
| 155 | case hwmon_in:
|
| 156 | ch = hwmon->in_ch[channel];
|
| 157 | break;
|
| 158 | case hwmon_temp:
|
| 159 | ch = hwmon->temp_ch[channel];
|
| 160 | break;
|
| 161 | case hwmon_fan:
|
| 162 | ch = hwmon->fan_ch[channel];
|
| 163 | break;
|
| 164 | default:
|
| 165 | return -EOPNOTSUPP;
|
| 166 | }
|
| 167 |
|
| 168 | sz = (ch->mode == mode_voltage_24bit) ? 3 : 2;
|
| 169 | ret = regmap_bulk_read(hwmon->regmap, ch->reg, buf, sz);
|
| 170 | if (ret)
|
| 171 | return ret;
|
| 172 |
|
| 173 | tmp = 0;
|
| 174 | while (sz-- > 0)
|
| 175 | tmp |= (buf[sz] << (8 * sz));
|
| 176 |
|
| 177 | switch (ch->mode) {
|
| 178 | case mode_temperature:
|
| 179 | if (tmp > 0x8000)
|
| 180 | tmp -= 0xffff;
|
| 181 | tmp *= 100;
|
| 182 | break;
|
| 183 | case mode_voltage_raw:
|
| 184 | tmp = clamp_val(tmp, 0, BIT(GSC_HWMON_RESOLUTION));
|
| 185 |
|
| 186 | tmp *= GSC_HWMON_VREF;
|
| 187 | tmp >>= GSC_HWMON_RESOLUTION;
|
| 188 |
|
| 189 | if (ch->vdiv[0] && ch->vdiv[1]) {
|
| 190 | tmp *= (ch->vdiv[0] + ch->vdiv[1]);
|
| 191 | tmp /= ch->vdiv[1];
|
| 192 | }
|
| 193 |
|
| 194 | tmp += ch->mvoffset;
|
| 195 | break;
|
| 196 | case mode_fan:
|
| 197 | tmp *= 30;
|
| 198 | break;
|
| 199 | case mode_voltage_24bit:
|
| 200 | case mode_voltage_16bit:
|
| 201 |
|
| 202 | break;
|
| 203 | }
|
| 204 |
|
| 205 | *val = tmp;
|
| 206 |
|
| 207 | return 0;
|
| 208 | }
|
| 209 |
|
| 210 | static int
|
| 211 | gsc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
|
| 212 | u32 attr, int channel, const char **buf)
|
| 213 | {
|
| 214 | struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
|
| 215 |
|
| 216 | switch (type) {
|
| 217 | case hwmon_in:
|
| 218 | *buf = hwmon->in_ch[channel]->name;
|
| 219 | break;
|
| 220 | case hwmon_temp:
|
| 221 | *buf = hwmon->temp_ch[channel]->name;
|
| 222 | break;
|
| 223 | case hwmon_fan:
|
| 224 | *buf = hwmon->fan_ch[channel]->name;
|
| 225 | break;
|
| 226 | default:
|
| 227 | return -ENOTSUPP;
|
| 228 | }
|
| 229 |
|
| 230 | return 0;
|
| 231 | }
|
| 232 |
|
| 233 | static const struct hwmon_ops gsc_hwmon_ops = {
|
| 234 | .visible = 0444,
|
| 235 | .read = gsc_hwmon_read,
|
| 236 | .read_string = gsc_hwmon_read_string,
|
| 237 | };
|
| 238 |
|
| 239 | static struct gsc_hwmon_platform_data *
|
| 240 | gsc_hwmon_get_devtree_pdata(struct device *dev)
|
| 241 | {
|
| 242 | struct gsc_hwmon_platform_data *pdata;
|
| 243 | struct gsc_hwmon_channel *ch;
|
| 244 | struct device_node *fan;
|
| 245 | int nchannels;
|
| 246 |
|
| 247 | nchannels = device_get_child_node_count(dev);
|
| 248 | if (nchannels == 0)
|
| 249 | return ERR_PTR(-ENODEV);
|
| 250 |
|
| 251 | pdata = devm_kzalloc(dev, struct_size(pdata, channels, nchannels),
|
| 252 | GFP_KERNEL);
|
| 253 | if (!pdata)
|
| 254 | return ERR_PTR(-ENOMEM);
|
| 255 | pdata->nchannels = nchannels;
|
| 256 |
|
| 257 |
|
| 258 | of_node_get(dev->parent->of_node);
|
| 259 | fan = of_find_compatible_node(dev->parent->of_node, NULL, "gw,gsc-fan");
|
| 260 | if (fan && of_property_read_u32(fan, "reg", &pdata->fan_base)) {
|
| 261 | of_node_put(fan);
|
| 262 | dev_err(dev, "fan node without base\n");
|
| 263 | return ERR_PTR(-EINVAL);
|
| 264 | }
|
| 265 |
|
| 266 | of_node_put(fan);
|
| 267 |
|
| 268 | ch = pdata->channels;
|
| 269 |
|
| 270 | device_for_each_child_node_scoped(dev, child) {
|
| 271 | if (fwnode_property_read_string(child, "label", &ch->name)) {
|
| 272 | dev_err(dev, "channel without label\n");
|
| 273 | return ERR_PTR(-EINVAL);
|
| 274 | }
|
| 275 | if (fwnode_property_read_u32(child, "reg", &ch->reg)) {
|
| 276 | dev_err(dev, "channel without reg\n");
|
| 277 | return ERR_PTR(-EINVAL);
|
| 278 | }
|
| 279 | if (fwnode_property_read_u32(child, "gw,mode", &ch->mode)) {
|
| 280 | dev_err(dev, "channel without mode\n");
|
| 281 | return ERR_PTR(-EINVAL);
|
| 282 | }
|
| 283 | if (ch->mode > mode_max) {
|
| 284 | dev_err(dev, "invalid channel mode\n");
|
| 285 | return ERR_PTR(-EINVAL);
|
| 286 | }
|
| 287 |
|
| 288 | if (!fwnode_property_read_u32(child,
|
| 289 | "gw,voltage-offset-microvolt",
|
| 290 | &ch->mvoffset))
|
| 291 | ch->mvoffset /= 1000;
|
| 292 | fwnode_property_read_u32_array(child,
|
| 293 | "gw,voltage-divider-ohms",
|
| 294 | ch->vdiv, ARRAY_SIZE(ch->vdiv));
|
| 295 | ch++;
|
| 296 | }
|
| 297 |
|
| 298 | return pdata;
|
| 299 | }
|
| 300 |
|
| 301 | static int gsc_hwmon_probe(struct platform_device *pdev)
|
| 302 | {
|
| 303 | struct gsc_dev *gsc = dev_get_drvdata(pdev->dev.parent);
|
| 304 | struct device *dev = &pdev->dev;
|
| 305 | struct device *hwmon_dev;
|
| 306 | struct gsc_hwmon_platform_data *pdata = dev_get_platdata(dev);
|
| 307 | struct gsc_hwmon_data *hwmon;
|
| 308 | const struct attribute_group **groups;
|
| 309 | int i, i_in, i_temp, i_fan;
|
| 310 |
|
| 311 | if (!pdata) {
|
| 312 | pdata = gsc_hwmon_get_devtree_pdata(dev);
|
| 313 | if (IS_ERR(pdata))
|
| 314 | return PTR_ERR(pdata);
|
| 315 | }
|
| 316 |
|
| 317 | hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
|
| 318 | if (!hwmon)
|
| 319 | return -ENOMEM;
|
| 320 | hwmon->gsc = gsc;
|
| 321 | hwmon->pdata = pdata;
|
| 322 |
|
| 323 | hwmon->regmap = devm_regmap_init(dev, &gsc_hwmon_regmap_bus,
|
| 324 | gsc->i2c_hwmon,
|
| 325 | &gsc_hwmon_regmap_config);
|
| 326 | if (IS_ERR(hwmon->regmap))
|
| 327 | return PTR_ERR(hwmon->regmap);
|
| 328 |
|
| 329 | for (i = 0, i_in = 0, i_temp = 0, i_fan = 0; i < hwmon->pdata->nchannels; i++) {
|
| 330 | const struct gsc_hwmon_channel *ch = &pdata->channels[i];
|
| 331 |
|
| 332 | switch (ch->mode) {
|
| 333 | case mode_temperature:
|
| 334 | if (i_temp == GSC_HWMON_MAX_TEMP_CH) {
|
| 335 | dev_err(gsc->dev, "too many temp channels\n");
|
| 336 | return -EINVAL;
|
| 337 | }
|
| 338 | hwmon->temp_ch[i_temp] = ch;
|
| 339 | hwmon->temp_config[i_temp] = HWMON_T_INPUT |
|
| 340 | HWMON_T_LABEL;
|
| 341 | i_temp++;
|
| 342 | break;
|
| 343 | case mode_fan:
|
| 344 | if (i_fan == GSC_HWMON_MAX_FAN_CH) {
|
| 345 | dev_err(gsc->dev, "too many fan channels\n");
|
| 346 | return -EINVAL;
|
| 347 | }
|
| 348 | hwmon->fan_ch[i_fan] = ch;
|
| 349 | hwmon->fan_config[i_fan] = HWMON_F_INPUT |
|
| 350 | HWMON_F_LABEL;
|
| 351 | i_fan++;
|
| 352 | break;
|
| 353 | case mode_voltage_24bit:
|
| 354 | case mode_voltage_16bit:
|
| 355 | case mode_voltage_raw:
|
| 356 | if (i_in == GSC_HWMON_MAX_IN_CH) {
|
| 357 | dev_err(gsc->dev, "too many input channels\n");
|
| 358 | return -EINVAL;
|
| 359 | }
|
| 360 | hwmon->in_ch[i_in] = ch;
|
| 361 | hwmon->in_config[i_in] =
|
| 362 | HWMON_I_INPUT | HWMON_I_LABEL;
|
| 363 | i_in++;
|
| 364 | break;
|
| 365 | default:
|
| 366 | dev_err(gsc->dev, "invalid mode: %d\n", ch->mode);
|
| 367 | return -EINVAL;
|
| 368 | }
|
| 369 | }
|
| 370 |
|
| 371 |
|
| 372 | hwmon->chip.ops = &gsc_hwmon_ops;
|
| 373 | hwmon->chip.info = hwmon->info;
|
| 374 | hwmon->info[0] = &hwmon->temp_info;
|
| 375 | hwmon->info[1] = &hwmon->in_info;
|
| 376 | hwmon->info[2] = &hwmon->fan_info;
|
| 377 | hwmon->temp_info.type = hwmon_temp;
|
| 378 | hwmon->temp_info.config = hwmon->temp_config;
|
| 379 | hwmon->in_info.type = hwmon_in;
|
| 380 | hwmon->in_info.config = hwmon->in_config;
|
| 381 | hwmon->fan_info.type = hwmon_fan;
|
| 382 | hwmon->fan_info.config = hwmon->fan_config;
|
| 383 |
|
| 384 | groups = pdata->fan_base ? gsc_hwmon_groups : NULL;
|
| 385 | hwmon_dev = devm_hwmon_device_register_with_info(dev,
|
| 386 | KBUILD_MODNAME, hwmon,
|
| 387 | &hwmon->chip, groups);
|
| 388 | return PTR_ERR_OR_ZERO(hwmon_dev);
|
| 389 | }
|
| 390 |
|
| 391 | static const struct of_device_id gsc_hwmon_of_match[] = {
|
| 392 | { .compatible = "gw,gsc-adc", },
|
| 393 | {}
|
| 394 | };
|
| 395 | MODULE_DEVICE_TABLE(of, gsc_hwmon_of_match);
|
| 396 |
|
| 397 | static struct platform_driver gsc_hwmon_driver = {
|
| 398 | .driver = {
|
| 399 | .name = "gsc-hwmon",
|
| 400 | .of_match_table = gsc_hwmon_of_match,
|
| 401 | },
|
| 402 | .probe = gsc_hwmon_probe,
|
| 403 | };
|
| 404 |
|
| 405 | module_platform_driver(gsc_hwmon_driver);
|
| 406 |
|
| 407 | MODULE_AUTHOR("Tim Harvey <[email protected]>");
|
| 408 | MODULE_DESCRIPTION("GSC hardware monitor driver");
|
| 409 | MODULE_LICENSE("GPL v2");
|