1 /* 2 * HWMON Driver for Dialog DA9052 3 * 4 * Copyright(c) 2012 Dialog Semiconductor Ltd. 5 * 6 * Author: David Dajun Chen <dchen@diasemi.com> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2 of the License, or (at your 11 * option) any later version. 12 * 13 */ 14 15 #include <linux/err.h> 16 #include <linux/hwmon.h> 17 #include <linux/hwmon-sysfs.h> 18 #include <linux/init.h> 19 #include <linux/kernel.h> 20 #include <linux/module.h> 21 #include <linux/slab.h> 22 #include <linux/platform_device.h> 23 #include <linux/property.h> 24 25 #include <linux/mfd/da9052/da9052.h> 26 #include <linux/mfd/da9052/reg.h> 27 #include <linux/regulator/consumer.h> 28 29 struct da9052_hwmon { 30 struct da9052 *da9052; 31 struct mutex hwmon_lock; 32 bool tsi_as_adc; 33 int tsiref_mv; 34 struct regulator *tsiref; 35 struct completion tsidone; 36 }; 37 38 static const char * const input_names[] = { 39 [DA9052_ADC_VDDOUT] = "VDDOUT", 40 [DA9052_ADC_ICH] = "CHARGING CURRENT", 41 [DA9052_ADC_TBAT] = "BATTERY TEMP", 42 [DA9052_ADC_VBAT] = "BATTERY VOLTAGE", 43 [DA9052_ADC_IN4] = "ADC IN4", 44 [DA9052_ADC_IN5] = "ADC IN5", 45 [DA9052_ADC_IN6] = "ADC IN6", 46 [DA9052_ADC_TSI_XP] = "ADC TS X+", 47 [DA9052_ADC_TSI_YP] = "ADC TS Y+", 48 [DA9052_ADC_TSI_XN] = "ADC TS X-", 49 [DA9052_ADC_TSI_YN] = "ADC TS Y-", 50 [DA9052_ADC_TJUNC] = "BATTERY JUNCTION TEMP", 51 [DA9052_ADC_VBBAT] = "BACK-UP BATTERY VOLTAGE", 52 }; 53 54 /* Conversion function for VDDOUT and VBAT */ 55 static inline int volt_reg_to_mv(int value) 56 { 57 return DIV_ROUND_CLOSEST(value * 2000, 1023) + 2500; 58 } 59 60 /* Conversion function for ADC channels 4, 5 and 6 */ 61 static inline int input_reg_to_mv(int value) 62 { 63 return DIV_ROUND_CLOSEST(value * 2500, 1023); 64 } 65 66 /* Conversion function for VBBAT */ 67 static inline int vbbat_reg_to_mv(int value) 68 { 69 return DIV_ROUND_CLOSEST(value * 5000, 1023); 70 } 71 72 static inline int input_tsireg_to_mv(struct da9052_hwmon *hwmon, int value) 73 { 74 return DIV_ROUND_CLOSEST(value * hwmon->tsiref_mv, 1023); 75 } 76 77 static inline int da9052_enable_vddout_channel(struct da9052 *da9052) 78 { 79 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG, 80 DA9052_ADCCONT_AUTOVDDEN, 81 DA9052_ADCCONT_AUTOVDDEN); 82 } 83 84 static inline int da9052_disable_vddout_channel(struct da9052 *da9052) 85 { 86 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG, 87 DA9052_ADCCONT_AUTOVDDEN, 0); 88 } 89 90 static ssize_t da9052_read_vddout(struct device *dev, 91 struct device_attribute *devattr, char *buf) 92 { 93 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 94 int ret, vdd; 95 96 mutex_lock(&hwmon->hwmon_lock); 97 98 ret = da9052_enable_vddout_channel(hwmon->da9052); 99 if (ret < 0) 100 goto hwmon_err; 101 102 vdd = da9052_reg_read(hwmon->da9052, DA9052_VDD_RES_REG); 103 if (vdd < 0) { 104 ret = vdd; 105 goto hwmon_err_release; 106 } 107 108 ret = da9052_disable_vddout_channel(hwmon->da9052); 109 if (ret < 0) 110 goto hwmon_err; 111 112 mutex_unlock(&hwmon->hwmon_lock); 113 return sprintf(buf, "%d\n", volt_reg_to_mv(vdd)); 114 115 hwmon_err_release: 116 da9052_disable_vddout_channel(hwmon->da9052); 117 hwmon_err: 118 mutex_unlock(&hwmon->hwmon_lock); 119 return ret; 120 } 121 122 static ssize_t da9052_read_ich(struct device *dev, 123 struct device_attribute *devattr, char *buf) 124 { 125 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 126 int ret; 127 128 ret = da9052_reg_read(hwmon->da9052, DA9052_ICHG_AV_REG); 129 if (ret < 0) 130 return ret; 131 132 /* Equivalent to 3.9mA/bit in register ICHG_AV */ 133 return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret * 39, 10)); 134 } 135 136 static ssize_t da9052_read_tbat(struct device *dev, 137 struct device_attribute *devattr, char *buf) 138 { 139 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 140 141 return sprintf(buf, "%d\n", da9052_adc_read_temp(hwmon->da9052)); 142 } 143 144 static ssize_t da9052_read_vbat(struct device *dev, 145 struct device_attribute *devattr, char *buf) 146 { 147 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 148 int ret; 149 150 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBAT); 151 if (ret < 0) 152 return ret; 153 154 return sprintf(buf, "%d\n", volt_reg_to_mv(ret)); 155 } 156 157 static ssize_t da9052_read_misc_channel(struct device *dev, 158 struct device_attribute *devattr, 159 char *buf) 160 { 161 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 162 int channel = to_sensor_dev_attr(devattr)->index; 163 int ret; 164 165 ret = da9052_adc_manual_read(hwmon->da9052, channel); 166 if (ret < 0) 167 return ret; 168 169 return sprintf(buf, "%d\n", input_reg_to_mv(ret)); 170 } 171 172 static int da9052_request_tsi_read(struct da9052_hwmon *hwmon, int channel) 173 { 174 u8 val = DA9052_TSICONTB_TSIMAN; 175 176 switch (channel) { 177 case DA9052_ADC_TSI_XP: 178 val |= DA9052_TSICONTB_TSIMUX_XP; 179 break; 180 case DA9052_ADC_TSI_YP: 181 val |= DA9052_TSICONTB_TSIMUX_YP; 182 break; 183 case DA9052_ADC_TSI_XN: 184 val |= DA9052_TSICONTB_TSIMUX_XN; 185 break; 186 case DA9052_ADC_TSI_YN: 187 val |= DA9052_TSICONTB_TSIMUX_YN; 188 break; 189 } 190 191 return da9052_reg_write(hwmon->da9052, DA9052_TSI_CONT_B_REG, val); 192 } 193 194 static int da9052_get_tsi_result(struct da9052_hwmon *hwmon, int channel) 195 { 196 u8 regs[3]; 197 int msb, lsb, err; 198 199 /* block read to avoid separation of MSB and LSB */ 200 err = da9052_group_read(hwmon->da9052, DA9052_TSI_X_MSB_REG, 201 ARRAY_SIZE(regs), regs); 202 if (err) 203 return err; 204 205 switch (channel) { 206 case DA9052_ADC_TSI_XP: 207 case DA9052_ADC_TSI_XN: 208 msb = regs[0] << DA9052_TSILSB_TSIXL_BITS; 209 lsb = regs[2] & DA9052_TSILSB_TSIXL; 210 lsb >>= DA9052_TSILSB_TSIXL_SHIFT; 211 break; 212 case DA9052_ADC_TSI_YP: 213 case DA9052_ADC_TSI_YN: 214 msb = regs[1] << DA9052_TSILSB_TSIYL_BITS; 215 lsb = regs[2] & DA9052_TSILSB_TSIYL; 216 lsb >>= DA9052_TSILSB_TSIYL_SHIFT; 217 break; 218 default: 219 return -EINVAL; 220 } 221 222 return msb | lsb; 223 } 224 225 226 static ssize_t __da9052_read_tsi(struct device *dev, int channel) 227 { 228 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 229 int ret; 230 231 reinit_completion(&hwmon->tsidone); 232 233 ret = da9052_request_tsi_read(hwmon, channel); 234 if (ret < 0) 235 return ret; 236 237 /* Wait for an conversion done interrupt */ 238 if (!wait_for_completion_timeout(&hwmon->tsidone, 239 msecs_to_jiffies(500))) 240 return -ETIMEDOUT; 241 242 return da9052_get_tsi_result(hwmon, channel); 243 } 244 245 static ssize_t da9052_read_tsi(struct device *dev, 246 struct device_attribute *devattr, 247 char *buf) 248 { 249 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 250 int channel = to_sensor_dev_attr(devattr)->index; 251 int ret; 252 253 mutex_lock(&hwmon->hwmon_lock); 254 ret = __da9052_read_tsi(dev, channel); 255 mutex_unlock(&hwmon->hwmon_lock); 256 257 if (ret < 0) 258 return ret; 259 else 260 return sprintf(buf, "%d\n", input_tsireg_to_mv(hwmon, ret)); 261 } 262 263 static ssize_t da9052_read_tjunc(struct device *dev, 264 struct device_attribute *devattr, char *buf) 265 { 266 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 267 int tjunc; 268 int toffset; 269 270 tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG); 271 if (tjunc < 0) 272 return tjunc; 273 274 toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG); 275 if (toffset < 0) 276 return toffset; 277 278 /* 279 * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8 280 * T_OFFSET is a trim value used to improve accuracy of the result 281 */ 282 return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800); 283 } 284 285 static ssize_t da9052_read_vbbat(struct device *dev, 286 struct device_attribute *devattr, char *buf) 287 { 288 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 289 int ret; 290 291 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT); 292 if (ret < 0) 293 return ret; 294 295 return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret)); 296 } 297 298 static ssize_t show_label(struct device *dev, 299 struct device_attribute *devattr, char *buf) 300 { 301 return sprintf(buf, "%s\n", 302 input_names[to_sensor_dev_attr(devattr)->index]); 303 } 304 305 static umode_t da9052_channel_is_visible(struct kobject *kobj, 306 struct attribute *attr, int index) 307 { 308 struct device *dev = container_of(kobj, struct device, kobj); 309 struct da9052_hwmon *hwmon = dev_get_drvdata(dev); 310 struct device_attribute *dattr = container_of(attr, 311 struct device_attribute, attr); 312 struct sensor_device_attribute *sattr = to_sensor_dev_attr(dattr); 313 314 if (!hwmon->tsi_as_adc) { 315 switch (sattr->index) { 316 case DA9052_ADC_TSI_XP: 317 case DA9052_ADC_TSI_YP: 318 case DA9052_ADC_TSI_XN: 319 case DA9052_ADC_TSI_YN: 320 return 0; 321 } 322 } 323 324 return attr->mode; 325 } 326 327 static SENSOR_DEVICE_ATTR(in0_input, 0444, da9052_read_vddout, NULL, 328 DA9052_ADC_VDDOUT); 329 static SENSOR_DEVICE_ATTR(in0_label, 0444, show_label, NULL, 330 DA9052_ADC_VDDOUT); 331 static SENSOR_DEVICE_ATTR(in3_input, 0444, da9052_read_vbat, NULL, 332 DA9052_ADC_VBAT); 333 static SENSOR_DEVICE_ATTR(in3_label, 0444, show_label, NULL, 334 DA9052_ADC_VBAT); 335 static SENSOR_DEVICE_ATTR(in4_input, 0444, da9052_read_misc_channel, NULL, 336 DA9052_ADC_IN4); 337 static SENSOR_DEVICE_ATTR(in4_label, 0444, show_label, NULL, 338 DA9052_ADC_IN4); 339 static SENSOR_DEVICE_ATTR(in5_input, 0444, da9052_read_misc_channel, NULL, 340 DA9052_ADC_IN5); 341 static SENSOR_DEVICE_ATTR(in5_label, 0444, show_label, NULL, 342 DA9052_ADC_IN5); 343 static SENSOR_DEVICE_ATTR(in6_input, 0444, da9052_read_misc_channel, NULL, 344 DA9052_ADC_IN6); 345 static SENSOR_DEVICE_ATTR(in6_label, 0444, show_label, NULL, 346 DA9052_ADC_IN6); 347 static SENSOR_DEVICE_ATTR(in9_input, 0444, da9052_read_vbbat, NULL, 348 DA9052_ADC_VBBAT); 349 static SENSOR_DEVICE_ATTR(in9_label, 0444, show_label, NULL, 350 DA9052_ADC_VBBAT); 351 352 static SENSOR_DEVICE_ATTR(in70_input, 0444, da9052_read_tsi, NULL, 353 DA9052_ADC_TSI_XP); 354 static SENSOR_DEVICE_ATTR(in70_label, 0444, show_label, NULL, 355 DA9052_ADC_TSI_XP); 356 static SENSOR_DEVICE_ATTR(in71_input, 0444, da9052_read_tsi, NULL, 357 DA9052_ADC_TSI_XN); 358 static SENSOR_DEVICE_ATTR(in71_label, 0444, show_label, NULL, 359 DA9052_ADC_TSI_XN); 360 static SENSOR_DEVICE_ATTR(in72_input, 0444, da9052_read_tsi, NULL, 361 DA9052_ADC_TSI_YP); 362 static SENSOR_DEVICE_ATTR(in72_label, 0444, show_label, NULL, 363 DA9052_ADC_TSI_YP); 364 static SENSOR_DEVICE_ATTR(in73_input, 0444, da9052_read_tsi, NULL, 365 DA9052_ADC_TSI_YN); 366 static SENSOR_DEVICE_ATTR(in73_label, 0444, show_label, NULL, 367 DA9052_ADC_TSI_YN); 368 369 static SENSOR_DEVICE_ATTR(curr1_input, 0444, da9052_read_ich, NULL, 370 DA9052_ADC_ICH); 371 static SENSOR_DEVICE_ATTR(curr1_label, 0444, show_label, NULL, 372 DA9052_ADC_ICH); 373 374 static SENSOR_DEVICE_ATTR(temp2_input, 0444, da9052_read_tbat, NULL, 375 DA9052_ADC_TBAT); 376 static SENSOR_DEVICE_ATTR(temp2_label, 0444, show_label, NULL, 377 DA9052_ADC_TBAT); 378 static SENSOR_DEVICE_ATTR(temp8_input, 0444, da9052_read_tjunc, NULL, 379 DA9052_ADC_TJUNC); 380 static SENSOR_DEVICE_ATTR(temp8_label, 0444, show_label, NULL, 381 DA9052_ADC_TJUNC); 382 383 static struct attribute *da9052_attrs[] = { 384 &sensor_dev_attr_in0_input.dev_attr.attr, 385 &sensor_dev_attr_in0_label.dev_attr.attr, 386 &sensor_dev_attr_in3_input.dev_attr.attr, 387 &sensor_dev_attr_in3_label.dev_attr.attr, 388 &sensor_dev_attr_in4_input.dev_attr.attr, 389 &sensor_dev_attr_in4_label.dev_attr.attr, 390 &sensor_dev_attr_in5_input.dev_attr.attr, 391 &sensor_dev_attr_in5_label.dev_attr.attr, 392 &sensor_dev_attr_in6_input.dev_attr.attr, 393 &sensor_dev_attr_in6_label.dev_attr.attr, 394 &sensor_dev_attr_in70_input.dev_attr.attr, 395 &sensor_dev_attr_in70_label.dev_attr.attr, 396 &sensor_dev_attr_in71_input.dev_attr.attr, 397 &sensor_dev_attr_in71_label.dev_attr.attr, 398 &sensor_dev_attr_in72_input.dev_attr.attr, 399 &sensor_dev_attr_in72_label.dev_attr.attr, 400 &sensor_dev_attr_in73_input.dev_attr.attr, 401 &sensor_dev_attr_in73_label.dev_attr.attr, 402 &sensor_dev_attr_in9_input.dev_attr.attr, 403 &sensor_dev_attr_in9_label.dev_attr.attr, 404 &sensor_dev_attr_curr1_input.dev_attr.attr, 405 &sensor_dev_attr_curr1_label.dev_attr.attr, 406 &sensor_dev_attr_temp2_input.dev_attr.attr, 407 &sensor_dev_attr_temp2_label.dev_attr.attr, 408 &sensor_dev_attr_temp8_input.dev_attr.attr, 409 &sensor_dev_attr_temp8_label.dev_attr.attr, 410 NULL 411 }; 412 413 static const struct attribute_group da9052_group = { 414 .attrs = da9052_attrs, 415 .is_visible = da9052_channel_is_visible, 416 }; 417 __ATTRIBUTE_GROUPS(da9052); 418 419 static irqreturn_t da9052_tsi_datardy_irq(int irq, void *data) 420 { 421 struct da9052_hwmon *hwmon = data; 422 423 complete(&hwmon->tsidone); 424 return IRQ_HANDLED; 425 } 426 427 static int da9052_hwmon_probe(struct platform_device *pdev) 428 { 429 struct device *dev = &pdev->dev; 430 struct da9052_hwmon *hwmon; 431 struct device *hwmon_dev; 432 int err; 433 434 hwmon = devm_kzalloc(dev, sizeof(struct da9052_hwmon), GFP_KERNEL); 435 if (!hwmon) 436 return -ENOMEM; 437 438 platform_set_drvdata(pdev, hwmon); 439 440 mutex_init(&hwmon->hwmon_lock); 441 hwmon->da9052 = dev_get_drvdata(pdev->dev.parent); 442 443 init_completion(&hwmon->tsidone); 444 445 hwmon->tsi_as_adc = 446 device_property_read_bool(pdev->dev.parent, "dlg,tsi-as-adc"); 447 448 if (hwmon->tsi_as_adc) { 449 hwmon->tsiref = devm_regulator_get(pdev->dev.parent, "tsiref"); 450 if (IS_ERR(hwmon->tsiref)) { 451 err = PTR_ERR(hwmon->tsiref); 452 dev_err(&pdev->dev, "failed to get tsiref: %d", err); 453 return err; 454 } 455 456 err = regulator_enable(hwmon->tsiref); 457 if (err) 458 return err; 459 460 hwmon->tsiref_mv = regulator_get_voltage(hwmon->tsiref); 461 if (hwmon->tsiref_mv < 0) { 462 err = hwmon->tsiref_mv; 463 goto exit_regulator; 464 } 465 466 /* convert from microvolt (DT) to millivolt (hwmon) */ 467 hwmon->tsiref_mv /= 1000; 468 469 /* TSIREF limits from datasheet */ 470 if (hwmon->tsiref_mv < 1800 || hwmon->tsiref_mv > 2600) { 471 dev_err(hwmon->da9052->dev, "invalid TSIREF voltage: %d", 472 hwmon->tsiref_mv); 473 err = -ENXIO; 474 goto exit_regulator; 475 } 476 477 /* disable touchscreen features */ 478 da9052_reg_write(hwmon->da9052, DA9052_TSI_CONT_A_REG, 0x00); 479 480 /* Sample every 1ms */ 481 da9052_reg_update(hwmon->da9052, DA9052_ADC_CONT_REG, 482 DA9052_ADCCONT_ADCMODE, 483 DA9052_ADCCONT_ADCMODE); 484 485 err = da9052_request_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, 486 "tsiready-irq", da9052_tsi_datardy_irq, 487 hwmon); 488 if (err) { 489 dev_err(&pdev->dev, "Failed to register TSIRDY IRQ: %d", 490 err); 491 goto exit_regulator; 492 } 493 } 494 495 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9052", 496 hwmon, 497 da9052_groups); 498 err = PTR_ERR_OR_ZERO(hwmon_dev); 499 if (err) 500 goto exit_irq; 501 502 return 0; 503 504 exit_irq: 505 if (hwmon->tsi_as_adc) 506 da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon); 507 exit_regulator: 508 if (hwmon->tsiref) 509 regulator_disable(hwmon->tsiref); 510 511 return err; 512 } 513 514 static int da9052_hwmon_remove(struct platform_device *pdev) 515 { 516 struct da9052_hwmon *hwmon = platform_get_drvdata(pdev); 517 518 if (hwmon->tsi_as_adc) { 519 da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon); 520 regulator_disable(hwmon->tsiref); 521 } 522 523 return 0; 524 } 525 526 static struct platform_driver da9052_hwmon_driver = { 527 .probe = da9052_hwmon_probe, 528 .remove = da9052_hwmon_remove, 529 .driver = { 530 .name = "da9052-hwmon", 531 }, 532 }; 533 534 module_platform_driver(da9052_hwmon_driver); 535 536 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); 537 MODULE_DESCRIPTION("DA9052 HWMON driver"); 538 MODULE_LICENSE("GPL"); 539 MODULE_ALIAS("platform:da9052-hwmon"); 540