0001
0002
0003
0004
0005
0006
0007 #include <linux/delay.h>
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/slab.h>
0011 #include <linux/i2c.h>
0012 #include <linux/hwmon.h>
0013 #include <linux/hwmon-sysfs.h>
0014 #include <linux/err.h>
0015 #include <linux/mutex.h>
0016 #include <linux/device.h>
0017 #include <linux/jiffies.h>
0018 #include <linux/regmap.h>
0019 #include <linux/of.h>
0020
0021 #define DRIVER_NAME "tmp102"
0022
0023 #define TMP102_TEMP_REG 0x00
0024 #define TMP102_CONF_REG 0x01
0025
0026 #define TMP102_CONF_SD 0x0100
0027 #define TMP102_CONF_TM 0x0200
0028 #define TMP102_CONF_POL 0x0400
0029 #define TMP102_CONF_F0 0x0800
0030 #define TMP102_CONF_F1 0x1000
0031 #define TMP102_CONF_R0 0x2000
0032 #define TMP102_CONF_R1 0x4000
0033 #define TMP102_CONF_OS 0x8000
0034 #define TMP102_CONF_EM 0x0010
0035 #define TMP102_CONF_AL 0x0020
0036 #define TMP102_CONF_CR0 0x0040
0037 #define TMP102_CONF_CR1 0x0080
0038 #define TMP102_TLOW_REG 0x02
0039 #define TMP102_THIGH_REG 0x03
0040
0041 #define TMP102_CONFREG_MASK (TMP102_CONF_SD | TMP102_CONF_TM | \
0042 TMP102_CONF_POL | TMP102_CONF_F0 | \
0043 TMP102_CONF_F1 | TMP102_CONF_OS | \
0044 TMP102_CONF_EM | TMP102_CONF_AL | \
0045 TMP102_CONF_CR0 | TMP102_CONF_CR1)
0046
0047 #define TMP102_CONFIG_CLEAR (TMP102_CONF_SD | TMP102_CONF_OS | \
0048 TMP102_CONF_CR0)
0049 #define TMP102_CONFIG_SET (TMP102_CONF_TM | TMP102_CONF_EM | \
0050 TMP102_CONF_CR1)
0051
0052 #define CONVERSION_TIME_MS 35
0053
0054 struct tmp102 {
0055 struct regmap *regmap;
0056 u16 config_orig;
0057 unsigned long ready_time;
0058 };
0059
0060
0061 static inline int tmp102_reg_to_mC(s16 val)
0062 {
0063 return ((val & ~0x01) * 1000) / 128;
0064 }
0065
0066
0067 static inline u16 tmp102_mC_to_reg(int val)
0068 {
0069 return (val * 128) / 1000;
0070 }
0071
0072 static int tmp102_read(struct device *dev, enum hwmon_sensor_types type,
0073 u32 attr, int channel, long *temp)
0074 {
0075 struct tmp102 *tmp102 = dev_get_drvdata(dev);
0076 unsigned int regval;
0077 int err, reg;
0078
0079 switch (attr) {
0080 case hwmon_temp_input:
0081
0082 if (time_before(jiffies, tmp102->ready_time)) {
0083 dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
0084 return -EAGAIN;
0085 }
0086 reg = TMP102_TEMP_REG;
0087 break;
0088 case hwmon_temp_max_hyst:
0089 reg = TMP102_TLOW_REG;
0090 break;
0091 case hwmon_temp_max:
0092 reg = TMP102_THIGH_REG;
0093 break;
0094 default:
0095 return -EOPNOTSUPP;
0096 }
0097
0098 err = regmap_read(tmp102->regmap, reg, ®val);
0099 if (err < 0)
0100 return err;
0101 *temp = tmp102_reg_to_mC(regval);
0102
0103 return 0;
0104 }
0105
0106 static int tmp102_write(struct device *dev, enum hwmon_sensor_types type,
0107 u32 attr, int channel, long temp)
0108 {
0109 struct tmp102 *tmp102 = dev_get_drvdata(dev);
0110 int reg;
0111
0112 switch (attr) {
0113 case hwmon_temp_max_hyst:
0114 reg = TMP102_TLOW_REG;
0115 break;
0116 case hwmon_temp_max:
0117 reg = TMP102_THIGH_REG;
0118 break;
0119 default:
0120 return -EOPNOTSUPP;
0121 }
0122
0123 temp = clamp_val(temp, -256000, 255000);
0124 return regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(temp));
0125 }
0126
0127 static umode_t tmp102_is_visible(const void *data, enum hwmon_sensor_types type,
0128 u32 attr, int channel)
0129 {
0130 if (type != hwmon_temp)
0131 return 0;
0132
0133 switch (attr) {
0134 case hwmon_temp_input:
0135 return 0444;
0136 case hwmon_temp_max_hyst:
0137 case hwmon_temp_max:
0138 return 0644;
0139 default:
0140 return 0;
0141 }
0142 }
0143
0144 static const struct hwmon_channel_info *tmp102_info[] = {
0145 HWMON_CHANNEL_INFO(chip,
0146 HWMON_C_REGISTER_TZ),
0147 HWMON_CHANNEL_INFO(temp,
0148 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
0149 NULL
0150 };
0151
0152 static const struct hwmon_ops tmp102_hwmon_ops = {
0153 .is_visible = tmp102_is_visible,
0154 .read = tmp102_read,
0155 .write = tmp102_write,
0156 };
0157
0158 static const struct hwmon_chip_info tmp102_chip_info = {
0159 .ops = &tmp102_hwmon_ops,
0160 .info = tmp102_info,
0161 };
0162
0163 static void tmp102_restore_config(void *data)
0164 {
0165 struct tmp102 *tmp102 = data;
0166
0167 regmap_write(tmp102->regmap, TMP102_CONF_REG, tmp102->config_orig);
0168 }
0169
0170 static bool tmp102_is_writeable_reg(struct device *dev, unsigned int reg)
0171 {
0172 return reg != TMP102_TEMP_REG;
0173 }
0174
0175 static bool tmp102_is_volatile_reg(struct device *dev, unsigned int reg)
0176 {
0177 return reg == TMP102_TEMP_REG;
0178 }
0179
0180 static const struct regmap_config tmp102_regmap_config = {
0181 .reg_bits = 8,
0182 .val_bits = 16,
0183 .max_register = TMP102_THIGH_REG,
0184 .writeable_reg = tmp102_is_writeable_reg,
0185 .volatile_reg = tmp102_is_volatile_reg,
0186 .val_format_endian = REGMAP_ENDIAN_BIG,
0187 .cache_type = REGCACHE_RBTREE,
0188 .use_single_read = true,
0189 .use_single_write = true,
0190 };
0191
0192 static int tmp102_probe(struct i2c_client *client)
0193 {
0194 struct device *dev = &client->dev;
0195 struct device *hwmon_dev;
0196 struct tmp102 *tmp102;
0197 unsigned int regval;
0198 int err;
0199
0200 if (!i2c_check_functionality(client->adapter,
0201 I2C_FUNC_SMBUS_WORD_DATA)) {
0202 dev_err(dev,
0203 "adapter doesn't support SMBus word transactions\n");
0204 return -ENODEV;
0205 }
0206
0207 tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL);
0208 if (!tmp102)
0209 return -ENOMEM;
0210
0211 i2c_set_clientdata(client, tmp102);
0212
0213 tmp102->regmap = devm_regmap_init_i2c(client, &tmp102_regmap_config);
0214 if (IS_ERR(tmp102->regmap))
0215 return PTR_ERR(tmp102->regmap);
0216
0217 err = regmap_read(tmp102->regmap, TMP102_CONF_REG, ®val);
0218 if (err < 0) {
0219 dev_err(dev, "error reading config register\n");
0220 return err;
0221 }
0222
0223 if ((regval & ~TMP102_CONFREG_MASK) !=
0224 (TMP102_CONF_R0 | TMP102_CONF_R1)) {
0225 dev_err(dev, "unexpected config register value\n");
0226 return -ENODEV;
0227 }
0228
0229 tmp102->config_orig = regval;
0230
0231 err = devm_add_action_or_reset(dev, tmp102_restore_config, tmp102);
0232 if (err)
0233 return err;
0234
0235 regval &= ~TMP102_CONFIG_CLEAR;
0236 regval |= TMP102_CONFIG_SET;
0237
0238 err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval);
0239 if (err < 0) {
0240 dev_err(dev, "error writing config register\n");
0241 return err;
0242 }
0243
0244
0245
0246
0247
0248 tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
0249
0250 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
0251 tmp102,
0252 &tmp102_chip_info,
0253 NULL);
0254 if (IS_ERR(hwmon_dev)) {
0255 dev_dbg(dev, "unable to register hwmon device\n");
0256 return PTR_ERR(hwmon_dev);
0257 }
0258 dev_info(dev, "initialized\n");
0259
0260 return 0;
0261 }
0262
0263 #ifdef CONFIG_PM_SLEEP
0264 static int tmp102_suspend(struct device *dev)
0265 {
0266 struct i2c_client *client = to_i2c_client(dev);
0267 struct tmp102 *tmp102 = i2c_get_clientdata(client);
0268
0269 return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
0270 TMP102_CONF_SD, TMP102_CONF_SD);
0271 }
0272
0273 static int tmp102_resume(struct device *dev)
0274 {
0275 struct i2c_client *client = to_i2c_client(dev);
0276 struct tmp102 *tmp102 = i2c_get_clientdata(client);
0277 int err;
0278
0279 err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
0280 TMP102_CONF_SD, 0);
0281
0282 tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
0283
0284 return err;
0285 }
0286 #endif
0287
0288 static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
0289
0290 static const struct i2c_device_id tmp102_id[] = {
0291 { "tmp102", 0 },
0292 { }
0293 };
0294 MODULE_DEVICE_TABLE(i2c, tmp102_id);
0295
0296 static const struct of_device_id __maybe_unused tmp102_of_match[] = {
0297 { .compatible = "ti,tmp102" },
0298 { },
0299 };
0300 MODULE_DEVICE_TABLE(of, tmp102_of_match);
0301
0302 static struct i2c_driver tmp102_driver = {
0303 .driver.name = DRIVER_NAME,
0304 .driver.of_match_table = of_match_ptr(tmp102_of_match),
0305 .driver.pm = &tmp102_dev_pm_ops,
0306 .probe_new = tmp102_probe,
0307 .id_table = tmp102_id,
0308 };
0309
0310 module_i2c_driver(tmp102_driver);
0311
0312 MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
0313 MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
0314 MODULE_LICENSE("GPL");