Linux 5.1-rc7
[sfrench/cifs-2.6.git] / drivers / hwmon / tmp103.c
1 /*
2  * Texas Instruments TMP103 SMBus temperature sensor driver
3  * Copyright (C) 2014 Heiko Schocher <hs@denx.de>
4  *
5  * Based on:
6  * Texas Instruments TMP102 SMBus temperature sensor driver
7  *
8  * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  */
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include <linux/device.h>
31 #include <linux/jiffies.h>
32 #include <linux/regmap.h>
33
34 #define TMP103_TEMP_REG         0x00
35 #define TMP103_CONF_REG         0x01
36 #define TMP103_TLOW_REG         0x02
37 #define TMP103_THIGH_REG        0x03
38
39 #define TMP103_CONF_M0          0x01
40 #define TMP103_CONF_M1          0x02
41 #define TMP103_CONF_LC          0x04
42 #define TMP103_CONF_FL          0x08
43 #define TMP103_CONF_FH          0x10
44 #define TMP103_CONF_CR0         0x20
45 #define TMP103_CONF_CR1         0x40
46 #define TMP103_CONF_ID          0x80
47 #define TMP103_CONF_SD          (TMP103_CONF_M1)
48 #define TMP103_CONF_SD_MASK     (TMP103_CONF_M0 | TMP103_CONF_M1)
49
50 #define TMP103_CONFIG           (TMP103_CONF_CR1 | TMP103_CONF_M1)
51 #define TMP103_CONFIG_MASK      (TMP103_CONF_CR0 | TMP103_CONF_CR1 | \
52                                  TMP103_CONF_M0 | TMP103_CONF_M1)
53
54 static inline int tmp103_reg_to_mc(s8 val)
55 {
56         return val * 1000;
57 }
58
59 static inline u8 tmp103_mc_to_reg(int val)
60 {
61         return DIV_ROUND_CLOSEST(val, 1000);
62 }
63
64 static ssize_t tmp103_temp_show(struct device *dev,
65                                 struct device_attribute *attr, char *buf)
66 {
67         struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
68         struct regmap *regmap = dev_get_drvdata(dev);
69         unsigned int regval;
70         int ret;
71
72         ret = regmap_read(regmap, sda->index, &regval);
73         if (ret < 0)
74                 return ret;
75
76         return sprintf(buf, "%d\n", tmp103_reg_to_mc(regval));
77 }
78
79 static ssize_t tmp103_temp_store(struct device *dev,
80                                  struct device_attribute *attr,
81                                  const char *buf, size_t count)
82 {
83         struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
84         struct regmap *regmap = dev_get_drvdata(dev);
85         long val;
86         int ret;
87
88         if (kstrtol(buf, 10, &val) < 0)
89                 return -EINVAL;
90
91         val = clamp_val(val, -55000, 127000);
92         ret = regmap_write(regmap, sda->index, tmp103_mc_to_reg(val));
93         return ret ? ret : count;
94 }
95
96 static SENSOR_DEVICE_ATTR_RO(temp1_input, tmp103_temp, TMP103_TEMP_REG);
97
98 static SENSOR_DEVICE_ATTR_RW(temp1_min, tmp103_temp, TMP103_TLOW_REG);
99
100 static SENSOR_DEVICE_ATTR_RW(temp1_max, tmp103_temp, TMP103_THIGH_REG);
101
102 static struct attribute *tmp103_attrs[] = {
103         &sensor_dev_attr_temp1_input.dev_attr.attr,
104         &sensor_dev_attr_temp1_min.dev_attr.attr,
105         &sensor_dev_attr_temp1_max.dev_attr.attr,
106         NULL
107 };
108 ATTRIBUTE_GROUPS(tmp103);
109
110 static bool tmp103_regmap_is_volatile(struct device *dev, unsigned int reg)
111 {
112         return reg == TMP103_TEMP_REG;
113 }
114
115 static const struct regmap_config tmp103_regmap_config = {
116         .reg_bits = 8,
117         .val_bits = 8,
118         .max_register = TMP103_THIGH_REG,
119         .volatile_reg = tmp103_regmap_is_volatile,
120 };
121
122 static int tmp103_probe(struct i2c_client *client,
123                         const struct i2c_device_id *id)
124 {
125         struct device *dev = &client->dev;
126         struct device *hwmon_dev;
127         struct regmap *regmap;
128         int ret;
129
130         regmap = devm_regmap_init_i2c(client, &tmp103_regmap_config);
131         if (IS_ERR(regmap)) {
132                 dev_err(dev, "failed to allocate register map\n");
133                 return PTR_ERR(regmap);
134         }
135
136         ret = regmap_update_bits(regmap, TMP103_CONF_REG, TMP103_CONFIG_MASK,
137                                  TMP103_CONFIG);
138         if (ret < 0) {
139                 dev_err(&client->dev, "error writing config register\n");
140                 return ret;
141         }
142
143         i2c_set_clientdata(client, regmap);
144         hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
145                                                       regmap, tmp103_groups);
146         return PTR_ERR_OR_ZERO(hwmon_dev);
147 }
148
149 static int __maybe_unused tmp103_suspend(struct device *dev)
150 {
151         struct regmap *regmap = dev_get_drvdata(dev);
152
153         return regmap_update_bits(regmap, TMP103_CONF_REG,
154                                   TMP103_CONF_SD_MASK, 0);
155 }
156
157 static int __maybe_unused tmp103_resume(struct device *dev)
158 {
159         struct regmap *regmap = dev_get_drvdata(dev);
160
161         return regmap_update_bits(regmap, TMP103_CONF_REG,
162                                   TMP103_CONF_SD_MASK, TMP103_CONF_SD);
163 }
164
165 static SIMPLE_DEV_PM_OPS(tmp103_dev_pm_ops, tmp103_suspend, tmp103_resume);
166
167 static const struct i2c_device_id tmp103_id[] = {
168         { "tmp103", 0 },
169         { }
170 };
171 MODULE_DEVICE_TABLE(i2c, tmp103_id);
172
173 static const struct of_device_id tmp103_of_match[] = {
174         { .compatible = "ti,tmp103" },
175         { },
176 };
177 MODULE_DEVICE_TABLE(of, tmp103_of_match);
178
179 static struct i2c_driver tmp103_driver = {
180         .driver = {
181                 .name   = "tmp103",
182                 .of_match_table = of_match_ptr(tmp103_of_match),
183                 .pm     = &tmp103_dev_pm_ops,
184         },
185         .probe          = tmp103_probe,
186         .id_table       = tmp103_id,
187 };
188
189 module_i2c_driver(tmp103_driver);
190
191 MODULE_AUTHOR("Heiko Schocher <hs@denx.de>");
192 MODULE_DESCRIPTION("Texas Instruments TMP103 temperature sensor driver");
193 MODULE_LICENSE("GPL");