linux/drivers/hwmon/da9052-hwmon.c
<<
>>
Prefs
   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
  29struct 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
  38static 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 */
  55static 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 */
  61static inline int input_reg_to_mv(int value)
  62{
  63        return DIV_ROUND_CLOSEST(value * 2500, 1023);
  64}
  65
  66/* Conversion function for VBBAT */
  67static inline int vbbat_reg_to_mv(int value)
  68{
  69        return DIV_ROUND_CLOSEST(value * 5000, 1023);
  70}
  71
  72static 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
  77static 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
  84static 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
  90static ssize_t da9052_vddout_show(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
 115hwmon_err_release:
 116        da9052_disable_vddout_channel(hwmon->da9052);
 117hwmon_err:
 118        mutex_unlock(&hwmon->hwmon_lock);
 119        return ret;
 120}
 121
 122static ssize_t da9052_ich_show(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
 136static ssize_t da9052_tbat_show(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
 144static ssize_t da9052_vbat_show(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
 157static ssize_t da9052_misc_channel_show(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
 172static 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
 194static 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
 226static 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
 245static ssize_t da9052_tsi_show(struct device *dev,
 246                               struct device_attribute *devattr, char *buf)
 247{
 248        struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
 249        int channel = to_sensor_dev_attr(devattr)->index;
 250        int ret;
 251
 252        mutex_lock(&hwmon->hwmon_lock);
 253        ret = __da9052_read_tsi(dev, channel);
 254        mutex_unlock(&hwmon->hwmon_lock);
 255
 256        if (ret < 0)
 257                return ret;
 258        else
 259                return sprintf(buf, "%d\n", input_tsireg_to_mv(hwmon, ret));
 260}
 261
 262static ssize_t da9052_tjunc_show(struct device *dev,
 263                                 struct device_attribute *devattr, char *buf)
 264{
 265        struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
 266        int tjunc;
 267        int toffset;
 268
 269        tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG);
 270        if (tjunc < 0)
 271                return tjunc;
 272
 273        toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG);
 274        if (toffset < 0)
 275                return toffset;
 276
 277        /*
 278         * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8
 279         * T_OFFSET is a trim value used to improve accuracy of the result
 280         */
 281        return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800);
 282}
 283
 284static ssize_t da9052_vbbat_show(struct device *dev,
 285                                 struct device_attribute *devattr, char *buf)
 286{
 287        struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
 288        int ret;
 289
 290        ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT);
 291        if (ret < 0)
 292                return ret;
 293
 294        return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret));
 295}
 296
 297static ssize_t label_show(struct device *dev,
 298                          struct device_attribute *devattr, char *buf)
 299{
 300        return sprintf(buf, "%s\n",
 301                       input_names[to_sensor_dev_attr(devattr)->index]);
 302}
 303
 304static umode_t da9052_channel_is_visible(struct kobject *kobj,
 305                                         struct attribute *attr, int index)
 306{
 307        struct device *dev = container_of(kobj, struct device, kobj);
 308        struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
 309        struct device_attribute *dattr = container_of(attr,
 310                                struct device_attribute, attr);
 311        struct sensor_device_attribute *sattr = to_sensor_dev_attr(dattr);
 312
 313        if (!hwmon->tsi_as_adc) {
 314                switch (sattr->index) {
 315                case DA9052_ADC_TSI_XP:
 316                case DA9052_ADC_TSI_YP:
 317                case DA9052_ADC_TSI_XN:
 318                case DA9052_ADC_TSI_YN:
 319                        return 0;
 320                }
 321        }
 322
 323        return attr->mode;
 324}
 325
 326static SENSOR_DEVICE_ATTR_RO(in0_input, da9052_vddout, DA9052_ADC_VDDOUT);
 327static SENSOR_DEVICE_ATTR_RO(in0_label, label, DA9052_ADC_VDDOUT);
 328static SENSOR_DEVICE_ATTR_RO(in3_input, da9052_vbat, DA9052_ADC_VBAT);
 329static SENSOR_DEVICE_ATTR_RO(in3_label, label, DA9052_ADC_VBAT);
 330static SENSOR_DEVICE_ATTR_RO(in4_input, da9052_misc_channel, DA9052_ADC_IN4);
 331static SENSOR_DEVICE_ATTR_RO(in4_label, label, DA9052_ADC_IN4);
 332static SENSOR_DEVICE_ATTR_RO(in5_input, da9052_misc_channel, DA9052_ADC_IN5);
 333static SENSOR_DEVICE_ATTR_RO(in5_label, label, DA9052_ADC_IN5);
 334static SENSOR_DEVICE_ATTR_RO(in6_input, da9052_misc_channel, DA9052_ADC_IN6);
 335static SENSOR_DEVICE_ATTR_RO(in6_label, label, DA9052_ADC_IN6);
 336static SENSOR_DEVICE_ATTR_RO(in9_input, da9052_vbbat, DA9052_ADC_VBBAT);
 337static SENSOR_DEVICE_ATTR_RO(in9_label, label, DA9052_ADC_VBBAT);
 338
 339static SENSOR_DEVICE_ATTR_RO(in70_input, da9052_tsi, DA9052_ADC_TSI_XP);
 340static SENSOR_DEVICE_ATTR_RO(in70_label, label, DA9052_ADC_TSI_XP);
 341static SENSOR_DEVICE_ATTR_RO(in71_input, da9052_tsi, DA9052_ADC_TSI_XN);
 342static SENSOR_DEVICE_ATTR_RO(in71_label, label, DA9052_ADC_TSI_XN);
 343static SENSOR_DEVICE_ATTR_RO(in72_input, da9052_tsi, DA9052_ADC_TSI_YP);
 344static SENSOR_DEVICE_ATTR_RO(in72_label, label, DA9052_ADC_TSI_YP);
 345static SENSOR_DEVICE_ATTR_RO(in73_input, da9052_tsi, DA9052_ADC_TSI_YN);
 346static SENSOR_DEVICE_ATTR_RO(in73_label, label, DA9052_ADC_TSI_YN);
 347
 348static SENSOR_DEVICE_ATTR_RO(curr1_input, da9052_ich, DA9052_ADC_ICH);
 349static SENSOR_DEVICE_ATTR_RO(curr1_label, label, DA9052_ADC_ICH);
 350
 351static SENSOR_DEVICE_ATTR_RO(temp2_input, da9052_tbat, DA9052_ADC_TBAT);
 352static SENSOR_DEVICE_ATTR_RO(temp2_label, label, DA9052_ADC_TBAT);
 353static SENSOR_DEVICE_ATTR_RO(temp8_input, da9052_tjunc, DA9052_ADC_TJUNC);
 354static SENSOR_DEVICE_ATTR_RO(temp8_label, label, DA9052_ADC_TJUNC);
 355
 356static struct attribute *da9052_attrs[] = {
 357        &sensor_dev_attr_in0_input.dev_attr.attr,
 358        &sensor_dev_attr_in0_label.dev_attr.attr,
 359        &sensor_dev_attr_in3_input.dev_attr.attr,
 360        &sensor_dev_attr_in3_label.dev_attr.attr,
 361        &sensor_dev_attr_in4_input.dev_attr.attr,
 362        &sensor_dev_attr_in4_label.dev_attr.attr,
 363        &sensor_dev_attr_in5_input.dev_attr.attr,
 364        &sensor_dev_attr_in5_label.dev_attr.attr,
 365        &sensor_dev_attr_in6_input.dev_attr.attr,
 366        &sensor_dev_attr_in6_label.dev_attr.attr,
 367        &sensor_dev_attr_in70_input.dev_attr.attr,
 368        &sensor_dev_attr_in70_label.dev_attr.attr,
 369        &sensor_dev_attr_in71_input.dev_attr.attr,
 370        &sensor_dev_attr_in71_label.dev_attr.attr,
 371        &sensor_dev_attr_in72_input.dev_attr.attr,
 372        &sensor_dev_attr_in72_label.dev_attr.attr,
 373        &sensor_dev_attr_in73_input.dev_attr.attr,
 374        &sensor_dev_attr_in73_label.dev_attr.attr,
 375        &sensor_dev_attr_in9_input.dev_attr.attr,
 376        &sensor_dev_attr_in9_label.dev_attr.attr,
 377        &sensor_dev_attr_curr1_input.dev_attr.attr,
 378        &sensor_dev_attr_curr1_label.dev_attr.attr,
 379        &sensor_dev_attr_temp2_input.dev_attr.attr,
 380        &sensor_dev_attr_temp2_label.dev_attr.attr,
 381        &sensor_dev_attr_temp8_input.dev_attr.attr,
 382        &sensor_dev_attr_temp8_label.dev_attr.attr,
 383        NULL
 384};
 385
 386static const struct attribute_group da9052_group = {
 387        .attrs = da9052_attrs,
 388        .is_visible = da9052_channel_is_visible,
 389};
 390__ATTRIBUTE_GROUPS(da9052);
 391
 392static irqreturn_t da9052_tsi_datardy_irq(int irq, void *data)
 393{
 394        struct da9052_hwmon *hwmon = data;
 395
 396        complete(&hwmon->tsidone);
 397        return IRQ_HANDLED;
 398}
 399
 400static int da9052_hwmon_probe(struct platform_device *pdev)
 401{
 402        struct device *dev = &pdev->dev;
 403        struct da9052_hwmon *hwmon;
 404        struct device *hwmon_dev;
 405        int err;
 406
 407        hwmon = devm_kzalloc(dev, sizeof(struct da9052_hwmon), GFP_KERNEL);
 408        if (!hwmon)
 409                return -ENOMEM;
 410
 411        platform_set_drvdata(pdev, hwmon);
 412
 413        mutex_init(&hwmon->hwmon_lock);
 414        hwmon->da9052 = dev_get_drvdata(pdev->dev.parent);
 415
 416        init_completion(&hwmon->tsidone);
 417
 418        hwmon->tsi_as_adc =
 419                device_property_read_bool(pdev->dev.parent, "dlg,tsi-as-adc");
 420
 421        if (hwmon->tsi_as_adc) {
 422                hwmon->tsiref = devm_regulator_get(pdev->dev.parent, "tsiref");
 423                if (IS_ERR(hwmon->tsiref)) {
 424                        err = PTR_ERR(hwmon->tsiref);
 425                        dev_err(&pdev->dev, "failed to get tsiref: %d", err);
 426                        return err;
 427                }
 428
 429                err = regulator_enable(hwmon->tsiref);
 430                if (err)
 431                        return err;
 432
 433                hwmon->tsiref_mv = regulator_get_voltage(hwmon->tsiref);
 434                if (hwmon->tsiref_mv < 0) {
 435                        err = hwmon->tsiref_mv;
 436                        goto exit_regulator;
 437                }
 438
 439                /* convert from microvolt (DT) to millivolt (hwmon) */
 440                hwmon->tsiref_mv /= 1000;
 441
 442                /* TSIREF limits from datasheet */
 443                if (hwmon->tsiref_mv < 1800 || hwmon->tsiref_mv > 2600) {
 444                        dev_err(hwmon->da9052->dev, "invalid TSIREF voltage: %d",
 445                                hwmon->tsiref_mv);
 446                        err = -ENXIO;
 447                        goto exit_regulator;
 448                }
 449
 450                /* disable touchscreen features */
 451                da9052_reg_write(hwmon->da9052, DA9052_TSI_CONT_A_REG, 0x00);
 452
 453                /* Sample every 1ms */
 454                da9052_reg_update(hwmon->da9052, DA9052_ADC_CONT_REG,
 455                                          DA9052_ADCCONT_ADCMODE,
 456                                          DA9052_ADCCONT_ADCMODE);
 457
 458                err = da9052_request_irq(hwmon->da9052, DA9052_IRQ_TSIREADY,
 459                                         "tsiready-irq", da9052_tsi_datardy_irq,
 460                                         hwmon);
 461                if (err) {
 462                        dev_err(&pdev->dev, "Failed to register TSIRDY IRQ: %d",
 463                                err);
 464                        goto exit_regulator;
 465                }
 466        }
 467
 468        hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9052",
 469                                                           hwmon,
 470                                                           da9052_groups);
 471        err = PTR_ERR_OR_ZERO(hwmon_dev);
 472        if (err)
 473                goto exit_irq;
 474
 475        return 0;
 476
 477exit_irq:
 478        if (hwmon->tsi_as_adc)
 479                da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
 480exit_regulator:
 481        if (hwmon->tsiref)
 482                regulator_disable(hwmon->tsiref);
 483
 484        return err;
 485}
 486
 487static int da9052_hwmon_remove(struct platform_device *pdev)
 488{
 489        struct da9052_hwmon *hwmon = platform_get_drvdata(pdev);
 490
 491        if (hwmon->tsi_as_adc) {
 492                da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
 493                regulator_disable(hwmon->tsiref);
 494        }
 495
 496        return 0;
 497}
 498
 499static struct platform_driver da9052_hwmon_driver = {
 500        .probe = da9052_hwmon_probe,
 501        .remove = da9052_hwmon_remove,
 502        .driver = {
 503                .name = "da9052-hwmon",
 504        },
 505};
 506
 507module_platform_driver(da9052_hwmon_driver);
 508
 509MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
 510MODULE_DESCRIPTION("DA9052 HWMON driver");
 511MODULE_LICENSE("GPL");
 512MODULE_ALIAS("platform:da9052-hwmon");
 513