uboot/drivers/hwmon/ds1775.c
<<
>>
Prefs
   1/*
   2 * SPDX-License-Identifier:     GPL-2.0+
   3 */
   4
   5/*
   6 * Dallas Semiconductor's DS1775 Digital Thermometer and Thermostat
   7 */
   8
   9#include <common.h>
  10
  11#include <i2c.h>
  12#include <dtt.h>
  13
  14#define DTT_I2C_DEV_CODE        CONFIG_SYS_I2C_DTT_ADDR /* Dallas Semi's DS1775 device code */
  15#define DTT_READ_TEMP           0x0
  16#define DTT_CONFIG              0x1
  17#define DTT_TEMP_HYST           0x2
  18#define DTT_TEMP_OS             0x3
  19
  20int dtt_read(int sensor, int reg)
  21{
  22        int dlen;
  23        uchar data[2];
  24
  25        /*
  26         * Calculate sensor address and command
  27         */
  28        sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1775 */
  29
  30        /*
  31         * Prepare to handle 2 byte result
  32         */
  33        if ((reg == DTT_READ_TEMP) ||
  34            (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST))
  35                dlen = 2;
  36        else
  37                dlen = 1;
  38
  39        /*
  40         * Now try to read the register
  41         */
  42        if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  43                return 1;
  44
  45        /*
  46         * Handle 2 byte result
  47         */
  48        if (dlen == 2)
  49                return ((int)((short)data[1] + (((short)data[0]) << 8)));
  50
  51        return (int) data[0];
  52}
  53
  54
  55int dtt_write(int sensor, int reg, int val)
  56{
  57        int dlen;
  58        uchar data[2];
  59
  60        /*
  61         * Calculate sensor address and register
  62         */
  63        sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
  64
  65        /*
  66         * Handle various data sizes
  67         */
  68        if ((reg == DTT_READ_TEMP) ||
  69            (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST)) {
  70                dlen = 2;
  71                data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  72                data[1] = (char)(val & 0xff);
  73        } else {
  74                dlen = 1;
  75                data[0] = (char)(val & 0xff);
  76        }
  77
  78        /*
  79         * Write value to device
  80         */
  81        if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  82                return 1;
  83
  84        return 0;
  85}
  86
  87
  88int dtt_init_one(int sensor)
  89{
  90        int val;
  91
  92        /*
  93         * Setup High Temp
  94         */
  95        val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80;
  96        if (dtt_write(sensor, DTT_TEMP_OS, val) != 0)
  97                return 1;
  98        udelay(50000);                  /* Max 50ms */
  99
 100        /*
 101         * Setup Low Temp - hysteresis
 102         */
 103        val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
 104        if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
 105                return 1;
 106        udelay(50000);                  /* Max 50ms */
 107
 108        /*
 109         * Setup configuraton register
 110         *
 111         * Fault Tolerance limits 4, Thermometer resolution bits is 9,
 112         * Polarity = Active Low,continuous conversion mode, Thermostat
 113         * mode is interrupt mode
 114         */
 115        val = 0xa;
 116        if (dtt_write(sensor, DTT_CONFIG, val) != 0)
 117                return 1;
 118        udelay(50000);                  /* Max 50ms */
 119
 120        return 0;
 121}
 122
 123int dtt_get_temp(int sensor)
 124{
 125        return (dtt_read(sensor, DTT_READ_TEMP) / 256);
 126}
 127