Loading...
1/*
2 * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/jiffies.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 "lm75.h"
31
32
33/*
34 * This driver handles the LM75 and compatible digital temperature sensors.
35 */
36
37enum lm75_type { /* keep sorted in alphabetical order */
38 ds1775,
39 ds75,
40 lm75,
41 lm75a,
42 max6625,
43 max6626,
44 mcp980x,
45 stds75,
46 tcn75,
47 tmp100,
48 tmp101,
49 tmp105,
50 tmp175,
51 tmp275,
52 tmp75,
53};
54
55/* Addresses scanned */
56static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
57 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
58
59
60/* The LM75 registers */
61#define LM75_REG_CONF 0x01
62static const u8 LM75_REG_TEMP[3] = {
63 0x00, /* input */
64 0x03, /* max */
65 0x02, /* hyst */
66};
67
68/* Each client has this additional data */
69struct lm75_data {
70 struct device *hwmon_dev;
71 struct mutex update_lock;
72 u8 orig_conf;
73 char valid; /* !=0 if registers are valid */
74 unsigned long last_updated; /* In jiffies */
75 u16 temp[3]; /* Register values,
76 0 = input
77 1 = max
78 2 = hyst */
79};
80
81static int lm75_read_value(struct i2c_client *client, u8 reg);
82static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
83static struct lm75_data *lm75_update_device(struct device *dev);
84
85
86/*-----------------------------------------------------------------------*/
87
88/* sysfs attributes for hwmon */
89
90static ssize_t show_temp(struct device *dev, struct device_attribute *da,
91 char *buf)
92{
93 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
94 struct lm75_data *data = lm75_update_device(dev);
95 return sprintf(buf, "%d\n",
96 LM75_TEMP_FROM_REG(data->temp[attr->index]));
97}
98
99static ssize_t set_temp(struct device *dev, struct device_attribute *da,
100 const char *buf, size_t count)
101{
102 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
103 struct i2c_client *client = to_i2c_client(dev);
104 struct lm75_data *data = i2c_get_clientdata(client);
105 int nr = attr->index;
106 long temp;
107 int error;
108
109 error = strict_strtol(buf, 10, &temp);
110 if (error)
111 return error;
112
113 mutex_lock(&data->update_lock);
114 data->temp[nr] = LM75_TEMP_TO_REG(temp);
115 lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
116 mutex_unlock(&data->update_lock);
117 return count;
118}
119
120static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
121 show_temp, set_temp, 1);
122static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
123 show_temp, set_temp, 2);
124static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
125
126static struct attribute *lm75_attributes[] = {
127 &sensor_dev_attr_temp1_input.dev_attr.attr,
128 &sensor_dev_attr_temp1_max.dev_attr.attr,
129 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
130
131 NULL
132};
133
134static const struct attribute_group lm75_group = {
135 .attrs = lm75_attributes,
136};
137
138/*-----------------------------------------------------------------------*/
139
140/* device probe and removal */
141
142static int
143lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
144{
145 struct lm75_data *data;
146 int status;
147 u8 set_mask, clr_mask;
148 int new;
149
150 if (!i2c_check_functionality(client->adapter,
151 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
152 return -EIO;
153
154 data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL);
155 if (!data)
156 return -ENOMEM;
157
158 i2c_set_clientdata(client, data);
159 mutex_init(&data->update_lock);
160
161 /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
162 * Then tweak to be more precise when appropriate.
163 */
164 set_mask = 0;
165 clr_mask = (1 << 0) /* continuous conversions */
166 | (1 << 6) | (1 << 5); /* 9-bit mode */
167
168 /* configure as specified */
169 status = lm75_read_value(client, LM75_REG_CONF);
170 if (status < 0) {
171 dev_dbg(&client->dev, "Can't read config? %d\n", status);
172 goto exit_free;
173 }
174 data->orig_conf = status;
175 new = status & ~clr_mask;
176 new |= set_mask;
177 if (status != new)
178 lm75_write_value(client, LM75_REG_CONF, new);
179 dev_dbg(&client->dev, "Config %02x\n", new);
180
181 /* Register sysfs hooks */
182 status = sysfs_create_group(&client->dev.kobj, &lm75_group);
183 if (status)
184 goto exit_free;
185
186 data->hwmon_dev = hwmon_device_register(&client->dev);
187 if (IS_ERR(data->hwmon_dev)) {
188 status = PTR_ERR(data->hwmon_dev);
189 goto exit_remove;
190 }
191
192 dev_info(&client->dev, "%s: sensor '%s'\n",
193 dev_name(data->hwmon_dev), client->name);
194
195 return 0;
196
197exit_remove:
198 sysfs_remove_group(&client->dev.kobj, &lm75_group);
199exit_free:
200 kfree(data);
201 return status;
202}
203
204static int lm75_remove(struct i2c_client *client)
205{
206 struct lm75_data *data = i2c_get_clientdata(client);
207
208 hwmon_device_unregister(data->hwmon_dev);
209 sysfs_remove_group(&client->dev.kobj, &lm75_group);
210 lm75_write_value(client, LM75_REG_CONF, data->orig_conf);
211 kfree(data);
212 return 0;
213}
214
215static const struct i2c_device_id lm75_ids[] = {
216 { "ds1775", ds1775, },
217 { "ds75", ds75, },
218 { "lm75", lm75, },
219 { "lm75a", lm75a, },
220 { "max6625", max6625, },
221 { "max6626", max6626, },
222 { "mcp980x", mcp980x, },
223 { "stds75", stds75, },
224 { "tcn75", tcn75, },
225 { "tmp100", tmp100, },
226 { "tmp101", tmp101, },
227 { "tmp105", tmp105, },
228 { "tmp175", tmp175, },
229 { "tmp275", tmp275, },
230 { "tmp75", tmp75, },
231 { /* LIST END */ }
232};
233MODULE_DEVICE_TABLE(i2c, lm75_ids);
234
235#define LM75A_ID 0xA1
236
237/* Return 0 if detection is successful, -ENODEV otherwise */
238static int lm75_detect(struct i2c_client *new_client,
239 struct i2c_board_info *info)
240{
241 struct i2c_adapter *adapter = new_client->adapter;
242 int i;
243 int conf, hyst, os;
244 bool is_lm75a = 0;
245
246 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
247 I2C_FUNC_SMBUS_WORD_DATA))
248 return -ENODEV;
249
250 /* Now, we do the remaining detection. There is no identification-
251 dedicated register so we have to rely on several tricks:
252 unused bits, registers cycling over 8-address boundaries,
253 addresses 0x04-0x07 returning the last read value.
254 The cycling+unused addresses combination is not tested,
255 since it would significantly slow the detection down and would
256 hardly add any value.
257
258 The National Semiconductor LM75A is different than earlier
259 LM75s. It has an ID byte of 0xaX (where X is the chip
260 revision, with 1 being the only revision in existence) in
261 register 7, and unused registers return 0xff rather than the
262 last read value. */
263
264 /* Unused bits */
265 conf = i2c_smbus_read_byte_data(new_client, 1);
266 if (conf & 0xe0)
267 return -ENODEV;
268
269 /* First check for LM75A */
270 if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
271 /* LM75A returns 0xff on unused registers so
272 just to be sure we check for that too. */
273 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
274 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
275 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
276 return -ENODEV;
277 is_lm75a = 1;
278 hyst = i2c_smbus_read_byte_data(new_client, 2);
279 os = i2c_smbus_read_byte_data(new_client, 3);
280 } else { /* Traditional style LM75 detection */
281 /* Unused addresses */
282 hyst = i2c_smbus_read_byte_data(new_client, 2);
283 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
284 || i2c_smbus_read_byte_data(new_client, 5) != hyst
285 || i2c_smbus_read_byte_data(new_client, 6) != hyst
286 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
287 return -ENODEV;
288 os = i2c_smbus_read_byte_data(new_client, 3);
289 if (i2c_smbus_read_byte_data(new_client, 4) != os
290 || i2c_smbus_read_byte_data(new_client, 5) != os
291 || i2c_smbus_read_byte_data(new_client, 6) != os
292 || i2c_smbus_read_byte_data(new_client, 7) != os)
293 return -ENODEV;
294 }
295
296 /* Addresses cycling */
297 for (i = 8; i <= 248; i += 40) {
298 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
299 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
300 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
301 return -ENODEV;
302 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
303 != LM75A_ID)
304 return -ENODEV;
305 }
306
307 strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
308
309 return 0;
310}
311
312#ifdef CONFIG_PM
313static int lm75_suspend(struct device *dev)
314{
315 int status;
316 struct i2c_client *client = to_i2c_client(dev);
317 status = lm75_read_value(client, LM75_REG_CONF);
318 if (status < 0) {
319 dev_dbg(&client->dev, "Can't read config? %d\n", status);
320 return status;
321 }
322 status = status | LM75_SHUTDOWN;
323 lm75_write_value(client, LM75_REG_CONF, status);
324 return 0;
325}
326
327static int lm75_resume(struct device *dev)
328{
329 int status;
330 struct i2c_client *client = to_i2c_client(dev);
331 status = lm75_read_value(client, LM75_REG_CONF);
332 if (status < 0) {
333 dev_dbg(&client->dev, "Can't read config? %d\n", status);
334 return status;
335 }
336 status = status & ~LM75_SHUTDOWN;
337 lm75_write_value(client, LM75_REG_CONF, status);
338 return 0;
339}
340
341static const struct dev_pm_ops lm75_dev_pm_ops = {
342 .suspend = lm75_suspend,
343 .resume = lm75_resume,
344};
345#define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
346#else
347#define LM75_DEV_PM_OPS NULL
348#endif /* CONFIG_PM */
349
350static struct i2c_driver lm75_driver = {
351 .class = I2C_CLASS_HWMON,
352 .driver = {
353 .name = "lm75",
354 .pm = LM75_DEV_PM_OPS,
355 },
356 .probe = lm75_probe,
357 .remove = lm75_remove,
358 .id_table = lm75_ids,
359 .detect = lm75_detect,
360 .address_list = normal_i2c,
361};
362
363/*-----------------------------------------------------------------------*/
364
365/* register access */
366
367/*
368 * All registers are word-sized, except for the configuration register.
369 * LM75 uses a high-byte first convention, which is exactly opposite to
370 * the SMBus standard.
371 */
372static int lm75_read_value(struct i2c_client *client, u8 reg)
373{
374 int value;
375
376 if (reg == LM75_REG_CONF)
377 return i2c_smbus_read_byte_data(client, reg);
378
379 value = i2c_smbus_read_word_data(client, reg);
380 return (value < 0) ? value : swab16(value);
381}
382
383static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
384{
385 if (reg == LM75_REG_CONF)
386 return i2c_smbus_write_byte_data(client, reg, value);
387 else
388 return i2c_smbus_write_word_data(client, reg, swab16(value));
389}
390
391static struct lm75_data *lm75_update_device(struct device *dev)
392{
393 struct i2c_client *client = to_i2c_client(dev);
394 struct lm75_data *data = i2c_get_clientdata(client);
395
396 mutex_lock(&data->update_lock);
397
398 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
399 || !data->valid) {
400 int i;
401 dev_dbg(&client->dev, "Starting lm75 update\n");
402
403 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
404 int status;
405
406 status = lm75_read_value(client, LM75_REG_TEMP[i]);
407 if (status < 0)
408 dev_dbg(&client->dev, "reg %d, err %d\n",
409 LM75_REG_TEMP[i], status);
410 else
411 data->temp[i] = status;
412 }
413 data->last_updated = jiffies;
414 data->valid = 1;
415 }
416
417 mutex_unlock(&data->update_lock);
418
419 return data;
420}
421
422/*-----------------------------------------------------------------------*/
423
424/* module glue */
425
426static int __init sensors_lm75_init(void)
427{
428 return i2c_add_driver(&lm75_driver);
429}
430
431static void __exit sensors_lm75_exit(void)
432{
433 i2c_del_driver(&lm75_driver);
434}
435
436MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
437MODULE_DESCRIPTION("LM75 driver");
438MODULE_LICENSE("GPL");
439
440module_init(sensors_lm75_init);
441module_exit(sensors_lm75_exit);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
6 */
7
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/jiffies.h>
12#include <linux/i2c.h>
13#include <linux/hwmon.h>
14#include <linux/hwmon-sysfs.h>
15#include <linux/err.h>
16#include <linux/of_device.h>
17#include <linux/of.h>
18#include <linux/regmap.h>
19#include <linux/util_macros.h>
20#include <linux/regulator/consumer.h>
21#include "lm75.h"
22
23/*
24 * This driver handles the LM75 and compatible digital temperature sensors.
25 */
26
27enum lm75_type { /* keep sorted in alphabetical order */
28 adt75,
29 ds1775,
30 ds75,
31 ds7505,
32 g751,
33 lm75,
34 lm75a,
35 lm75b,
36 max6625,
37 max6626,
38 max31725,
39 mcp980x,
40 pct2075,
41 stds75,
42 stlm75,
43 tcn75,
44 tmp100,
45 tmp101,
46 tmp105,
47 tmp112,
48 tmp175,
49 tmp275,
50 tmp75,
51 tmp75b,
52 tmp75c,
53 tmp1075,
54};
55
56/**
57 * struct lm75_params - lm75 configuration parameters.
58 * @set_mask: Bits to set in configuration register when configuring
59 * the chip.
60 * @clr_mask: Bits to clear in configuration register when configuring
61 * the chip.
62 * @default_resolution: Default number of bits to represent the temperature
63 * value.
64 * @resolution_limits: Limit register resolution. Optional. Should be set if
65 * the resolution of limit registers does not match the
66 * resolution of the temperature register.
67 * @resolutions: List of resolutions associated with sample times.
68 * Optional. Should be set if num_sample_times is larger
69 * than 1, and if the resolution changes with sample times.
70 * If set, number of entries must match num_sample_times.
71 * @default_sample_time:Sample time to be set by default.
72 * @num_sample_times: Number of possible sample times to be set. Optional.
73 * Should be set if the number of sample times is larger
74 * than one.
75 * @sample_times: All the possible sample times to be set. Mandatory if
76 * num_sample_times is larger than 1. If set, number of
77 * entries must match num_sample_times.
78 */
79
80struct lm75_params {
81 u8 set_mask;
82 u8 clr_mask;
83 u8 default_resolution;
84 u8 resolution_limits;
85 const u8 *resolutions;
86 unsigned int default_sample_time;
87 u8 num_sample_times;
88 const unsigned int *sample_times;
89};
90
91/* Addresses scanned */
92static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
93 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
94
95/* The LM75 registers */
96#define LM75_REG_TEMP 0x00
97#define LM75_REG_CONF 0x01
98#define LM75_REG_HYST 0x02
99#define LM75_REG_MAX 0x03
100#define PCT2075_REG_IDLE 0x04
101
102/* Each client has this additional data */
103struct lm75_data {
104 struct i2c_client *client;
105 struct regmap *regmap;
106 struct regulator *vs;
107 u8 orig_conf;
108 u8 current_conf;
109 u8 resolution; /* In bits, 9 to 16 */
110 unsigned int sample_time; /* In ms */
111 enum lm75_type kind;
112 const struct lm75_params *params;
113};
114
115/*-----------------------------------------------------------------------*/
116
117static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
118
119#define LM75_SAMPLE_CLEAR_MASK (3 << 5)
120
121/* The structure below stores the configuration values of the supported devices.
122 * In case of being supported multiple configurations, the default one must
123 * always be the first element of the array
124 */
125static const struct lm75_params device_params[] = {
126 [adt75] = {
127 .clr_mask = 1 << 5, /* not one-shot mode */
128 .default_resolution = 12,
129 .default_sample_time = MSEC_PER_SEC / 10,
130 },
131 [ds1775] = {
132 .clr_mask = 3 << 5,
133 .set_mask = 2 << 5, /* 11-bit mode */
134 .default_resolution = 11,
135 .default_sample_time = 500,
136 .num_sample_times = 4,
137 .sample_times = (unsigned int []){ 125, 250, 500, 1000 },
138 .resolutions = (u8 []) {9, 10, 11, 12 },
139 },
140 [ds75] = {
141 .clr_mask = 3 << 5,
142 .set_mask = 2 << 5, /* 11-bit mode */
143 .default_resolution = 11,
144 .default_sample_time = 600,
145 .num_sample_times = 4,
146 .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
147 .resolutions = (u8 []) {9, 10, 11, 12 },
148 },
149 [stds75] = {
150 .clr_mask = 3 << 5,
151 .set_mask = 2 << 5, /* 11-bit mode */
152 .default_resolution = 11,
153 .default_sample_time = 600,
154 .num_sample_times = 4,
155 .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
156 .resolutions = (u8 []) {9, 10, 11, 12 },
157 },
158 [stlm75] = {
159 .default_resolution = 9,
160 .default_sample_time = MSEC_PER_SEC / 6,
161 },
162 [ds7505] = {
163 .set_mask = 3 << 5, /* 12-bit mode*/
164 .default_resolution = 12,
165 .default_sample_time = 200,
166 .num_sample_times = 4,
167 .sample_times = (unsigned int []){ 25, 50, 100, 200 },
168 .resolutions = (u8 []) {9, 10, 11, 12 },
169 },
170 [g751] = {
171 .default_resolution = 9,
172 .default_sample_time = MSEC_PER_SEC / 10,
173 },
174 [lm75] = {
175 .default_resolution = 9,
176 .default_sample_time = MSEC_PER_SEC / 10,
177 },
178 [lm75a] = {
179 .default_resolution = 9,
180 .default_sample_time = MSEC_PER_SEC / 10,
181 },
182 [lm75b] = {
183 .default_resolution = 11,
184 .default_sample_time = MSEC_PER_SEC / 10,
185 },
186 [max6625] = {
187 .default_resolution = 9,
188 .default_sample_time = MSEC_PER_SEC / 7,
189 },
190 [max6626] = {
191 .default_resolution = 12,
192 .default_sample_time = MSEC_PER_SEC / 7,
193 .resolution_limits = 9,
194 },
195 [max31725] = {
196 .default_resolution = 16,
197 .default_sample_time = MSEC_PER_SEC / 20,
198 },
199 [tcn75] = {
200 .default_resolution = 9,
201 .default_sample_time = MSEC_PER_SEC / 18,
202 },
203 [pct2075] = {
204 .default_resolution = 11,
205 .default_sample_time = MSEC_PER_SEC / 10,
206 .num_sample_times = 31,
207 .sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600,
208 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
209 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700,
210 2800, 2900, 3000, 3100 },
211 },
212 [mcp980x] = {
213 .set_mask = 3 << 5, /* 12-bit mode */
214 .clr_mask = 1 << 7, /* not one-shot mode */
215 .default_resolution = 12,
216 .resolution_limits = 9,
217 .default_sample_time = 240,
218 .num_sample_times = 4,
219 .sample_times = (unsigned int []){ 30, 60, 120, 240 },
220 .resolutions = (u8 []) {9, 10, 11, 12 },
221 },
222 [tmp100] = {
223 .set_mask = 3 << 5, /* 12-bit mode */
224 .clr_mask = 1 << 7, /* not one-shot mode */
225 .default_resolution = 12,
226 .default_sample_time = 320,
227 .num_sample_times = 4,
228 .sample_times = (unsigned int []){ 40, 80, 160, 320 },
229 .resolutions = (u8 []) {9, 10, 11, 12 },
230 },
231 [tmp101] = {
232 .set_mask = 3 << 5, /* 12-bit mode */
233 .clr_mask = 1 << 7, /* not one-shot mode */
234 .default_resolution = 12,
235 .default_sample_time = 320,
236 .num_sample_times = 4,
237 .sample_times = (unsigned int []){ 40, 80, 160, 320 },
238 .resolutions = (u8 []) {9, 10, 11, 12 },
239 },
240 [tmp105] = {
241 .set_mask = 3 << 5, /* 12-bit mode */
242 .clr_mask = 1 << 7, /* not one-shot mode*/
243 .default_resolution = 12,
244 .default_sample_time = 220,
245 .num_sample_times = 4,
246 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
247 .resolutions = (u8 []) {9, 10, 11, 12 },
248 },
249 [tmp112] = {
250 .set_mask = 3 << 5, /* 8 samples / second */
251 .clr_mask = 1 << 7, /* no one-shot mode*/
252 .default_resolution = 12,
253 .default_sample_time = 125,
254 .num_sample_times = 4,
255 .sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
256 },
257 [tmp175] = {
258 .set_mask = 3 << 5, /* 12-bit mode */
259 .clr_mask = 1 << 7, /* not one-shot mode*/
260 .default_resolution = 12,
261 .default_sample_time = 220,
262 .num_sample_times = 4,
263 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
264 .resolutions = (u8 []) {9, 10, 11, 12 },
265 },
266 [tmp275] = {
267 .set_mask = 3 << 5, /* 12-bit mode */
268 .clr_mask = 1 << 7, /* not one-shot mode*/
269 .default_resolution = 12,
270 .default_sample_time = 220,
271 .num_sample_times = 4,
272 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
273 .resolutions = (u8 []) {9, 10, 11, 12 },
274 },
275 [tmp75] = {
276 .set_mask = 3 << 5, /* 12-bit mode */
277 .clr_mask = 1 << 7, /* not one-shot mode*/
278 .default_resolution = 12,
279 .default_sample_time = 220,
280 .num_sample_times = 4,
281 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
282 .resolutions = (u8 []) {9, 10, 11, 12 },
283 },
284 [tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */
285 .clr_mask = 1 << 7 | 3 << 5,
286 .default_resolution = 12,
287 .default_sample_time = MSEC_PER_SEC / 37,
288 .sample_times = (unsigned int []){ MSEC_PER_SEC / 37,
289 MSEC_PER_SEC / 18,
290 MSEC_PER_SEC / 9, MSEC_PER_SEC / 4 },
291 .num_sample_times = 4,
292 },
293 [tmp75c] = {
294 .clr_mask = 1 << 5, /*not one-shot mode*/
295 .default_resolution = 12,
296 .default_sample_time = MSEC_PER_SEC / 12,
297 },
298 [tmp1075] = { /* not one-shot mode, 27.5 ms sample rate */
299 .clr_mask = 1 << 5 | 1 << 6 | 1 << 7,
300 .default_resolution = 12,
301 .default_sample_time = 28,
302 .num_sample_times = 4,
303 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
304 }
305};
306
307static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
308{
309 return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
310}
311
312static int lm75_write_config(struct lm75_data *data, u8 set_mask,
313 u8 clr_mask)
314{
315 u8 value;
316
317 clr_mask |= LM75_SHUTDOWN;
318 value = data->current_conf & ~clr_mask;
319 value |= set_mask;
320
321 if (data->current_conf != value) {
322 s32 err;
323
324 err = i2c_smbus_write_byte_data(data->client, LM75_REG_CONF,
325 value);
326 if (err)
327 return err;
328 data->current_conf = value;
329 }
330 return 0;
331}
332
333static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
334 u32 attr, int channel, long *val)
335{
336 struct lm75_data *data = dev_get_drvdata(dev);
337 unsigned int regval;
338 int err, reg;
339
340 switch (type) {
341 case hwmon_chip:
342 switch (attr) {
343 case hwmon_chip_update_interval:
344 *val = data->sample_time;
345 break;
346 default:
347 return -EINVAL;
348 }
349 break;
350 case hwmon_temp:
351 switch (attr) {
352 case hwmon_temp_input:
353 reg = LM75_REG_TEMP;
354 break;
355 case hwmon_temp_max:
356 reg = LM75_REG_MAX;
357 break;
358 case hwmon_temp_max_hyst:
359 reg = LM75_REG_HYST;
360 break;
361 default:
362 return -EINVAL;
363 }
364 err = regmap_read(data->regmap, reg, ®val);
365 if (err < 0)
366 return err;
367
368 *val = lm75_reg_to_mc(regval, data->resolution);
369 break;
370 default:
371 return -EINVAL;
372 }
373 return 0;
374}
375
376static int lm75_write_temp(struct device *dev, u32 attr, long temp)
377{
378 struct lm75_data *data = dev_get_drvdata(dev);
379 u8 resolution;
380 int reg;
381
382 switch (attr) {
383 case hwmon_temp_max:
384 reg = LM75_REG_MAX;
385 break;
386 case hwmon_temp_max_hyst:
387 reg = LM75_REG_HYST;
388 break;
389 default:
390 return -EINVAL;
391 }
392
393 /*
394 * Resolution of limit registers is assumed to be the same as the
395 * temperature input register resolution unless given explicitly.
396 */
397 if (data->params->resolution_limits)
398 resolution = data->params->resolution_limits;
399 else
400 resolution = data->resolution;
401
402 temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
403 temp = DIV_ROUND_CLOSEST(temp << (resolution - 8),
404 1000) << (16 - resolution);
405
406 return regmap_write(data->regmap, reg, (u16)temp);
407}
408
409static int lm75_update_interval(struct device *dev, long val)
410{
411 struct lm75_data *data = dev_get_drvdata(dev);
412 unsigned int reg;
413 u8 index;
414 s32 err;
415
416 index = find_closest(val, data->params->sample_times,
417 (int)data->params->num_sample_times);
418
419 switch (data->kind) {
420 default:
421 err = lm75_write_config(data, lm75_sample_set_masks[index],
422 LM75_SAMPLE_CLEAR_MASK);
423 if (err)
424 return err;
425
426 data->sample_time = data->params->sample_times[index];
427 if (data->params->resolutions)
428 data->resolution = data->params->resolutions[index];
429 break;
430 case tmp112:
431 err = regmap_read(data->regmap, LM75_REG_CONF, ®);
432 if (err < 0)
433 return err;
434 reg &= ~0x00c0;
435 reg |= (3 - index) << 6;
436 err = regmap_write(data->regmap, LM75_REG_CONF, reg);
437 if (err < 0)
438 return err;
439 data->sample_time = data->params->sample_times[index];
440 break;
441 case pct2075:
442 err = i2c_smbus_write_byte_data(data->client, PCT2075_REG_IDLE,
443 index + 1);
444 if (err)
445 return err;
446 data->sample_time = data->params->sample_times[index];
447 break;
448 }
449 return 0;
450}
451
452static int lm75_write_chip(struct device *dev, u32 attr, long val)
453{
454 switch (attr) {
455 case hwmon_chip_update_interval:
456 return lm75_update_interval(dev, val);
457 default:
458 return -EINVAL;
459 }
460 return 0;
461}
462
463static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
464 u32 attr, int channel, long val)
465{
466 switch (type) {
467 case hwmon_chip:
468 return lm75_write_chip(dev, attr, val);
469 case hwmon_temp:
470 return lm75_write_temp(dev, attr, val);
471 default:
472 return -EINVAL;
473 }
474 return 0;
475}
476
477static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
478 u32 attr, int channel)
479{
480 const struct lm75_data *config_data = data;
481
482 switch (type) {
483 case hwmon_chip:
484 switch (attr) {
485 case hwmon_chip_update_interval:
486 if (config_data->params->num_sample_times > 1)
487 return 0644;
488 return 0444;
489 }
490 break;
491 case hwmon_temp:
492 switch (attr) {
493 case hwmon_temp_input:
494 return 0444;
495 case hwmon_temp_max:
496 case hwmon_temp_max_hyst:
497 return 0644;
498 }
499 break;
500 default:
501 break;
502 }
503 return 0;
504}
505
506static const struct hwmon_channel_info *lm75_info[] = {
507 HWMON_CHANNEL_INFO(chip,
508 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
509 HWMON_CHANNEL_INFO(temp,
510 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
511 NULL
512};
513
514static const struct hwmon_ops lm75_hwmon_ops = {
515 .is_visible = lm75_is_visible,
516 .read = lm75_read,
517 .write = lm75_write,
518};
519
520static const struct hwmon_chip_info lm75_chip_info = {
521 .ops = &lm75_hwmon_ops,
522 .info = lm75_info,
523};
524
525static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
526{
527 return reg != LM75_REG_TEMP;
528}
529
530static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
531{
532 return reg == LM75_REG_TEMP || reg == LM75_REG_CONF;
533}
534
535static const struct regmap_config lm75_regmap_config = {
536 .reg_bits = 8,
537 .val_bits = 16,
538 .max_register = PCT2075_REG_IDLE,
539 .writeable_reg = lm75_is_writeable_reg,
540 .volatile_reg = lm75_is_volatile_reg,
541 .val_format_endian = REGMAP_ENDIAN_BIG,
542 .cache_type = REGCACHE_RBTREE,
543 .use_single_read = true,
544 .use_single_write = true,
545};
546
547static void lm75_disable_regulator(void *data)
548{
549 struct lm75_data *lm75 = data;
550
551 regulator_disable(lm75->vs);
552}
553
554static void lm75_remove(void *data)
555{
556 struct lm75_data *lm75 = data;
557 struct i2c_client *client = lm75->client;
558
559 i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
560}
561
562static const struct i2c_device_id lm75_ids[];
563
564static int lm75_probe(struct i2c_client *client)
565{
566 struct device *dev = &client->dev;
567 struct device *hwmon_dev;
568 struct lm75_data *data;
569 int status, err;
570 enum lm75_type kind;
571
572 if (client->dev.of_node)
573 kind = (enum lm75_type)of_device_get_match_data(&client->dev);
574 else
575 kind = i2c_match_id(lm75_ids, client)->driver_data;
576
577 if (!i2c_check_functionality(client->adapter,
578 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
579 return -EIO;
580
581 data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
582 if (!data)
583 return -ENOMEM;
584
585 data->client = client;
586 data->kind = kind;
587
588 data->vs = devm_regulator_get(dev, "vs");
589 if (IS_ERR(data->vs))
590 return PTR_ERR(data->vs);
591
592 data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
593 if (IS_ERR(data->regmap))
594 return PTR_ERR(data->regmap);
595
596 /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
597 * Then tweak to be more precise when appropriate.
598 */
599
600 data->params = &device_params[data->kind];
601
602 /* Save default sample time and resolution*/
603 data->sample_time = data->params->default_sample_time;
604 data->resolution = data->params->default_resolution;
605
606 /* Enable the power */
607 err = regulator_enable(data->vs);
608 if (err) {
609 dev_err(dev, "failed to enable regulator: %d\n", err);
610 return err;
611 }
612
613 err = devm_add_action_or_reset(dev, lm75_disable_regulator, data);
614 if (err)
615 return err;
616
617 /* Cache original configuration */
618 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
619 if (status < 0) {
620 dev_dbg(dev, "Can't read config? %d\n", status);
621 return status;
622 }
623 data->orig_conf = status;
624 data->current_conf = status;
625
626 err = lm75_write_config(data, data->params->set_mask,
627 data->params->clr_mask);
628 if (err)
629 return err;
630
631 err = devm_add_action_or_reset(dev, lm75_remove, data);
632 if (err)
633 return err;
634
635 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
636 data, &lm75_chip_info,
637 NULL);
638 if (IS_ERR(hwmon_dev))
639 return PTR_ERR(hwmon_dev);
640
641 dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
642
643 return 0;
644}
645
646static const struct i2c_device_id lm75_ids[] = {
647 { "adt75", adt75, },
648 { "ds1775", ds1775, },
649 { "ds75", ds75, },
650 { "ds7505", ds7505, },
651 { "g751", g751, },
652 { "lm75", lm75, },
653 { "lm75a", lm75a, },
654 { "lm75b", lm75b, },
655 { "max6625", max6625, },
656 { "max6626", max6626, },
657 { "max31725", max31725, },
658 { "max31726", max31725, },
659 { "mcp980x", mcp980x, },
660 { "pct2075", pct2075, },
661 { "stds75", stds75, },
662 { "stlm75", stlm75, },
663 { "tcn75", tcn75, },
664 { "tmp100", tmp100, },
665 { "tmp101", tmp101, },
666 { "tmp105", tmp105, },
667 { "tmp112", tmp112, },
668 { "tmp175", tmp175, },
669 { "tmp275", tmp275, },
670 { "tmp75", tmp75, },
671 { "tmp75b", tmp75b, },
672 { "tmp75c", tmp75c, },
673 { "tmp1075", tmp1075, },
674 { /* LIST END */ }
675};
676MODULE_DEVICE_TABLE(i2c, lm75_ids);
677
678static const struct of_device_id __maybe_unused lm75_of_match[] = {
679 {
680 .compatible = "adi,adt75",
681 .data = (void *)adt75
682 },
683 {
684 .compatible = "dallas,ds1775",
685 .data = (void *)ds1775
686 },
687 {
688 .compatible = "dallas,ds75",
689 .data = (void *)ds75
690 },
691 {
692 .compatible = "dallas,ds7505",
693 .data = (void *)ds7505
694 },
695 {
696 .compatible = "gmt,g751",
697 .data = (void *)g751
698 },
699 {
700 .compatible = "national,lm75",
701 .data = (void *)lm75
702 },
703 {
704 .compatible = "national,lm75a",
705 .data = (void *)lm75a
706 },
707 {
708 .compatible = "national,lm75b",
709 .data = (void *)lm75b
710 },
711 {
712 .compatible = "maxim,max6625",
713 .data = (void *)max6625
714 },
715 {
716 .compatible = "maxim,max6626",
717 .data = (void *)max6626
718 },
719 {
720 .compatible = "maxim,max31725",
721 .data = (void *)max31725
722 },
723 {
724 .compatible = "maxim,max31726",
725 .data = (void *)max31725
726 },
727 {
728 .compatible = "maxim,mcp980x",
729 .data = (void *)mcp980x
730 },
731 {
732 .compatible = "nxp,pct2075",
733 .data = (void *)pct2075
734 },
735 {
736 .compatible = "st,stds75",
737 .data = (void *)stds75
738 },
739 {
740 .compatible = "st,stlm75",
741 .data = (void *)stlm75
742 },
743 {
744 .compatible = "microchip,tcn75",
745 .data = (void *)tcn75
746 },
747 {
748 .compatible = "ti,tmp100",
749 .data = (void *)tmp100
750 },
751 {
752 .compatible = "ti,tmp101",
753 .data = (void *)tmp101
754 },
755 {
756 .compatible = "ti,tmp105",
757 .data = (void *)tmp105
758 },
759 {
760 .compatible = "ti,tmp112",
761 .data = (void *)tmp112
762 },
763 {
764 .compatible = "ti,tmp175",
765 .data = (void *)tmp175
766 },
767 {
768 .compatible = "ti,tmp275",
769 .data = (void *)tmp275
770 },
771 {
772 .compatible = "ti,tmp75",
773 .data = (void *)tmp75
774 },
775 {
776 .compatible = "ti,tmp75b",
777 .data = (void *)tmp75b
778 },
779 {
780 .compatible = "ti,tmp75c",
781 .data = (void *)tmp75c
782 },
783 {
784 .compatible = "ti,tmp1075",
785 .data = (void *)tmp1075
786 },
787 { },
788};
789MODULE_DEVICE_TABLE(of, lm75_of_match);
790
791#define LM75A_ID 0xA1
792
793/* Return 0 if detection is successful, -ENODEV otherwise */
794static int lm75_detect(struct i2c_client *new_client,
795 struct i2c_board_info *info)
796{
797 struct i2c_adapter *adapter = new_client->adapter;
798 int i;
799 int conf, hyst, os;
800 bool is_lm75a = 0;
801
802 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
803 I2C_FUNC_SMBUS_WORD_DATA))
804 return -ENODEV;
805
806 /*
807 * Now, we do the remaining detection. There is no identification-
808 * dedicated register so we have to rely on several tricks:
809 * unused bits, registers cycling over 8-address boundaries,
810 * addresses 0x04-0x07 returning the last read value.
811 * The cycling+unused addresses combination is not tested,
812 * since it would significantly slow the detection down and would
813 * hardly add any value.
814 *
815 * The National Semiconductor LM75A is different than earlier
816 * LM75s. It has an ID byte of 0xaX (where X is the chip
817 * revision, with 1 being the only revision in existence) in
818 * register 7, and unused registers return 0xff rather than the
819 * last read value.
820 *
821 * Note that this function only detects the original National
822 * Semiconductor LM75 and the LM75A. Clones from other vendors
823 * aren't detected, on purpose, because they are typically never
824 * found on PC hardware. They are found on embedded designs where
825 * they can be instantiated explicitly so detection is not needed.
826 * The absence of identification registers on all these clones
827 * would make their exhaustive detection very difficult and weak,
828 * and odds are that the driver would bind to unsupported devices.
829 */
830
831 /* Unused bits */
832 conf = i2c_smbus_read_byte_data(new_client, 1);
833 if (conf & 0xe0)
834 return -ENODEV;
835
836 /* First check for LM75A */
837 if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
838 /*
839 * LM75A returns 0xff on unused registers so
840 * just to be sure we check for that too.
841 */
842 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
843 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
844 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
845 return -ENODEV;
846 is_lm75a = 1;
847 hyst = i2c_smbus_read_byte_data(new_client, 2);
848 os = i2c_smbus_read_byte_data(new_client, 3);
849 } else { /* Traditional style LM75 detection */
850 /* Unused addresses */
851 hyst = i2c_smbus_read_byte_data(new_client, 2);
852 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
853 || i2c_smbus_read_byte_data(new_client, 5) != hyst
854 || i2c_smbus_read_byte_data(new_client, 6) != hyst
855 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
856 return -ENODEV;
857 os = i2c_smbus_read_byte_data(new_client, 3);
858 if (i2c_smbus_read_byte_data(new_client, 4) != os
859 || i2c_smbus_read_byte_data(new_client, 5) != os
860 || i2c_smbus_read_byte_data(new_client, 6) != os
861 || i2c_smbus_read_byte_data(new_client, 7) != os)
862 return -ENODEV;
863 }
864 /*
865 * It is very unlikely that this is a LM75 if both
866 * hysteresis and temperature limit registers are 0.
867 */
868 if (hyst == 0 && os == 0)
869 return -ENODEV;
870
871 /* Addresses cycling */
872 for (i = 8; i <= 248; i += 40) {
873 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
874 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
875 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
876 return -ENODEV;
877 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
878 != LM75A_ID)
879 return -ENODEV;
880 }
881
882 strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
883
884 return 0;
885}
886
887#ifdef CONFIG_PM
888static int lm75_suspend(struct device *dev)
889{
890 int status;
891 struct i2c_client *client = to_i2c_client(dev);
892
893 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
894 if (status < 0) {
895 dev_dbg(&client->dev, "Can't read config? %d\n", status);
896 return status;
897 }
898 status = status | LM75_SHUTDOWN;
899 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
900 return 0;
901}
902
903static int lm75_resume(struct device *dev)
904{
905 int status;
906 struct i2c_client *client = to_i2c_client(dev);
907
908 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
909 if (status < 0) {
910 dev_dbg(&client->dev, "Can't read config? %d\n", status);
911 return status;
912 }
913 status = status & ~LM75_SHUTDOWN;
914 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
915 return 0;
916}
917
918static const struct dev_pm_ops lm75_dev_pm_ops = {
919 .suspend = lm75_suspend,
920 .resume = lm75_resume,
921};
922#define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
923#else
924#define LM75_DEV_PM_OPS NULL
925#endif /* CONFIG_PM */
926
927static struct i2c_driver lm75_driver = {
928 .class = I2C_CLASS_HWMON,
929 .driver = {
930 .name = "lm75",
931 .of_match_table = of_match_ptr(lm75_of_match),
932 .pm = LM75_DEV_PM_OPS,
933 },
934 .probe_new = lm75_probe,
935 .id_table = lm75_ids,
936 .detect = lm75_detect,
937 .address_list = normal_i2c,
938};
939
940module_i2c_driver(lm75_driver);
941
942MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
943MODULE_DESCRIPTION("LM75 driver");
944MODULE_LICENSE("GPL");