Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Texas Instruments TMP103 SMBus temperature sensor driver |
| 4 | * Copyright (C) 2014 Heiko Schocher <hs@denx.de> |
| 5 | * |
| 6 | * Based on: |
| 7 | * Texas Instruments TMP102 SMBus temperature sensor driver |
| 8 | * |
| 9 | * Copyright (C) 2010 Steven King <sfking@fdwdc.com> |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/hwmon.h> |
| 17 | #include <linux/hwmon-sysfs.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/jiffies.h> |
| 22 | #include <linux/regmap.h> |
| 23 | |
| 24 | #define TMP103_TEMP_REG 0x00 |
| 25 | #define TMP103_CONF_REG 0x01 |
| 26 | #define TMP103_TLOW_REG 0x02 |
| 27 | #define TMP103_THIGH_REG 0x03 |
| 28 | |
| 29 | #define TMP103_CONF_M0 0x01 |
| 30 | #define TMP103_CONF_M1 0x02 |
| 31 | #define TMP103_CONF_LC 0x04 |
| 32 | #define TMP103_CONF_FL 0x08 |
| 33 | #define TMP103_CONF_FH 0x10 |
| 34 | #define TMP103_CONF_CR0 0x20 |
| 35 | #define TMP103_CONF_CR1 0x40 |
| 36 | #define TMP103_CONF_ID 0x80 |
| 37 | #define TMP103_CONF_SD (TMP103_CONF_M1) |
| 38 | #define TMP103_CONF_SD_MASK (TMP103_CONF_M0 | TMP103_CONF_M1) |
| 39 | |
| 40 | #define TMP103_CONFIG (TMP103_CONF_CR1 | TMP103_CONF_M1) |
| 41 | #define TMP103_CONFIG_MASK (TMP103_CONF_CR0 | TMP103_CONF_CR1 | \ |
| 42 | TMP103_CONF_M0 | TMP103_CONF_M1) |
| 43 | |
| 44 | static inline int tmp103_reg_to_mc(s8 val) |
| 45 | { |
| 46 | return val * 1000; |
| 47 | } |
| 48 | |
| 49 | static inline u8 tmp103_mc_to_reg(int val) |
| 50 | { |
| 51 | return DIV_ROUND_CLOSEST(val, 1000); |
| 52 | } |
| 53 | |
Guenter Roeck | 69dd7cd | 2018-12-10 14:02:22 -0800 | [diff] [blame] | 54 | static ssize_t tmp103_temp_show(struct device *dev, |
| 55 | struct device_attribute *attr, char *buf) |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 56 | { |
| 57 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
| 58 | struct regmap *regmap = dev_get_drvdata(dev); |
| 59 | unsigned int regval; |
| 60 | int ret; |
| 61 | |
| 62 | ret = regmap_read(regmap, sda->index, ®val); |
| 63 | if (ret < 0) |
| 64 | return ret; |
| 65 | |
| 66 | return sprintf(buf, "%d\n", tmp103_reg_to_mc(regval)); |
| 67 | } |
| 68 | |
Guenter Roeck | 69dd7cd | 2018-12-10 14:02:22 -0800 | [diff] [blame] | 69 | static ssize_t tmp103_temp_store(struct device *dev, |
| 70 | struct device_attribute *attr, |
| 71 | const char *buf, size_t count) |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 72 | { |
| 73 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
| 74 | struct regmap *regmap = dev_get_drvdata(dev); |
| 75 | long val; |
| 76 | int ret; |
| 77 | |
| 78 | if (kstrtol(buf, 10, &val) < 0) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | val = clamp_val(val, -55000, 127000); |
| 82 | ret = regmap_write(regmap, sda->index, tmp103_mc_to_reg(val)); |
| 83 | return ret ? ret : count; |
| 84 | } |
| 85 | |
Guenter Roeck | 69dd7cd | 2018-12-10 14:02:22 -0800 | [diff] [blame] | 86 | static SENSOR_DEVICE_ATTR_RO(temp1_input, tmp103_temp, TMP103_TEMP_REG); |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 87 | |
Guenter Roeck | 69dd7cd | 2018-12-10 14:02:22 -0800 | [diff] [blame] | 88 | static SENSOR_DEVICE_ATTR_RW(temp1_min, tmp103_temp, TMP103_TLOW_REG); |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 89 | |
Guenter Roeck | 69dd7cd | 2018-12-10 14:02:22 -0800 | [diff] [blame] | 90 | static SENSOR_DEVICE_ATTR_RW(temp1_max, tmp103_temp, TMP103_THIGH_REG); |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 91 | |
| 92 | static struct attribute *tmp103_attrs[] = { |
| 93 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 94 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 95 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 96 | NULL |
| 97 | }; |
| 98 | ATTRIBUTE_GROUPS(tmp103); |
| 99 | |
| 100 | static bool tmp103_regmap_is_volatile(struct device *dev, unsigned int reg) |
| 101 | { |
| 102 | return reg == TMP103_TEMP_REG; |
| 103 | } |
| 104 | |
Axel Lin | 034b44b | 2014-07-02 08:42:18 +0800 | [diff] [blame] | 105 | static const struct regmap_config tmp103_regmap_config = { |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 106 | .reg_bits = 8, |
| 107 | .val_bits = 8, |
| 108 | .max_register = TMP103_THIGH_REG, |
| 109 | .volatile_reg = tmp103_regmap_is_volatile, |
| 110 | }; |
| 111 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 112 | static int tmp103_probe(struct i2c_client *client) |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 113 | { |
| 114 | struct device *dev = &client->dev; |
| 115 | struct device *hwmon_dev; |
| 116 | struct regmap *regmap; |
| 117 | int ret; |
| 118 | |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 119 | regmap = devm_regmap_init_i2c(client, &tmp103_regmap_config); |
| 120 | if (IS_ERR(regmap)) { |
| 121 | dev_err(dev, "failed to allocate register map\n"); |
| 122 | return PTR_ERR(regmap); |
| 123 | } |
| 124 | |
| 125 | ret = regmap_update_bits(regmap, TMP103_CONF_REG, TMP103_CONFIG_MASK, |
| 126 | TMP103_CONFIG); |
| 127 | if (ret < 0) { |
| 128 | dev_err(&client->dev, "error writing config register\n"); |
| 129 | return ret; |
| 130 | } |
| 131 | |
Axel Lin | 233adce | 2014-06-27 22:49:29 +0800 | [diff] [blame] | 132 | i2c_set_clientdata(client, regmap); |
sundarjdev | 4e66cd1 | 2014-09-22 10:31:39 -0700 | [diff] [blame] | 133 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 134 | regmap, tmp103_groups); |
| 135 | return PTR_ERR_OR_ZERO(hwmon_dev); |
| 136 | } |
| 137 | |
Rahul Bedarkar | 39c382a | 2017-04-23 19:40:51 +0530 | [diff] [blame] | 138 | static int __maybe_unused tmp103_suspend(struct device *dev) |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 139 | { |
| 140 | struct regmap *regmap = dev_get_drvdata(dev); |
| 141 | |
| 142 | return regmap_update_bits(regmap, TMP103_CONF_REG, |
| 143 | TMP103_CONF_SD_MASK, 0); |
| 144 | } |
| 145 | |
Rahul Bedarkar | 39c382a | 2017-04-23 19:40:51 +0530 | [diff] [blame] | 146 | static int __maybe_unused tmp103_resume(struct device *dev) |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 147 | { |
| 148 | struct regmap *regmap = dev_get_drvdata(dev); |
| 149 | |
| 150 | return regmap_update_bits(regmap, TMP103_CONF_REG, |
| 151 | TMP103_CONF_SD_MASK, TMP103_CONF_SD); |
| 152 | } |
| 153 | |
Rahul Bedarkar | 39c382a | 2017-04-23 19:40:51 +0530 | [diff] [blame] | 154 | static SIMPLE_DEV_PM_OPS(tmp103_dev_pm_ops, tmp103_suspend, tmp103_resume); |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 155 | |
| 156 | static const struct i2c_device_id tmp103_id[] = { |
| 157 | { "tmp103", 0 }, |
| 158 | { } |
| 159 | }; |
| 160 | MODULE_DEVICE_TABLE(i2c, tmp103_id); |
| 161 | |
Guenter Roeck | 2089421 | 2019-04-04 06:28:34 -0700 | [diff] [blame] | 162 | static const struct of_device_id __maybe_unused tmp103_of_match[] = { |
Javier Martinez Canillas | 72fc64c | 2017-02-24 10:13:11 -0300 | [diff] [blame] | 163 | { .compatible = "ti,tmp103" }, |
| 164 | { }, |
| 165 | }; |
| 166 | MODULE_DEVICE_TABLE(of, tmp103_of_match); |
| 167 | |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 168 | static struct i2c_driver tmp103_driver = { |
| 169 | .driver = { |
| 170 | .name = "tmp103", |
Javier Martinez Canillas | 72fc64c | 2017-02-24 10:13:11 -0300 | [diff] [blame] | 171 | .of_match_table = of_match_ptr(tmp103_of_match), |
Rahul Bedarkar | 39c382a | 2017-04-23 19:40:51 +0530 | [diff] [blame] | 172 | .pm = &tmp103_dev_pm_ops, |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 173 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 174 | .probe_new = tmp103_probe, |
Heiko Schocher | d17a7dc | 2014-06-18 07:37:17 +0200 | [diff] [blame] | 175 | .id_table = tmp103_id, |
| 176 | }; |
| 177 | |
| 178 | module_i2c_driver(tmp103_driver); |
| 179 | |
| 180 | MODULE_AUTHOR("Heiko Schocher <hs@denx.de>"); |
| 181 | MODULE_DESCRIPTION("Texas Instruments TMP103 temperature sensor driver"); |
| 182 | MODULE_LICENSE("GPL"); |